package com.ssm.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.manage.dao.*;
import com.ssm.manage.dto.BlackListDto;
import com.ssm.manage.dto.JobContentListDto;
import com.ssm.manage.dto.StaffTemporaryDto;
import com.ssm.manage.entity.*;
import com.ssm.manage.mapper.BlackListMapper;
import com.ssm.manage.mapper.RegionListMapper;
import com.ssm.manage.mapper.StaffTemporaryMapper;
import com.ssm.manage.response.BlackListVo;
import com.ssm.manage.response.JobContentListVo;
import com.ssm.manage.service.BlackListService;
import com.ssm.manage.utils.DateUtils;
import com.ssm.manage.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

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

/**
 * (BlackList)表服务实现类
 *
 * @author makejava
 * @since 2023-12-20 11:41:45
 */
@Service("blackListService")
public class BlackListServiceImpl extends BaseServiceImpl<BlackList> implements BlackListService {
    @Autowired
    private BlackListDao blackListDao;
    @Autowired
    private StaffTemporaryDao staffTemporaryDao;
    @Autowired
    private StaffTemporaryMapper staffTemporaryMapper;
    @Autowired
    private JobContentListDao jobContentListDao;
    @Autowired
    private RegionListDao regionListDao;
    @Autowired
    private BlackListMapper blackListMapper;
    @Autowired
    private RegionListMapper regionListMapper;

    @Override
    public BaseDao<BlackList> getDao() {
        return blackListDao;
    }

    @Override
    public PageInfo<BlackListVo> selectPage(BlackListDto dto) {
        //查询查询当前部门和当前部门下的所有子部门
        if (Objects.nonNull(dto.getZhongkongDeptId())){
            List<Integer> ids = regionListDao.selectByChildren(dto.getZhongkongDeptId());
            dto.setDeptIds(ids);
        }
        //TODO 修改了这里
        List<StaffTemporaryAndBlackList> staffTemporaryList = new ArrayList<>();
        int pageSize = dto.getPageSize();
        PageHelper.startPage(dto.getPageNum(), pageSize, "ruzhi_time desc");
        staffTemporaryList = staffTemporaryDao.selectByStaffTemporary1(dto);
        /*List<Integer> deptIds = dto.getDeptIds();
        boolean deptIdsNotEmpty = !CollectionUtils.isEmpty(deptIds);*/

        Date lastTime = dto.getLastTime();
        boolean lastTimeNonNull = lastTime != null;

        PageInfo<StaffTemporaryAndBlackList> staffTemporaryPageInfo = new PageInfo<>(staffTemporaryList);
//        long count = staffTemporaryMapper.countByStaffTemporary(dto);
//        staffTemporaryPageInfo.setTotal(count);
//        if (Objects.nonNull(dto.getIsBeisen()) || Objects.nonNull(dto.getIsZhongkong())) {
//            if (!CollectionUtils.isEmpty(staffTemporaryList)) {
//                List<String> collect = staffTemporaryList.stream().map(StaffTemporary::getIdentityCard).collect(Collectors.toList());
//                Example example = new Example(BlackList.class);
//                Example.Criteria criteria = example.createCriteria();
//                criteria.andIn(BlackListDto.Fields.identityCard, collect);
//                Integer type = dto.getType();
//                if (type != null) {
//                    criteria.andEqualTo(BlackListDto.Fields.type, type);
//                }
//                /*if (deptIdsNotEmpty) {
//                    criteria.andIn(BlackListDto.Fields.regionId, deptIds);
//                }*/
//                PageHelper.startPage(dto.getPageNum(), pageSize, "input_date desc");
//                List<BlackList> list = blackListDao.selectByExample(example);
//                PageInfo<BlackList> pageInfo = new PageInfo<>(list);
//
//                List<BlackListVo> blackListVos = staffTemporaryList.stream().map(item -> {
//                    BlackListVo blackListVo = new BlackListVo();
//                    for (BlackList blackList : list) {
//                        BeanUtils.copyProperties(blackList, blackListVo);
//                        if (item.getIdentityCard().equals(blackListVo.getIdentityCard())) {
//                            blackListVo.setIsBeisen(item.getIsBeisen() == 2 ? 1 : 0);
//                            blackListVo.setJob(item.getType());
//                            blackListVo.setRemark(item.getRemark());
//                            blackListVo.setUrgentName(item.getUrgentName());
//                            blackListVo.setUrgentPhone(item.getUrgentPhone());
//                            blackListVo.setUrgentRelationship(item.getUrgentRelationship());
//                            blackListVo.setIsZhongkong(item.getIsZhongkong());
//
//                            blackListVo.setZhongkongDimission(item.getZhongkongDimission());
//                            blackListVo.setBeisenDimission(item.getBeisenDimission());
//                            // 按照日期筛选，更新筛选日期之前的状态
//                            /*Date zhongkongDimissionDate = item.getZhongkongDimissionDate();
//                            Date beisenDimissionDate = item.getBeisenDimissionDate();
//                            if (lastTimeNonNull) {
//                                if (DateUtils.compareDateTime(lastTime, zhongkongDimissionDate) < 0) {
//                                    // lastTime 在 zhongkongDimissionDate 之前, 显示为未离职
//                                    blackListVo.setZhongkongDimission(0);
//                                }
//                                if (DateUtils.compareDateTime(lastTime, beisenDimissionDate) < 0) {
//                                    // lastTime 在 beisenDimissionDate 之前, 显示为未离职
//                                    blackListVo.setBeisenDimission(0);
//                                }
//                            }*/
//
//                            blackListVo.setZhongkongDeptId(item.getZhongkongDeptId());
//                            blackListVo.setZhongkongDeptNum(item.getZhongkongDeptNum());
//                            blackListVo.setZhongkongDeptName(item.getZhongkongDeptName());
//                            blackListVo.setBeisenDeptId(item.getBeisenDeptId());
//                            blackListVo.setBeisenDeptNum(item.getBeisenDeptNum());
//                            blackListVo.setBeisenDeptName(item.getBeisenDeptName());
//                            blackListVo.setRuzhiTime(item.getRuzhiTime());
//                            if (item.getType() == 0) {
//                                blackListVo.setEndTime(item.getZhongkongDimissionDate() == null ? item.getEndTime() : item.getZhongkongDimissionDate());
//                            } else if (item.getType() == 1 && item.getZhongkongDimissionDate() != null) {
//                                blackListVo.setEndTime(item.getZhongkongDimissionDate());
//                            }
//                            if (item.getZhongkongDimission() == null || item.getZhongkongDimission() == 0) {
//                                blackListVo.setEndTime(null);
//                            }
//                            break;
//                        }
//                    }
//                    return blackListVo;
//                }).collect(Collectors.toList());
//                PageInfo<BlackListVo> info = new PageInfo<>();
//                BeanUtils.copyProperties(pageInfo, info);
//                info.setTotal(staffTemporaryPageInfo.getTotal());
//                info.setList(blackListVos);
//                return info;
//            } else {
//                PageInfo<BlackListVo> info = new PageInfo<>(new ArrayList<>());
//                return info;
//            }
//        }
        Example example11 = new Example(BlackList.class);
        Example.Criteria criteria11 = example11.createCriteria();
        criteria11.andEqualTo(BlackListDto.Fields.storeId, dto.getStoreId());
        if (CollectionUtils.isEmpty(staffTemporaryList)) {
            return new PageInfo<>(new ArrayList<>());
        }
        List<String> stringList = staffTemporaryList.stream().map(StaffTemporary::getIdentityCard).collect(Collectors.toList());
        criteria11.andIn(BlackListDto.Fields.identityCard, stringList);
        /*if (deptIdsNotEmpty) {
            criteria11.andIn(BlackListDto.Fields.regionId, deptIds);
        }*/
        //TODO 修改了这里
        List<BlackList> list = blackListMapper.selectByStoreIdAndIndentityCard(dto.getStoreId(),stringList);
        List<String> collect = staffTemporaryList.stream().map(StaffTemporary::getIdentityCard).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)) {
            list = new ArrayList<>();
        }
//        Map<String, List<BlackList>> blackListMap = list.stream().collect(Collectors.groupingBy(BlackList::getIdentityCard));
        Map<String, List<BlackList>> blackListMap = new HashMap<>();
        if (list != null) {
            blackListMap = list.stream()
                    .filter(Objects::nonNull)  // 过滤掉 null 的元素
                    .collect(Collectors.groupingBy(bl -> {
                        String identityCard = bl.getIdentityCard();
                        return identityCard != null ? identityCard : "";  // 处理可能为 null 的 identityCard
                    }));
        }


//        List<BlackList> list = blackListDao.selectByExample(example11);
//
//
//        List<String> collect = staffTemporaryList.stream().map(StaffTemporary::getIdentityCard).collect(Collectors.toList());
//        if (CollectionUtils.isEmpty(collect)) {
//            list = new ArrayList<>();
//        }
//        Map<String, List<BlackList>> blackListMap = list.stream().collect(Collectors.groupingBy(BlackList::getIdentityCard));
        /*Example staffExample1 = new Example(StaffTemporary.class);
        Example.Criteria criteria12 = staffExample1.createCriteria();
        if (!CollectionUtils.isEmpty(collect)) {
            criteria12.andIn(StaffTemporaryDto.Fields.identityCard, collect);
        } else list = new ArrayList<>();*/
//        PageInfo<StaffTemporary> staffTemporaryPageInfo = new PageInfo<>(staffTemporaryList);
        PageInfo<BlackList> pageInfo = new PageInfo<>(list);
        List<BlackListVo> blackListVos = new ArrayList<>();

        for (StaffTemporaryAndBlackList temporary : staffTemporaryList) {
            BlackListVo blackListVo = new BlackListVo();
            List<BlackList> blackLists = blackListMap.get(temporary.getIdentityCard());

            if (!CollectionUtils.isEmpty(blackLists)) {
                BeanUtils.copyProperties(blackLists.get(0), blackListVo);
            }

            // 设置离职原因和离职类型（以中控为准）
            blackListVo.setReason(temporary.getZhongkongDimissionReason());
            blackListVo.setLeavetype(temporary.getZhongkongDimissionType());

            blackListVo.setRemark(temporary.getRemark());
            blackListVo.setJob(temporary.getType());
            blackListVo.setUrgentName(temporary.getUrgentName());
            blackListVo.setUrgentPhone(temporary.getUrgentPhone());
            blackListVo.setUrgentRelationship(temporary.getUrgentRelationship());
            blackListVo.setIsBeisen(temporary.getIsBeisen() == 2 ? 1 : 0);
            blackListVo.setIsZhongkong(temporary.getIsZhongkong());

            blackListVo.setZhongkongDimission(temporary.getZhongkongDimission());
            blackListVo.setBeisenDimission(temporary.getBeisenDimission());
            // 按照日期筛选，更新筛选日期之前的状态
            Date zhongkongDimissionDate = temporary.getZhongkongDimissionDate();
            Date beisenDimissionDate = temporary.getBeisenDimissionDate();
            // 显示实时状态
            if (lastTimeNonNull && dto.getStartTime() == null) {
                if (DateUtils.compareDateTime(lastTime, zhongkongDimissionDate) < 0) {
                    // lastTime 在 zhongkongDimissionDate 之前, 显示为未离职
                    blackListVo.setZhongkongDimission(0);
                }
                if (DateUtils.compareDateTime(lastTime, beisenDimissionDate) < 0) {
                    // lastTime 在 beisenDimissionDate 之前, 显示为未离职
                    blackListVo.setBeisenDimission(0);
                }
            }

            blackListVo.setZhongkongDeptId(temporary.getZhongkongDeptId());
            blackListVo.setZhongkongDeptNum(temporary.getZhongkongDeptNum());
            blackListVo.setZhongkongDeptName(temporary.getZhongkongDeptName());
            blackListVo.setBeisenDeptId(temporary.getBeisenDeptId());
            blackListVo.setBeisenDeptNum(temporary.getBeisenDeptNum());
            blackListVo.setBeisenDeptName(temporary.getBeisenDeptName());
            blackListVo.setRuzhiTime(temporary.getRuzhiTime());
            if (Objects.nonNull(temporary.getZhongkongDeptId())) {
                blackListVo.setIsZhongkong(1);
            }
            if (Objects.nonNull(temporary.getBeisenDeptId())) {
                blackListVo.setIsBeisen(1);
            }
            blackListVo.setEndTime(zhongkongDimissionDate == null ? temporary.getEndTime() : zhongkongDimissionDate);
            if (temporary.getZhongkongDimission() == null || temporary.getZhongkongDimission() == 0) {
                blackListVo.setEndTime(null);
            }
            BeanUtils.copyProperties(temporary,blackListVo);
            blackListVo.setBlacklistReasons(temporary.getBlacklistReasons());

            blackListVos.add(blackListVo);
        }
        PageInfo<BlackListVo> info = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo, info);
        info.setTotal(staffTemporaryPageInfo.getTotal());
        info.setList(blackListVos);
        return info;
    }

    @Override
    public List<BlackListVo> selectAll(BlackListDto dto) {
        //查询查询当前部门和当前部门下的所有子部门
        if (Objects.nonNull(dto.getZhongkongDeptId())){
            List<Integer> ids = regionListDao.selectByChildren(dto.getZhongkongDeptId());
            dto.setDeptIds(ids);
        }
        List<StaffTemporary> staffTemporaryList;
        staffTemporaryList = staffTemporaryDao.selectByStaffTemporary(dto);

        if (Objects.nonNull(dto.getIsBeisen()) || Objects.nonNull(dto.getIsZhongkong())) {
            if (!CollectionUtils.isEmpty(staffTemporaryList)) {
                List<String> collect = staffTemporaryList.stream().map(StaffTemporary::getIdentityCard).collect(Collectors.toList());
                Example example = new Example(BlackList.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn(BlackListDto.Fields.identityCard, collect);
                Integer type = dto.getType();
                if (type != null) {
                    criteria.andEqualTo(BlackListDto.Fields.type, type);
                }
                List<BlackList> list = blackListDao.selectByExample(example);

                List<BlackListVo> blackListVos = staffTemporaryList.stream().map(item -> {
                    BlackListVo blackListVo = new BlackListVo();
                    for (BlackList blackList : list) {
                        BeanUtils.copyProperties(blackList, blackListVo);
                        if (item.getIdentityCard().equals(blackListVo.getIdentityCard())) {
                            blackListVo.setIsBeisen(item.getIsBeisen() == 2 ? 1 : 0);
                            blackListVo.setJob(item.getType());
                            blackListVo.setRemark(item.getRemark());
                            blackListVo.setUrgentName(item.getUrgentName());
                            blackListVo.setUrgentPhone(item.getUrgentPhone());
                            blackListVo.setUrgentRelationship(item.getUrgentRelationship());
                            blackListVo.setIsZhongkong(item.getIsZhongkong());

                            blackListVo.setZhongkongDimission(item.getZhongkongDimission());
                            blackListVo.setBeisenDimission(item.getBeisenDimission());
                            // 按照日期筛选，更新筛选日期之前的状态
                            /*Date zhongkongDimissionDate = item.getZhongkongDimissionDate();
                            Date beisenDimissionDate = item.getBeisenDimissionDate();
                            if (lastTimeNonNull) {
                                if (DateUtils.compareDateTime(lastTime, zhongkongDimissionDate) < 0) {
                                    // lastTime 在 zhongkongDimissionDate 之前, 显示为未离职
                                    blackListVo.setZhongkongDimission(0);
                                }
                                if (DateUtils.compareDateTime(lastTime, beisenDimissionDate) < 0) {
                                    // lastTime 在 beisenDimissionDate 之前, 显示为未离职
                                    blackListVo.setBeisenDimission(0);
                                }
                            }*/

                            blackListVo.setZhongkongDeptId(item.getZhongkongDeptId());
                            blackListVo.setZhongkongDeptNum(item.getZhongkongDeptNum());
                            blackListVo.setZhongkongDeptName(item.getZhongkongDeptName());
                            blackListVo.setBeisenDeptId(item.getBeisenDeptId());
                            blackListVo.setBeisenDeptNum(item.getBeisenDeptNum());
                            blackListVo.setBeisenDeptName(item.getBeisenDeptName());
                            blackListVo.setRuzhiTime(item.getRuzhiTime());
                            if (item.getType() == 0) {
                                blackListVo.setEndTime(item.getZhongkongDimissionDate() == null ? item.getEndTime() : item.getZhongkongDimissionDate());
                            } else if (item.getType() == 1 && item.getZhongkongDimissionDate() != null) {
                                blackListVo.setEndTime(item.getZhongkongDimissionDate());
                            }
                            if (item.getZhongkongDimission() == null || item.getZhongkongDimission() == 0) {
                                blackListVo.setEndTime(null);
                            }
                            break;
                        }
                    }
                    return blackListVo;
                }).collect(Collectors.toList());
                return blackListVos;
            } else {
                return new ArrayList<>();
            }
        }
        Example example11 = new Example(BlackList.class);
        Example.Criteria criteria11 = example11.createCriteria();
        criteria11.andEqualTo(BlackListDto.Fields.storeId, dto.getStoreId());
        if (CollectionUtils.isEmpty(staffTemporaryList)) {
            return new ArrayList<>();
        }
        List<String> stringList = staffTemporaryList.stream().map(StaffTemporary::getIdentityCard).collect(Collectors.toList());
        criteria11.andIn(BlackListDto.Fields.identityCard, stringList);
        /*if (deptIdsNotEmpty) {
            criteria11.andIn(BlackListDto.Fields.regionId, deptIds);
        }*/
        List<BlackList> list = blackListDao.selectByExample(example11);
        List<String> collect = staffTemporaryList.stream().map(StaffTemporary::getIdentityCard).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)) {
            list = new ArrayList<>();
        }
        Map<String, List<BlackList>> blackListMap = list.stream().collect(Collectors.groupingBy(BlackList::getIdentityCard));
        PageInfo<BlackList> pageInfo = new PageInfo<>(list);
        List<BlackListVo> blackListVos = new ArrayList<>();

        for (StaffTemporary temporary : staffTemporaryList) {
            BlackListVo blackListVo = new BlackListVo();
            List<BlackList> blackLists = blackListMap.get(temporary.getIdentityCard());

            if (!CollectionUtils.isEmpty(blackLists)) {
                BeanUtils.copyProperties(blackLists.get(0), blackListVo);
            }

            blackListVo.setRemark(temporary.getRemark());
            blackListVo.setJob(temporary.getType());
            blackListVo.setUrgentName(temporary.getUrgentName());
            blackListVo.setUrgentPhone(temporary.getUrgentPhone());
            blackListVo.setUrgentRelationship(temporary.getUrgentRelationship());
            blackListVo.setIsBeisen(temporary.getIsBeisen() == 2 ? 1 : 0);
            blackListVo.setIsZhongkong(temporary.getIsZhongkong());

            blackListVo.setZhongkongDimission(temporary.getZhongkongDimission());
            blackListVo.setBeisenDimission(temporary.getBeisenDimission());
            // 按照日期筛选，更新筛选日期之前的状态
            Date zhongkongDimissionDate = temporary.getZhongkongDimissionDate();

            blackListVo.setZhongkongDeptId(temporary.getZhongkongDeptId());
            blackListVo.setZhongkongDeptNum(temporary.getZhongkongDeptNum());
            blackListVo.setZhongkongDeptName(temporary.getZhongkongDeptName());
            blackListVo.setBeisenDeptId(temporary.getBeisenDeptId());
            blackListVo.setBeisenDeptNum(temporary.getBeisenDeptNum());
            blackListVo.setBeisenDeptName(temporary.getBeisenDeptName());
            blackListVo.setRuzhiTime(temporary.getRuzhiTime());
            if (Objects.nonNull(temporary.getZhongkongDeptId())) {
                blackListVo.setIsZhongkong(1);
            }
            if (Objects.nonNull(temporary.getBeisenDeptId())) {
                blackListVo.setIsBeisen(1);
            }
            blackListVo.setEndTime(zhongkongDimissionDate == null ? temporary.getEndTime() : zhongkongDimissionDate);
            if (temporary.getZhongkongDimission() == null || temporary.getZhongkongDimission() == 0) {
                blackListVo.setEndTime(null);
            }
            blackListVos.add(blackListVo);
        }

        return blackListVos;
    }

    @Override
    public PageInfo<StaffTemporary> getStaffTemporary(BlackListDto dto) {
        Example example = new Example(StaffTemporary.class);
        Example.Criteria criteria = example.createCriteria();
        //身份证
        if (StringUtils.isNotBlank(dto.getIdentityCard())) {
            criteria.andEqualTo(StaffTemporaryDto.Fields.identityCard, dto.getIdentityCard());
        }
        //门店id
        if (Objects.nonNull(dto.getStoreId())) {
            criteria.andEqualTo(StaffTemporaryDto.Fields.storeId, dto.getStoreId());
        }
        if (Objects.nonNull(dto.getRegionId())) {
            criteria.andEqualTo(StaffTemporaryDto.Fields.regionId, dto.getRegionId());
        }
        Example.Criteria exampleCriteria = example.createCriteria();
        exampleCriteria.orEqualTo(BlackListDto.Fields.isZhongkong, 1);
        exampleCriteria.orEqualTo(BlackListDto.Fields.isBeisen, 1);
        example.and(exampleCriteria);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize(), "ruzhi_time desc");
        List<StaffTemporary> staffTemporaryList = staffTemporaryDao.selectByExample(example);
        PageInfo<StaffTemporary> pageInfo = new PageInfo<>(staffTemporaryList);
        return pageInfo;
    }

    @Override
    public PageInfo<JobContentListVo> getJonContent(BlackListDto dto) {
        //查询查询当前部门和当前部门下的所有子部门
        if (StringUtils.isNotBlank(dto.getDeptNum())){
            List<String> ids = regionListDao.selectByChildrenByDeptName(dto.getDeptNum());
            dto.setDepyNums(ids);
        }

        List<String> snNum = new ArrayList<>();
        snNum.add("TBB5235000125");
        snNum.add("TBB5235000149");
        snNum.add("TBB5235000151");
        snNum.add("TBB5235000152");
        snNum.add("TBB5235000243");
        snNum.add("TBB5235000254");


        Example example = new Example(JobContentList.class);
        example.setOrderByClause("check_time desc");
        Example.Criteria criteria = example.createCriteria();
//        这里查询日结月结只能逆推，因为表没保存
        if (!ObjectUtils.isEmpty(dto.getType())) {
          List<Long> collect= blackListDao.selectTypeId(dto.getType());
            criteria.andIn(JobContentListDto.Fields.id, collect);
        }
        if (StringUtils.isNotBlank(dto.getIdentityCard())) {
            criteria.andEqualTo(JobContentListDto.Fields.identityCard, dto.getIdentityCard());
        }
        if (Objects.nonNull(dto.getStoreId())) {
            criteria.andEqualTo(JobContentListDto.Fields.storeId, dto.getStoreId());
        }
        List<Integer> deptIds = dto.getDeptIds();
        if (Objects.nonNull(dto.getRegionId())) {
            criteria.andEqualTo(JobContentListDto.Fields.regionId, dto.getRegionId());
        } else if (!CollectionUtils.isEmpty(deptIds)) {
            // 获取部门编号
            Example regionExample = new Example(RegionList.class);
            Example.Criteria regionExampleCriteria = regionExample.createCriteria();
            regionExampleCriteria.andIn("id", deptIds);
            List<RegionList> regionLists = regionListDao.selectByExample(regionExample);
            Set<String> regionIds = regionLists.stream().map(RegionList::getRegionNum)
                    .filter(Objects::nonNull).collect(Collectors.toSet());
            if (!CollectionUtils.isEmpty(regionIds)) {
                // 多部门筛选
                criteria.andIn(JobContentListDto.Fields.deptNumber, regionIds);
            }
        }

        if (Objects.nonNull(dto.getStartTime())) {
            criteria.andBetween(JobContentListDto.Fields.checkTime, dto.getStartTime(), dto.getLastTime());
        }
        if (StringUtils.isNotBlank(dto.getStaffName())) {
            criteria.andLike(JobContentListDto.Fields.staffName, dto.getStaffName());
        }
        if (StringUtils.isNotBlank(dto.getStaffNum())) {
            criteria.andLike(JobContentListDto.Fields.staffNum, dto.getStaffNum());
        }
        if (StringUtils.isNotBlank(dto.getMobilePhone())) {
            criteria.andLike(JobContentListDto.Fields.mobilePhone, dto.getMobilePhone());
        }

        if (Objects.nonNull(dto.getSex())) {
            criteria.andEqualTo(JobContentListDto.Fields.sex, dto.getSex());
        }
        /*if (!CollectionUtils.isEmpty(dto.getDepyNums())){
            criteria.andIn(JobContentListDto.Fields.deptNumber, dto.getDepyNums());
        }*/

        criteria.andIn(JobContentListDto.Fields.equipmentNum, snNum);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<JobContentListVo> list = new ArrayList<>();
        List<JobContentList> jobContentLists = jobContentListDao.selectByExample(example);

        if (CollectionUtils.isEmpty(jobContentLists)) {
            return new PageInfo<>();
        }

        // BlackList 批量查询
//        Set<Long> staffIds = jobContentLists.stream().map(JobContentList::getStaffId)
//                .filter(Objects::nonNull).collect(Collectors.toSet());
//        Map<Long, BlackList> blackListMap = blackListMapper.selectByIds(staffIds);

//        Set<String> deptNumSet = jobContentLists.stream().map(JobContentList::getDeptNumber).collect(Collectors.toSet());

        // 根据部门编码获取三级部门
        Set<String> deptNums = jobContentLists.stream().map(JobContentList::getDeptNumber)
                .filter(Objects::nonNull).collect(Collectors.toSet());
        Map<String, RegionThreeLevel> regionThreeLevelMap = regionListMapper.selectParentByRegionNum(deptNums);

        jobContentLists.forEach(contentList -> {
//            RegionList regionList = regionListDao.selectByPrimaryKey(contentList.getRegionId());
            JobContentListVo jobContentListVo = new JobContentListVo();
            BeanUtils.copyProperties(contentList, jobContentListVo);
//            jobContentListVo.setRegionList(regionList);
//            BlackList blackList = blackListDao.selectByPrimaryKey(jobContentListVo.getStaffId());
           /* BlackList blackList = blackListMap.get(jobContentListVo.getStaffId());
            jobContentListVo.setBlackList(blackList);
            String data = "验证方式：" + jobContentListVo.getVerify() + "，部门编码：" + jobContentListVo.getDeptNumber() +
                    "，部门名称：" + jobContentListVo.getDeptName() + "，设备编码：" + jobContentListVo.getEquipmentNum();
            jobContentListVo.setData(data);*/

            Example stExample = new Example(StaffTemporary.class);
            Example.Criteria stCriteria = stExample.createCriteria();
            stCriteria.andEqualTo("staffNum", jobContentListVo.getStaffNum());
            stCriteria.andLessThanOrEqualTo("scanTime", jobContentListVo.getCheckTime());
            stExample.setOrderByClause("scan_time desc");
            List<StaffTemporary> staffTemporaries = staffTemporaryDao.selectByExample(stExample);

            if (!CollectionUtils.isEmpty(staffTemporaries)) {
                StaffTemporary staffTemporary = staffTemporaries.get(0);
                jobContentListVo.setRemark(staffTemporary.getRemark());
                // 原本是考勤状态, 暂时替换成日结or月结
                jobContentListVo.setType(staffTemporary.getType());

                // 入职时间和离场时间
                jobContentListVo.setRuzhiTime(staffTemporary.getRuzhiTime());
                jobContentListVo.setEndTime(staffTemporary.getEndTime());

            }

            // 三级部门
            RegionThreeLevel regionThreeLevel = regionThreeLevelMap.get(jobContentListVo.getDeptNumber());
            if (regionThreeLevel != null) {
                String threeDeptName = regionThreeLevel.getThreeDeptName();
                String secondDeptName = regionThreeLevel.getSecondDeptName();
                if (org.apache.commons.lang.StringUtils.isBlank(secondDeptName)) {  // 二级为空, 说明当前部门只有一级
                    // 将三级提到一级
                    jobContentListVo.setFirstDeptName(threeDeptName);
                    // 设置三级为空
//                    jobContentListVo.setThreeDeptName("");
                } else {    // 二级不为空, 说明当前部门有可能有 一二两个级别
                    String firstDeptName = regionThreeLevel.getFirstDeptName();
                    if (org.apache.commons.lang.StringUtils.isBlank(firstDeptName)) {   // 一级为空, 说明当前部门只有前两级
                        // 将二级提到一级
                        jobContentListVo.setFirstDeptName(secondDeptName);
                        // 将三级提到二级
                        jobContentListVo.setSecondDeptName(threeDeptName);
                        // 设置三级为空
//                        jobContentListVo.setThreeDeptName("");
                    } else {    // 一级不为空, 说明当前部门有完整的三个级别
                        // 分别设置三个级别
                        jobContentListVo.setFirstDeptName(firstDeptName);
                        jobContentListVo.setSecondDeptName(secondDeptName);
                        jobContentListVo.setThreeDeptName(threeDeptName);
                    }
                }
            }

            list.add(jobContentListVo);
        });

        PageInfo<JobContentList> pageInfo = new PageInfo<>(jobContentLists);
        PageInfo<JobContentListVo> info = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo, info);
        info.setList(list);
        return info;
    }
}
