package com.hzs.bonus.achieve.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import com.hzs.bonus.achieve.param.MemberPeriodParam;
import com.hzs.bonus.achieve.service.ICuMemberMonthAchieveService;
import com.hzs.bonus.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.bonus.achieve.service.ICuMemberTreeService;
import com.hzs.bonus.bonus.service.IBonusGrantService;
import com.hzs.bonus.bonus.service.ICuMemberBonusService;
import com.hzs.bonus.bonus.vo.MemberSettlePeriodVO;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.BonusMsgConstants;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.achieve.ext.CuMemberSettlePeriodExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/manage/settle")
public class CuMemberSettleController extends BaseController {

    private ICuMemberBonusService cuMemberBonusService;

    private ICuMemberTreeService cuMemberTreeService;

    private ICuMemberSettlePeriodService cuMemberSettlePeriodService;

    private IBonusGrantService bonusGrantService;

    private ICuMemberMonthAchieveService cuMemberMonthAchieveService;


    @Autowired
    public void setCuMemberBonusService(ICuMemberBonusService cuMemberBonusService) {
        this.cuMemberBonusService = cuMemberBonusService;
    }

    @Autowired
    public void setCuMemberMonthAchieveService(ICuMemberMonthAchieveService cuMemberMonthAchieveService) {
        this.cuMemberMonthAchieveService = cuMemberMonthAchieveService;
    }

    @Autowired
    public void setBonusGrantService(IBonusGrantService bonusGrantService) {
        this.bonusGrantService = bonusGrantService;
    }

    @Autowired
    public void setCuMemberSettlePeriodService(ICuMemberSettlePeriodService cuMemberSettlePeriodService) {
        this.cuMemberSettlePeriodService = cuMemberSettlePeriodService;
    }

    @Autowired
    public void setCuMemberTreeService(ICuMemberTreeService cuMemberTreeService) {
        this.cuMemberTreeService = cuMemberTreeService;
    }

    /**
     * 初始化奖金结算表
     *
     * @return AjaxResult
     */
    @GetMapping("/init-table")
    public AjaxResult initTable(String settleDate) {
        cuMemberTreeService.createCuMemberTree(settleDate);
        return AjaxResult.success();
    }

    /**
     * 初始化奖金结算表
     *
     * @return AjaxResult
     */
    @GetMapping("/init-period")
    public AjaxResult initPeriod(String settleDate) {
        cuMemberSettlePeriodService.initCuMemberPeriod(settleDate);
        return AjaxResult.success();
    }

    /**
     * 结算业绩
     *
     * @return AjaxResult
     */
    @GetMapping("/settle-bonus")
    @Log(module = EOperationModule.BONUS_SETTLE, business = EOperationBusiness.BONUS_SETTLE, method = EOperationMethod.INSERT)
    public AjaxResult settleBonus(String settleDate) {
        Date startDate = DateUtils.parseStringToDate(settleDate);
        if (DateUtils.compareDateAfter(DateUtils.beforeDate(6, ChronoUnit.DAYS, DateUtils.currentDate()), startDate)) {
            return AjaxResult.error(TransactionUtils.getContent(BonusMsgConstants.SETTLE_VALIDATE_DATE));
        }
        cuMemberBonusService.autoCalculateBonus(startDate, startDate);
        return AjaxResult.success();
    }

    /**
     * 公布奖金
     *
     * @return AjaxResult
     */
    @GetMapping("/publish-bonus")
    @Log(module = EOperationModule.BONUS_SETTLE, business = EOperationBusiness.BONUS_SETTLE, method = EOperationMethod.PUBLISH)
    public AjaxResult publishBonus(String settleDate) {
        Date startDate = DateUtils.parseStringToDate(settleDate);
        if (DateUtils.compareDate(DateUtils.beforeDate(6, ChronoUnit.DAYS, DateUtils.currentDate()), startDate) > 0) {
            return AjaxResult.error(TransactionUtils.getContent(BonusMsgConstants.PUBLISH_VALIDATE_DATE));
        }
        bonusGrantService.publishBonusByPeriod(SecurityUtils.getPkCountry(), settleDate);
        return AjaxResult.success();
    }

    /**
     * 不公布奖金
     *
     * @return AjaxResult
     */
    @GetMapping("/no-publish-bonus")
    @Log(module = EOperationModule.BONUS_SETTLE, business = EOperationBusiness.BONUS_SETTLE, method = EOperationMethod.NO_PUBLISH)
    public AjaxResult noPublishBonus(String settleDate) {
        Date startDate = DateUtils.parseStringToDate(settleDate);
        if (!DateUtils.compareDateEqual(DateUtils.beforeDate(1, ChronoUnit.DAYS, DateUtils.currentDate()), startDate)) {
            return AjaxResult.error(TransactionUtils.getContent(BonusMsgConstants.NO_PUBLISH_VALIDATE_DATE));
        }
        bonusGrantService.publishBonusByPeriod(SecurityUtils.getPkCountry(), settleDate);
        return AjaxResult.success();
    }

    /**
     * 发放奖金
     *
     * @return AjaxResult
     */
    @GetMapping("/grant-bonus")
    @Log(module = EOperationModule.BONUS_SETTLE, business = EOperationBusiness.BONUS_SETTLE, method = EOperationMethod.GRANT)
    public AjaxResult grantBonus(String settleDate) {
        if (!bonusGrantService.grantBonusByPeriod(SecurityUtils.getPkCountry(), settleDate)) {
            return AjaxResult.error(TransactionUtils.getContent(BonusMsgConstants.GRANT_VALIDATE_DATE));
        }
        bonusGrantService.withdrawalBonusByPeriod(SecurityUtils.getPkCountry(), settleDate);
        return AjaxResult.success();
    }

    /**
     * 发放奖金
     *
     * @return AjaxResult
     */
    @GetMapping("/grant-temp-bonus")
    public AjaxResult grantTempBonus(Integer pkCountry, String settleDate) {
        bonusGrantService.grantBonusByTempPeriod(pkCountry, settleDate);
        return AjaxResult.success();
    }

    /**
     * 发放奖金
     *
     * @return AjaxResult
     */
    @GetMapping("/grant-bonus_all")
    public AjaxResult grantBonusAll() {
        bonusGrantService.grantWithdrawBonusByPeriodEveryDay();
        return AjaxResult.success();
    }

    /**
     * 奖金提现
     *
     * @return AjaxResult
     */
    @GetMapping("/withdrawal-bonus")
    @Log(module = EOperationModule.BONUS_SETTLE, business = EOperationBusiness.BONUS_SETTLE, method = EOperationMethod.WITHDRAWAL)
    public AjaxResult withdrawalBonus(String settleDate) {
        Date startDate = DateUtils.parseStringToDate(settleDate);
        if (DateUtils.compareDateBefore(DateUtils.beforeDate(14, ChronoUnit.DAYS, DateUtils.currentDate()), startDate)) {
            return AjaxResult.error(TransactionUtils.getContent(BonusMsgConstants.WITHDRAW_VALIDATE_DATE));
        }
        bonusGrantService.withdrawalBonusByPeriod(SecurityUtils.getPkCountry(), settleDate);
        return AjaxResult.success();
    }

    /**
     * 发放直推达人、抗衰达人
     *
     * @return AjaxResult
     */
    @GetMapping("/settle-wall")
    public AjaxResult settleMemberHonorWall(String startDate) {
        cuMemberMonthAchieveService.insertCuMemberMonthAchieve(DateUtils.parseStringToDate(startDate));
        cuMemberMonthAchieveService.insertCuMemberCuMemberMonthYearAchieve(DateUtils.parseStringToDate(startDate));
        return AjaxResult.success();
    }

    /*
     *  查询奖金计算期数列表
     **/
    @Log(module = EOperationModule.BONUS_SETTLE, business = EOperationBusiness.BONUS_SETTLE, method = EOperationMethod.SELECT)
    @PostMapping("/list-period")
    public TableDataInfo list(@RequestBody MemberPeriodParam periodParam) {
        startPage();
        periodParam.setPkCountry(SecurityUtils.getPkCountry());
        List<CuMemberSettlePeriodExt> cuMemberSettlePeriods = cuMemberSettlePeriodService.queryCuMemberSettlePeriod(periodParam);
        List<MemberSettlePeriodVO> list = new ArrayList<>();
        cuMemberSettlePeriods.forEach(cuMemberSettlePeriod ->
                list.add(BeanUtil.copyProperties(cuMemberSettlePeriod, MemberSettlePeriodVO.class))
        );
        TableDataInfo dataTable = getDataTable(cuMemberSettlePeriods);
        dataTable.setRows(list);
        return dataTable;
    }


}
