package com.hzs.bonus.bonus.provider;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.bonus.achieve.service.ICuMemberAchieveService;
import com.hzs.bonus.achieve.service.ICuMemberMonthAchieveService;
import com.hzs.bonus.achieve.service.ICuMemberSettlePeriodService;
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.member.achieve.ext.CuMemberSettlePeriodExt;
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;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private ICuMemberMonthAchieveService cuMemberMonthAchieveService;

    @Autowired
    private ICuMemberBonusService cuMemberBonusService;

    @Autowired
    private IBonusGrantService bonusGrantService;

    @Autowired
    private ICuMemberAchieveService cuMemberAchieveService;

    @Autowired
    private ICuMemberSettlePeriodService iCuMemberSettlePeriodService;

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

    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 {
                cuMemberBonusService.autoCalculateBonus(settleDate, settleEndDate);
            } catch (Exception e) {
                log.info("11111111", e);
            }
        }
    }

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

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

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

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

    @Override
    public R<Map<String, CuMemberSettlePeriodExt>> queryPeriodList(List<String> dateList) {
        // key：日期， value：期间数据
        Map<String, CuMemberSettlePeriodExt> resultMap = new HashMap<>();

        List<CuMemberSettlePeriodExt> list = iCuMemberSettlePeriodService.queryPeriodList(dateList);
        if (CollectionUtil.isNotEmpty(list)) {
            for (CuMemberSettlePeriodExt cuMemberSettlePeriodExt : list) {
                resultMap.put(DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, cuMemberSettlePeriodExt.getSettleDate()), cuMemberSettlePeriodExt);
            }
        }

        return R.ok(resultMap);
    }

}
