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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.ibs.grid.domain.dto.GridUserDTO;
import com.ruoyi.ibs.grid.domain.entity.RegionAdminDivisionRelate;
import com.ruoyi.ibs.grid.domain.entity.RegionCustUser;
import com.ruoyi.ibs.grid.domain.entity.RegionGrid;
import com.ruoyi.ibs.grid.domain.entity.RegionUserRelate;
import com.ruoyi.ibs.grid.domain.vo.CustVO;
import com.ruoyi.ibs.grid.mapper.RegionAdminDivisionRelateMapper;
import com.ruoyi.ibs.grid.mapper.RegionCustUserMapper;
import com.ruoyi.ibs.grid.mapper.RegionGridMapper;
import com.ruoyi.ibs.grid.mapper.RegionUserRelateMapper;
import com.ruoyi.ibs.grid.service.RegionGridUpdateService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

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

/**
 * @Author 吴凯程
 * @Date 2025/2/8
 **/
@Service
@Slf4j
@EnableAsync
public class RegionGridUpdateServiceImpl implements RegionGridUpdateService {

    @Resource
    private RegionCustUserMapper regionCustUserMapper;

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private RegionGridMapper regionGridMapper;

    @Resource
    private RegionAdminDivisionRelateMapper regionAdminDivisionRelateMapper;

    @Resource
    private RegionUserRelateMapper regionUserRelateMapper;


    /**
     * @param gridId
     */
    @Override
    @Async
    public void updateTopGrid(Long gridId) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("开始更新网格：{}", gridId);
        RegionGrid regionGrid = regionGridMapper.selectById(gridId);
        SysUser creator = sysUserMapper.selectUserByUserName(regionGrid.getCreateBy());
        SysDept createDept = sysDeptMapper.selectDeptById(regionGrid.getDeptId());
        String deptCode = String.valueOf(regionGrid.getDeptId()).substring(0, 3);

        regionCustUserMapper.batchDeleteTopGrid(gridId, deptCode);

        LambdaQueryWrapper<RegionAdminDivisionRelate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RegionAdminDivisionRelate::getGridId, gridId);
        List<String> codeList = regionAdminDivisionRelateMapper.selectList(queryWrapper).stream().map(RegionAdminDivisionRelate::getCode).collect(Collectors.toList());

        LambdaQueryWrapper<RegionUserRelate> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(RegionUserRelate::getGridId, gridId);
        List<RegionUserRelate> deptList = regionUserRelateMapper.selectList(queryWrapper1);
        String branchIds = deptList.stream().map(regionUserRelate -> regionUserRelate.getRelateDeptId().toString()).collect(Collectors.joining(","));
        String branchNames = deptList.stream().map(regionUserRelate -> regionUserRelate.getRelateDeptName()).collect(Collectors.joining(","));

        int pageNum = 1;
        int pageSize = 10000;
        PageInfo<CustVO> pageInfo;
        do {
            PageHelper.startPage(pageNum, pageSize);
            List<CustVO> custVOS = new ArrayList<>();
            if(regionGrid.getOpsDept().equals("0")){
                custVOS = regionCustUserMapper.selectPrivateCustByCode(codeList, deptCode);
            }else if(regionGrid.getOpsDept().equals("1")){
                custVOS = regionCustUserMapper.selectPublicCustByCode(codeList, deptCode);
            }else if(regionGrid.getOpsDept().equals("2")){
                custVOS = regionCustUserMapper.selectCustByCode(codeList, deptCode);
            }
            pageInfo = new PageInfo<>(custVOS);
            List<RegionCustUser> custUserList = new ArrayList<>();
            for (CustVO custVO : pageInfo.getList()) {
                RegionCustUser regionCustUser = new RegionCustUser();
                regionCustUser.setTopGridId(regionGrid.getGridId());
                regionCustUser.setTopGridName(regionGrid.getGridName());
                regionCustUser.setOpsDept(regionGrid.getOpsDept());
                BeanUtils.copyProperties(custVO, regionCustUser);
                regionCustUser.setCreator(regionGrid.getCreateBy());
                regionCustUser.setCreatorId(creator.getUserId());
                regionCustUser.setCreatorName(creator.getNickName());
                regionCustUser.setCreateDeptId(regionGrid.getDeptId());
                regionCustUser.setCreateDeptName(createDept.getDeptName());
                regionCustUser.setBranchIds(branchIds);
                regionCustUser.setBranchNames(branchNames);
                custUserList.add(regionCustUser);
            }
            regionCustUserMapper.batchInsert(custUserList, deptCode);
            pageNum++;
        } while (pageNum <= pageInfo.getPages());
        log.info("开始更新归属二级网格：{}", gridId);
        List<Long> secGridIdByTopGridId = regionGridMapper.getSecGridIdByTopGridId(gridId);
        if(!secGridIdByTopGridId.isEmpty()){
            for (Long secGridId : secGridIdByTopGridId) {
                updateSecGrid(secGridId);
            }
        }
        log.info("更新网格：{}成功", gridId);
    }

    /**
     * @param gridId
     */
    @Override
    @Async
    public void updateSecGrid(Long gridId) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("开始更新二级网格：{}", gridId);
        RegionGrid regionGrid = regionGridMapper.selectById(gridId);
        if (Objects.isNull(regionGrid)){
            log.error("二级网格不存在：{}", gridId);
            return;
        }
        String deptCode = String.valueOf(regionGrid.getDeptId()).substring(0, 3);
        RegionGrid parentGrid = regionGridMapper.selectById(regionGrid.getParentGridId());

        LambdaQueryWrapper<RegionAdminDivisionRelate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RegionAdminDivisionRelate::getGridId, gridId);
        List<String> code = regionAdminDivisionRelateMapper.selectList(queryWrapper).stream().map(RegionAdminDivisionRelate::getCode).collect(Collectors.toList());

        LambdaQueryWrapper<RegionUserRelate> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(RegionUserRelate::getGridId, gridId);
        List<RegionUserRelate> regionUserRelates = regionUserRelateMapper.selectList(queryWrapper1);
        List<GridUserDTO> userList = regionUserRelates.stream().map(regionUserRelate -> {
            GridUserDTO gridUserDTO = new GridUserDTO();
            gridUserDTO.setUserName(regionUserRelate.getUserName());
            gridUserDTO.setNickName(regionUserRelate.getNickName() + "-" + regionUserRelate.getUserName());
            if (regionUserRelate.getRelateDeptType().equals("outlet")) {
                gridUserDTO.setOutletId(regionUserRelate.getRelateDeptId());
                gridUserDTO.setOutletName(regionUserRelate.getRelateDeptName());
            }
            return gridUserDTO;
        }).collect(Collectors.toList());
        List<Long> collect = userList.stream().filter(gridUserDTO -> Objects.nonNull(gridUserDTO.getOutletId())).map(gridUserDTO -> gridUserDTO.getOutletId()).distinct().collect(Collectors.toList());
        String outletIds = null;
        String outletNames = null;
        if (collect.size() > 0) {
            outletIds = collect.stream().map(String::valueOf).collect(Collectors.joining(","));
            outletNames = userList.stream().filter(gridUserDTO -> Objects.nonNull(gridUserDTO.getOutletName()))
                    .map(gridUserDTO -> gridUserDTO.getOutletName()).distinct().collect(Collectors.joining(","));
        }
        String userNames = userList.stream().map(gridUserDTO -> gridUserDTO.getUserName()).distinct().collect(Collectors.joining(","));
        String nickNames = userList.stream().map(gridUserDTO -> gridUserDTO.getNickName()).distinct().collect(Collectors.joining(","));
        regionCustUserMapper.batchUpdate(deptCode, code, parentGrid.getGridId(), regionGrid.getGridId(), regionGrid.getGridName(),outletIds, outletNames, userNames, nickNames);
        log.info("更新二级网格：{}成功", gridId);
    }

    /**
     * @param gridId
     */
    @Override
    @Async
    public void deleteTopGrid(String dept,Long gridId) {
        log.info("开始删除一级网格：{}", gridId);
        regionCustUserMapper.batchDeleteTopGrid(gridId, dept);
        log.info("删除一级网格：{}成功", gridId);
    }

    /**
     * @param gridId
     */
    @Override
    @Async
    public void deleteSecGrid(String dept, Long gridId) {
        log.info("开始删除二级网格：{}", gridId);
        regionCustUserMapper.batchDeleteSecGrid(gridId, dept);
        log.info("删除二级网格：{}成功", gridId);
    }

    /**
     *
     */
    @Override
    @Async
    public void updateCurrentGrid() {
        log.info("开始全量更新行政网格");
        LambdaQueryWrapper<RegionGrid> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RegionGrid::getGridLevel, "1");
        List<RegionGrid> regionGrids = regionGridMapper.selectList(queryWrapper);
        regionGrids.forEach(regionGrid -> {
            updateTopGrid(regionGrid.getGridId());
        });
    }
}
