package com.kxs.daemon.quartz.task;

import cn.hutool.core.util.NumberUtil;
import com.kxs.common.core.constant.SecurityConstants;
import com.kxs.daemon.quartz.constants.SkyQuartzEnum;
import com.kxs.daemon.quartz.exception.TaskException;
import com.kxs.stat.api.feign.RemoteKxsStatService;
import com.kxs.user.api.feign.RemoteKxsUserService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 用户模块 Bean 任务
 *
 * @author 没秃顶的码农
 * @date 2024-03-21
 */
@Slf4j
@Component("userjob")
@RequiredArgsConstructor
public class UserBeanTask {


    private final RemoteKxsUserService remoteKxsUserService;
    private final RemoteKxsStatService remoteKxsStatService;

    /**
     * 自动表创建收支明细表任务
     */
    @SneakyThrows
    public String automaticTableCreationTasks() {
        try {
            log.info("自动表创建任务:{}", LocalDateTime.now());
            remoteKxsUserService.automaticTableCreationTasks(SecurityConstants.FROM_IN);
            //stat统计表新建
            remoteKxsStatService.autoStatTableCreationTasks(SecurityConstants.FROM_IN);
            return SkyQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType();

        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
        }
        return SkyQuartzEnum.JOB_LOG_STATUS_FAIL.getType();
    }

    /**
     * 用户欠款扣除定时任务
     * 1.扫创客合伙人可提现额度，若有合伙人可提现额度，则扣除可提现额度，最大扣除金额=欠款总金额，扣除后减少欠款总金额，同时在前端增加扣除记录，备注为「预借款项还款」
     * 2.扫创客盟主可提现余额，若有盟主可提现余额，则扣除可提现余额，最大扣除金额=欠款总金额，扣除后减少欠款总金额，同时在前端增加扣除记录，备注为「预借款项还款」
     */
    @SneakyThrows
    public String userTotalDebtSub(String para) {
        remoteKxsUserService.userTotalDebtSub(SecurityConstants.FROM_IN);
        log.info("用户欠款扣除定时任务:{}，输入参数{}", LocalDateTime.now(), para);
        return SkyQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType();
    }

    /**
     * 奖池同步交易额
     * 同步老系统交易额汇算奖金池
     */
    @SneakyThrows
    public String prizePoolSynchronization(String para) {
        try {
            if (!NumberUtil.isNumber(para)) {
                para = "";
            }
            remoteKxsUserService.prizePoolSynchronization(para, SecurityConstants.FROM_IN);
            log.info("股东大盘分红奖池同步交易额:{}，输入参数{}", LocalDateTime.now(), para);
            return SkyQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType();
        } catch (Exception e) {
            log.error("定时任务股东大盘分红奖池同步交易额执行失败，输入参数{}：", para);
        }
        return SkyQuartzEnum.JOB_LOG_STATUS_FAIL.getType();
    }
//    /**
//     *  股东大盘分红职级汇算
//     *  扫描个人交易记录总表  计算用户真实职级
//     */
//    @SneakyThrows
//    public String userRealLevel(String para) {
//        remoteKxsUserService.userRealLevel(SecurityConstants.FROM_IN);
//        log.info("股东大盘真实职级计算:{}，输入参数{}", LocalDateTime.now(), para);
//        return SkyQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType();
//    }
}
