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.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;

/**
 * @Description:
 * @Author: sui q
 * @Time: 2022/11/2 16:21
 * @Classname: CuMemberSettleController
 * @PackageName: com.hzs.member.achieve.controller
 */
@RestController
@RequestMapping("/manage/settle")
public class CuMemberSettleController extends BaseController {

    @Autowired
    private ICuMemberBonusService cuMemberBonusService;
    @Autowired
    private IBonusSettleService bonusSettleService;
    @Autowired
    private ICuMemberTreeService cuMemberTreeService;
    @Autowired
    private ICuMemberSettlePeriodService cuMemberSettlePeriodService;
    @Autowired
    private IBonusGrantService bonusGrantService;
    @Autowired
    private ICuMemberMonthAchieveService cuMemberMonthAchieveService;

    /**
     * 初始化奖金结算表
     *
     * @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(@RequestParam("settleDate") String settleDate) {
//        initTable(settleDate);
//        settleDate = "2024-04-29";
        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));
        }
//        List<SaOrderExt> saOrderExtList = saOrderServiceApi.querySaOrderByDay(startDate,
//                DateUtils.afterDate(1, ChronoUnit.DAYS, startDate)).getData();
//        for(int i=0;i<=92;i++) {
//            startDate=DateUtils.afterDate(1,ChronoUnit.DAYS, startDate);
        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));
        }
        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-second-bonus")
    public AjaxResult settleSecondBonus(String orderCode) {
        if (StringUtils.isEmpty(orderCode)) {
            return AjaxResult.error();
        }
//        bonusSettleService.calculateCumberBonusBySecond(orderCode);
        bonusSettleService.calculateCuMemberRangAwardsBySaOrder(orderCode);
        return AjaxResult.success();
    }

    /**
     * 秒接结算业绩，仅用于测试
     *
     * @return AjaxResult
     */
    @GetMapping("/settle-month")
    public AjaxResult settleMonth() {
        cuMemberMonthAchieveService.insertCuMemberAchieveByEveryDay(DateUtils.beforeDate(1, ChronoUnit.DAYS));
        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();
    }

    /*
     * @description: 查询奖金计算期数列表
     * @author: sui q
     * @date: 2023/4/26 17:10
     * @param: null null
     **/
    @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 = 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;
    }

}
