package com.ruoyi.ibs.cmpm.service;

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.bean.BeanUtils;
import com.ruoyi.ibs.cmpm.domain.dto.GridCmpmUpdateDTO;
import com.ruoyi.ibs.cmpm.domain.entity.GridCmpm;
import com.ruoyi.ibs.cmpm.domain.entity.GridCmpmTransfer;
import com.ruoyi.ibs.cmpm.mapper.GridCmpmMapper;
import com.ruoyi.ibs.cmpm.mapper.GridCmpmTransferMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Author 吴凯程
 * @Date 2025/10/24
 **/
@Service
@Slf4j
@EnableAsync
public class GridCmpmUpdateService {

    @Resource
    private GridCmpmMapper gridCmpmMapper;
    @Resource
    private GridCmpmTransferMapper gridCmpmTransferMapper;
    @Resource
    private ISysDeptService sysDeptService;
    @Resource
    private ISysUserService sysUserService;

    @Async
    @Transactional(rollbackFor = Exception.class)
    public void updateGridCmpm(GridCmpmTransfer gridCmpmTransfer) {
        GridCmpm gridCmpm = gridCmpmMapper.getGridCmpmByCustKey(gridCmpmTransfer.getCustId(), gridCmpmTransfer.getAccountNo(), gridCmpmTransfer.getPrevUserName(),
                gridCmpmTransfer.getGridType(), gridCmpmTransfer.getHeadId());
        if (Objects.isNull(gridCmpm)){
            throw new ServiceException("绩效网格管户不存在");
        }

        SysUser sysUser = sysUserService.selectUserByUserName(gridCmpmTransfer.getNextUserName());
        if (Objects.isNull(sysUser)){
            throw new ServiceException("移交用户不存在");
        }
        gridCmpm.setUserName(sysUser.getUserName());
        gridCmpm.setNickName(sysUser.getNickName());
        if(Objects.nonNull(gridCmpmTransfer.getNextBranchId())){
            SysDept sysDept = sysDeptService.selectDeptById(gridCmpmTransfer.getNextBranchId());
            gridCmpm.setBranchId(gridCmpmTransfer.getNextBranchId());
            gridCmpm.setBranchName(sysDept.getDeptName());
        }else{
            gridCmpm.setBranchId(null);
            gridCmpm.setBranchName(null);
        }
        if(Objects.nonNull(gridCmpmTransfer.getNextOutletId())){
            SysDept sysDept = sysDeptService.selectDeptById(gridCmpmTransfer.getNextOutletId());
            gridCmpm.setOutletId(gridCmpmTransfer.getNextOutletId());
            gridCmpm.setOutletName(sysDept.getDeptName());
        }else{
            gridCmpm.setOutletId(null);
            gridCmpm.setOutletName(null);
        }
        GridCmpmUpdateDTO gridCmpmUpdateDTO = new GridCmpmUpdateDTO();
        BeanUtils.copyProperties(gridCmpm, gridCmpmUpdateDTO);
        gridCmpmUpdateDTO.setPrevUserName(gridCmpmTransfer.getPrevUserName());
        gridCmpmUpdateDTO.setDeptCode(gridCmpmTransfer.getHeadId());
        gridCmpmMapper.updateGridCmpm(gridCmpmUpdateDTO);
        log.info("更新绩效网格客户管护:" + gridCmpm);
    }

    @Async
    @Transactional(rollbackFor = Exception.class)
    public void createFullTransferApprove(GridCmpmTransfer gridCmpmTransfer){
        List<GridCmpm> gridCmpms = gridCmpmMapper.getGridCmpmByUserName(gridCmpmTransfer.getPrevUserName(), gridCmpmTransfer.getHeadId(), gridCmpmTransfer.getGridType());
        if (!gridCmpms.isEmpty()){
            List<GridCmpmTransfer> gridCmpmTransferList = new ArrayList<>();
            for (GridCmpm gridCmpm : gridCmpms) {
                GridCmpmTransfer transfer = new GridCmpmTransfer();
                BeanUtils.copyProperties(gridCmpmTransfer, transfer);
                transfer.setCustId(gridCmpm.getCustId());
                transfer.setCustName(gridCmpm.getCustName());
                transfer.setCustType(gridCmpm.getCustType());
                transfer.setAccountNo(gridCmpm.getAccountNo());
                gridCmpmTransferList.add(transfer);
                if (gridCmpmTransferList.size() == 1000){
                    gridCmpmTransferMapper.batchInsert(gridCmpmTransferList);
                    gridCmpmTransferList.clear();
                    log.info("开始异步插入第{}个审批，总共有{}个", gridCmpmTransferList.size(), gridCmpms.size());
                }
            }
            if (!gridCmpmTransferList.isEmpty()){
                gridCmpmTransferMapper.batchInsert(gridCmpmTransferList);
            }
            log.info("开始异步插入第{}个审批，总共有{}个", gridCmpmTransferList.size(), gridCmpms.size());
        }else{
            log.info("该用户没有客户需要移交: " + gridCmpmTransfer.getPrevUserName());
        }
        log.info("全量移交审批流程已插入:" + gridCmpmTransfer.getPrevUserName());
    }


}
