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.framework.util.DateUtils;
import com.mortals.xhx.common.code.StaffSatusEnum;
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.*;
import com.mortals.xhx.module.staff.service.StaffPerformStatService;
import com.mortals.xhx.module.staff.service.StaffPerformSummaryService;
import com.mortals.xhx.module.staff.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 员工绩效数据初始化任务
 */
@Slf4j
@Service("StaffPerformInitDataTask")
public class StaffPerformInitDataTaskImpl implements ITaskExcuteService {

    @Autowired
    private StaffPerformStatService staffPerformStatService;
    @Autowired
    private StaffPerformSummaryService staffPerformSummaryService;
    @Autowired
    private HolidayService holidayService;
    @Autowired
    private StaffService staffService;

    @Override
    public void excuteTask(ITask task) throws AppException {
        boolean isReturn = false;
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        String dateStr = DateUtils.getStrDate(calendar.getTime());
        //判断是否为特定节假日
        HolidayQuery holidayQuery = new HolidayQuery();
        holidayQuery.setStartTimeStart(dateStr);
        holidayQuery.setStartTimeEnd(dateStr);
        HolidayEntity holidayEntity = holidayService.selectOne(holidayQuery);
        if(holidayEntity!=null){
            if(holidayEntity.getWorkorholiday()==0){
                //特定节假日
                isReturn = true;
            }
        }else {
            //判断是否为周末
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) {
                isReturn = true;
            }
        }
        BigDecimal totalScore = new BigDecimal(100);
        List<StaffEntity> allStaff = staffService.getAllList();
        if(!isReturn) {
            //非节假日非周末才做绩效初始化
            if(CollectionUtils.isNotEmpty(allStaff)) {
                List<StaffPerformStatEntity> list = new ArrayList<>();
                allStaff.stream().forEach(item -> {
                    boolean flag = true;
                    if(item.getStatus() == StaffSatusEnum.离职.getValue()) {
                        if(item.getLeaveDate()!=null){
                            String leaveDateStr = DateUtils.getStrDate(item.getLeaveDate());
                            if(DateUtils.StrToDate(leaveDateStr).compareTo(DateUtils.StrToDate(dateStr)) < 0){
                                //离职日期在统计日期之前不统计
                                flag = false;
                            }
                        }
                    }
                    //绩效考核授权4个维度不启用时不再生成绩效信息
                    if(item.getAttendCheck()==0 && item.getEffectCheck()==0 && item.getGoworkCheck()==0 && item.getComplainCheck()==0){
                        flag = false;
                    }
                    if(flag){
                        StaffPerformStatEntity statEntity = new StaffPerformStatEntity();
                        statEntity.initAttrValue();
                        statEntity.setStaffId(item.getId());
                        statEntity.setStaffName(item.getName());
                        statEntity.setDeptId(item.getDeptId());
                        statEntity.setDeptName(item.getDeptName());
                        statEntity.setSalaId(item.getSalaId());
                        statEntity.setSalaName(item.getSalaName());
                        statEntity.setYear(year);
                        statEntity.setMonth(month);
                        statEntity.setDay(dayOfMonth);
                        statEntity.setTotalScore(totalScore);
                        statEntity.setCreateUserId(1l);
                        statEntity.setCreateTime(new Date());
                        StaffPerformStatQuery tempQuery = new StaffPerformStatQuery();
                        tempQuery.setStaffId(statEntity.getStaffId());
                        tempQuery.setYear(statEntity.getYear());
                        tempQuery.setMonth(statEntity.getMonth());
                        tempQuery.setDay(statEntity.getDay());
                        StaffPerformStatEntity temp = staffPerformStatService.selectOne(tempQuery);
                        if (temp == null) {
                            list.add(statEntity);
                        }
                    }
                });
                staffPerformStatService.save(list);
            }
        }

        if(dayOfMonth==1){
            //月初第一天初始化绩效汇总信息
            if(CollectionUtils.isNotEmpty(allStaff)) {
                List<StaffPerformSummaryEntity> summaryList = new ArrayList<>();
                allStaff.stream().forEach(item -> {
                    boolean flag = true;
                    if(item.getStatus() == StaffSatusEnum.离职.getValue()) {
                        if(item.getLeaveDate()!=null){
                            String leaveDateStr = DateUtils.getStrDate(item.getLeaveDate());
                            if(DateUtils.StrToDate(leaveDateStr).compareTo(DateUtils.StrToDate(dateStr)) < 0){
                                //离职日期在统计日期之前不统计
                                flag = false;
                            }
                        }
                    }
                    //绩效考核授权4个维度不启用时不再生成绩效信息
                    if(item.getAttendCheck()==0 && item.getEffectCheck()==0 && item.getGoworkCheck()==0 && item.getComplainCheck()==0){
                        flag = false;
                    }
                    if(flag) {
                        StaffPerformSummaryEntity staffPerformSummaryEntity = new StaffPerformSummaryEntity();
                        staffPerformSummaryEntity.initAttrValue();
                        staffPerformSummaryEntity.setStaffId(item.getId());
                        staffPerformSummaryEntity.setStaffName(item.getName());
                        staffPerformSummaryEntity.setDeptId(item.getDeptId());
                        staffPerformSummaryEntity.setDeptName(item.getDeptName());
                        staffPerformSummaryEntity.setSalaId(item.getSalaId());
                        staffPerformSummaryEntity.setSalaName(item.getSalaName());
                        staffPerformSummaryEntity.setYear(year);
                        staffPerformSummaryEntity.setMonth(month);
                        staffPerformSummaryEntity.setTotalScore(totalScore);
                        staffPerformSummaryEntity.setCreateUserId(1l);
                        staffPerformSummaryEntity.setCreateTime(new Date());
                        StaffPerformSummaryQuery summaryQuery = new StaffPerformSummaryQuery();
                        summaryQuery.setStaffId(staffPerformSummaryEntity.getStaffId());
                        summaryQuery.setYear(staffPerformSummaryEntity.getYear());
                        summaryQuery.setMonth(staffPerformSummaryEntity.getMonth());
                        StaffPerformSummaryEntity tempSummary = staffPerformSummaryService.selectOne(summaryQuery);
                        if (tempSummary == null) {
                            summaryList.add(staffPerformSummaryEntity);
                        }
                    }
                });
                staffPerformSummaryService.save(summaryList);
            }
        }

    }

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

    }

}
