package com.blueland.service.impl;


/**
 * @author 代码自动生成工具
 * @version 1.0
 * @since
 */

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blueland.bo.*;
import com.blueland.constant.Constant;
import com.blueland.constant.RedisConstant;
import com.blueland.core.util.UserUtils;
import com.blueland.dao.TucStaffDao;
import com.blueland.model.TUcOrg;
import com.blueland.model.TUcStaff;
import com.blueland.service.IParameterService;
import com.blueland.service.ITucOrgService;
import com.blueland.service.ITucStaffService;
import com.blueland.service.SysRedisService;
import com.blueland.vo.StaffNumVO;
import com.blueland.vo.StaffPostVO;
import com.blueland.vo.TUcStaffVO;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class TucStaffServiceImpl extends ServiceImpl<TucStaffDao, TUcStaff> implements ITucStaffService {
    @Autowired
    //TucStaff对应的DAO类,主要用于数据库的增删改查等操作
    private TucStaffDao tucStaffDao;
    @Autowired
    private ITucOrgService tucOrgServiceImpl;

    @Autowired
    private SysRedisService sysRedisService;

    @Autowired
    private IParameterService iParameterService;

    @Override
    public Page<TucStaffEntity> getStaffInfoByOrgTop(TucStaffEntity staff) {
        Page<TucStaffEntity> pageInfo = null;
        if (staff.getPageNum() == null) {
            staff.setPageNum(1);
        }
        if (staff.getPageSize() == null) {
            staff.setPageSize(5);
        }
        Page<TucStaffEntity> page = new Page(staff.getPageNum(), staff.getPageSize());
        pageInfo = tucStaffDao.getStaffInfoByOrgTopPage(page, staff);
        return pageInfo;
    }

    @Override
    public Page<TucStaffEntity> getSubStaffs(TucStaffEntity entity) {
        if (null == entity || StringUtils.isEmpty(entity.getOrgId())) {
            throw new RuntimeException("组织ID不能为空");
        }
        Page<TucStaffEntity> pageInfo = null;
        if (entity.getPageNum() == null) {
            entity.setPageNum(1);
        }
        if (entity.getPageSize() == null) {
            entity.setPageSize(5);
        }
        TucStaffEntity staff = new TucStaffEntity();

        String orgId = entity.getOrgId();
//		Org qOrg = new Org();
//		qOrg.setOrgId(orgId);
//		qOrg.setOrgType(entity.getStaffType());
        // qOrg.setOrgStatus(entity.getStaffStatus());
        Org exsitsOrg = tucOrgServiceImpl.getByOgIdAndType(orgId, entity.getStaffType());
        if (null != exsitsOrg.getOrgLevel() && 1 == exsitsOrg.getOrgLevel()) {
            staff.setCompanyId(orgId);
            staff.setStaffId(entity.getStaffId());
            staff.setStaffName(entity.getStaffName());
            staff.setStaffType(entity.getStaffType());
            staff.setStaffStatus(entity.getStaffStatus());
            staff.setPageNum(entity.getPageNum());
            staff.setPageSize(entity.getPageSize());
            staff.setSortColumns(entity.getSortColumns());
            staff.setMainIdentityStaff(entity.getMainIdentityStaff());
            if (ObjectUtil.isNotEmpty(entity.getOrgId())) {
                staff.setQueryOrgId(entity.getOrgId());
            }
            Page<TucStaffEntity> page = new Page(staff.getPageNum(), staff.getPageSize());
            return tucStaffDao.findPage(page, staff);
        } else {
            Page<TucStaffEntity> page = new Page(entity.getPageNum(), entity.getPageSize());
            Page<TucStaffEntity> subStaffs = tucStaffDao.getSubStaffsPage(page, entity);
            if (ObjectUtil.isNotEmpty(subStaffs) && ObjectUtil.isNotEmpty(subStaffs.getRecords())) {
                for (TucStaffEntity tucStaffEntity : subStaffs.getRecords()) {
                    String staffFax = tucStaffEntity.getStaffFax();
                    TucStaffEntity byStaffFax = tucStaffDao.getByStaffFax(staffFax);
                    if (byStaffFax != null) {
                        tucStaffEntity.setStaffFax(byStaffFax.getStaffName());
                    }
                }
            }

            pageInfo = subStaffs;
        }
        return pageInfo;
    }

    @Override
    public Page<TucStaffEntity> getStaffByOrgs(TucStaffEntity entity) {
        if (entity.getPageNum() == null) {
            entity.setPageNum(1);
        }
        if (entity.getPageSize() == null) {
            entity.setPageSize(5);
        }
        TucStaffEntity staff = new TucStaffEntity();
        String[] orgIds = entity.getOrgId().split(",");
        entity.setOrgIds(orgIds);
        Page<TucStaffEntity> page = new Page(entity.getPageNum(), entity.getPageSize());
        return tucStaffDao.getStaffByOrgsPage(page, entity);
    }

    @Override
    public void deleteBycode(String staffCode) {
        tucStaffDao.deleteByCode(staffCode);
    }

    @Override
    public void updateByNum(TucStaffEntity model) {
        //20200907 add  TASK1227 crm系统管理 对接HR 用户接口调整
        tucStaffDao.updateByNum(model);
    }

    public List<TUcStaff> getByStaffNum(String staffNum) {
        QueryWrapper<TUcStaff> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TUcStaff::getStaffStatus, Constant.QY);
        queryWrapper.lambda().eq(TUcStaff::getStaffNum, staffNum);
        return this.baseMapper.selectList(queryWrapper);
    }

    //20210202 add 批量更新上级人工号
    @Override
    public void batchupdateStaffFax(List<TucStaffEntity> stafflist) {
        for (TucStaffEntity model : stafflist) {
            String staffCode = model.getStaffCode();
            if (StringUtils.isNotEmpty(staffCode)) {
                TucStaffEntity dbmodel = tucStaffDao.getByStaffCode(staffCode);
                if (null != dbmodel) {
                    TUcStaff tUcStaff = BeanUtil.copyProperties(dbmodel, TUcStaff.class);
                    tUcStaff.setStaffFax(model.getStaffFax());
                    tucStaffDao.updateById(tUcStaff);
                }
            }
        }
    }

    @Override
    public TucStaffEntity getStaffByStaffId(String staffId) {
        if (!StringUtils.isNotEmpty(staffId)) {
            throw new RuntimeException("staffId不能为空");
        }
        TucStaffEntity staff = new TucStaffEntity();
//		staff.setStaffCode(staffId);
//		staff.setStaffStatus("QY");
        QueryWrapper<TUcStaff> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TUcStaff::getStaffCode, staffId);
        queryWrapper.lambda().eq(TUcStaff::getStaffStatus, "QY");
        List<TUcStaff> list = tucStaffDao.selectList(queryWrapper);
        if (ObjectUtil.isNotEmpty(list)) {
            TUcStaff tUcStaff = list.get(0);
            staff = BeanUtil.copyProperties(tUcStaff, TucStaffEntity.class);
        }
        if (staff == null) {
            return null;
        }
        //20200827 add
        String fulldeptname = tucOrgServiceImpl.getfulldeptname(staff.getDeptId());
        staff.setFulldeptname(fulldeptname);

        return staff;
    }

    @Override
    public Page<RoleUserRest> getAppRoleUser(String appId, String roleId, String companyId, String deptId,
                                             String orgId, Integer curPage, Integer curPageSize) {
        Map<String, Object> params = Maps.newHashMap();
        Page<RoleUserRest> page = new Page<>(curPage, curPageSize);
        params.put("appId", appId);
        params.put("roleId", roleId);
        params.put("companyId", companyId);
        params.put("deptId", deptId);
        params.put("orgId", orgId);
        Page<RoleUserRest> result = tucStaffDao.getAppRoleUserPage(page, params);
        return result;
    }

    @Override
    public  Page<RoleUserRest> getAppRoleUser2(String appId, String roleId, String companyId, String deptId,
                                               String orgId, Integer curPage, Integer curPageSize) {
        Map<String, Object> params = Maps.newHashMap();
        Page<RoleUserRest> page = new Page<>(curPage, curPageSize);
        params.put("appId", appId);
        params.put("roleId", roleId);
        params.put("companyId", companyId);
        params.put("deptId", deptId);
        params.put("orgId", orgId);
        Page<RoleUserRest> result = tucStaffDao.getAppRoleUserPage2(page, params);
        return result;
    }

    /**
     * 20200728 add 获取某租户某角色 或某部门下的用户
     *
     * @param appId
     * @param roleId
     * @param companyId
     * @param deptId
     * @param orgId
     * @return
     */
    @Override
    public List<TucStaffEntity> getAppRoleUserList(String appId, String roleId, String companyId, String deptId,
                                                   String orgId) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("appId", appId);
        params.put("roleId", roleId);
        params.put("companyId", companyId);
        params.put("deptId", deptId);
        params.put("orgId", orgId);
        return tucStaffDao.getAppRoleUserList(params);
    }

    @Override
    public List<TreeEntity> searchUser(TucStaffEntity tucStaffEntity) {
        //20200423 modify 改为 用 部门编码like 查询
        if (ObjectUtil.isNotEmpty(tucStaffEntity.getQueryOrgId())) {
            //根据orgid查询组织
            Org org = tucOrgServiceImpl.getByOgId(tucStaffEntity.getQueryOrgId());
            if (null != org) {
                tucStaffEntity.setQueryOrgId(null);
                tucStaffEntity.setQueryDeptId(org.getDeptId());
                return tucStaffDao.searchUser4deptId(tucStaffEntity);
            } else {
                return tucStaffDao.searchUser(tucStaffEntity);
            }
        }

        return tucStaffDao.searchUser(tucStaffEntity);
    }

    /**
     * 根据顶级orgId条件查询人员
     *
     * @param tucStaffEntity
     * @return
     */
    @Override
    public List<TucStaffEntity> findListByName(TucStaffEntity tucStaffEntity) {
        // TODO Auto-generated method stub
        return tucStaffDao.findListByName(tucStaffEntity);
    }

    /**
     * 根据用户
     *
     * @param
     * @return
     * @author hpp
     * @Time 2020年10月27日 下午5:53:18
     */
    @Override
    public List<TucStaffEntity> getByStaffcodes(List<String> staffcodes) {
        return tucStaffDao.getByStaffcodes(staffcodes);
    }

    /**
     * 根据hr部门编码更新销售的部门名称
     *
     * @param orgName
     * @param deptcode
     * @author hpp
     * @Time 2020年12月2日 下午5:05:21
     */
    @Override
    public void updateDeptnameByDeptid(String orgName, String deptcode) {
        tucStaffDao.updateDeptnameByDeptid(orgName, deptcode);
    }

    /**
     * 根据设备码来清空设备码
     *
     * @param
     */
    @Override
    public void updateByStaffPhoneBak(String staffPhoneBak) {
        tucStaffDao.updateByStaffPhoneBak(staffPhoneBak);

    }

    @Override
    public void enablePassport(String staffId, String enableStatus) {
        TUcStaff user = tucStaffDao.selectById(staffId);
        user.setStaffPassport(enableStatus);
        tucStaffDao.updateById(user);
    }

    //根据名称模糊查询列表
    @Override
    public List<TucStaffEntity> getStaffList(String name) {
        List<TucStaffEntity> list = tucStaffDao.getStaffList(name);
        return list;
    }

    /**
     * 批量查询员工
     *
     * @param codes
     * @return
     */
    @Override
    public List<TucStaffEntity> getListByCodes(List<String> codes) {
        List<TucStaffEntity> list = sysRedisService.getStaffList(codes);
        if (ObjectUtil.isEmpty(list)) {
            list = tucStaffDao.getListByCodes(codes);
        }
        return list;
    }


    @Override
    public Page<TucStaffEntity> findPage(TucStaffEntity bo) {
        Page<TucStaffEntity> page = new Page<>(bo.getPageNum(), bo.getPageSize());
        return this.baseMapper.findPage(page, bo);
    }

    @Override
    public TUcStaff get(TucStaffEntity bo) {
        return this.baseMapper.selectOne(buildQueryWrapper(bo));
    }

    @Override
    public List<TucStaffEntity> findList(TucStaffEntity bo) {
        return this.baseMapper.findList(bo);
    }

    @Override
    public List<StaffPostVO> findStaffPostList() {
        List<StaffPostVO> list = this.baseMapper.findStaffPostList(UserUtils.getIvUser());
        if (ObjectUtil.isNotEmpty(list)) {
            List<String> codes = list.stream().map(x -> x.getStaffCode()).distinct().collect(Collectors.toList());
//            TucStaffEntity bo = new TucStaffEntity();
//            bo.setStaffStatus(Constant.QY);
//            bo.setStaffCodes(codes);
            List<TucStaffEntity> staffList = this.baseMapper.findListByRoleAndStaffCode(codes);
            if (ObjectUtil.isNotEmpty(staffList)) {
                for (StaffPostVO vo : list) {
                    List<TucStaffEntity> staffs = staffList.stream().
                            filter(x -> x.getStaffCode().equals(vo.getStaffCode()) &&
                                    x.getAppId().equals(vo.getTenantId()))
                            .collect(Collectors.toList());
                    vo.setStaffList(staffs);
                }
            }
        }
        list = list.stream().filter(x -> ObjectUtil.isNotEmpty(x.getStaffList())).collect(Collectors.toList());
        return list;
    }

    @Override
    public Long selectUserCount(String postCode) {
        QueryWrapper<TUcStaff> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TUcStaff::getStaffPostCode, postCode);
        queryWrapper.lambda().eq(TUcStaff::getStaffStatus, Constant.QY);
        return tucStaffDao.selectCount(queryWrapper);
    }

    @Override
    public void deleteByPostCode(String postCode) {
        this.baseMapper.deleteByPostCode(postCode);
    }

    @Override
    public TucStaffEntity getStaffByPostCode(String postCode) {
        if (!StringUtils.isNotEmpty(postCode)) {
            throw new RuntimeException("postCode不能为空");
        }
        TucStaffEntity staff =  sysRedisService.getStaff(postCode);
        String appId = UserUtils.getTenantId();
        if (ObjectUtil.isEmpty(staff)) {
            staff = tucStaffDao.getByPostCode(postCode, appId);
        }
        if (staff == null) {
            return null;
        }
        //20200827 add
        String fulldeptname = tucOrgServiceImpl.getfulldeptname(staff.getDeptId());
        staff.setFulldeptname(fulldeptname);

        return staff;
    }

    @Override
    public void batchUpdateFaxByPostCode(List<TucStaffEntity> stafflist) {
        for (TucStaffEntity model : stafflist) {
            if (StringUtils.isNotEmpty(model.getStaffCode())) {
                TucStaffEntity dbmodel = tucStaffDao.getByStaffCode(model.getStaffCode());
                if (null != dbmodel) {
                    TUcStaff tUcStaff = BeanUtil.copyProperties(dbmodel, TUcStaff.class);
                    tUcStaff.setStaffFax(model.getStaffFax());
                    tucStaffDao.updateById(tUcStaff);
                }
            }
        }
    }

    @Override
    public List<TUcStaff> getListByPostCodes(List<String> codes) {
        QueryWrapper<TUcStaff> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(TUcStaff::getStaffPostCode, codes);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public TucStaffEntity getUserByCode(String postCode) {
        TucStaffEntity staff = sysRedisService.getStaff(postCode);
        if (ObjectUtil.isEmpty(staff)) {
            String appId = UserUtils.getTenantId();
            staff = tucStaffDao.getByPostCode(postCode, appId);
        }
        return staff;
    }

    @Override
    public List<TucStaffEntity> selectStaffByPostCodes(List<String> postCodes) {
        List<TucStaffEntity> list = sysRedisService.getStaffList(postCodes);
        if (ObjectUtil.isEmpty(list)) {
            TucStaffEntity bo = new TucStaffEntity();
            String appId = UserUtils.getTenantId();
            bo.setAppId(appId);
            bo.setStaffStatus(Constant.QY);
            bo.setPostCodes(postCodes);
            list = this.baseMapper.findList(bo);
        }
        return list;
    }

    @Override
    public TucStaffEntity selectStaffByNumAndDept(String staffNum, String deptId) {
        return this.baseMapper.selectStaffByNumAndDept(staffNum, deptId);
    }

    @Override
    public TucStaffEntity selectStaffByNum(String staffNum) {
        return this.baseMapper.selectStaffByNum(staffNum);
    }

    private LambdaQueryWrapper<TUcStaff> buildQueryWrapper(TucStaffEntity bo) {
        LambdaQueryWrapper<TUcStaff> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getStaffName()), TUcStaff::getStaffName, bo.getStaffName());
        lqw.like(StringUtils.isNotBlank(bo.getStaffEnName()), TUcStaff::getStaffEnName, bo.getStaffEnName());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), TUcStaff::getOrgId, bo.getOrgId());
        lqw.like(StringUtils.isNotBlank(bo.getOrgName()), TUcStaff::getOrgName, bo.getOrgName());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffSpells()), TUcStaff::getStaffSpells, bo.getStaffSpells());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffShortSpells()), TUcStaff::getStaffShortSpells, bo.getStaffShortSpells());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffNum()), TUcStaff::getStaffNum, bo.getStaffNum());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffShortNum()), TUcStaff::getStaffShortNum, bo.getStaffShortNum());
        lqw.eq(StringUtils.isNotBlank(bo.getPositionId()), TUcStaff::getPositionId, bo.getPositionId());
        lqw.like(StringUtils.isNotBlank(bo.getPositionName()), TUcStaff::getPositionName, bo.getPositionName());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffSex()), TUcStaff::getStaffSex, bo.getStaffSex());
        lqw.eq(bo.getStaffAge() != null, TUcStaff::getStaffAge, bo.getStaffAge());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffPaperworkType()), TUcStaff::getStaffPaperworkType, bo.getStaffPaperworkType());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffPaperworkNum()), TUcStaff::getStaffPaperworkNum, bo.getStaffPaperworkNum());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffMail()), TUcStaff::getStaffMail, bo.getStaffMail());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffMobile()), TUcStaff::getStaffMobile, bo.getStaffMobile());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffMobileBak()), TUcStaff::getStaffMobileBak, bo.getStaffMobileBak());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffFax()), TUcStaff::getStaffFax, bo.getStaffFax());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffShortFax()), TUcStaff::getStaffShortFax, bo.getStaffShortFax());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffWechart()), TUcStaff::getStaffWechart, bo.getStaffWechart());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffHomeAddress()), TUcStaff::getStaffHomeAddress, bo.getStaffHomeAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffHomePhone()), TUcStaff::getStaffHomePhone, bo.getStaffHomePhone());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffWorkAddress()), TUcStaff::getStaffWorkAddress, bo.getStaffWorkAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffWorkPhone()), TUcStaff::getStaffWorkPhone, bo.getStaffWorkPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffPassport()), TUcStaff::getStaffPassport, bo.getStaffPassport());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffNation()), TUcStaff::getStaffNation, bo.getStaffNation());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffPoliticalStatus()), TUcStaff::getStaffPoliticalStatus, bo.getStaffPoliticalStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffNativePlace()), TUcStaff::getStaffNativePlace, bo.getStaffNativePlace());
        lqw.eq(bo.getStaffSort() != null, TUcStaff::getStaffSort, bo.getStaffSort());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffStatus()), TUcStaff::getStaffStatus, bo.getStaffStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffType()), TUcStaff::getStaffType, bo.getStaffType());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffSubType()), TUcStaff::getStaffSubType, bo.getStaffSubType());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffCreator()), TUcStaff::getStaffCreator, bo.getStaffCreator());
        lqw.eq(bo.getStaffCreateTime() != null, TUcStaff::getStaffCreateTime, bo.getStaffCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffUpdator()), TUcStaff::getStaffUpdator, bo.getStaffUpdator());
        lqw.eq(bo.getStaffLastupdateTime() != null, TUcStaff::getStaffLastupdateTime, bo.getStaffLastupdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffWorkType()), TUcStaff::getStaffWorkType, bo.getStaffWorkType());
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyId()), TUcStaff::getCompanyId, bo.getCompanyId());
        lqw.like(StringUtils.isNotBlank(bo.getCompanyName()), TUcStaff::getCompanyName, bo.getCompanyName());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptId()), TUcStaff::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), TUcStaff::getDeptName, bo.getDeptName());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffHrId()), TUcStaff::getStaffHrId, bo.getStaffHrId());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffCode()), TUcStaff::getStaffCode, bo.getStaffCode());
//        lqw.eq(bo.getStaffBirthday() != null, TUcStaff::getStaffBirthday, bo.getStaffBirthday());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffEducation()), TUcStaff::getStaffEducation, bo.getStaffEducation());
        lqw.eq(bo.getStaffRetiredTime() != null, TUcStaff::getStaffRetiredTime, bo.getStaffRetiredTime());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffJobStatus()), TUcStaff::getStaffJobStatus, bo.getStaffJobStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffAccountType()), TUcStaff::getStaffAccountType, bo.getStaffAccountType());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffPhoneBak()), TUcStaff::getStaffPhoneBak, bo.getStaffPhoneBak());
        return lqw;
    }

    @Override
    public List<TUcStaff> getByStaffCode(String staffCode) {
        LambdaQueryWrapper<TUcStaff> wrapper = Wrappers.<TUcStaff>lambdaQuery()
                .eq(TUcStaff::getStaffCode, staffCode);
        return list(wrapper);
    }

    @Override
    public List<TUcStaff> handleStaff(TucStaffEntity staff) {
        List<TUcStaff> addList = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(staff)) {
            ParameterEntity bo = new ParameterEntity();
            bo.setPkey(Constant.ORG_APP);
            List<ParameterEntity> configList = iParameterService.findList(bo);
            if (ObjectUtil.isNotEmpty(configList)) {
                for (ParameterEntity entity : configList) {
                    // 部门id长度小于配置的长度，直接跳出当次循环
                    if (staff.getDeptId().length() < entity.getPvalue().length()) {
                        continue;
                    }
                    // 截取和配置一样的长度进行匹配
                    String deptId = StrUtil.sub(staff.getDeptId(), 0, entity.getPvalue().length());
                    // 判断部门和租户的关系，如果对应多个则每个租户都新增一条数据
                    if (Objects.equals(entity.getPvalue(), deptId) && this.baseMapper.countStaffByNum(staff.getStaffNum(), entity.getAppId()) <= 0) {
                        TUcStaffVO vo = BeanUtil.copyProperties(staff, TUcStaffVO.class);
                        TUcStaff model = BeanUtil.copyProperties(vo, TUcStaff.class);
//                        TUcStaff model = BeanUtil.copyProperties(staff, TUcStaff.class);
                        if (ObjectUtil.isNotEmpty(staff.getStaffBirthday())) {
                            model.setStaffBirthday(new Date(Long.valueOf(staff.getStaffBirthday())));
                        }
                        if (ObjectUtil.isNotEmpty(staff.getStaffId())) {
                            model.setStaffId(staff.getStaffId()+entity.getAppId());
                        }
                        model.setAppId(entity.getAppId());
                        model.setStaffCreateTime(new Date());
                        staff.setAppId(entity.getAppId());
                        // 如果没传岗位编码，就取员工编码，岗位类型就为主岗
                        if (ObjectUtil.isEmpty(model.getStaffPostCode())) {
                            model.setStaffPostCode(model.getStaffCode());
                            model.setStaffPostType("main");
                        }
                        addList.add(model);
                    }
                }
            }
        }
        return addList;
    }

    @Override
    public List<TUcStaff> handleStaffToPostCode(TUcStaff staff) {
        List<TUcStaff> addList = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(staff)) {
            ParameterEntity bo = new ParameterEntity();
            bo.setPkey(Constant.ORG_APP);
            List<ParameterEntity> configList = iParameterService.findList(bo);
            if (ObjectUtil.isNotEmpty(configList)) {
                for (ParameterEntity entity : configList) {
                    // 部门id长度小于配置的长度，直接跳出当次循环
                    if (staff.getDeptId().length() < entity.getPvalue().length()) {
                        continue;
                    }
                    // 截取和配置一样的长度进行匹配
                    String deptId = StrUtil.sub(staff.getDeptId(), 0, entity.getPvalue().length());
                    // 判断部门和租户的关系，如果对应多个则每个租户都新增一条数据
                    if (Objects.equals(entity.getPvalue(), deptId) && !Objects.equals(staff.getStaffPostType(), "main")) {
                        TUcStaff model = BeanUtil.copyProperties(staff, TUcStaff.class);
                        if (ObjectUtil.isNotEmpty(model.getStaffId())) {
                            model.setStaffId(model.getStaffId()+entity.getAppId());
                        }
                        model.setAppId(entity.getAppId());
                        model.setStaffCreateTime(new Date());
                        // 如果没传岗位编码，就取员工编码，岗位类型就为主岗
                        if (ObjectUtil.isEmpty(model.getStaffPostCode())) {
                            model.setStaffPostCode(model.getStaffCode());
                            model.setStaffPostType("main");
                        }
                        addList.add(model);
                    }
                }
            }
        }
        return addList;
    }

    @Override
    public Integer countStaff(String id, String appId) {
        return this.baseMapper.countStaff(id, appId);
    }


    @Override
    public Integer countStaffByNum(String num, String appId) {
        return this.baseMapper.countStaffByNum(num, appId);
    }

    @Override
    public void deleteStaffByAppId(String appId) {
        this.baseMapper.deleteStaffByAppId(appId);
    }

}
