package com.mortals.xhx.daemon.applicationservice;

import com.mortals.framework.service.ICacheService;
import com.mortals.framework.springcloud.service.IApplicationStartedService;
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 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 CheckWindowWorkmanPerformService checkWindowWorkmanPerformService;

    @Override
    public void start() {
        Thread sendThread = new Thread(new Runnable() {
            @Override
            public void run() {
                int waitTime = 1000;
                while (!stopped) {
                    try {
                        StaffCheckSummaryQuery query = cacheService.brpop(KEY_CHECK_SUMMARY_CACHE,60, StaffCheckSummaryQuery.class);
                        if(!ObjectUtils.isEmpty(query)){
                            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);
                            }
                            if(query.getCheckType() == CheckTypeEnum.其它绩效.getValue()){
                                checkWindowWorkmanPerformService.summaryCheck(query);
                            }
                        }

                        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;
    }
}
