package com.cpop.jambox.business.task;

import com.alibaba.fastjson.JSONObject;
import com.cpop.common.utils.DateUtils;
import com.cpop.common.utils.StringUtils;
import com.cpop.core.base.exception.ServiceException;
import com.cpop.core.utils.SpringUtils;
import com.cpop.jambox.business.entity.CardTemplate;
import com.cpop.jambox.business.service.CardTemplateService;
import com.cpop.jambox.framework.constant.JamboxCloudUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @author DB
 * @createTime 2023/11/10 17:52
 * @description 课卡模板异步任务
 */
@Component
public class CardTemplateAsyncTask {

    /**
     * 生成课卡
     */
    @Async("customAsyncThreadPool")
    public void cloudCreateCard(JSONObject jsonBody,String cardTemplateId) {
        //查询课卡信息
        CardTemplate cardTemplate = SpringUtils.getBean(CardTemplateService.class).getById(cardTemplateId);
        //时限卡
        if (cardTemplate.getTemplateType().equals(1)) {
            jsonBody.put("moneyCard", true);
            jsonBody.put("amount", cardTemplate.getPrice());
        } else if (cardTemplate.getTemplateType().equals(2)) {
            jsonBody.put("timeLimit", true);
            jsonBody.put("periodNumber", cardTemplate.getValidDay());
            jsonBody.put("periodType", cardTemplate.getTemplateType());
        } else {
            // 课卡类型课时卡
            jsonBody.put("periodType", cardTemplate.getTemplateType());
            // 课卡计费类型 true   false 课时卡
            jsonBody.put("timeLimit", false);
            // 课时卡课时数
            jsonBody.put("periodNumber", cardTemplate.getClassNumber());
        }
        // 课卡到期时间
        if (null != cardTemplate.getDayAppointment()){
            // 日最大约课次数
            jsonBody.put("maxFrequency", cardTemplate.getDayAppointment());
        }
        if (StringUtils.isNotBlank(cardTemplate.getName())){
            // 课卡名称
            jsonBody.put("periodName", cardTemplate.getName());
        }
        if (null != cardTemplate.getWeekAppointment()){
            // 周最大约课次数
            jsonBody.put("maxWeekFrequency", cardTemplate.getWeekAppointment());
        }
        // 首次上课开始计有效期
        if (null != cardTemplate.getBufferDay()){
            // 最大停卡次数
            jsonBody.put("maxStopTime", cardTemplate.getBufferDay());
        }
        //课卡有效期
        String periodExpire;
        if (null != cardTemplate.getValidDay() && cardTemplate.getValidDay() > 0) {
            //减一天
            LocalDate localDate = LocalDate.now().plusDays(cardTemplate.getValidDay() - 1);
            periodExpire = localDate.format(DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD));
        } else {
            periodExpire = cardTemplate.getEndDate().format(DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD));
        }
        jsonBody.put("periodExpire", periodExpire);
        SpringUtils.getBean(RestTemplate.class).postForObject(JamboxCloudUrl.COMMON_CARD_URL, jsonBody, JSONObject.class);
    }
}
