package com.ruoyi.ibs.grid.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.ibs.enumertion.CustIdTypeEnum;
import com.ruoyi.ibs.enumertion.CustTypeEnum;
import com.ruoyi.ibs.grid.domain.dto.DeleteDTO;
import com.ruoyi.ibs.grid.domain.dto.GridUserRelateDTO;
import com.ruoyi.ibs.grid.domain.dto.GridUserSelectDTO;
import com.ruoyi.ibs.grid.domain.dto.virtual.VirtualCustDTO;
import com.ruoyi.ibs.grid.domain.dto.virtual.VirtualCustUserDTO;
import com.ruoyi.ibs.grid.domain.dto.virtual.VirtualGridDTO;
import com.ruoyi.ibs.grid.domain.dto.virtual.VirtualGridUpdateDTO;
import com.ruoyi.ibs.grid.domain.entity.*;
import com.ruoyi.ibs.grid.domain.excel.VirtualCustImportDTO;
import com.ruoyi.ibs.grid.domain.excel.VirtualCustUserImportDTO;
import com.ruoyi.ibs.grid.domain.vo.GridUserVO;
import com.ruoyi.ibs.grid.domain.vo.VirtualGridVO;
import com.ruoyi.ibs.grid.mapper.*;
import com.ruoyi.ibs.grid.service.GridInfoRedisService;
import com.ruoyi.ibs.grid.service.VirtualGridCustService;
import com.ruoyi.ibs.grid.service.VirtualGridService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author 吴凯程
 * @Date 2024/9/20
 **/
@Service
@Slf4j
@EnableAsync
public class VirtualGridServiceImpl implements VirtualGridService {

    @Resource
    private VirtualCustUserMapper virtualCustUserMapper;

    @Resource
    private VirtualCustMapper virtualCustMapper;


    @Resource
    private VirtualGridMapper virtualGridMapper;

    @Resource
    private GridUserMapper gridUserMapper;

    @Resource
    private VirtualUserMapper virtualUserMapper;

    @Resource
    private VirtualGridRuleMapper virtualGridRuleMapper;

    @Resource
    private GridInfoRedisService gridInfoRedisService;

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private VirtualGridCustService virtualGridCustService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String createVirtualGrid(VirtualGridDTO virtualGridDTO) {

        virtualGridDTO.checkValid();
        if (checkGridNameExist(virtualGridDTO.getGridName())) {
            throw new ServiceException("网格名称已存在");
        }

        VirtualGrid virtualGrid = new VirtualGrid();
        BeanUtils.copyProperties(virtualGridDTO, virtualGrid);
        if (virtualGridDTO.getGridAssignType().equals("2")) {
            virtualGrid.setGridDutyType("2");
            virtualGrid.setBatchFlag(Boolean.TRUE);
        } else {
            virtualGrid.setBatchFlag(Boolean.FALSE);
        }
        virtualGrid.setCreateMode("1");
        SysDept sysDept = sysDeptMapper.selectDeptById(SecurityUtils.getDeptId());
        virtualGrid.setDeptType(sysDept.getDeptType());
        virtualGridMapper.insert(virtualGrid);
        Long gridId = virtualGrid.getGridId();

        // 插入客户
        virtualGridDTO.getCustDTOList().forEach(virtualGridCustDTO -> {
            VirtualCust virtualCust = new VirtualCust();
            BeanUtils.copyProperties(virtualGridCustDTO, virtualCust);

            CustTypeEnum custType = CustTypeEnum.getByName(virtualGridCustDTO.getCustType());
            virtualCust.setCustType(custType);
            CustIdTypeEnum custIdType = CustIdTypeEnum.getByName(virtualGridCustDTO.getCustIdType());
            virtualCust.setCustIdType(custIdType);

            virtualCust.setGridId(gridId);
            virtualCustMapper.insert(virtualCust);
        });

        // 指定客户经理：插入客户经理，插入客户-客户经理关系
        if (virtualGridDTO.getGridAssignType().equals("1")) {
            // 插入客户经理
            virtualGridDTO.getGridUserRelateDTOList().forEach(gridUserRelateDTO -> {
                if (gridUserRelateDTO.getRelateType().equals("2")) {
                    throw new ServiceException("虚拟网格创建失败 不可以绑定机构");
                }
                VirtualUser virtualUser = new VirtualUser();
                BeanUtils.copyProperties(gridUserRelateDTO, virtualUser);
                virtualUser.setGridId(gridId);
                virtualUserMapper.insert(virtualUser);

                // 插入客户-客户经理关系
                virtualGridDTO.getCustDTOList().forEach(virtualGridCustDTO -> {
                    VirtualCustUser virtualCustUser = new VirtualCustUser();
                    BeanUtils.copyProperties(gridUserRelateDTO, virtualCustUser);
                    virtualCustUser.setGridId(gridId);
                    virtualCustUser.setCustId(virtualGridCustDTO.getCustId());
                    virtualCustUserMapper.insert(virtualCustUser);
                });
            });
            // 插入网格分配规则
        } else if (virtualGridDTO.getGridAssignType().equals("2")) {
            virtualGridDTO.getVirtualGridRuleDTOList().forEach(virtualGridRuleDTO -> {
                VirtualGridRule virtualGridRule = new VirtualGridRule();
                BeanUtils.copyProperties(virtualGridRuleDTO, virtualGridRule);
                virtualGridRule.setRelateType(CustTypeEnum.getByName(virtualGridRuleDTO.getRelateType()));
                virtualGridRule.setGridId(gridId);
                virtualGridRuleMapper.insert(virtualGridRule);
                if (virtualGridRuleDTO.getRuleKey().equals(3)) {
                    List<VirtualCustDTO> virtualCustDTOS = virtualGridDTO.getCustDTOList().stream()
                            .filter(virtualCustDTO -> virtualCustDTO.getCustType().equals(virtualGridRuleDTO.getRelateType()))
                            .collect(Collectors.toList());
                    for (VirtualCustDTO virtualCustDTO : virtualCustDTOS) {
                        VirtualCustUser virtualCustUser = new VirtualCustUser();
                        virtualCustUser.setGridId(gridId);
                        virtualCustUser.setCustId(virtualCustDTO.getCustId());
                        virtualCustUserMapper.insert(virtualCustUser);
                    }
                }
            });
        }
        gridInfoRedisService.cleanGridInfo("3");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date().getTime());
        String headId = SecurityUtils.getHeadId();
        virtualGridCustService.updateToCustInfo(headId, virtualGridDTO.getCustDTOList(), SecurityUtils.getUsername(), format);
        return "虚拟网格创建成功！";
    }

    /**
     * @param virtualGridRelateAddDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String createVirtualGridByRelate(VirtualGridDTO virtualGridRelateAddDTO) {
        if (checkGridNameExist(virtualGridRelateAddDTO.getGridName())) {
            throw new ServiceException("网格名称已存在");
        }
        virtualGridRelateAddDTO.checkValid();
        VirtualGrid virtualGrid = new VirtualGrid();
        BeanUtils.copyProperties(virtualGridRelateAddDTO, virtualGrid);
        virtualGrid.setCreateMode("2");
        virtualGrid.setGridAssignType("1");
        virtualGrid.setGridDutyType("2");
        SysDept sysDept = sysDeptMapper.selectDeptById(SecurityUtils.getDeptId());
        virtualGrid.setDeptType(sysDept.getDeptType());
        virtualGridMapper.insert(virtualGrid);

        Long gridId = virtualGrid.getGridId();

        List<String> custCollect = virtualGridRelateAddDTO.getVirtualCustUserDTOList().stream()
                .map(virtualCustUserDTO -> virtualCustUserDTO.getCustId()).distinct().collect(Collectors.toList());

        List<VirtualCustDTO> virtualCustDTOList = new ArrayList<>();
        custCollect.forEach(custId -> {
            VirtualCustUserDTO custDTO = virtualGridRelateAddDTO.getVirtualCustUserDTOList().stream().filter(virtualCustUserDTO -> virtualCustUserDTO.getCustId().equals(custId))
                    .findFirst().orElse(null);
            if (Objects.nonNull(custDTO)) {
                VirtualCust virtualCust = new VirtualCust();
                BeanUtils.copyProperties(custDTO, virtualCust);
                CustTypeEnum custType = CustTypeEnum.getByName(custDTO.getCustType());
                virtualCust.setCustType(custType);
                CustIdTypeEnum custIdType = CustIdTypeEnum.getByName(custDTO.getCustIdType());
                virtualCust.setCustIdType(custIdType);
                virtualCust.setLockFlag(Boolean.TRUE);
                virtualCust.setGridId(gridId);
                virtualCustMapper.insert(virtualCust);

                VirtualCustDTO virtualCustDTO = new VirtualCustDTO();
                BeanUtils.copyProperties(custDTO, virtualCustDTO);
                virtualCustDTOList.add(virtualCustDTO);
            }
        });

        List<String> userCollect = virtualGridRelateAddDTO.getVirtualCustUserDTOList().stream()
                .map(virtualCustUserDTO -> virtualCustUserDTO.getUserName()).distinct().collect(Collectors.toList());
        userCollect.forEach(userName -> {
            SysUser sysUser = sysUserMapper.selectUserByUserName(userName);
            if (Objects.isNull(sysUser)) {
                throw new ServiceException("存在未知客户经理：" + userName);
            }
            SysDept sysDept1 = sysDeptMapper.selectDeptById(sysUser.getDeptId());
            VirtualUser virtualUser = new VirtualUser();
            virtualUser.setGridId(gridId);
            virtualUser.setUserName(sysUser.getUserName());
            virtualUser.setNickName(sysUser.getNickName());
            virtualUser.setRelateType("1");
            virtualUser.setRelateDeptId(sysDept1.getDeptId());
            virtualUser.setRelateDeptName(sysDept1.getDeptName());
            virtualUser.setRelateDeptType(sysDept1.getDeptType());
            virtualUserMapper.insert(virtualUser);

            List<VirtualCustUserDTO> collect = virtualGridRelateAddDTO.getVirtualCustUserDTOList().stream()
                    .filter(virtualCustUserDTO -> virtualCustUserDTO.getUserName().equals(userName))
                    .collect(Collectors.toList());
            collect.forEach(dto -> {
                VirtualCustUser virtualCustUser = new VirtualCustUser();
                virtualCustUser.setGridId(gridId);
                virtualCustUser.setCustId(dto.getCustId());
                virtualCustUser.setUserName(userName);
                virtualCustUser.setNickName(sysUser.getNickName());
                virtualCustUser.setRelateDeptId(sysDept1.getDeptId());
                virtualCustUser.setRelateDeptName(sysDept1.getDeptName());
                virtualCustUserMapper.insert(virtualCustUser);
            });
        });
        gridInfoRedisService.cleanGridInfo("3");
        if (!virtualCustDTOList.isEmpty()){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(new Date().getTime());
            String headId = SecurityUtils.getHeadId();
            virtualGridCustService.updateToCustInfo(headId, virtualCustDTOList, SecurityUtils.getUsername(), format);
        }
        return "虚拟网格创建成功！";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateVirtualGrid(VirtualGridUpdateDTO virtualGridUpdateDTO) {

        VirtualGrid virtualGrid = virtualGridMapper.selectById(virtualGridUpdateDTO.getGridId());
        if (Objects.isNull(virtualGrid)) {
            throw new ServiceException("该虚拟网格不存在！");
        }
        if (!virtualGrid.getGridName().equals(virtualGridUpdateDTO.getGridName())
                && checkGridNameExist(virtualGridUpdateDTO.getGridName())) {
            throw new ServiceException("网格名称已存在");
        }
        if (virtualGrid.getCreateMode().equals("1")) {
            return updateVirtualGridByCustInfo(virtualGridUpdateDTO);
        } else if (virtualGrid.getCreateMode().equals("2")) {
            return updateVirtualGridByRelate(virtualGridUpdateDTO);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateVirtualGridByCustInfo(VirtualGridUpdateDTO virtualGridUpdateDTO) {
        VirtualGrid virtualGrid = virtualGridMapper.selectById(virtualGridUpdateDTO.getGridId());
        if (Objects.isNull(virtualGrid)) {
            throw new ServiceException("该虚拟网格不存在！");
        }
        BeanUtils.copyProperties(virtualGridUpdateDTO, virtualGrid);
        virtualGridMapper.updateById(virtualGrid);
        if (Objects.nonNull(virtualGridUpdateDTO.getCustDTOList()) && !virtualGridUpdateDTO.getCustDTOList().isEmpty()) {
            virtualGridUpdateDTO.getCustDTOList().forEach(virtualGridCustDTO -> {
                VirtualCust virtualCust = new VirtualCust();
                BeanUtils.copyProperties(virtualGridCustDTO, virtualCust);

                CustTypeEnum custType = CustTypeEnum.getByName(virtualGridCustDTO.getCustType());
                virtualCust.setCustType(custType);
                CustIdTypeEnum custIdType = CustIdTypeEnum.getByName(virtualGridCustDTO.getCustIdType());
                virtualCust.setCustIdType(custIdType);

                virtualCust.setGridId(virtualGridUpdateDTO.getGridId());
                virtualCustMapper.insert(virtualCust);
            });

            if (virtualGrid.getGridAssignType().equals("1")) {
                LambdaQueryWrapper<VirtualUser> userRelateLambdaQueryWrapper = new LambdaQueryWrapper<>();
                userRelateLambdaQueryWrapper.eq(VirtualUser::getGridId, virtualGridUpdateDTO.getGridId());
                List<VirtualUser> virtualUsers = virtualUserMapper.selectList(userRelateLambdaQueryWrapper);
                for (VirtualUser virtualUser : virtualUsers) {
                    virtualGridUpdateDTO.getCustDTOList().forEach(virtualGridCustDTO -> {
                        VirtualCustUser virtualCustUser = new VirtualCustUser();
                        virtualCustUser.setCustId(virtualGridCustDTO.getCustId());
                        virtualCustUser.setUserName(virtualUser.getUserName());
                        virtualCustUser.setNickName(virtualUser.getNickName());
                        virtualCustUser.setRelateDeptId(virtualUser.getRelateDeptId());
                        virtualCustUser.setRelateDeptName(virtualUser.getRelateDeptName());
                        virtualCustUser.setGridId(virtualGridUpdateDTO.getGridId());
                        virtualCustUserMapper.insert(virtualCustUser);
                    });
                }
            } else {
                LambdaQueryWrapper<VirtualGridRule> ruleLambdaQueryWrapper = new LambdaQueryWrapper<>();
                ruleLambdaQueryWrapper.eq(VirtualGridRule::getGridId, virtualGrid.getGridId())
                        .eq(VirtualGridRule::getRuleKey, 3);
                if (virtualGridRuleMapper.selectCount(ruleLambdaQueryWrapper) > 0) {
                    virtualGridUpdateDTO.getCustDTOList().forEach(virtualGridCustDTO -> {
                        VirtualCustUser virtualCustUser = new VirtualCustUser();
                        virtualCustUser.setCustId(virtualGridCustDTO.getCustId());
                        virtualCustUser.setGridId(virtualGridUpdateDTO.getGridId());
                        virtualCustUserMapper.insert(virtualCustUser);
                    });
                }
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(new Date().getTime());
            String headId = SecurityUtils.getHeadId();
            virtualGridCustService.updateToCustInfo(headId, virtualGridUpdateDTO.getCustDTOList(), SecurityUtils.getUsername(), format);
        }

        return "虚拟网格修改成功";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateVirtualGridByRelate(VirtualGridUpdateDTO virtualGridUpdateDTO) {
        VirtualGrid virtualGrid = virtualGridMapper.selectById(virtualGridUpdateDTO.getGridId());
        if (Objects.isNull(virtualGrid)) {
            throw new ServiceException("该虚拟网格不存在！");
        }
        BeanUtils.copyProperties(virtualGridUpdateDTO, virtualGrid);
        virtualGridMapper.updateById(virtualGrid);

        if (Objects.nonNull(virtualGridUpdateDTO.getVirtualCustUserDTOList()) && !virtualGridUpdateDTO.getVirtualCustUserDTOList().isEmpty()) {
            List<VirtualCustDTO> virtualCustDTOList = new ArrayList<>();
            virtualGridUpdateDTO.getVirtualCustUserDTOList().forEach(virtualCustUserDTO -> {
                SysUser sysUser = sysUserMapper.selectUserByUserName(virtualCustUserDTO.getUserName());
                SysDept sysDept = sysDeptMapper.selectDeptById(sysUser.getDeptId());

                LambdaQueryWrapper<VirtualCust> custWrapper = new LambdaQueryWrapper<>();
                custWrapper.eq(VirtualCust::getCustId, virtualCustUserDTO.getCustId())
                        .eq(VirtualCust::getGridId, virtualGridUpdateDTO.getGridId());
                if (virtualCustMapper.selectCount(custWrapper) < 1) {
                    VirtualCust virtualCust = new VirtualCust();
                    BeanUtils.copyProperties(virtualCustUserDTO, virtualCust);
                    CustTypeEnum custType = CustTypeEnum.getByName(virtualCustUserDTO.getCustType());
                    virtualCust.setCustType(custType);
                    CustIdTypeEnum custIdType = CustIdTypeEnum.getByName(virtualCustUserDTO.getCustIdType());
                    virtualCust.setCustIdType(custIdType);
                    virtualCust.setLockFlag(Boolean.TRUE);
                    virtualCust.setGridId(virtualGridUpdateDTO.getGridId());
                    virtualCustMapper.insert(virtualCust);

                    VirtualCustDTO virtualCustDTO = new VirtualCustDTO();
                    BeanUtils.copyProperties(virtualCustUserDTO, virtualCustDTO);
                    virtualCustDTOList.add(virtualCustDTO);
                }

                LambdaQueryWrapper<VirtualUser> userWrapper = new LambdaQueryWrapper<>();
                userWrapper.eq(VirtualUser::getGridId, virtualGridUpdateDTO.getGridId())
                        .eq(VirtualUser::getUserName, virtualCustUserDTO.getUserName());
                if (virtualUserMapper.selectCount(userWrapper) < 1) {
                    VirtualUser virtualUser = new VirtualUser();
                    virtualUser.setGridId(virtualGridUpdateDTO.getGridId());
                    virtualUser.setUserName(sysUser.getUserName());
                    virtualUser.setNickName(sysUser.getNickName());
                    virtualUser.setRelateType("1");
                    virtualUser.setRelateDeptId(sysDept.getDeptId());
                    virtualUser.setRelateDeptName(sysDept.getDeptName());
                    virtualUser.setRelateDeptType(sysDept.getDeptType());
                    virtualUserMapper.insert(virtualUser);
                }

                LambdaQueryWrapper<VirtualCustUser> custUserWrapper = new LambdaQueryWrapper<>();
                custUserWrapper.eq(VirtualCustUser::getGridId, virtualGridUpdateDTO.getGridId())
                        .eq(VirtualCustUser::getCustId, virtualCustUserDTO.getCustId())
                        .eq(VirtualCustUser::getUserName, virtualCustUserDTO.getUserName());
                if (virtualCustUserMapper.selectCount(custUserWrapper) < 1) {
                    VirtualCustUser virtualCustUser = new VirtualCustUser();
                    virtualCustUser.setGridId(virtualGridUpdateDTO.getGridId());
                    virtualCustUser.setCustId(virtualCustUserDTO.getCustId());
                    virtualCustUser.setUserName(virtualCustUserDTO.getUserName());
                    virtualCustUser.setNickName(sysUser.getNickName());
                    virtualCustUser.setRelateDeptId(sysDept.getDeptId());
                    virtualCustUser.setRelateDeptName(sysDept.getDeptName());
                    virtualCustUserMapper.insert(virtualCustUser);
                }
            });
            if(virtualCustDTOList.size() > 0){
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = simpleDateFormat.format(new Date().getTime());
                String headId = SecurityUtils.getHeadId();
                virtualGridCustService.updateToCustInfo(headId, virtualCustDTOList, SecurityUtils.getUsername(), format);
            }
        }

        return "虚拟网格修改成功";
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public VirtualGridVO getVirtualGrid(Long gridId) {
        VirtualGridVO virtualGridVO = new VirtualGridVO();
        VirtualGrid virtualGrid = virtualGridMapper.selectById(gridId);
        if (Objects.isNull(virtualGrid)) {
            throw new ServiceException("该虚拟网格id不存在！");
        }
        BeanUtils.copyProperties(virtualGrid, virtualGridVO);

        LambdaQueryWrapper<VirtualCust> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VirtualCust::getGridId, gridId);
        List<VirtualCust> virtualCustUsers = virtualCustMapper.selectList(queryWrapper);
        virtualGridVO.setVirtualCustUserList(virtualCustUsers);

        if (virtualGrid.getGridAssignType().equals("1")) {
            LambdaQueryWrapper<VirtualUser> userRelateLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userRelateLambdaQueryWrapper.eq(VirtualUser::getGridId, gridId);
            List<VirtualUser> virtualUsers = virtualUserMapper.selectList(userRelateLambdaQueryWrapper);
            virtualGridVO.setVirtualUserList(virtualUsers);
        } else if (virtualGrid.getGridAssignType().equals("2")) {
            LambdaQueryWrapper<VirtualGridRule> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(VirtualGridRule::getGridId, gridId);
            List<VirtualGridRule> virtualGridRules = virtualGridRuleMapper.selectList(queryWrapper1);
            virtualGridVO.setGridVirtualRulesDTOList(virtualGridRules);
        }
        return virtualGridVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteVirtualGrids(DeleteDTO deleteDTO) {
        Long deptId = SecurityUtils.getDeptId();

        deleteDTO.getIdList().forEach(gridId -> {
            VirtualGrid virtualGrid = virtualGridMapper.selectById(gridId);
            if (Objects.isNull(virtualGrid)) {
                throw new ServiceException("网格不存在或已删除！");
            }
            if (!virtualGrid.getDeptId().equals(deptId)) {
                throw new ServiceException("当前用户无权限进行删除：" + virtualGrid.getGridName());
            }
            LambdaQueryWrapper<VirtualUser> userRelateLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userRelateLambdaQueryWrapper.eq(VirtualUser::getGridId, gridId);
            virtualUserMapper.delete(userRelateLambdaQueryWrapper);

            LambdaQueryWrapper<VirtualCust> custWrapper = new LambdaQueryWrapper<>();
            custWrapper.eq(VirtualCust::getGridId, gridId);
            virtualCustMapper.delete(custWrapper);

            LambdaQueryWrapper<VirtualCustUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VirtualCustUser::getGridId, gridId);
            virtualCustUserMapper.delete(queryWrapper);

            LambdaQueryWrapper<VirtualGridRule> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(VirtualGridRule::getGridId, gridId);
            virtualGridRuleMapper.delete(queryWrapper1);

            if (virtualGrid.getGridAssignType().equals("2")) {
                LambdaQueryWrapper<VirtualGridRule> queryWrapper2 = new LambdaQueryWrapper<>();
                queryWrapper2.eq(VirtualGridRule::getGridId, gridId);
                virtualGridRuleMapper.delete(queryWrapper2);
            }
            virtualGridMapper.deleteById(gridId);
        });
        return "虚拟网格删除成功！";
    }

    /**
     * @param gridUserSelectDTO
     * @return
     */
    @Override
    public List<GridUserVO> getUserListByDeptId(GridUserSelectDTO gridUserSelectDTO) {
        return gridUserMapper.selectGridUser(gridUserSelectDTO.getDeptIds());
    }

    private boolean checkGridUserRelateExist(Long gridId, GridUserRelateDTO gridUserRelateDTO) {
        LambdaQueryWrapper<VirtualUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VirtualUser::getGridId, gridId)
                .eq(VirtualUser::getRelateType, gridUserRelateDTO.getRelateType())
                .eq(VirtualUser::getRelateDeptId, gridUserRelateDTO.getRelateDeptId())
                .eq(VirtualUser::getUserName, gridUserRelateDTO.getUserName());

        return virtualUserMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public boolean checkVirtualCustInGrid(Long gridId, VirtualCustImportDTO virtualCustImportDTO) {
        LambdaQueryWrapper<VirtualCust> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(VirtualCust::getGridId, gridId)
                .eq(VirtualCust::getCustId, virtualCustImportDTO.getCustId());
        return virtualCustMapper.selectCount(lambdaQueryWrapper) > 0;
    }

    /**
     * @param gridId
     * @param virtualCustUserImportDTO
     * @return
     */
    @Override
    public boolean checkVirtualCustRelateInGrid(Long gridId, VirtualCustUserImportDTO virtualCustUserImportDTO) {
        LambdaQueryWrapper<VirtualCustUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VirtualCustUser::getGridId, gridId)
                .eq(VirtualCustUser::getCustId, virtualCustUserImportDTO.getCustId())
                .eq(VirtualCustUser::getUserName, virtualCustUserImportDTO.getUserName());
        return virtualCustUserMapper.selectCount(queryWrapper) > 0;
    }

    /**
     * @param gridName
     * @return
     */
    @Override
    public boolean checkGridNameExist(String gridName) {
        LambdaQueryWrapper<VirtualGrid> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VirtualGrid::getGridName, gridName).eq(VirtualGrid::getDeptId, SecurityUtils.getDeptId());

        return virtualGridMapper.selectCount(queryWrapper) > 0;
    }
}
