package com.gree.mall.manager.schedule;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.gree.mall.manager.enums.workorder.AppraiseApplyStatusEnum;
import com.gree.mall.manager.enums.workorder.AppraiseConfigTypeEnum;
import com.gree.mall.manager.enums.workorder.AppraiseOnusEnum;
import com.gree.mall.manager.enums.workorder.AppraiseStatusEnum;
import com.gree.mall.manager.plus.entity.PgAppraiseApply;
import com.gree.mall.manager.plus.entity.PgAppraiseConfig;
import com.gree.mall.manager.plus.entity.PgOrderBase;
import com.gree.mall.manager.plus.service.PgAppraiseApplyService;
import com.gree.mall.manager.plus.service.PgAppraiseConfigService;
import com.gree.mall.manager.plus.service.PgOrderBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@ConditionalOnProperty(name = "schedule.enable", havingValue = "true", matchIfMissing = true)
@Slf4j
@Component
public class AppraiseApplySchedule {

    @Resource
    PgAppraiseApplyService pgAppraiseApplyService;
    @Resource
    PgAppraiseConfigService pgAppraiseConfigService;
    @Resource
    PgOrderBaseService pgOrderBaseService;
    @Value("${spring.profiles.active}")
    private String env;

    /**
     * 待中心审核检查超时
     */
    @Scheduled(fixedDelay = 2 * 1000)
    public void task1(){
        if(env.equals("test") || env.equals("prd")) {
            final List<PgAppraiseApply> applyList = pgAppraiseApplyService.lambdaQuery()
                    .eq(PgAppraiseApply::getStatus, AppraiseApplyStatusEnum.WAIT_CENTER.getKey())
                    .list();
            if (CollectionUtil.isNotEmpty(applyList)) {
                // 首次审核时间不能超时, 二次审核时间不能超时，从网点提交首次申诉时间开始计算
                final List<PgAppraiseConfig> configList = pgAppraiseConfigService.lambdaQuery()
                        .eq(PgAppraiseConfig::getCompanyWechatId, applyList.get(0).getCompanyWechatId())
                        .in(PgAppraiseConfig::getType, AppraiseConfigTypeEnum.FIRST_C.getKey(), AppraiseConfigTypeEnum.SECOND_C.getKey())
                        .list();

                final Map<String, String> configMap = configList.stream().collect(Collectors.toMap(PgAppraiseConfig::getType, PgAppraiseConfig::getVal));

                for (PgAppraiseApply apply : applyList) {
                    String value = configMap.get(!apply.getIsSecond() ? AppraiseConfigTypeEnum.FIRST_C.getKey() : AppraiseConfigTypeEnum.SECOND_C.getKey());
                    final int val = Integer.parseInt(value);
                    Date endTime = DateUtil.offsetHour(apply.getApplyTime(), val);
                    if (DateUtil.date().after(endTime)) {
                        pgAppraiseApplyService.lambdaUpdate()
                                .set(PgAppraiseApply::getExamineResult, AppraiseStatusEnum.A.getKey())
                                .set(PgAppraiseApply::getStatus, AppraiseApplyStatusEnum.OVER.getKey())
                                .set(PgAppraiseApply::getWebsitResult, AppraiseOnusEnum.NOT.getKey())
                                .set(PgAppraiseApply::getCategoryId, null)
                                .set(PgAppraiseApply::getParentCategoryId, null)
                                .set(PgAppraiseApply::getCategoryName, null)
                                .set(PgAppraiseApply::getParentCategoryName, null)
                                .set(PgAppraiseApply::getIsWebsitCall, null)
                                .set(PgAppraiseApply::getConfirmBy, "中心超时未审核")
                                .set(PgAppraiseApply::getInfoConfirmTime, null)
                                .set(PgAppraiseApply::getSettleConfirmTime, null)
                                .eq(PgAppraiseApply::getId, apply.getId())
                                .update();
                        pgOrderBaseService.lambdaUpdate()
                                .eq(PgOrderBase::getAppraiseApplyResult, AppraiseStatusEnum.A.getKey())
                                .eq(PgOrderBase::getId, apply.getId())
                                .update();
                    }
                }
            }
        }
    }

    /**
     * 二次申诉检查超时
     */
    @Scheduled(fixedDelay = 2 * 1000)
    public void task2(){
        if(env.equals("test") || env.equals("prd")) {
            final List<PgAppraiseApply> applyList = pgAppraiseApplyService.lambdaQuery()
                    .eq(PgAppraiseApply::getStatus, AppraiseApplyStatusEnum.WAIT.getKey())
                    .eq(PgAppraiseApply::getIsSecond, true)
                    .isNotNull(PgAppraiseApply::getSettleConfirmTime)
                    .list();
            if (CollectionUtil.isNotEmpty(applyList)) {
                // 二次申诉时间不能超时，从中心首次审核时间开始计算
                final PgAppraiseConfig config = pgAppraiseConfigService.lambdaQuery()
                        .eq(PgAppraiseConfig::getCompanyWechatId, applyList.get(0).getCompanyWechatId())
                        .eq(PgAppraiseConfig::getType, AppraiseConfigTypeEnum.SECOND_A.getKey())
                        .one();

                for (PgAppraiseApply apply : applyList) {
                    String value = config.getVal();
                    final int val = Integer.parseInt(value);
                    Date endTime = DateUtil.offsetHour(apply.getApplyTime(), val);
                    if (DateUtil.date().after(endTime)) {
                        pgAppraiseApplyService.lambdaUpdate()
                                .set(PgAppraiseApply::getExamineResult, apply.getAppraiseStatus())
                                .set(PgAppraiseApply::getStatus, AppraiseApplyStatusEnum.OVER_APPLY.getKey())
                                .set(PgAppraiseApply::getWebsitResult, AppraiseOnusEnum.ALL.getKey())
                                .set(PgAppraiseApply::getCategoryId, null)
                                .set(PgAppraiseApply::getParentCategoryId, null)
                                .set(PgAppraiseApply::getCategoryName, null)
                                .set(PgAppraiseApply::getParentCategoryName, null)
                                .set(PgAppraiseApply::getIsWebsitCall, null)
                                .set(PgAppraiseApply::getConfirmBy, "超时未申诉")
                                .eq(PgAppraiseApply::getId, apply.getId())
                                .update();
                        pgOrderBaseService.lambdaUpdate()
                                .eq(PgOrderBase::getAppraiseApplyResult, apply.getAppraiseStatus())
                                .eq(PgOrderBase::getId, apply.getId())
                                .update();
                    }
                }
            }
        }
    }
}
