package com.qianhe.zqcz.xmrylsjl.service.impl;

import com.qianhe.common.enums.zqcz.ZqczRyflEnum;
import com.qianhe.common.enums.zqcz.ZqczXmlbEnum;
import com.qianhe.common.enums.zqcz.ZqczXmzyEnum;
import com.qianhe.common.exception.BusinessException;
import com.qianhe.common.utils.MtUtils;
import com.qianhe.common.utils.SecurityUtils;
import com.qianhe.zqcz.pwxx.domain.ZqczPwxxQuery;
import com.qianhe.zqcz.pwxx.domain.ZqczPwxxVo;
import com.qianhe.zqcz.pwxx.mapper.ZqczPwxxMapper;
import com.qianhe.zqcz.ryxx.domain.ZqczRyxxQuery;
import com.qianhe.zqcz.ryxx.domain.ZqczRyxxVo;
import com.qianhe.zqcz.ryxx.mapper.ZqczRyxxMapper;
import com.qianhe.zqcz.xmgl.domain.ZqczXmglQuery;
import com.qianhe.zqcz.xmgl.domain.ZqczXmglVo;
import com.qianhe.zqcz.xmgl.mapper.ZqczXmglMapper;
import com.qianhe.zqcz.xmrylsjl.domain.ZqczXmRyLsjl;
import com.qianhe.zqcz.xmrylsjl.domain.ZqczXmRyLsjlQuery;
import com.qianhe.zqcz.xmrylsjl.domain.ZqczXmRyLsjlVo;
import com.qianhe.zqcz.xmrylsjl.mapper.ZqczXmRyLsjlMapper;
import com.qianhe.zqcz.xmrylsjl.service.ZqczXmRyLsjlService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Slf4j
@Service
public class ZqczXmRyLsjlServiceImpl implements ZqczXmRyLsjlService {
    @Autowired
    private ZqczXmRyLsjlMapper zqczXmRyLsjlMapper;
    @Autowired
    private ZqczXmglMapper zqczXmglMapper;
    @Autowired
    private ZqczRyxxMapper zqczRyxxMapper;
    @Autowired
    private ZqczPwxxMapper zqczPwxxMapper;

    /**
     * 条件查询
     */
    @Override
    public List<ZqczXmRyLsjlVo> list(ZqczXmRyLsjlQuery zqczXmRyLsjlQuery) {
        List<ZqczXmRyLsjlVo> dataList = zqczXmRyLsjlMapper.list(zqczXmRyLsjlQuery);
        return dataList;
    }

    /**
     * 开始抽检 参数校验
     *
     * @param zqczXmRyLsjlQuery
     */
    private void spotCheckAddCheck(ZqczXmRyLsjlQuery zqczXmRyLsjlQuery) {
        if (zqczXmRyLsjlQuery.getZsrwid() == null) {
            throw new BusinessException("直属任务ID不能为空");
        }
        if (zqczXmRyLsjlQuery.getJcrwid() == null) {
            throw new BusinessException("基层任务ID不能为空");
        }
        if (zqczXmRyLsjlQuery.getJcdwid() == null) {
            throw new BusinessException("基层单位ID不能为空");
        }
        if (zqczXmRyLsjlQuery.getXmzy() == null) {
            throw new BusinessException("项目专业不能为空");
        }
        if (!ZqczXmzyEnum.getAllCodes().contains(zqczXmRyLsjlQuery.getXmzy())) {
            throw new BusinessException(String.format("项目专业必须为%s, 当前值 %s", ZqczXmzyEnum.getAllCodeNameStrWithSeparator(), zqczXmRyLsjlQuery.getXmzy()));
        }
    }

    /**
     * 根据基层任务ID 开始项目抽检
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int spotCheckAdd(ZqczXmRyLsjlQuery zqczXmRyLsjlQuery) {
        // 数据校验
        spotCheckAddCheck(zqczXmRyLsjlQuery);

        // 项目字段
        Long zsrwid = zqczXmRyLsjlQuery.getZsrwid();  // 直属任务ID
        Long jcrwid = zqczXmRyLsjlQuery.getJcrwid();  // 基层任务ID
        Long jcdwid = zqczXmRyLsjlQuery.getJcdwid();  // 基层单位ID
        String xmzy = zqczXmRyLsjlQuery.getXmzy();  // 项目专业 1-采油 2-集输

        // 根据基层任务ID 删除已经抽取的项目
        zqczXmRyLsjlMapper.deleteByJcrwid(jcrwid);

        // 查询评委信息
        List<ZqczPwxxVo> pwxxList = zqczPwxxMapper.list(ZqczPwxxQuery.builder().jcrwid(jcrwid).build());
        if (Optional.ofNullable(pwxxList).orElse(Collections.emptyList()).isEmpty()) {
            throw new BusinessException("未录入评委信息");
        }

        // 1. 查询人员信息（姓名、单位、分类）
        List<ZqczRyxxVo> ryxxList = zqczRyxxMapper.list(ZqczRyxxQuery.builder().jcrwid(jcrwid).build());
        if (Optional.ofNullable(ryxxList).orElse(Collections.emptyList()).isEmpty()) {
            throw new BusinessException("未录入在岗人员信息");
        }

        // 2. 根据项目专业 查询项目信息
        List<ZqczXmglVo> xmglList = zqczXmglMapper.list(ZqczXmglQuery.builder().xmzy(xmzy).build());
        if (Optional.ofNullable(xmglList).orElse(Collections.emptyList()).isEmpty()) {
            throw new BusinessException("未录入项目信息");
        }

        // 3. 处理人员信息，按照人员分类 分组
        Map<String, List<ZqczRyxxVo>> ryxxGroupByRyflMap = ryxxList.stream().collect(Collectors.groupingBy(ZqczRyxxVo::getRyfl));
        List<ZqczRyxxVo> ldbzRyxxList = ryxxGroupByRyflMap.get(ZqczRyflEnum.LINGDAO_BANZI.getCode());  // 领导班子 人员信息
        List<ZqczRyxxVo> ssyzxRyxxList = ryxxGroupByRyflMap.get(ZqczRyflEnum.SANSHI_YIZHONGXIN.getCode());  // 三室一中心 人员信息
        List<ZqczRyxxVo> bzRyxxList = ryxxGroupByRyflMap.get(ZqczRyflEnum.BANZHAN.getCode());  // 班站 人员信息

        List<String> bzDistinctList = new ArrayList<>(); // 获取所有班站单位(去重)
        if (!Optional.ofNullable(bzRyxxList).orElse(Collections.emptyList()).isEmpty()) {
            bzDistinctList = bzRyxxList.stream().map(ZqczRyxxVo::getDwmc).distinct().collect(Collectors.toList());// 获取所有班站单位（去重）
        }

        // 4. 处理项目信息，按照项目类别 分组
        Map<String, List<ZqczXmglVo>> xmglGroupByXmlbMap = xmglList.stream().collect(Collectors.groupingBy(ZqczXmglVo::getXmlb));
        List<ZqczXmglVo> gryjXmglList = xmglGroupByXmlbMap.get(ZqczXmlbEnum.GEREN_YINGJI.getCode());  // 个人应急项目
        List<ZqczXmglVo> tdyjXmglList = xmglGroupByXmlbMap.get(ZqczXmlbEnum.TUANDUI_YINGJI.getCode());  // 团队应急项目
        List<ZqczXmglVo> drsrXmglList = xmglGroupByXmlbMap.get(ZqczXmlbEnum.DANREN_SHUANGREN.getCode());  // 单人/双人操作项目
        List<ZqczXmglVo> tdczXmglList = xmglGroupByXmlbMap.get(ZqczXmlbEnum.TUANDUI_CAOZUO.getCode());  // 团队操作项目

        List<ZqczRyxxVo> desigRyxxList = new ArrayList<>();  // 抽取指定班站 人员信息
        if (!Optional.ofNullable(bzDistinctList).orElse(Collections.emptyList()).isEmpty()) {
            if (ZqczXmzyEnum.CAIYOU_ZHUANYE.getCode().equals(xmzy)) {
                String lockbzName = MtUtils.getRandomList(bzDistinctList, 1).get(0);
                if (!Optional.ofNullable(bzRyxxList).orElse(Collections.emptyList()).isEmpty()) {
                    desigRyxxList = bzRyxxList.stream().filter(item -> lockbzName.equals(item.getDwmc())).collect(Collectors.toList());// 从指定班站中抽取人员信息
                }
            } else {
                List<String> spotBzList = MtUtils.getRandomList(bzDistinctList, 3);
                if (!Optional.ofNullable(bzRyxxList).orElse(Collections.emptyList()).isEmpty()) {
                    desigRyxxList = bzRyxxList.stream().filter(item -> spotBzList.contains(item.getDwmc())).collect(Collectors.toList());// 从指定班站中抽取人员信息
                }
            }
        }

        List<ZqczXmRyLsjl> finalList = new ArrayList<>();

        if (ZqczXmzyEnum.CAIYOU_ZHUANYE.getCode().equals(xmzy)) {  // 采油专业 项目抽取
            // 5. 处理 个人应急项目（从领导班子中抽取一人、抽三室一中心抽取两人、从班站中抽取一人） 先抽人，在抽项目
            List<ZqczXmRyLsjl> finalCyGryjxmList = spotCheckCyGryjxm(zsrwid, jcrwid, jcdwid, ldbzRyxxList, ssyzxRyxxList, desigRyxxList, gryjXmglList);
            if (!Optional.ofNullable(finalCyGryjxmList).orElse(Collections.emptyList()).isEmpty()) {
                finalList.addAll(finalCyGryjxmList);
            }

            // 6. 处理 标准化操作项目 （根据项目人数从班站中随机抽取指定人数）先抽项目，在抽人
            ZqczXmRyLsjl finalCyBzhczxm = spotCheckCyBzhczxm(zsrwid, jcrwid, jcdwid, desigRyxxList, drsrXmglList, tdczXmglList);
            if (finalCyBzhczxm != null) {
                finalList.add(finalCyBzhczxm);
            }

            // 7. 处理 团队应急项目（从领导班子中抽取一人、抽三室一中心抽取两人、在根据剩余项目人数从班站中抽取）先抽项目，在抽人
            ZqczXmRyLsjl finalCjTdyjxm = spotCheckCyTdyjxm(zsrwid, jcrwid, jcdwid, ldbzRyxxList, ssyzxRyxxList, desigRyxxList, tdyjXmglList);
            if (finalCjTdyjxm != null) {
                finalList.add(finalCjTdyjxm);
            }
        } else {  // 集输专业 项目抽取
            // 1. 抽检个人应急项目（从领导班子中抽取一人、抽三室一中心抽取两人、从选中的3个班站中各抽取一人） 先抽人，在抽项目
            List<ZqczXmRyLsjl> finalJsGryjxmList = spotCheckJsGryjxm(zsrwid, jcrwid, jcdwid, ldbzRyxxList, ssyzxRyxxList, desigRyxxList, gryjXmglList);
            if (!Optional.ofNullable(finalJsGryjxmList).orElse(Collections.emptyList()).isEmpty()) {
                finalList.addAll(finalJsGryjxmList);
            }

            // 2. 抽检 标准化操作项目（从选中的3个班站中各抽取2人） 先抽人，在抽项目
            List<ZqczXmRyLsjl> finalJsBzhczxmList = spotCheckJsBzhczxm(zsrwid, jcrwid, jcdwid, desigRyxxList, drsrXmglList, tdczXmglList);
            if (!Optional.ofNullable(finalJsBzhczxmList).orElse(Collections.emptyList()).isEmpty()) {
                finalList.addAll(finalJsBzhczxmList);
            }

            // 3. 抽检 团队应急项目 （第一个班站从领导班子中抽取一人、抽三室一中心抽取一人、班站成员自行选择，默认情况下选择所有的班站成员，其余班站随机抽取项目，班站人员默认全部）
            List<ZqczXmRyLsjl> finalJsTdyjxmList = spotCheckJsTdyjxm(zsrwid, jcrwid, jcdwid, ldbzRyxxList, ssyzxRyxxList, desigRyxxList, tdyjXmglList);
            if (!Optional.ofNullable(finalJsTdyjxmList).orElse(Collections.emptyList()).isEmpty()) {
                finalList.addAll(finalJsTdyjxmList);
            }
        }

        return zqczXmRyLsjlMapper.batchAdd(finalList);
    }

    /**
     * 修改
     */
    @Override
    public int edit(ZqczXmRyLsjl zqczXmRyLsjl, String username) {
        if (zqczXmRyLsjl.getXmryid() == null) {
            throw new BusinessException("项目人员ID不能为空");
        }
        zqczXmRyLsjl.setUpdate_by(username);
        return zqczXmRyLsjlMapper.edit(zqczXmRyLsjl);
    }

    /**
     * 抽检 个人应急项目（采油）
     *
     * @param zsrwid        直属任务ID
     * @param jcrwid        基层任务ID
     * @param jcdwid        基层单位ID
     * @param ldbzRyxxList  领导班子 人员信息
     * @param ssyzxRyxxList 三室一中心 人员信息
     * @param desigRyxxList 抽取指定班站 人员信息
     * @param gryjXmglList  个人应急项目
     * @return
     */
    private List<ZqczXmRyLsjl> spotCheckCyGryjxm(Long zsrwid, Long jcrwid, Long jcdwid, List<ZqczRyxxVo> ldbzRyxxList, List<ZqczRyxxVo> ssyzxRyxxList, List<ZqczRyxxVo> desigRyxxList, List<ZqczXmglVo> gryjXmglList) {
        List<ZqczXmRyLsjl> dataList = new ArrayList<>();

        ZqczRyxxVo lockldbzRyxxVo = null;  // 锁定 领导班子
        if (!Optional.ofNullable(ldbzRyxxList).orElse(Collections.emptyList()).isEmpty()) {
            List<ZqczRyxxVo> spotLdbzList = MtUtils.getRandomList(ldbzRyxxList, 1);  // 抽取 领导班子 人员信息
            lockldbzRyxxVo = spotLdbzList.get(0);
        }

        List<ZqczRyxxVo> spotLdbzList = new ArrayList<>();  // 锁定 三室一中心
        if (!Optional.ofNullable(ssyzxRyxxList).orElse(Collections.emptyList()).isEmpty()) {
            if (ssyzxRyxxList.size() >= 2) {  // 这里判断主要是为了判断 如果 三室一中心中的人数不够2个人
                spotLdbzList = MtUtils.getRandomList(ssyzxRyxxList, 2);  // 抽取 三室一中心 人员信息
            } else {
                spotLdbzList = MtUtils.getRandomList(ssyzxRyxxList, 1);  // 抽取 三室一中心 人员信息
            }
        }

        ZqczRyxxVo lockbzRyxxVo = null;  // 锁定 班站
        if (!Optional.ofNullable(desigRyxxList).orElse(Collections.emptyList()).isEmpty()) {
            List<ZqczRyxxVo> spotbzList = MtUtils.getRandomList(desigRyxxList, 1);  // 抽取 班站 人员信息
            lockbzRyxxVo = spotbzList.get(0);
        }

        // 需要处理个人应急项目为空的情况
        if (!Optional.ofNullable(gryjXmglList).orElse(Collections.emptyList()).isEmpty()) {
            if (lockldbzRyxxVo != null) {
                ZqczXmglVo lockldbzXmgl = MtUtils.getRandomList(gryjXmglList, 1).get(0);  // 领导班子 锁定项目
                dataList.add(new ZqczXmRyLsjl(zsrwid, jcrwid, jcdwid, lockldbzRyxxVo, lockldbzXmgl));
            }
            if (!Optional.ofNullable(spotLdbzList).orElse(Collections.emptyList()).isEmpty()) {
                for (ZqczRyxxVo zqczRyxxVo : spotLdbzList) {
                    ZqczXmglVo lockssyzxXmgl = MtUtils.getRandomList(gryjXmglList, 1).get(0);  // 三室一中心 锁定项目
                    dataList.add(new ZqczXmRyLsjl(zsrwid, jcrwid, jcdwid, zqczRyxxVo, lockssyzxXmgl));
                }
            }
            if (lockbzRyxxVo != null) {
                ZqczXmglVo lockbzXmgl = MtUtils.getRandomList(gryjXmglList, 1).get(0);  // 班站 锁定项目
                dataList.add(new ZqczXmRyLsjl(zsrwid, jcrwid, jcdwid, lockbzRyxxVo, lockbzXmgl));
            }
        }
        return dataList;
    }

    /**
     * 抽检 标准化操作项目（采油）
     *
     * @param zsrwid        直属任务ID
     * @param jcrwid        基层任务ID
     * @param jcdwid        基层单位ID
     * @param desigRyxxList 抽取指定班站 人员信息
     * @param drsrXmglList  单人/双人操作项目
     * @param tdczXmglList  团队操作项目
     * @return
     */
    private ZqczXmRyLsjl spotCheckCyBzhczxm(Long zsrwid, Long jcrwid, Long jcdwid, List<ZqczRyxxVo> desigRyxxList, List<ZqczXmglVo> drsrXmglList, List<ZqczXmglVo> tdczXmglList) {
        // 合并单人/双人操作项目，团队操作项目
        List<ZqczXmglVo> bzhczXmglList = new ArrayList<>();
        if (!Optional.ofNullable(drsrXmglList).orElse(Collections.emptyList()).isEmpty()) {
            bzhczXmglList.addAll(drsrXmglList);
        }
        if (!Optional.ofNullable(tdczXmglList).orElse(Collections.emptyList()).isEmpty()) {
            bzhczXmglList.addAll(tdczXmglList);
        }
        // 抽检项目
        if (Optional.ofNullable(bzhczXmglList).orElse(Collections.emptyList()).isEmpty()) {
            return null;
        }
        // 判断班站人员信息是否为空
        if (Optional.ofNullable(desigRyxxList).orElse(Collections.emptyList()).isEmpty()) {
            return null;
        }
        ZqczXmglVo bzhczXmglVo = MtUtils.getRandomList(bzhczXmglList, 1).get(0);
        Integer xmrs = bzhczXmglVo.getXmrs();// 获取项目所需要的人数

        // 最终的人员信息列表
        List<ZqczRyxxVo> finalList = desigRyxxList.size() <= xmrs ? desigRyxxList : MtUtils.getRandomList(desigRyxxList, xmrs);

        String dwmc = finalList.stream().map(ZqczRyxxVo::getDwmc).distinct().collect(Collectors.joining(","));
        String rymc = finalList.stream().map(ZqczRyxxVo::getRymc).distinct().collect(Collectors.joining(","));

        ZqczXmRyLsjl xmRyLsjl = new ZqczXmRyLsjl(zsrwid, jcrwid, jcdwid, bzhczXmglVo, dwmc, rymc);
        return xmRyLsjl;
    }

    /**
     * 抽检 团队应急项目（采油）
     *
     * @param zsrwid        直属任务ID
     * @param jcrwid        基层任务ID
     * @param jcdwid        基层单位ID
     * @param ldbzRyxxList  领导班子 人员信息
     * @param ssyzxRyxxList 三室一中心 人员信息
     * @param desigRyxxList 抽取指定班站 人员信息
     * @param tdyjXmglList  团队应急项目
     * @return
     */
    private ZqczXmRyLsjl spotCheckCyTdyjxm(Long zsrwid, Long jcrwid, Long jcdwid, List<ZqczRyxxVo> ldbzRyxxList, List<ZqczRyxxVo> ssyzxRyxxList, List<ZqczRyxxVo> desigRyxxList, List<ZqczXmglVo> tdyjXmglList) {
        // 抽检项目
        if (Optional.ofNullable(tdyjXmglList).orElse(Collections.emptyList()).isEmpty()) {
            return null;
        }
        // 获取项目所需要的人数
        ZqczXmglVo bzhczXmglVo = MtUtils.getRandomList(tdyjXmglList, 1).get(0);
        Integer xmrs = bzhczXmglVo.getXmrs();
        if (xmrs == null) {
            return null;
        }

        List<ZqczRyxxVo> finalList = new ArrayList<>();
        // 始终锁定领导班子（xmrs >= 1）
        if (!Optional.ofNullable(ldbzRyxxList).orElse(Collections.emptyList()).isEmpty()) {
            finalList.add(MtUtils.getRandomList(ldbzRyxxList, 1).get(0));
        }
        // xmrs >= 2 时叠加三室一中心
        if (xmrs >= 2 && !Optional.ofNullable(ssyzxRyxxList).orElse(Collections.emptyList()).isEmpty()) {
            finalList.add(MtUtils.getRandomList(ssyzxRyxxList, 1).get(0));
        }
        // xmrs >= 3 时叠加班站人员
        if (xmrs >= 3 && !Optional.ofNullable(desigRyxxList).orElse(Collections.emptyList()).isEmpty()) {
            int bzNum = xmrs - 2;
            finalList.addAll(MtUtils.getRandomList(desigRyxxList, bzNum));
        }

        String dwmc = finalList.stream().map(ZqczRyxxVo::getDwmc).distinct().collect(Collectors.joining(","));
        String rymc = finalList.stream().map(ZqczRyxxVo::getRymc).distinct().collect(Collectors.joining(","));


        ZqczXmRyLsjl xmRyLsjl = new ZqczXmRyLsjl(zsrwid, jcrwid, jcdwid, bzhczXmglVo, dwmc, rymc);
        return xmRyLsjl;
    }

    /**
     * 抽检 个人应急项目（集输）
     *
     * @param zsrwid        直属任务ID
     * @param jcrwid        基层任务ID
     * @param jcdwid        基层单位ID
     * @param ldbzRyxxList  领导班子 人员信息
     * @param ssyzxRyxxList 三室一中心 人员信息
     * @param desigRyxxList 抽取指定班站 人员信息
     * @param gryjXmglList  个人应急项目
     * @return
     */
    private List<ZqczXmRyLsjl> spotCheckJsGryjxm(Long zsrwid, Long jcrwid, Long jcdwid, List<ZqczRyxxVo> ldbzRyxxList, List<ZqczRyxxVo> ssyzxRyxxList, List<ZqczRyxxVo> desigRyxxList, List<ZqczXmglVo> gryjXmglList) {
        List<ZqczXmRyLsjl> dataList = new ArrayList<>();

        ZqczRyxxVo lockldbzRyxxVo = null;  // 锁定 领导班子
        if (!Optional.ofNullable(ldbzRyxxList).orElse(Collections.emptyList()).isEmpty()) {
            List<ZqczRyxxVo> spotLdbzList = MtUtils.getRandomList(ldbzRyxxList, 1);  // 抽取 领导班子 人员信息
            lockldbzRyxxVo = spotLdbzList.get(0);
        }

        List<ZqczRyxxVo> spotLdbzList = new ArrayList<>();  // 锁定 三室一中心
        if (!Optional.ofNullable(ssyzxRyxxList).orElse(Collections.emptyList()).isEmpty()) {
            if (ssyzxRyxxList.size() >= 2) {  // 这里判断主要是为了判断 如果 三室一中心中的人数不够2个人
                spotLdbzList = MtUtils.getRandomList(ssyzxRyxxList, 2);  // 抽取 三室一中心 人员信息
            } else {
                spotLdbzList = MtUtils.getRandomList(ssyzxRyxxList, 1);  // 抽取 三室一中心 人员信息
            }
        }

        List<ZqczRyxxVo> spotbzList = new ArrayList<>();  // 锁定 班站
        Map<String, List<ZqczRyxxVo>> spotbzGroupMap = desigRyxxList.stream().collect(Collectors.groupingBy(ZqczRyxxVo::getDwmc));  // 根据班站名称分组
        spotbzGroupMap.forEach((key, value) -> {
            if (!Optional.ofNullable(value).orElse(Collections.emptyList()).isEmpty()) {
                spotbzList.add(MtUtils.getRandomList(value, 1).get(0));
            }
        });

        // 需要处理个人应急项目为空的情况
        if (!Optional.ofNullable(gryjXmglList).orElse(Collections.emptyList()).isEmpty()) {
            if (lockldbzRyxxVo != null) {
                ZqczXmglVo lockldbzXmgl = MtUtils.getRandomList(gryjXmglList, 1).get(0);  // 领导班子 锁定项目
                dataList.add(new ZqczXmRyLsjl(zsrwid, jcrwid, jcdwid, lockldbzRyxxVo, lockldbzXmgl));
            }
            if (!Optional.ofNullable(spotLdbzList).orElse(Collections.emptyList()).isEmpty()) {
                for (ZqczRyxxVo zqczRyxxVo : spotLdbzList) {
                    ZqczXmglVo lockssyzxXmgl = MtUtils.getRandomList(gryjXmglList, 1).get(0);  // 三室一中心 锁定项目
                    dataList.add(new ZqczXmRyLsjl(zsrwid, jcrwid, jcdwid, zqczRyxxVo, lockssyzxXmgl));
                }
            }
            if (!Optional.ofNullable(spotbzList).orElse(Collections.emptyList()).isEmpty()) {
                for (ZqczRyxxVo zqczRyxxVo : spotbzList) {
                    ZqczXmglVo lockbzXmgl = MtUtils.getRandomList(gryjXmglList, 1).get(0);  // 班站 锁定项目
                    dataList.add(new ZqczXmRyLsjl(zsrwid, jcrwid, jcdwid, zqczRyxxVo, lockbzXmgl));
                }
            }
        }
        return dataList;
    }

    /**
     * 抽检 标准化操作项目（集输）
     *
     * @param zsrwid        直属任务ID
     * @param jcrwid        基层任务ID
     * @param jcdwid        基层单位ID
     * @param desigRyxxList 抽取指定班站 人员信息
     * @param drsrXmglList  单人/双人操作项目
     * @param tdczXmglList  团队操作项目
     * @return
     */
    private List<ZqczXmRyLsjl> spotCheckJsBzhczxm(Long zsrwid, Long jcrwid, Long jcdwid, List<ZqczRyxxVo> desigRyxxList, List<ZqczXmglVo> drsrXmglList, List<ZqczXmglVo> tdczXmglList) {
        List<ZqczXmRyLsjl> dataList = new ArrayList<>();

        // 合并单人/双人操作项目，团队操作项目
        List<ZqczXmglVo> bzhczXmglList = new ArrayList<>();
        if (!Optional.ofNullable(drsrXmglList).orElse(Collections.emptyList()).isEmpty()) {
            bzhczXmglList.addAll(drsrXmglList);
        }
        if (!Optional.ofNullable(tdczXmglList).orElse(Collections.emptyList()).isEmpty()) {
            bzhczXmglList.addAll(tdczXmglList);
        }

        // 抽检项目
        if (Optional.ofNullable(bzhczXmglList).orElse(Collections.emptyList()).isEmpty()) {
            return dataList;
        }
        // 判断班站人员信息是否为空
        if (Optional.ofNullable(desigRyxxList).orElse(Collections.emptyList()).isEmpty()) {
            return dataList;
        }

        // 抽人
        List<ZqczRyxxVo> spotbzList = new ArrayList<>();  // 锁定 班站
        Map<String, List<ZqczRyxxVo>> spotbzGroupMap = desigRyxxList.stream().collect(Collectors.groupingBy(ZqczRyxxVo::getDwmc));  // 根据班站名称分组
        spotbzGroupMap.forEach((key, value) -> {  // 每一组抽取两个人
            if (!Optional.ofNullable(value).orElse(Collections.emptyList()).isEmpty()) {
                List<ZqczRyxxVo> randomList = MtUtils.getRandomList(value, 2);
                if (!Optional.ofNullable(randomList).orElse(Collections.emptyList()).isEmpty()) {
                    spotbzList.addAll(randomList);
                }
            }
        });

        // 需要处理 标准化操作项目 为空的情况
        if (!Optional.ofNullable(bzhczXmglList).orElse(Collections.emptyList()).isEmpty()) {
            if (!Optional.ofNullable(spotbzList).orElse(Collections.emptyList()).isEmpty()) {
                for (ZqczRyxxVo zqczRyxxVo : spotbzList) {
                    ZqczXmglVo lockbzXmgl = MtUtils.getRandomList(bzhczXmglList, 1).get(0);  // 班站 锁定项目
                    dataList.add(new ZqczXmRyLsjl(zsrwid, jcrwid, jcdwid, zqczRyxxVo, lockbzXmgl));
                }
            }
        }
        return dataList;
    }

    /**
     * 抽检 团队应急项目（集输）
     *
     * @param zsrwid        直属任务ID
     * @param jcrwid        基层任务ID
     * @param jcdwid        基层单位ID
     * @param ldbzRyxxList  领导班子 人员信息
     * @param ssyzxRyxxList 三室一中心 人员信息
     * @param desigRyxxList 抽取指定班站 人员信息
     * @param tdyjXmglList  团队应急项目
     * @return
     */
    private List<ZqczXmRyLsjl> spotCheckJsTdyjxm(Long zsrwid, Long jcrwid, Long jcdwid, List<ZqczRyxxVo> ldbzRyxxList, List<ZqczRyxxVo> ssyzxRyxxList, List<ZqczRyxxVo> desigRyxxList, List<ZqczXmglVo> tdyjXmglList) {
        List<ZqczXmRyLsjl> dataList = new ArrayList<>();

        // 抽检项目
        if (Optional.ofNullable(tdyjXmglList).orElse(Collections.emptyList()).isEmpty()) {
            return null;
        }

        // 查询班站列表
        List<String> spotCheckBzNameList = desigRyxxList.stream().map(ZqczRyxxVo::getDwmc).distinct().collect(Collectors.toList());
        if (Optional.ofNullable(spotCheckBzNameList).orElse(Collections.emptyList()).isEmpty()) {
            return null;
        }

        // 分组
        Map<String, List<ZqczRyxxVo>> spotCheckBzGroupMap = desigRyxxList.stream().collect(Collectors.groupingBy(ZqczRyxxVo::getDwmc));

        for (int i = 0; i < spotCheckBzNameList.size(); i++) {
            ZqczXmglVo spotCheckTdyjXmgl = MtUtils.getRandomList(tdyjXmglList, 1).get(0);  // 当前班站抽取的项目

            String groupField = spotCheckBzNameList.get(i);
            List<ZqczRyxxVo> currentRyxxList = spotCheckBzGroupMap.get(groupField);  // 获取当前选中班站的人员信息
            if (Optional.ofNullable(currentRyxxList).orElse(Collections.emptyList()).isEmpty()) {  // 如果人员信息为空则跳过此次循环
                continue;
            }

            ZqczRyxxVo lockldbzRyxxVo = null;  // 锁定 领导班子 人员信息（抽一个人）
            ZqczRyxxVo lockSpotCheckSsyzxRyxx = null;  // 锁定 三室一中心 人员信息（抽一个人）

            if (i == 0) {  // 第一个班站坐特殊处理
                if (!Optional.ofNullable(ldbzRyxxList).orElse(Collections.emptyList()).isEmpty()) {
                    List<ZqczRyxxVo> spotLdbzList = MtUtils.getRandomList(ldbzRyxxList, 1);  // 抽取 领导班子 人员信息
                    lockldbzRyxxVo = spotLdbzList.get(0);
                }
                if (!Optional.ofNullable(ssyzxRyxxList).orElse(Collections.emptyList()).isEmpty()) {
                    lockSpotCheckSsyzxRyxx = MtUtils.getRandomList(ldbzRyxxList, 1).get(0);  // 抽取 三室一中心 人员信息
                }
            }

            // 锁定 班站 人员信息（所有人）
            List<String> dwmcList = currentRyxxList.stream().map(ZqczRyxxVo::getDwmc).collect(Collectors.toList());
            List<String> rymcList = currentRyxxList.stream().map(ZqczRyxxVo::getRymc).distinct().collect(Collectors.toList());

            // 拼接数据单位名称、人员名称
            if (lockldbzRyxxVo != null) {
                dwmcList.add(lockldbzRyxxVo.getDwmc());
                rymcList.add(lockldbzRyxxVo.getRymc());
            }
            if (lockSpotCheckSsyzxRyxx != null) {
                dwmcList.add(lockSpotCheckSsyzxRyxx.getDwmc());
                rymcList.add(lockSpotCheckSsyzxRyxx.getRymc());
            }
            String dwmc = dwmcList.stream().distinct().collect(Collectors.joining(","));
            String rymc = rymcList.stream().distinct().collect(Collectors.joining(","));


            ZqczXmRyLsjl spotCheckXmRyLsjl = new ZqczXmRyLsjl(zsrwid, jcrwid, jcdwid, spotCheckTdyjXmgl, dwmc, rymc);
            dataList.add(spotCheckXmRyLsjl);
        }
        return dataList;
    }
}
