package com.mortals.xhx.daemon.task;

import cn.hutool.core.date.DateUtil;
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.AuditStatusEnum;
import com.mortals.xhx.common.code.StaffSatusEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.holiday.HolidayPdu;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.feign.holiday.IHolidayFeign;
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.holiday.model.HolidayEntity;
import com.mortals.xhx.module.holiday.model.HolidayQuery;
import com.mortals.xhx.module.holiday.service.HolidayService;
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.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 同步节假日
 */
@Slf4j
@Service("SyncHolidayTask")
public class SyncHolidayTaskImpl implements ITaskExcuteService {

    @Autowired
    private IHolidayFeign holidayFeign;

    @Autowired
    private HolidayService holidayService;

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

        HolidayPdu holidayPdu = new HolidayPdu();
        holidayPdu.setYear(DateUtil.year(new Date()));
        holidayPdu.setSize(-1);
        Rest<RespData<List<HolidayPdu>>> respDataRest = holidayFeign.list(holidayPdu);
        if (respDataRest.getCode() == YesNoEnum.YES.getValue()) {
            List<HolidayPdu> holidayPduList = respDataRest.getData().getData();

            for (HolidayPdu pdu : holidayPduList) {
                //判断当前日期的节假日是否存在
                HolidayQuery holidayQuery = new HolidayQuery();
                holidayQuery.setStartTimeStart(DateUtil.formatDate(pdu.getStartTime()));
                holidayQuery.setStartTimeEnd(DateUtil.formatDate(pdu.getStartTime()));
                HolidayEntity holidayEntity = holidayService.selectOne(holidayQuery);
                if (ObjectUtils.isEmpty(holidayEntity)) {
                    //新增
                    holidayEntity = new HolidayEntity();
                    holidayEntity.initAttrValue();
                    BeanUtils.copyProperties(pdu, holidayEntity, BeanUtil.getNullPropertyNames(pdu));
                    holidayEntity.setCreateTime(new Date());
                    holidayEntity.setCreateUserId(1L);
                    holidayService.save(holidayEntity);
                }
            }
        }
    }


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

    }
}
