package com.mortals.xhx.daemon.task;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.xhx.base.framework.config.JsonUtils;
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 JsonUtils jsonUtils;

    @Autowired
    private StaffDaoImpl staffDao;

    @Autowired
    private DeptDaoImpl deptDao;

    @Override
    public void excuteTask(ITask task) throws AppException {
        log.info("同步用户--部门");
        try {
            String resultPerson = ArtemisPostTest.callPostApiGetPersonList();
            PersonHikData personHikData = jsonUtils.json2Bean(resultPerson, PersonHikData.class);
            if (Objects.nonNull(personHikData)) {
                for (ListItem list : personHikData.getList()) {
                    //根据id获取本地数据
                    StaffEntity staffEntity1 = staffDao.get(Long.valueOf(list.getPersonId()));
                    List<StaffEntity> staffEntitieList = staffDao.queryAllList();
                    //判断本地数据是否为空
                    if (Objects.isNull(staffEntity1)) {
                        //如果为空则将数据存入数据库
                        staffEntity1.setName(list.getPersonName());
                        staffEntity1.setRemark(list.getPersonId());
                        staffEntity1.setPhotoPath(list.getPersonPhoto().getPicUri());
                        staffEntity1.setDeptName(list.getOrgName());
                        staffEntity1.setDeptId(Long.valueOf(list.getOrgIndexCode()));
                        staffEntity1.setDeptName(list.getOrgName());
                        staffEntity1.setSource(1);
                        if (list.getBirthday()!=null){
                            staffEntity1.setBirthday((Date) list.getBirthday());
                        }
                        if (list.getPhoneNo()!=null){
                            staffEntity1.setPhoneNumber(String.valueOf(list.getPhoneNo()));
                        }
                        staffDao.insert(staffEntity1);
                    }
                    //本地数据不为空
                    else {
                        //本地数据遍历数据
                        for (StaffEntity staffEntity : staffEntitieList) {
                            //如果本地的id等于海康的id
                            if (staffEntity.getRemark().equals(list.getPersonId())) {
                                //执行修改
                                staffEntity.setName(list.getPersonName());
                                staffEntity.setRemark(list.getPersonId());
                                staffEntity.setPhotoPath(list.getPersonPhoto().getPicUri());
                                staffEntity.setDeptName(list.getOrgName());
                                staffEntity.setDeptId(Long.valueOf(list.getOrgIndexCode()));
                                staffEntity.setDeptName(list.getOrgName());
                                staffEntity.setSource(1);
                                if (list.getBirthday()==null && staffEntity.getBirthday()!=null){
                                    staffEntity.setBirthday(staffEntity.getBirthday());
                                }
                                if (list.getPhoneNo()!=null && staffEntity.getPhoneNumber()!=null){
                                    staffEntity1.setPhoneNumber(staffEntity.getPhoneNumber());
                                }
                                staffDao.update(staffEntity);
                            }
                            for (ListItem item :personHikData.getList()){
                                //如果本地id不等于海康id并且海康id不等于本地id
                                if (!item.getPersonId().equals(staffEntity.getRemark())){
                                    //否则执行删除
                                    staffDao.delete(Long.valueOf(staffEntity.getRemark()));

                                }
                            }

                        }
                    }
                }
            }
            String resultDept = ArtemisPostTest.callPostApiGetDeptList();
            DeptHikData deptHikData = jsonUtils.json2Bean(resultDept,DeptHikData.class);
            if (Objects.isNull(deptHikData)){
                for (ListDept listDept:deptHikData.getList()){
                    DeptEntity deptEntity = deptDao.get(Long.valueOf(listDept.getOrgIndexCode()));
                    List<DeptEntity> deptEntityList = deptDao.queryAllList();
                    if (Objects.isNull(deptEntity)){
                        deptEntity.setDeptName(listDept.getOrgName());
                        deptEntity.setParentId(Long.valueOf(listDept.getParentOrgIndexCode()));
                        deptEntity.setAncestors(listDept.getOrgPath());
                        deptDao.insert(deptEntity);
                    }else {
                        for (DeptEntity deptEntity1 :deptEntityList){
                            if (!deptEntity1.getParentId().equals(listDept.getOrgIndexCode())){
                                deptEntity1.setAncestors(listDept.getOrgPath());
                                deptEntity1.setDeptName(listDept.getOrgName());
                                deptDao.update(deptEntity1);
                            }
                            for (ListDept dept : deptHikData.getList()){
                                if (!dept.getOrgIndexCode().equals(deptEntity1.getParentId())){
                                    deptDao.delete(Long.valueOf(listDept.getOrgIndexCode()));
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


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

    }


}
