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.ICuMemberSettlePeriodService;
import com.hzs.bonus.achieve.service.ICuMemberTreeService;
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.vo.MemberSettlePeriodVO;
import com.hzs.common.core.annotation.AccessPermissions;
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.utils.StringUtils;
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.util.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 {

    @Autowired
    private ICuMemberBonusService iCuMemberBonusService;
    @Autowired
    private IBonusSettleService iBonusSettleService;
    @Autowired
    private ICuMemberTreeService iCuMemberTreeService;
    @Autowired
    private ICuMemberSettlePeriodService iCuMemberSettlePeriodService;
    @Autowired
    private IBonusGrantService iBonusGrantService;


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

    /**
     * 初始化奖金结算表
     *
     * @return AjaxResult
     */
    @GetMapping("/init-period")
    public AjaxResult initPeriod(String settleDate) {
        iCuMemberSettlePeriodService.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));
        }
        iCuMemberBonusService.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));
        }
        iBonusGrantService.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));
        }
        iBonusGrantService.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) {
        Date startDate = DateUtils.parseStringToDate(settleDate);
        if (DateUtils.compareDateBefore(DateUtils.beforeDate(7, ChronoUnit.DAYS, DateUtils.currentDate()), startDate)) {
            return AjaxResult.error(TransactionUtils.getContent(BonusMsgConstants.GRANT_VALIDATE_DATE));
        }
        iBonusGrantService.grantBonusByPeriod(SecurityUtils.getPkCountry(), settleDate);
        return AjaxResult.success();
    }

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

    /**
     * 发放奖金
     *
     * @return AjaxResult
     */
    @GetMapping("/grant-bonus_all")
    public AjaxResult grantBonusAll() {
        iBonusGrantService.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(7, ChronoUnit.DAYS, DateUtils.currentDate()), startDate)) {
            return AjaxResult.error(TransactionUtils.getContent(BonusMsgConstants.WITHDRAW_VALIDATE_DATE));
        }
        iBonusGrantService.withdrawalBonusByPeriod(SecurityUtils.getPkCountry(), settleDate);
        return AjaxResult.success();
    }

    /**
     * 秒接结算业绩，仅用于测试
     *
     * @return AjaxResult
     */
    @GetMapping("/settle-second-bonus")
    public AjaxResult settleSecondBonus(String orderCode) {
        if (StringUtils.isEmpty(orderCode)) {
            return AjaxResult.error();
        }
        iBonusSettleService.calculateCuMemberRetailRangeBonusBySaOrder(orderCode);
        return AjaxResult.success();
    }

    /*
     *  查询奖金计算期数列表
     **/
    @AccessPermissions("bonusCount")
    @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 = iCuMemberSettlePeriodService.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;
    }

}
