package com.hzs.bonus.bonus.provider;

import com.hzs.bonus.achieve.service.ICuMemberAchieveService;
import com.hzs.bonus.achieve.service.ICuMemberMonthAchieveService;
import com.hzs.bonus.bonus.service.IBonusGrantService;
import com.hzs.bonus.bonus.service.IBonusSettleService;
import com.hzs.bonus.bonus.service.ICuMemberBonusService;
import com.hzs.bonus.settle.IMemberSettleBonusApi;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.sale.order.SaOrder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * @description: 奖金计算
 * @author: sui q
 * @time: 2023/9/18 14:26
 * @classname: MemberBonusSettleProvider
 * @package_name: com.hzs.bonus.bonus.settle
 * version 1.0.0
 */
@DubboService
@Slf4j
public class MemberSettleBonusProvider implements IMemberSettleBonusApi {

    @Autowired
    private ICuMemberMonthAchieveService iCuMemberMonthAchieveService;
    @Autowired
    private IBonusSettleService iBonusSettleService;
    @Autowired
    private ICuMemberBonusService iCuMemberBonusService;
    @Autowired
    private IBonusGrantService iBonusGrantService;
    @Autowired
    private ICuMemberAchieveService iCuMemberAchieveService;

    @Override
    public R<Boolean> calculateCumberBonusBySecond(String orderCode) {
        try {
            iBonusSettleService.calculateCumberBonusBySecond(orderCode);
        } catch (Exception e) {
            return R.fail();
        }
        return R.ok();
    }

    @Override
    public R<Boolean> autoCalculateBonus(Date settleDate) {
        try {
            new BonusCalculateBonus(settleDate).start();
            return R.ok();
        } catch (Exception e) {
            return R.fail();
        }
    }

    @Override
    public void backupsSettleBonusEveryday() {
        iCuMemberBonusService.backupsSettleBonusEveryday();
    }

    class BonusCalculateBonus extends Thread {

        private final Date settleDate;

        public BonusCalculateBonus(Date settleDate) {
            this.settleDate = settleDate;
        }

        @Override
        public void run() {
            Date settleEndDate = DateUtils.afterDate(1, ChronoUnit.DAYS, settleDate);
            log.info("奖金结算开始, 结算时间：{}", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, settleDate));
            // 奖金结算
            try {
                iCuMemberBonusService.autoCalculateBonus(settleDate, settleEndDate);
                log.info("奖金结算结束");
            } catch (Exception e) {
                log.error("奖金结算异常,结算时间：{}", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, settleDate), e);
            }
        }
    }

    @Override
    public void insertCuMemberAchieveByEveryDay() {
        iCuMemberMonthAchieveService.insertCuMemberAchieveByEveryDay(DateUtils.beforeDate(1, ChronoUnit.DAYS));
    }

    @Override
    public void autoPublishBonus() {
        iBonusGrantService.publishBonusByPeriodEveryDay();
    }

    @Override
    public void autoGrantBonus() {
        iBonusGrantService.grantWithdrawBonusByPeriodEveryDay();
    }

    @Override
    public Boolean chargeBackSaOrder(SaOrder saOrder) {
        return iCuMemberAchieveService.chargeBackSaOrder(saOrder);
    }
}
