package org.dromara.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.work.domain.TpWechat;
import org.dromara.work.domain.TpWechatNum;
import org.dromara.work.domain.bo.TpWechatNumBo;
import org.dromara.work.domain.vo.TpWechatNumVo;
import org.dromara.work.mapper.TpWechatMapper;
import org.dromara.work.mapper.TpWechatNumMapper;
import org.dromara.work.service.ITpWechatNumService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 更新好友数记录Service业务层处理
 *
 * @author Maosw
 * @date 2024-08-12
 */
@RequiredArgsConstructor
@Service
public class TpWechatNumServiceImpl implements ITpWechatNumService {

    private final TpWechatNumMapper baseMapper;

    private final TpWechatMapper wechatMapper;

    /**
     * 查询更新好友数记录
     *
     * @param id 主键
     * @return 更新好友数记录
     */
    @Override
    public TpWechatNumVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询更新好友数记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 更新好友数记录分页列表
     */
    @Override
    public TableDataInfo<TpWechatNumVo> queryPageList(TpWechatNumBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TpWechatNum> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(TpWechatNum::getId);
        Page<TpWechatNumVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的更新好友数记录列表
     *
     * @param bo 查询条件
     * @return 更新好友数记录列表
     */
    @Override
    public List<TpWechatNumVo> queryList(TpWechatNumBo bo) {
        LambdaQueryWrapper<TpWechatNum> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TpWechatNum> buildQueryWrapper(TpWechatNumBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TpWechatNum> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getWid() != null, TpWechatNum::getWid, bo.getWid());
        lqw.eq(bo.getNum() != null, TpWechatNum::getNum, bo.getNum());
        lqw.eq(bo.getAddUser() != null, TpWechatNum::getAddUser, bo.getAddUser());
        lqw.like(StringUtils.isNotBlank(bo.getAddname()), TpWechatNum::getAddname, bo.getAddname());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            TpWechatNum::getAddTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

    /**
     * 新增更新好友数记录
     *
     * @param bo 更新好友数记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TpWechatNumBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();

        TpWechat tpWechat = wechatMapper.selectById(bo.getWid());
        boolean exist = baseMapper.exists(new LambdaQueryWrapper<TpWechatNum>().eq(TpWechatNum::getWid, bo.getWid()).eq(TpWechatNum::getAddUser, loginUser.getUserId()).orderByDesc(TpWechatNum::getId));
        if(exist){
            TpWechatNum tpWechatNum = baseMapper.selectList(new LambdaQueryWrapper<TpWechatNum>().eq(TpWechatNum::getWid, bo.getWid()).eq(TpWechatNum::getAddUser, loginUser.getUserId()).orderByDesc(TpWechatNum::getId)).get(0);
            if(tpWechatNum != null){
                Long num = bo.getTotal() - tpWechatNum.getTotal();
                bo.setNum(num);
                bo.setInitial(tpWechat.getNum());
            }
        }else {
            Long num = bo.getTotal() - tpWechat.getNum();
            bo.setNum(num);
            bo.setInitial(tpWechat.getNum());
        }

        bo.setAddUser(loginUser.getUserId());
        bo.setAddname(loginUser.getNickname());
        bo.setAddTime(new Date());
        TpWechatNum add = MapstructUtils.convert(bo, TpWechatNum.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            TpWechat wechat = wechatMapper.selectById(bo.getWid());
            wechat.setAllnum(bo.getTotal());
            wechatMapper.updateById(wechat);
        }
        return flag;
    }

    /**
     * 修改更新好友数记录
     *
     * @param bo 更新好友数记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TpWechatNumBo bo) {
        TpWechatNum tpWechatNum = baseMapper.selectById(bo.getId());
        Long num = bo.getTotal() - tpWechatNum.getTotal();
        bo.setNum(num);
        TpWechatNum update = MapstructUtils.convert(bo, TpWechatNum.class);

        TpWechat wechat = wechatMapper.selectById(bo.getWid());
        wechat.setAllnum(bo.getTotal());
        wechatMapper.updateById(wechat);
        return baseMapper.updateById(update) > 0;
    }

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