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.StaffTemporaryDto;
import com.ssm.manage.entity.BlackList;
import com.ssm.manage.entity.StaffTemporary;
import com.ssm.manage.response.StaffTemporaryVo;
import com.ssm.manage.service.StaffTemporaryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (StaffTemporary)表服务实现类
 *
 * @author makejava
 * @since 2023-12-19 08:59:37
 */
@Service("staffTemporaryService")
public class StaffTemporaryServiceImpl extends BaseServiceImpl<StaffTemporary> implements StaffTemporaryService {
    @Autowired
    private StaffTemporaryDao staffTemporaryDao;
    @Autowired
    private SsmBaseStoreDao ssmBaseStoreDao;
    @Autowired
    private RegionListDao regionListDao;
    @Autowired
    private BlackListDao blackListDao;

    @Override
    public BaseDao<StaffTemporary> getDao() {
        return staffTemporaryDao;
    }

    @Override
    public PageInfo<StaffTemporaryVo> selectPage(StaffTemporaryDto dto) {
        Example example = new Example(StaffTemporary.class);
        example.setOrderByClause("ruzhi_time desc");
        Example.Criteria criteria = example.createCriteria();
        // 外包服务商模糊查询
        String remark = dto.getRemark();
        if (StringUtils.isNotBlank(remark)) {
            criteria.andLike(StaffTemporaryDto.Fields.remark, "%" + remark + "%");
        }
        if(dto.getStartTime()!=null){
//            criteria.andBetween(StaffTemporaryDto.Fields.scanTime,dto.getStartTime(),dto.getLastTime());
            criteria.andBetween(StaffTemporaryDto.Fields.ruzhiTime,dto.getStartTime(),dto.getLastTime());
        }
        if(dto.getMobilePhone()!=null){
            criteria.andLike(StaffTemporaryDto.Fields.mobilePhone,"%"+dto.getMobilePhone()+"%");
        }
        if(dto.getType()!=null){
            criteria.andEqualTo(StaffTemporaryDto.Fields.type,dto.getType());
        }
        if(dto.getStaffName()!=null){
            criteria.andLike(StaffTemporaryDto.Fields.staffName,"%"+dto.getStaffName()+"%");
        }
        if(dto.getStaffNum()!=null){
            criteria.andLike(StaffTemporaryDto.Fields.staffNum,"%"+dto.getStaffNum()+"%");
        }
        if(dto.getIdentityCard()!=null){
            criteria.andLike(StaffTemporaryDto.Fields.identityCard,"%"+dto.getIdentityCard()+"%");
        }
        if(dto.getState()!=null){
            criteria.andEqualTo(StaffTemporaryDto.Fields.state,dto.getState());
        }
        if (Objects.nonNull(dto.getStoreId())){
            criteria.andEqualTo(StaffTemporaryDto.Fields.storeId,dto.getStoreId());
        }
        List<Integer> deptIds = dto.getDeptIds();
        if (Objects.nonNull(dto.getRegionId())){
            criteria.andEqualTo(StaffTemporaryDto.Fields.regionId, dto.getRegionId());
        } else if (!CollectionUtils.isEmpty(deptIds)) {
            // 多部门筛选
            criteria.andIn(StaffTemporaryDto.Fields.zhongkongDeptId, deptIds);
        }
        if (Objects.nonNull(dto.getIsZhongkong())){
            if (dto.getIsZhongkong()==1) {
                criteria.andEqualTo(StaffTemporaryDto.Fields.isZhongkong, dto.getIsZhongkong());
            }else {
                Example.Criteria criteria1 = example.createCriteria();
                criteria1.orEqualTo(StaffTemporaryDto.Fields.isZhongkong,0);
                criteria1.orIsNull(StaffTemporaryDto.Fields.isZhongkong);
                example.and(criteria1);
            }
        }
        if (Objects.nonNull(dto.getIsBeisen())){
            criteria.andEqualTo(StaffTemporaryDto.Fields.isBeisen,dto.getIsBeisen());
        }
        if (Objects.nonNull(dto.getSex())){
            criteria.andEqualTo(StaffTemporaryDto.Fields.sex,dto.getSex());
        }
        if (Objects.nonNull(dto.getZhongkongDeptId())){
            criteria.andEqualTo(StaffTemporaryDto.Fields.zhongkongDeptId,dto.getZhongkongDeptId());
        }
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<StaffTemporary> list = this.staffTemporaryDao.selectByExample(example);
        PageInfo<StaffTemporary> pageInfo = new PageInfo<>(list);
        PageInfo<StaffTemporaryVo> info = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo,info);
        List<StaffTemporaryVo> staffTemporaryVos = new ArrayList<>();
        List<String> identity = list.stream().map(StaffTemporary::getIdentityCard).collect(Collectors.toList());
        List<BlackList> blackLists = new ArrayList<>();
        if (!CollectionUtils.isEmpty(identity)) {
            Example blackExample = new Example(BlackList.class);
            blackExample.selectProperties("urgentName","urgentPhone","urgentRelationship","id","beisenUserId",
                    "email","sex","staffNum","staffName","mobilePhone","identityCard","storeId","inputId",
                    "inputName","inputDate","type","job","regionId","endType","endId","endName","endTime",
                    "updateTime","employeeInformationId","employmentRecordId","isBeisen","isZhongkong",
                    "blacklistReasons");
            blackExample.createCriteria().andIn(BlackListDto.Fields.identityCard, identity);
            blackLists = blackListDao.selectByExample(blackExample);
        }
        for (StaffTemporary staffTemporary : list) {
            StaffTemporaryVo staffTemporaryVo = new StaffTemporaryVo();
            BeanUtils.copyProperties(staffTemporary,staffTemporaryVo);
            for (BlackList blackList : blackLists) {
                if (staffTemporary.getIdentityCard().equals(blackList.getIdentityCard())){
                    blackList.setFacesInformation(null);
                    staffTemporaryVo.setBlackList(blackList);
                }
            }
            staffTemporaryVo.setBaseStores(ssmBaseStoreDao.selectByPrimaryKey(staffTemporaryVo.getStoreId()));
            staffTemporaryVo.setRegionList(regionListDao.selectByPrimaryKey(staffTemporaryVo.getRegionId()));
            staffTemporaryVos.add(staffTemporaryVo);
        }
        info.setList(staffTemporaryVos);
        return info;
    }

    @Override
    public void updateIsUpdateByIds(Date ruzhiTime, Integer type, List<Long> ids) {
        staffTemporaryDao.updateIsUpdateByIds(ruzhiTime,  type,ids);
    }

    @Override
    public void updateByBeisenDimissionDate(StaffTemporary staffTemporary) {
        staffTemporaryDao.updateByBeisenDimissionDate(staffTemporary);
    }
}
