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.bonus.service.ICuMemberRetailLogService;
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.member.bonus.CuMemberRetailLog;
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.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 奖金计算
 */
@DubboService
@Slf4j
public class MemberSettleBonusProvider implements IMemberSettleBonusApi {

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

    @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);
    }

    @Override
    public void calculateCuMemberRetailRangeBonusBySaOrder() {
        List<CuMemberRetailLog> cuMemberRetailLogs = iCuMemberRetailLogService.queryCuMemberRetailLog();
        if (cuMemberRetailLogs.size() > 0) {
            List<CuMemberRetailLog> successLogList = new ArrayList<>();
            try {
                for (CuMemberRetailLog cuMemberRetailLog : cuMemberRetailLogs) {
                    iBonusSettleService.calculateCuMemberRetailRangeBonusBySaOrder(cuMemberRetailLog.getOrderCode());
                    successLogList.add(cuMemberRetailLog);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (successLogList.size() > 0) {
                    iCuMemberRetailLogService.updateCuMemberRetailLogByList(successLogList);
                }
            }
        }
    }

}
