package com.mortals.xhx.daemon.task;

import cn.hutool.core.util.StrUtil;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.attendance.model.AttendanceStatEntity;
import com.mortals.xhx.module.attendance.model.AttendanceVacationBalanceEntity;
import com.mortals.xhx.module.attendance.service.AttendanceStatService;
import com.mortals.xhx.module.attendance.service.AttendanceVacationBalanceService;
import com.mortals.xhx.module.dept.model.DeptEntity;
import com.mortals.xhx.module.dept.model.DeptQuery;
import com.mortals.xhx.module.dept.service.DeptService;
import com.mortals.xhx.module.hik.person.model.req.org.OrgListReq;
import com.mortals.xhx.module.hik.person.model.req.person.PersonReq;
import com.mortals.xhx.module.hik.person.model.rsp.org.OrgDataInfo;
import com.mortals.xhx.module.hik.person.model.rsp.org.OrgInfo;
import com.mortals.xhx.module.hik.person.model.rsp.person.PersonDataInfo;
import com.mortals.xhx.module.hik.person.model.rsp.person.PersonInfo;
import com.mortals.xhx.module.hik.person.service.IHikPersonService;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 同步用户
 */
@Slf4j
@Service("SyncUserTask")
public class SyncUserTaskImpl implements ITaskExcuteService {

    @Autowired
    private StaffService staffService;
    @Autowired
    private DeptService deptService;

    @Autowired
    private AttendanceStatService attendanceStatService;

    @Autowired
    private AttendanceVacationBalanceService balanceService;

    @Autowired
    private IHikPersonService hikPersonService;

    @Override
    public void excuteTask(ITask task) throws AppException {
        try {
            log.info("同步用户");
            syncPersons();
            log.info("同步部门");
            syncDepts();
        } catch (Exception e) {
            log.error("同步人事异常", e);
        }
    }

    private void syncPersons() {
        PersonReq personReq = new PersonReq();
        personReq.setPageNo(1);
        personReq.setPageSize(1000);
        Rest<PersonDataInfo> personRest = hikPersonService.getPersonList(personReq);
        if (personRest.getCode() == YesNoEnum.YES.getValue()) {
            List<PersonInfo> personInfoList = personRest.getData().getList();
            for (PersonInfo personInfo : personInfoList) {
                StaffEntity staffEntity = staffService.getExtCache(StrUtil.padPre(personInfo.getJobNo(), 8, "0"));
                DeptEntity deptEntity = deptService.selectOne(new DeptQuery().deptCode(personInfo.getOrgIndexCode()));
                // AttendanceVacationBalanceEntity balanceEntity = balanceService.selectOne(new AttendanceVacationBalanceQuery().staffId(staffEntity.getId()));
                //  AttendanceStatEntity statEntity = attendanceStatService.selectOne(new AttendanceStatQuery().staffId(staffEntity.getId()));
                //判断本地数据是否为空
                if (ObjectUtils.isEmpty(staffEntity)) {
                    //新增员工信息
                    staffEntity = new StaffEntity();
                    staffEntity.initAttrValue();
                    if (!ObjectUtils.isEmpty(deptEntity)) {
                        staffEntity.setDeptId(deptEntity.getId());
                    }
                    staffEntity.setName(personInfo.getPersonName());
                    staffEntity.setRemarkId(personInfo.getPersonId());
                    staffEntity.setPicUri(personInfo.getPersonPhoto().getPicUri());
                    staffEntity.setServerIndexCode(personInfo.getPersonPhoto().getServerIndexCode());
                    staffEntity.setDeptName(personInfo.getOrgName());
                    staffEntity.setGender(personInfo.getGender());
                    staffEntity.setWorkNum(personInfo.getJobNo());
                    staffEntity.setPhoneNumber(personInfo.getPhoneNo());
                    staffEntity.setCreateUserId(1L);
                    staffEntity.setCreateTime(new Date());
                    staffService.save(staffEntity);

                    AttendanceVacationBalanceEntity balanceEntity = new AttendanceVacationBalanceEntity();
                    balanceEntity.initAttrValue();

                    balanceEntity.setStaffId(staffEntity.getId());
                    balanceEntity.setStaffName(staffEntity.getName());
                    balanceEntity.setDeptId(staffEntity.getDeptId());
                    balanceEntity.setDeptName(staffEntity.getDeptName());
                    balanceEntity.setCreateUserId(1L);
                    balanceEntity.setCreateTime(new Date());
                    balanceService.save(balanceEntity);
                    //AttendanceVacationBalanceEntity balanceEntity = balanceService.selectOne(new AttendanceVacationBalanceQuery().staffId(staffEntity.getId()));

                    AttendanceStatEntity attendanceStatEntity = new AttendanceStatEntity();
                    attendanceStatEntity.initAttrValue();
                    attendanceStatEntity.setStaffId(staffEntity.getId());
                    attendanceStatEntity.setStaffName(staffEntity.getName());
                    attendanceStatEntity.setDeptId(staffEntity.getDeptId());
                    attendanceStatEntity.setDeptName(staffEntity.getDeptName());
                    attendanceStatEntity.setCreateUserId(1L);
                    attendanceStatEntity.setCreateTime(new Date());
                    //21假
                    attendanceStatEntity.setBackToUnit(balanceEntity.getBackToUnit());
                    attendanceStatEntity.setOnDutyLeave(balanceEntity.getOnDutyLeave());
                    attendanceStatEntity.setOutOfOffice(balanceEntity.getOutOfOffice());
                    attendanceStatEntity.setShiftCompensation(balanceEntity.getShiftCompensation());
                    attendanceStatEntity.setPhysicalExamination(balanceEntity.getPhysicalExamination());
                    attendanceStatEntity.setQuarantine(balanceEntity.getQuarantine());
                    attendanceStatEntity.setBusinessTrip(balanceEntity.getBusinessTrip());
                    attendanceStatEntity.setPublicHoliday(balanceEntity.getPublicHoliday());
                    attendanceStatEntity.setSickLeave(balanceEntity.getSickLeaveDays());
                    attendanceStatEntity.setFuneralLeave(balanceEntity.getBereavementLeaveDays());
                    attendanceStatEntity.setMarriageLeave(balanceEntity.getMarriageLeaveDays());
                    attendanceStatEntity.setChildRearingLeave(balanceEntity.getChildRearingLeave());
                    attendanceStatEntity.setBreastfeedingLeaveDays(balanceEntity.getBreastfeedingLeaveDays());
                    attendanceStatEntity.setMenstrualLeaveDays(balanceEntity.getMenstrualLeaveDays());
                    attendanceStatEntity.setAnnualLeaveDays(balanceEntity.getAnnualLeaveDays());
                    attendanceStatEntity.setCompensatedLeaveDays(balanceEntity.getCompensatedLeaveDays());
                    attendanceStatEntity.setPaternityLeaveDays(balanceEntity.getPaternityLeaveDays());
                    attendanceStatEntity.setMaternityLeave(balanceEntity.getMaternityLeaveDays());
                    attendanceStatEntity.setTransferBack(balanceEntity.getTransferBack());
                    attendanceStatEntity.setHomeLeave(balanceEntity.getHomeLeave());
                    attendanceStatEntity.setPersonalLeave(balanceEntity.getPersonalLeaveDays());
                    attendanceStatService.save(attendanceStatEntity);

                } else {
                    //更新
                    if (!ObjectUtils.isEmpty(deptEntity)) {
                        staffEntity.setDeptId(deptEntity.getId());
                    }
                    staffEntity.setName(personInfo.getPersonName());
                    staffEntity.setRemarkId(personInfo.getPersonId());
                    staffEntity.setPicUri(personInfo.getPersonPhoto().getPicUri());
                    staffEntity.setServerIndexCode(personInfo.getPersonPhoto().getServerIndexCode());
                    staffEntity.setDeptName(personInfo.getOrgName());
                    staffEntity.setGender(personInfo.getGender());
                    staffEntity.setWorkNum(personInfo.getJobNo());
                    staffEntity.setPhoneNumber(personInfo.getPhoneNo());
                    staffEntity.setUpdateUserId(1L);
                    staffEntity.setUpdateTime(new Date());
                    staffService.update(staffEntity);

                }
            }
        }
    }

    private void syncDepts() {
        //部门同步
        OrgListReq orgListReq = new OrgListReq();
        orgListReq.setPageNo(1);
        orgListReq.setPageSize(1000);
        Rest<OrgDataInfo> orgRest = hikPersonService.getOrgList(orgListReq);
        if (orgRest.getCode() == YesNoEnum.YES.getValue()) {
            List<OrgInfo> orgInfoList = orgRest.getData().getList();
            for (OrgInfo orgInfo : orgInfoList) {
                //查询数据库信息
                DeptEntity deptEntity = deptService.selectOne(new DeptQuery().deptCode(orgInfo.getOrgIndexCode()));

                DeptEntity deptEntityParent = deptService.selectOne(new DeptQuery().deptCode(orgInfo.getParentOrgIndexCode()));
                //DeptEntity deptEntityParent = deptDao.queryDeptParient(orgInfo.getParentOrgIndexCode());
                //新增
                if (ObjectUtils.isEmpty(deptEntity)) {
                    deptEntity = new DeptEntity();
                    deptEntity.initAttrValue();
                    if (ObjectUtils.isEmpty(deptEntityParent)) {
                        deptEntity.setParentId(deptEntityParent.getId());
                    }
                    deptEntity.setDeptName(orgInfo.getOrgName());
                    deptEntity.setDeptCode(orgInfo.getOrgIndexCode());
                    deptEntity.setAncestors(orgInfo.getOrgPath());
                    deptEntity.setRemark(orgInfo.getParentOrgIndexCode());
                    deptEntity.setCreateTime(new Date());
                    deptEntity.setCreateUserId(1L);
                    deptService.save(deptEntity);
                } else {
                    //更新
                    deptEntity.setDeptName(orgInfo.getOrgName());
                    deptEntity.setDeptCode(orgInfo.getOrgIndexCode());
                    deptEntity.setAncestors(orgInfo.getOrgPath());
                    deptEntity.setUpdateTime(new Date());
                    deptEntity.setUpdateUserId(1L);
                    deptService.update(deptEntity);
                }
            }
        }
    }

    @Override
    public void stopTask(ITask task) throws AppException {

    }
}
