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.List;
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());
                    List<StaffEntity> staffEntities = staffDao.queryAllList();
                    //判断本地数据是否为空
                    if (Objects.isNull(staffEntity1)) {
                        //如果为空则将数据存入数据库
                        StaffEntity staffEntity = new StaffEntity();
                        System.out.println(list.getPersonName());
                        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()));
                        }
                        System.out.println("111" + staffEntity);
                        staffDao.insert(staffEntity);
                    }

                    //本地数据不为空
                    else {
                        //本地数据遍历数据
                        for (StaffEntity staffEntity : staffEntities) {
                            //如果本地的id等于海康的id
                            if (staffEntity.getRemarkId().equals(list.getPersonId())) {
                                //执行修改
                                StaffEntity staffEntity2 = new StaffEntity();
                                System.out.println(list.getPersonName());
                                staffEntity2.setName(list.getPersonName());
                                staffEntity2.setRemarkId(list.getPersonId());
                                staffEntity2.setPhotoPath(list.getPersonPhoto().getPicUri());
                                staffEntity2.setDeptName(list.getOrgName());
                                staffEntity2.setSource(1);
                                staffEntity2.setStatus(1);
                                staffEntity2.setGender(list.getGender());
                                staffEntity2.setWorkNum(list.getJobNo());
                                staffEntity2.setCreateUserId(Long.valueOf(1));
                                staffEntity2.setCreateTime(new Date());
                                if (list.getBirthday() != null) {
                                    staffEntity2.setBirthday((Date) list.getBirthday());
                                }
                                if (list.getPhoneNo() != null) {
                                    staffEntity2.setPhoneNumber(String.valueOf(list.getPhoneNo()));
                                }
                                staffDao.update(staffEntity2);
                            }
                            for (ListItem item : personHikData.getList()) {
                                //如果本地id不等于海康id并且海康id不等于本地id
                                if (!item.getPersonId().equals(staffEntity.getRemarkId())) {
                                    //否则执行删除
                                    staffDao.delete(staffEntity.getId());

                                }
                            }

                        }
                    }
                }
            }
        } 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("111"+deptHikData);
            int i = 0;
            if (Objects.nonNull(deptHikData)) {
                for (ListDept listDept : deptHikData.getList()) {
                    DeptEntity deptEntity = deptDao.queryDept(listDept.getOrgIndexCode());
                    List<DeptEntity> deptEntities = deptDao.queryAllList();

                    if (Objects.isNull(deptEntity)) {
                        DeptEntity deptEntity1 = new DeptEntity();
                        deptEntity1.setDeptName(listDept.getOrgName());
                        deptEntity1.setDeptCode(listDept.getOrgIndexCode());
                        deptEntity1.setAncestors(listDept.getOrgPath());
                        deptEntity1.setDeptStatus(1);
                        deptEntity1.setOrderNum(0);
                        deptEntity1.setPersonNum(i+1);
                        deptEntity1.setCreateTime(new Date());
                        deptEntity1.setCreateUserId(Long.valueOf(1));
                        deptDao.insert(deptEntity1);
                    } else {
                        for (DeptEntity deptEntity2 : deptEntities) {
                            if (!deptEntity2.getDeptCode().equals(listDept.getOrgIndexCode())) {
                                deptEntity2.setDeptName(listDept.getOrgName());
                                deptEntity2.setDeptCode(listDept.getOrgIndexCode());
                                deptEntity2.setAncestors(listDept.getOrgPath());
                                deptEntity2.setDeptStatus(1);
                                deptEntity2.setOrderNum(0);
                                deptEntity2.setPersonNum(i+1);
                                deptEntity2.setCreateTime(new Date());
                                deptEntity2.setCreateUserId(Long.valueOf(1));
                                deptDao.update(deptEntity2);
                            }
                            for (ListDept dept : deptHikData.getList()) {
                                if (!dept.getOrgIndexCode().equals(deptEntity2.getDeptCode())) {
                                    deptDao.delete(deptEntity2.getId());
                                }
                            }
                        }
                    }
                }
            }

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

    }


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

    }


}
