package com.mortals.xhx.daemon.task;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
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.*;
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.model.StaffLeaveEntity;
import com.mortals.xhx.module.staff.model.StaffQuery;
import com.mortals.xhx.module.staff.service.StaffLeaveService;
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.*;
import java.util.stream.Collectors;

/**
 * 同步海康用户
 */
@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;
    @Autowired
    private StaffLeaveService staffLeaveService;

    @Override
    public void excuteTask(ITask task) throws AppException {
        try {
            log.info("同步部门");
            //syncDepts();
            log.info("同步用户");

            staffService.syncPersons(null);

        } catch (Exception e) {
            log.error("同步人事异常", e);
        }
    }

    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()));
                //新增
                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 {

    }
}
