package org.dromara.work.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.dto.RoleDTO;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.exception.ServiceException;
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.system.domain.SysDept;
import org.dromara.system.domain.SysRoleDept;
import org.dromara.system.domain.SysUser;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.mapper.SysDeptMapper;
import org.dromara.system.mapper.SysRoleDeptMapper;
import org.dromara.system.mapper.SysUserMapper;
import org.dromara.work.domain.TpOrder;
import org.dromara.work.domain.TpWechat;
import org.dromara.work.domain.TpWechatNum;
import org.dromara.work.domain.TpWechatUser;
import org.dromara.work.domain.bo.OrderRankingBo;
import org.dromara.work.domain.bo.TpWechatBo;
import org.dromara.work.domain.vo.TpWechatVo;
import org.dromara.work.domain.vo.WechatSumVo;
import org.dromara.work.domain.vo.WxNumDayListVo;
import org.dromara.work.mapper.TpOrderMapper;
import org.dromara.work.mapper.TpWechatMapper;
import org.dromara.work.mapper.TpWechatNumMapper;
import org.dromara.work.mapper.TpWechatUserMapper;
import org.dromara.work.service.ITpWechatService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 基本信息Service业务层处理
 *
 * @author Maosw
 * @date 2024-08-12
 */
@RequiredArgsConstructor
@Service
public class TpWechatServiceImpl implements ITpWechatService {

    private final TpWechatMapper baseMapper;

    private final SysUserMapper userMapper;

    private final TpWechatUserMapper wechatUserMapper;

    private final TpWechatNumMapper wechatNumMapper;

    private final SysDeptMapper deptMapper;

    private final SysRoleDeptMapper roleDeptMapper;

    private final TpOrderMapper orderMapper;


    /**
     * 查询基本信息
     *
     * @param id 主键
     * @return 基本信息
     */
    @Override
    public TpWechatVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询基本信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 基本信息分页列表
     */
    @Override
    public TableDataInfo<TpWechatVo> queryPageList(TpWechatBo bo, PageQuery pageQuery) {
        if(bo.getUid() != null){
            LoginUser loginUser = LoginHelper.getLoginUser();
            if(!LoginHelper.isSuperAdmin()){
                if(loginUser.getIdentity() == 2){
                    throw new ServiceException("您无权限查看客户信息");
                } else if (loginUser.getIdentity() == 3) {
                    int scope = getDataScope();
                    if(scope == 2){
                        List<Long> deptIds = getDeptIds();
                        bo.setDeptIds(deptIds);
                    }else if(scope == 3){
                        List<Long> deptIds = new ArrayList<>();
                        deptIds.add(loginUser.getDeptId());
                        bo.setDeptIds(deptIds);
                    }else if(scope == 4){
                        List<Long> deptIds = deptMapper.selectList(new LambdaQueryWrapper<SysDept>().like(SysDept::getAncestors, loginUser.getDeptId())).stream().map(SysDept::getDeptId).collect(Collectors.toList());
                        deptIds.add(loginUser.getDeptId());
                        bo.setDeptIds(deptIds);
                    }else if(scope == 5){
                        bo.setUid(loginUser.getUserId());
                    }
                }else if(loginUser.getIdentity() == 1){
                    int scope = getDataScope();
                    if(scope == 2){
                        List<Long> deptIds = getDeptIds();
                        bo.setDeptIds(deptIds);
                    }else if(scope == 3){
                        List<Long> deptIds = new ArrayList<>();
                        deptIds.add(loginUser.getDeptId());
                        bo.setDeptIds(deptIds);
                    }else if(scope == 4){
                        List<Long> deptIds = deptMapper.selectList(new LambdaQueryWrapper<SysDept>().like(SysDept::getAncestors, loginUser.getDeptId())).stream().map(SysDept::getDeptId).collect(Collectors.toList());
                        deptIds.add(loginUser.getDeptId());
                        bo.setDeptIds(deptIds);
                    }else if(scope == 5){
                        bo.setUid(loginUser.getUserId());
                    }
                    if(bo.getUid() == 2){
                        bo.setUid(null);
                    }
                }
            }
        }
        bo.setIsDel(1);
        if(ObjectUtil.isNotNull(bo.getCreateDept())){
            List<Long> deptIds = deptMapper.selectList(new LambdaQueryWrapper<SysDept>().like(SysDept::getAncestors, bo.getCreateDept())).stream().map(SysDept::getDeptId).collect(Collectors.toList());
            deptIds.add(bo.getCreateDept());
            bo.setDeptIds(deptIds);
        }
        IPage<TpWechatVo> result = baseMapper.selectVoPageList(new Page(pageQuery.getPageNum(), pageQuery.getPageSize()),bo);
        return TableDataInfo.build(result);
    }

    public int getDataScope() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<String> ids = loginUser != null ?
            loginUser.getRoles().stream().filter(Objects::nonNull).map(role -> role.getDataScope()).filter(Objects::nonNull).collect(Collectors.toList()) :
            Collections.emptyList();

        if (ids.contains("1")) {
            return 1;
        } else if (ids.contains("2")) {
            return 2;
        } else if (ids.contains("3")) {
            return 3;
        } else if (ids.contains("4")) {
            return 4;
        } else if (ids.contains("5")) {
            return 5;
        }
        return 0;
    }

    public List<Long> getDeptIds(){
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<Long> roleIds = loginUser.getRoles().stream().map(RoleDTO::getRoleId).collect(Collectors.toList());
        List<Long> getDeptIds = roleDeptMapper.selectList(new LambdaQueryWrapper<SysRoleDept>().in(SysRoleDept::getRoleId, roleIds)).stream().map(SysRoleDept::getDeptId).collect(Collectors.toList());
        return getDeptIds.stream().distinct().collect(Collectors.toList());
    }

    /**
     * 导出基础信息列表
     * @param bo 查询条件
     * @return 基本信息列表
     */
    @Override
    public List<TpWechatVo> queryList(TpWechatBo bo) {
        if(bo.getUid() != null){
            LoginUser loginUser = LoginHelper.getLoginUser();
            if(!LoginHelper.isSuperAdmin()){
                if(loginUser.getIdentity() == 2){
                    throw new ServiceException("您无权限查看客户信息");
                } else if (loginUser.getIdentity() == 3) {
                    int scope = getDataScope();
                    if(scope == 2){
                        List<Long> deptIds = getDeptIds();
                        bo.setDeptIds(deptIds);
                    }else if(scope == 3){
                        List<Long> deptIds = new ArrayList<>();
                        deptIds.add(loginUser.getDeptId());
                        bo.setDeptIds(deptIds);
                    }else if(scope == 4){
                        List<Long> deptIds = deptMapper.selectList(new LambdaQueryWrapper<SysDept>().like(SysDept::getAncestors, loginUser.getDeptId())).stream().map(SysDept::getDeptId).collect(Collectors.toList());
                        deptIds.add(loginUser.getDeptId());
                        bo.setDeptIds(deptIds);
                    }else if(scope == 5){
                        bo.setUid(loginUser.getUserId());
                    }
                }else if(loginUser.getIdentity() == 1){
                    int scope = getDataScope();
                    if(scope == 2){
                        List<Long> deptIds = getDeptIds();
                        bo.setDeptIds(deptIds);
                    }else if(scope == 3){
                        List<Long> deptIds = new ArrayList<>();
                        deptIds.add(loginUser.getDeptId());
                        bo.setDeptIds(deptIds);
                    }else if(scope == 4){
                        List<Long> deptIds = deptMapper.selectList(new LambdaQueryWrapper<SysDept>().like(SysDept::getAncestors, loginUser.getDeptId())).stream().map(SysDept::getDeptId).collect(Collectors.toList());
                        deptIds.add(loginUser.getDeptId());
                        bo.setDeptIds(deptIds);
                    }else if(scope == 5){
                        bo.setUid(loginUser.getUserId());
                    }
                    if(bo.getUid() == 2){
                        bo.setUid(null);
                    }
                }
            }
        }
        bo.setIsDel(1);
        if(ObjectUtil.isNotNull(bo.getCreateDept())){
            List<Long> deptIds = deptMapper.selectList(new LambdaQueryWrapper<SysDept>().like(SysDept::getAncestors, bo.getCreateDept())).stream().map(SysDept::getDeptId).collect(Collectors.toList());
            deptIds.add(bo.getCreateDept());
            bo.setDeptIds(deptIds);
        }
        return baseMapper.selectVoListExport(bo);
    }

    private LambdaQueryWrapper<TpWechat> buildQueryWrapper(TpWechatBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TpWechat> lqw = Wrappers.lambdaQuery();

        LoginUser loginUser = LoginHelper.getLoginUser();
        if(!LoginHelper.isSuperAdmin()){
            if(loginUser.getIdentity() == 2){
                throw new ServiceException("您无权限查看客户信息");
            } else if (loginUser.getIdentity() == 3 || loginUser.getIdentity() == 1) {
                int scope = getDataScope();
                if(scope == 2){
                    List<Long> deptIds = getDeptIds();
                    List<Long> userIds = userMapper.selectList(new LambdaQueryWrapper<SysUser>().in(SysUser::getDeptId, deptIds).eq(SysUser::getIdentity, 3)).stream().map(SysUser::getUserId).collect(Collectors.toList());
                    if (userIds != null && userIds.size() > 0){
                        lqw.in(TpWechat::getUid, userIds);
                    }
                }else if(scope == 3){
                    List<Long> deptIds = new ArrayList<>();
                    deptIds.add(loginUser.getDeptId());
                    List<Long> userIds = userMapper.selectList(new LambdaQueryWrapper<SysUser>().in(SysUser::getDeptId, deptIds).eq(SysUser::getIdentity, 3)).stream().map(SysUser::getUserId).collect(Collectors.toList());
                    if (userIds != null && userIds.size() > 0){
                        lqw.in(TpWechat::getUid, userIds);
                    }
                }else if(scope == 4){
                    List<Long> deptIds = deptMapper.selectList(new LambdaQueryWrapper<SysDept>().like(SysDept::getAncestors, loginUser.getDeptId())).stream().map(SysDept::getDeptId).collect(Collectors.toList());
                    deptIds.add(loginUser.getDeptId());
                    List<Long> userIds = userMapper.selectList(new LambdaQueryWrapper<SysUser>().in(SysUser::getDeptId, deptIds).eq(SysUser::getIdentity, 3)).stream().map(SysUser::getUserId).collect(Collectors.toList());
                    if (userIds != null && userIds.size() > 0){
                        lqw.in(TpWechat::getUid, userIds);
                    }
                }else if(scope == 5){
                    bo.setUid(loginUser.getUserId());
                    lqw.eq(bo.getUid() != null, TpWechat::getUid, bo.getUid());
                }
            }
        }

        if(bo.getDeptIds() != null && bo.getDeptIds().size() > 0){
            List<Long> userIds = userMapper.selectList(new LambdaQueryWrapper<SysUser>().in(SysUser::getDeptId, bo.getDeptIds()).eq(SysUser::getIdentity, 3)).stream().map(SysUser::getUserId).collect(Collectors.toList());
            if (userIds != null && userIds.size() > 0){
                lqw.in(TpWechat::getUid, userIds);
            }
        }

        lqw.like(StringUtils.isNotBlank(bo.getCode()), TpWechat::getCode, bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getNikename()), TpWechat::getNikename, bo.getNikename());
//        lqw.eq(bo.getUid() != null, TpWechat::getUid, bo.getUid());
        lqw.like(StringUtils.isNotBlank(bo.getUser()), TpWechat::getUser, bo.getUser());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), TpWechat::getPhone, bo.getPhone());
        lqw.eq(bo.getUserTime() != null, TpWechat::getUserTime, bo.getUserTime());
        lqw.like(StringUtils.isNotBlank(bo.getName()), TpWechat::getName, bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getCardId()), TpWechat::getCardId, bo.getCardId());
        lqw.like(StringUtils.isNotBlank(bo.getBank()), TpWechat::getBank, bo.getBank());
        lqw.like(StringUtils.isNotBlank(bo.getNumber()), TpWechat::getNumber, bo.getNumber());
        lqw.eq(bo.getNum() != null, TpWechat::getNum, bo.getNum());
        lqw.eq(bo.getAllnum() != null, TpWechat::getAllnum, bo.getAllnum());
        lqw.eq(bo.getNowTime() != null, TpWechat::getNowTime, bo.getNowTime());
        lqw.eq(bo.getAddUser() != null, TpWechat::getAddUser, bo.getAddUser());
        lqw.like(StringUtils.isNotBlank(bo.getAddname()), TpWechat::getAddname, bo.getAddname());
        lqw.eq(bo.getIsDel() != null, TpWechat::getIsDel, bo.getIsDel());
        lqw.eq(bo.getAddTime() != null, TpWechat::getAddTime, bo.getAddTime());
        lqw.eq(bo.getNowPrice() != null, TpWechat::getNowPrice, bo.getNowPrice());
        return lqw;
    }

    private QueryWrapper<TpWechat> queryWrapper(TpWechatBo bo) {
        QueryWrapper<TpWechat> lqw = new QueryWrapper<>();
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(!LoginHelper.isSuperAdmin()){
            if(loginUser.getIdentity() == 2){
                throw new ServiceException("您无权限查看客户信息");
            } else if (loginUser.getIdentity() == 3 || loginUser.getIdentity() == 1) {
                int scope = getDataScope();
                if(scope == 2){
                    List<Long> deptIds = getDeptIds();
                    List<Long> userIds = userMapper.selectList(new LambdaQueryWrapper<SysUser>().in(SysUser::getDeptId, deptIds).eq(SysUser::getIdentity, 3)).stream().map(SysUser::getUserId).collect(Collectors.toList());
                    if (userIds != null && userIds.size() > 0){
                        String idsStr = userIds.stream().map(f -> f.toString()).collect(Collectors.joining(","));
                        lqw.inSql("uid", idsStr);
                    }
                }else if(scope == 3){
                    List<Long> deptIds = new ArrayList<>();
                    deptIds.add(loginUser.getDeptId());
                    List<Long> userIds = userMapper.selectList(new LambdaQueryWrapper<SysUser>().in(SysUser::getDeptId, deptIds).eq(SysUser::getIdentity, 3)).stream().map(SysUser::getUserId).collect(Collectors.toList());
                    if (userIds != null && userIds.size() > 0){
                        String idsStr = userIds.stream().map(f -> f.toString()).collect(Collectors.joining(","));
                        lqw.inSql("uid", idsStr);
                    }
                }else if(scope == 4){
                    List<Long> deptIds = deptMapper.selectList(new LambdaQueryWrapper<SysDept>().like(SysDept::getAncestors, loginUser.getDeptId())).stream().map(SysDept::getDeptId).collect(Collectors.toList());
                    deptIds.add(loginUser.getDeptId());
                    List<Long> userIds = userMapper.selectList(new LambdaQueryWrapper<SysUser>().in(SysUser::getDeptId, deptIds).eq(SysUser::getIdentity, 3)).stream().map(SysUser::getUserId).collect(Collectors.toList());
                    if (userIds != null && userIds.size() > 0){
                        String idsStr = userIds.stream().map(f -> f.toString()).collect(Collectors.joining(","));
                        lqw.inSql("uid", idsStr);
                    }
                }else if(scope == 5){
                    bo.setUid(loginUser.getUserId());
                    lqw.eq(bo.getUid() != null, "uid", bo.getUid());
                }
            }
        }

        if(bo.getDeptIds() != null && bo.getDeptIds().size() > 0){
            List<Long> userIds = userMapper.selectList(new LambdaQueryWrapper<SysUser>().in(SysUser::getDeptId, bo.getDeptIds()).eq(SysUser::getIdentity, 3)).stream().map(SysUser::getUserId).collect(Collectors.toList());
           if (userIds != null && userIds.size() > 0){
               String idsStr = userIds.stream().map(f -> f.toString()).collect(Collectors.joining(","));
               lqw.inSql("uid", idsStr);
           }
        }

        lqw.like(StringUtils.isNotBlank(bo.getCode()), "code", bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getNikename()), "nikename", bo.getNikename());
        lqw.like(StringUtils.isNotBlank(bo.getUser()), "user", bo.getUser());
//        lqw.eq(bo.getUid() != null, "uid", bo.getUid());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), "phone", bo.getPhone());
        lqw.eq(bo.getUserTime() != null, "user_time", bo.getUserTime());
        lqw.like(StringUtils.isNotBlank(bo.getName()), "name", bo.getName());
        lqw.eq(bo.getNowTime() != null, "now_time", bo.getNowTime());
        lqw.eq(bo.getAddUser() != null, "add_user", bo.getAddUser());
        lqw.like(StringUtils.isNotBlank(bo.getCardId()), "card_id", bo.getCardId());
        lqw.like(StringUtils.isNotBlank(bo.getNumber()), "number", bo.getNumber());
        lqw.eq(bo.getIsDel() != null, "is_del", bo.getIsDel());
        lqw.eq(bo.getAddTime() != null, "add_time", bo.getAddTime());
        return lqw;
    }

    /**
     * 新增基本信息
     *
     * @param bo 基本信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TpWechatBo bo) {
        boolean exist = baseMapper.exists(new LambdaQueryWrapper<TpWechat>().eq(TpWechat::getCode, bo.getCode()));
        if(exist){
            throw new ServiceException("唯一编号已存在，请修改后提交");
        }
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(loginUser.getIdentity() == 3){
            bo.setUid(loginUser.getUserId());
            bo.setUser(loginUser.getNickname());
        }

        bo.setAddUser(loginUser.getUserId());
        bo.setAddname(loginUser.getRealName());
        bo.setAllnum(bo.getNum());
        bo.setAddTime(new Date());
        bo.setNowTime(new Date());
        TpWechat add = MapstructUtils.convert(bo, TpWechat.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改基本信息
     *
     * @param bo 基本信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TpWechatBo bo) {
        TpWechatVo wechatVo = baseMapper.selectVoOne(new LambdaQueryWrapper<TpWechat>().eq(TpWechat::getCode,bo.getCode()).eq(TpWechat::getIsDel, 1));
        if (ObjectUtil.isNotEmpty(wechatVo) && !Objects.equals(bo.getId(), wechatVo.getId())) {
            throw new ServiceException("唯一编号已存在，请修改后提交");
        }
        TpWechat update = MapstructUtils.convert(bo, TpWechat.class);

        if(bo.getNum() != null && bo.getNum() != 0){
            bo.setAllnum(bo.getNum());
        }
        return baseMapper.updateById(update) > 0;
    }


    /**
     * 校验并批量删除基本信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        List<TpWechat> list = baseMapper.selectByIds(ids);
        list.stream().forEach(f ->f.setIsDel(2));
        return baseMapper.updateBatchById(list);
    }

    @Override
    public boolean updateUserByBo(TpWechatBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();

        if(bo.getUid() != null){
            TpWechatVo wechatVo = baseMapper.selectVoById(bo.getId());
            TpWechatUser tpWechatUser = new TpWechatUser();
            tpWechatUser.setWid(wechatVo.getId());
            tpWechatUser.setUid(wechatVo.getUid());
            tpWechatUser.setUser(wechatVo.getUser());
            tpWechatUser.setNowPrice(wechatVo.getNowPrice());
            tpWechatUser.setNowTime(wechatVo.getNowTime());
            tpWechatUser.setEndTime(new Date());
            tpWechatUser.setAddTime(new Date());
            tpWechatUser.setAddUser(loginUser.getUserId());
            tpWechatUser.setAddname(loginUser.getRealName());
            wechatUserMapper.insert(tpWechatUser);

            SysUserVo sysUserVo = userMapper.selectVoById(bo.getUid());
            bo.setUid(sysUserVo.getUserId());
            bo.setUser(sysUserVo.getNickName());
            bo.setAddUser(loginUser.getUserId());
            bo.setAddname(loginUser.getRealName());
            bo.setNowTime(new Date());
            bo.setNowPrice(new BigDecimal(0));
        }

        TpWechat update = MapstructUtils.convert(bo, TpWechat.class);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public WechatSumVo queryListSum(TpWechatBo bo) {
        bo.setIsDel(1);
        WechatSumVo wechatSumVo = new WechatSumVo();
        LambdaQueryWrapper<TpWechat> lqw = buildQueryWrapper(bo);
        wechatSumVo.setWxSum(baseMapper.selectCount(lqw));

        QueryWrapper<TpWechat> wrapper = queryWrapper(bo);
        //wrapper.select("sum(deal_num) as dealNum","sum(total_sum) as totalSum","sum(deal_order_num) as dealOrderNum","sum(allnum) as allnum");
        wrapper.select("id, `code`, nikename, uid, `user`, phone, user_time, `name`, num, allnum");
        List<TpWechatVo> wechatVoList = baseMapper.selectVoList(wrapper);
        wechatSumVo.setZhySum(wechatVoList.stream().mapToLong(f ->f.getAllnum()).sum());

        List<Long> ids = wechatVoList.stream().map(f -> f.getId()).toList();

        if(ids.size() > 0){
            List<TpOrder> list = orderMapper.selectList(new LambdaQueryWrapper<TpOrder>().in(TpOrder::getWid, ids));
            wechatSumVo.setCjdsSum(list.stream().count());
            wechatSumVo.setCjzjeSum(list.stream().map(f -> f.getPrice()).reduce(BigDecimal.ZERO, BigDecimal::add));

            /*QueryWrapper<TpOrder> lqwo = new QueryWrapper<>();
            lqwo.select("COUNT(DISTINCT kid) as count");
            String idsStr = ids.stream().map(f -> f.toString()).collect(Collectors.joining(","));
            lqwo.inSql("wid", idsStr);
            Map<String, Object> result = orderMapper.selectMaps(lqwo).get(0);
            Long cjkhSum = (Long) result.get("count");
            wechatSumVo.setCjkhSum(cjkhSum);*/

            QueryWrapper<TpOrder> lqwo = new QueryWrapper<>();
            lqwo.select("COUNT(DISTINCT kid) as count");
            lqwo.in("wid", ids);
            Map<String, Object> result = orderMapper.selectMaps(lqwo).get(0);
            Long cjkhSum = (Long) result.get("count");
            wechatSumVo.setCjkhSum(cjkhSum);
        }

        /*LambdaQueryWrapper<TpWechatNum> lqw1 = new LambdaQueryWrapper<>();
        lqw1.inSql(bo.getUid() != null,TpWechatNum::getWid,"SELECT id FROM tp_wechat as w WHERE w.is_del = 1 and  w.uid = "+bo.getUid());
        lqw1.geSql(TpWechatNum::getAddTime,"CONCAT( YEAR ( CURDATE()), '-01-01' )");
        Long yearNum = wechatNumMapper.selectList(lqw1).stream().mapToLong(TpWechatNum::getNum).sum();*/

        LambdaQueryWrapper<TpWechatNum> lqw1 = new LambdaQueryWrapper<>();
        lqw1.in(TpWechatNum::getWid,ids);
        lqw1.geSql(TpWechatNum::getAddTime,"CONCAT( YEAR ( CURRENT_DATE ()), '-01-01' )");
        Long yearNum = wechatNumMapper.selectList(lqw1).stream().mapToLong(TpWechatNum::getNum).sum();

        /*LambdaQueryWrapper<TpWechatNum> lqw2 = new LambdaQueryWrapper<>();
        lqw2.inSql(bo.getUid() != null,TpWechatNum::getWid,"SELECT id FROM tp_wechat as w WHERE w.is_del = 1 and w.uid = "+bo.getUid());
        lqw2.geSql(TpWechatNum::getAddTime,"CONCAT( YEAR ( CURRENT_DATE ()), '-', MONTH ( CURRENT_DATE ()), '-01' )");
        Long monthNum = wechatNumMapper.selectList(lqw2).stream().mapToLong(TpWechatNum::getNum).sum();*/

        LambdaQueryWrapper<TpWechatNum> lqw2 = new LambdaQueryWrapper<>();
        lqw2.in(TpWechatNum::getWid,ids);
        lqw2.geSql(TpWechatNum::getAddTime,"CONCAT( YEAR ( CURRENT_DATE ()), '-', MONTH ( CURRENT_DATE ()), '-01' )");
        Long monthNum = wechatNumMapper.selectList(lqw2).stream().mapToLong(TpWechatNum::getNum).sum();


        /*LambdaQueryWrapper<TpWechatNum> lqw3 = new LambdaQueryWrapper<>();
        lqw3.inSql(bo.getUid() != null,TpWechatNum::getWid,"SELECT id FROM tp_wechat as w WHERE w.is_del = 1 and w.uid = "+bo.getUid());
        lqw3.geSql(TpWechatNum::getAddTime,"( CURDATE() - INTERVAL 1 DAY )");
        Long dayNum = wechatNumMapper.selectList(lqw3).stream().mapToLong(TpWechatNum::getNum).sum();*/

        LambdaQueryWrapper<TpWechatNum> lqw3 = new LambdaQueryWrapper<>();
        lqw3.in(TpWechatNum::getWid,ids);
        lqw3.geSql(TpWechatNum::getAddTime,"( CURDATE() - INTERVAL 1 DAY )");
        Long dayNum = wechatNumMapper.selectList(lqw3).stream().mapToLong(TpWechatNum::getNum).sum();

        wechatSumVo.setBnxzSum(yearNum);
        wechatSumVo.setByxzSum(monthNum);
        wechatSumVo.setZrxzSum(dayNum);
        // 检查 wechatVo 是否为 null
        /*if (wechatVo != null) {
            wechatSumVo.setCjkhSum(wechatVo.getDealNum());
            wechatSumVo.setCjdsSum(wechatVo.getDealOrderNum());
            wechatSumVo.setCjzjeSum(wechatVo.getTotalSum());
            wechatSumVo.setZhySum(wechatVo.getAllnum());
        } else {
            // 如果 wechatVo 为 null，则设置默认值或抛出异常
            wechatSumVo.setCjkhSum(0L);
            wechatSumVo.setCjdsSum(0L);
            wechatSumVo.setCjzjeSum(new BigDecimal(0));
            wechatSumVo.setZhySum(0L);
        }*/
        return wechatSumVo;
    }

    /**
     * 微信日统计
     *
     * @param bo
     * @param wxIds
     * @return
     */
    @Override
    public List<WxNumDayListVo> wxDayList(OrderRankingBo bo, List<Long> wxIds) {
        return baseMapper.wxDayList(bo, wxIds);
    }

    /**
     * 微信月统计
     *
     * @param bo
     * @param wxIds
     * @return
     */
    @Override
    public List<WxNumDayListVo> wxMonthList(OrderRankingBo bo, List<Long> wxIds) {
        return baseMapper.wxMonthList(bo, wxIds);
    }

    /**
     * @param userIds
     * @return
     */
    @Override
    public List<TpWechatVo> queryListByIds(List<Long> userIds) {
        return baseMapper.selectVoList(new LambdaQueryWrapper<TpWechat>().in(TpWechat::getUid, userIds).eq(TpWechat::getIsDel, 1));
    }
}
