package org.dromara.talk.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.talk.domain.bo.WhatsAppUserBo;
import org.dromara.talk.domain.vo.WhatsAppUserVo;
import org.dromara.talk.domain.WhatsAppUser;
import org.dromara.talk.mapper.WhatsAppUserMapper;
import org.dromara.talk.service.IWhatsAppUserService;

import java.util.List;
import java.util.Collection;

/**
 * WhatsApp用户Service业务层处理
 *
 * @author Lion Li
 * 2026-02-06
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class WhatsAppUserServiceImpl implements IWhatsAppUserService {

    private final WhatsAppUserMapper baseMapper;

    /**
     * 查询WhatsApp用户
     *
     * @param id 主键
     * @return WhatsApp用户
     */
    @Override
    public WhatsAppUserVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询WhatsApp用户列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return WhatsApp用户分页列表
     */
    @Override
    public TableDataInfo<WhatsAppUserVo> queryPageList(WhatsAppUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WhatsAppUser> lqw = buildQueryWrapper(bo);
        Page<WhatsAppUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的WhatsApp用户列表
     *
     * @param bo 查询条件
     * @return WhatsApp用户列表
     */
    @Override
    public List<WhatsAppUserVo> queryList(WhatsAppUserBo bo) {
        LambdaQueryWrapper<WhatsAppUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WhatsAppUser> buildQueryWrapper(WhatsAppUserBo bo) {
        LambdaQueryWrapper<WhatsAppUser> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(WhatsAppUser::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getUserAccount()), WhatsAppUser::getUserAccount, bo.getUserAccount());
        return lqw;
    }

    /**
     * 新增WhatsApp用户
     *
     * @param bo WhatsApp用户
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(WhatsAppUserBo bo) {
        WhatsAppUser add = MapstructUtils.convert(bo, WhatsAppUser.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改WhatsApp用户
     *
     * @param bo WhatsApp用户
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(WhatsAppUserBo bo) {
        WhatsAppUser update = MapstructUtils.convert(bo, WhatsAppUser.class);
        return baseMapper.updateById(update) > 0;
    }


    /**
     * 校验并批量删除WhatsApp用户信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        return baseMapper.deleteByIds(ids) > 0;
    }
}
