package com.mortals.xhx.daemon.applicationservice;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.springcloud.service.IApplicationStartedService;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.common.code.CheckTypeEnum;
import com.mortals.xhx.module.check.model.vo.StaffCheckSummaryQuery;
import com.mortals.xhx.module.check.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Date;

import static com.mortals.xhx.common.key.RedisKey.KEY_CHECK_SUMMARY_CACHE;

/***
 * 绩效汇总任务执行
 */
@Component
@Slf4j
public class StaffCheckSummaryService implements IApplicationStartedService {

    protected Boolean stopped = false;

    @Autowired
    private ICacheService cacheService;
    @Autowired
    private CheckAttendRecordService checkAttendRecordService;
    @Autowired
    private CheckEffectRecordService checkEffectRecordService;
    @Autowired
    private CheckGoworkRecordService checkGoworkRecordService;
    @Autowired
    private CheckReviewRecordService checkReviewRecordService;
    @Autowired
    private CheckComplainRecordService checkComplainRecordService;
    @Autowired
    private CheckWindowWorkmanPerformService checkWindowWorkmanPerformService;
    @Autowired
    private CheckOtherRecordService checkOtherRecordService;

    @Override
    public void start() {
        Thread sendThread = new Thread(new Runnable() {
            @Override
            public void run() {
                int waitTime = 50;
                while (!stopped) {
                    try {
                        StaffCheckSummaryQuery query = cacheService.brpop(KEY_CHECK_SUMMARY_CACHE,60, StaffCheckSummaryQuery.class);
                        if(!ObjectUtils.isEmpty(query)){
                            if(StringUtils.isNotEmpty(query.getCheckTimeStart())){
                                Date date = DateUtil.parse(query.getCheckTimeStart(),"yyyy-MM-dd");
                                query.setCheckTimeStart(DateUtil.beginOfMonth(date).toDateStr());
                                query.setCheckTimeEnd(DateUtil.endOfMonth(date).toDateStr());
                                query.setYear(DateUtil.year(date));
                                query.setMonth(DateUtil.month(date)+1);
                            }
                            if(query.getCheckType() == CheckTypeEnum.考勤绩效.getValue()){
                                checkAttendRecordService.summaryCheck(query);
                            }
                            if(query.getCheckType() == CheckTypeEnum.效能绩效.getValue()){
                                checkEffectRecordService.summaryCheck(query);
                            }
                            if(query.getCheckType() == CheckTypeEnum.办件绩效.getValue()){
                                checkGoworkRecordService.summaryCheck(query);
                            }
                            if(query.getCheckType() == CheckTypeEnum.评价绩效.getValue()){
                                checkReviewRecordService.summaryCheck(query);
                                checkComplainRecordService.summaryCheck(query);
                            }
                            if(query.getCheckType() == CheckTypeEnum.自评绩效.getValue()){
                                checkWindowWorkmanPerformService.summaryCheck(query);
                            }
                            if(query.getCheckType() == CheckTypeEnum.其它绩效.getValue()){
                                checkOtherRecordService.summaryCheck(query);
                            }
                            if(query.getCheckType()==null){
                                log.info("绩效分数汇总开始:"+ JSONObject.toJSONString(query));
                                checkAttendRecordService.summaryCheck(query);
                                checkEffectRecordService.summaryCheck(query);
                                checkGoworkRecordService.summaryCheck(query);
                                checkReviewRecordService.summaryCheck(query);
                                checkComplainRecordService.summaryCheck(query);
                                checkWindowWorkmanPerformService.summaryCheck(query);
                                checkOtherRecordService.summaryCheck(query);
                                log.info("绩效分数汇总完成");
                            }
                        }


                        try {
                            Thread.sleep(waitTime);
                        } catch (InterruptedException e2) {
                        }

                    } catch (Exception e) {
                      //  log.error("异常", e);

                        try {
                            Thread.sleep(waitTime);
                        } catch (InterruptedException e2) {
                        }
                    }
                }
            }
        });
        sendThread.start();
    }

    @Override
    public void stop() {
        log.info("停止服务..");
        this.stopped = true;
    }

    @Override
    public int getOrder() {
        return 30;
    }

}
