package com.kxs.stat.biz.task;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.kxs.common.core.constant.CommonConstants;
import com.kxs.common.security.annotation.Inner;
import com.kxs.stat.biz.mapper.KxsMerchantTradeMapper;
import com.kxs.stat.biz.mapper.KxsUserTradeAfterMapper;
import com.kxs.stat.biz.mapper.KxsUserTradeBeforeMapper;
import com.kxs.stat.biz.mapper.KxsUserTradeMapper;
import com.kxs.stat.biz.service.KxsUserTradeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

/**
 * 统计模块定时任务
 *
 * @author 没秃顶的码农
 * @date 2024-03-13
 */
@RequiredArgsConstructor
@Slf4j
@RestController
@RequestMapping("stat-job")
public class KxsStatTaskJob {

    private final KxsUserTradeMapper kxsUserTradeMapper;

    private final KxsUserTradeBeforeMapper kxsUserTradeBeforeMapper;

    private final KxsUserTradeAfterMapper kxsUserTradeAfterMapper;

    private final KxsUserTradeService kxsUserTradeService;
    private final KxsMerchantTradeMapper kxsMerchantTradeMapper;

    private final static String PREFIX_TABLE_NAME = "kxs_user_trade_";


    /**
     * 股东大盘分红积分计算任务
     */
    @Inner
    @GetMapping("/shdUserTradeTotalList")
    public void shdUserTradeTotalList(@RequestParam(value = "para", required = false) String para) {
        kxsUserTradeService.getUserTradeList(para);
    }

    /**
     * 自动表创建任务
     * 每个月15号建立下个月的统计表表
     */
    @Inner
    @GetMapping("/autoStatTableCreationTasks")
    public void autoStatTableCreationTasks() {
        String thisMoth = LocalDateTimeUtil.format(LocalDate.now().plusMonths(1L), DatePattern.SIMPLE_MONTH_PATTERN);
        kxsUserTradeMapper.automaticTableCreationTasks(PREFIX_TABLE_NAME + thisMoth);
        kxsUserTradeBeforeMapper.automaticTableCreationTasks(PREFIX_TABLE_NAME + "before_" + thisMoth);
        kxsUserTradeAfterMapper.automaticTableCreationTasks(PREFIX_TABLE_NAME + "after_" + thisMoth);
        //新建商户交易统计表
        kxsMerchantTradeMapper.automaticTableCreationTasks(CommonConstants.MCH_TABLE_PREFIX + thisMoth);
    }



}
