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.staff.dao.ibatis.StaffDaoImpl;
import com.mortals.xhx.module.staff.model.StaffEntity;
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;

    @Override
    public void excuteTask(ITask task) throws AppException {
        log.info("同步用户--部门");
        try {
            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)) {
                        //如果为空则将数据存入数据库
                        StaffEntity staffEntity = new StaffEntity();
                        if (Objects.nonNull(deptEntity)) {
                            staffEntity.setDeptId(deptEntity.getId());
                        }
                        staffEntity.setName(list.getPersonName());
                        staffEntity.setRemarkId(list.getPersonId());
                        staffEntity.setPhotoPath(list.getPersonPhoto().getPicUri());
                        staffEntity.setDeptName(list.getOrgName());
                        staffEntity.setSource(1);
                        staffEntity.setStatus(1);
                        staffEntity.setGender(list.getGender());
                        staffEntity.setWorkNum(list.getJobNo());
                        staffEntity.setCreateUserId(Long.valueOf(1));
                        staffEntity.setCreateTime(new Date());
                        if (list.getBirthday() != null) {
                            staffEntity.setBirthday((Date) list.getBirthday());
                        }
                        if (list.getPhoneNo() != null) {
                            staffEntity.setPhoneNumber(String.valueOf(list.getPhoneNo()));
                        }
                        staffDao.insert(staffEntity);
                    }
                    //本地数据不为空
                    else {
                        //执行修改
                        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.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()));
                        }
                        staffDao.update(staffEntity1);
                    }
                }
            }

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

        try {
            String resultDept = ArtemisPostTest.callPostApiGetDeptList();
            JSONObject jsonObjectDept = JSON.parseObject(resultDept);
            JSONObject jsonObjectDept1 = jsonObjectDept.getJSONObject("data");
            DeptHikData deptHikData = JSONObject.parseObject(jsonObjectDept1.toJSONString(), DeptHikData.class);
            System.out.println(resultDept);
            if (Objects.nonNull(deptHikData)) {
                for (ListDept listDept : deptHikData.getList()) {
                    //查询数据库信息
                    DeptEntity deptEntity = deptDao.queryDept(listDept.getOrgIndexCode());
                    DeptEntity deptEntityParent = deptDao.queryDeptParient(listDept.getParentOrgIndexCode());
                    DeptEntity deptEntity1 = new DeptEntity();
                    if (Objects.isNull(deptEntity)) {
                        if (Objects.nonNull(deptEntityParent)) {
                            deptEntity1.setParentId(deptEntityParent.getId());
                        }
                        deptEntity1.setDeptName(listDept.getOrgName());
                        deptEntity1.setDeptCode(listDept.getOrgIndexCode());
                        deptEntity1.setAncestors(listDept.getOrgPath());
                        deptEntity1.setDeptStatus(1);
                        deptEntity1.setOrderNum(0);
                        deptEntity1.setRemark(listDept.getParentOrgIndexCode());
                        deptEntity1.setCreateTime(new Date());
                        deptEntity1.setCreateUserId(Long.valueOf(1));
                        System.out.println(deptEntity1);
                        deptDao.insert(deptEntity1);
                    } 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));
                        deptDao.update(deptEntity);
                    }
                }
            }

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


    }


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

    }


}
