package com.mortals.xhx.daemon.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.xhx.busiz.hik.ArtemisPostTest;
import com.mortals.xhx.busiz.rsp.DeptHikData;
import com.mortals.xhx.busiz.rsp.ListDept;
import com.mortals.xhx.busiz.rsp.ListItem;
import com.mortals.xhx.busiz.rsp.PersonHikData;
import com.mortals.xhx.module.dept.dao.ibatis.DeptDaoImpl;
import com.mortals.xhx.module.dept.model.DeptEntity;
import com.mortals.xhx.module.dept.service.DeptService;
import com.mortals.xhx.module.staff.dao.ibatis.StaffDaoImpl;
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 java.util.Date;
import java.util.Objects;

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


    @Autowired
    private StaffDaoImpl staffDao;

    @Autowired
    private DeptDaoImpl deptDao;

    @Autowired
    private DeptService deptService;

    @Autowired
    private StaffService service;

    @Override
    public void excuteTask(ITask task) throws AppException {
        log.info("同步用户--部门");
        try {
            String resultDept = ArtemisPostTest.callPostApiGetDeptList();
            JSONObject jsonObjectDept = JSON.parseObject(resultDept);
            JSONObject jsonObjectDept1 = jsonObjectDept.getJSONObject("data");
            DeptHikData deptHikData = JSONObject.parseObject(jsonObjectDept1.toJSONString(), DeptHikData.class);
            if (Objects.nonNull(deptHikData)) {
                for (ListDept listDept : deptHikData.getList()) {
                    //查询数据库信息
                    DeptEntity deptEntity = deptDao.queryDept(listDept.getOrgIndexCode());
                    DeptEntity deptEntityParent = deptDao.queryDeptParient(listDept.getParentOrgIndexCode());

                    if (Objects.isNull(deptEntity)) {
                        if (Objects.nonNull(deptEntityParent)) {
                            deptEntity.setParentId(deptEntityParent.getId());
                        }
                        deptEntity.setDeptName(listDept.getOrgName());
                        deptEntity.setDeptCode(listDept.getOrgIndexCode());
                        deptEntity.setAncestors(listDept.getOrgPath());
                        deptEntity.setDeptStatus(1);
                        deptEntity.setOrderNum(0);
                        deptEntity.setRemark(listDept.getParentOrgIndexCode());
                        deptEntity.setCreateTime(new Date());
                        deptEntity.setCreateUserId(Long.valueOf(1));
                        deptEntity.setUpdateTime(new Date());
                        deptEntity.setUpdateUserId(Long.valueOf(1));
                        deptService.save(deptEntity);
                    } else {
                        if (Objects.nonNull(deptEntityParent)) {
                            deptEntity.setParentId(deptEntityParent.getId());
                        }
                        deptEntity.setDeptName(listDept.getOrgName());
                        deptEntity.setDeptCode(listDept.getOrgIndexCode());
                        deptEntity.setAncestors(listDept.getOrgPath());
                        deptEntity.setCreateTime(new Date());
                        deptEntity.setCreateUserId(Long.valueOf(1));
                        deptService.update(deptEntity);
                    }
                }
            }
            //同步员工
            String resultPerson = ArtemisPostTest.callPostApiGetPersonList();
            JSONObject jsonObject = JSON.parseObject(resultPerson);
            JSONObject jsonObject1 = jsonObject.getJSONObject("data");
            PersonHikData personHikData = JSONObject.parseObject(jsonObject1.toJSONString(), PersonHikData.class);
            if (Objects.nonNull(personHikData)) {
                for (ListItem list : personHikData.getList()) {
                    //根据id获取本地数据
                    StaffEntity staffEntity1 = staffDao.queryHik(list.getPersonId());
                    DeptEntity deptEntity = deptDao.queryDept(list.getOrgIndexCode());
                    //判断本地数据是否为空
                    if (Objects.isNull(staffEntity1)) {
                        //如果为空则将数据存入数据库
                        staffEntity1.initAttrValue();
                        if (Objects.nonNull(staffEntity1)) {
                            staffEntity1.setDeptId(deptEntity.getId());
                        }
                        staffEntity1.setName(list.getPersonName());
                        staffEntity1.setRemarkId(list.getPersonId());
                        staffEntity1.setPhotoPath(list.getPersonPhoto().getPicUri());
                        staffEntity1.setDeptName(list.getOrgName());
                        staffEntity1.setSource(1);
                        staffEntity1.setStatus(1);
                        staffEntity1.setGender(list.getGender());
                        staffEntity1.setWorkNum(list.getJobNo());
                        staffEntity1.setCreateUserId(Long.valueOf(1));
                        staffEntity1.setCreateTime(new Date());
                        if (list.getBirthday() != null) {
                            staffEntity1.setBirthday((Date) list.getBirthday());
                        }
                        if (list.getPhoneNo() != null) {
                            staffEntity1.setPhoneNumber(String.valueOf(list.getPhoneNo()));
                        }
                        service.save(staffEntity1);
                        DeptEntity deptEntity1 = deptService.get(staffEntity1.getDeptId());
                        String ancestor = deptEntity1.getAncestors().split(",", 2)[1];
                        String[] ancestors = ancestor.split(",");
                        for (String newAncestor : ancestors) {
                            DeptEntity deptEntity2 = deptService.get(Long.valueOf(newAncestor));
                            if (Objects.nonNull(deptEntity2)) {
                                int personNumcount = staffDao.queryPersonNum(staffEntity1.getDeptId());
                                if (Objects.nonNull(personNumcount) && Objects.nonNull(deptEntity2)) {
                                    deptEntity2.setPersonNum(personNumcount);
                                    deptService.update(deptEntity2);
                                }
                            }

                        }


                    }
                    //本地数据不为空
                    else {
                        //执行修改
                        staffEntity1.initAttrValue();
                        if (Objects.nonNull(deptEntity)) {
                            staffEntity1.setDeptId(deptEntity.getId());
                        }
                        staffEntity1.setName(list.getPersonName());
                        staffEntity1.setRemarkId(list.getPersonId());
                        staffEntity1.setPhotoPath(list.getPersonPhoto().getPicUri());
                        staffEntity1.setDeptName(list.getOrgName());
                        staffEntity1.setGender(list.getGender());
                        staffEntity1.setWorkNum(list.getJobNo());
                        staffEntity1.setCreateUserId(Long.valueOf(1));
                        staffEntity1.setCreateTime(new Date());
                        if (list.getBirthday() != null) {
                            staffEntity1.setBirthday((Date) list.getBirthday());
                        }
                        if (list.getPhoneNo() != null) {
                            staffEntity1.setPhoneNumber(String.valueOf(list.getPhoneNo()));
                        }
                        service.update(staffEntity1);
                        DeptEntity deptEntity1 = deptService.get(staffEntity1.getDeptId());
                        String ancestor = deptEntity1.getAncestors().split(",", 2)[1];
                        String[] ancestors = ancestor.split(",");
                        for (String newAncestor : ancestors) {
                            DeptEntity deptEntity2 = deptService.get(Long.valueOf(newAncestor));
                            if (Objects.nonNull(deptEntity2)) {
                                int personNumcount = staffDao.queryPersonNum(staffEntity1.getDeptId());
                                if (Objects.nonNull(personNumcount) && Objects.nonNull(deptEntity2)) {
                                    deptEntity2.setPersonNum(personNumcount);
                                    deptService.update(deptEntity2);
                                }
                            }

                        }

                    }

                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }


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

    }


}
