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

import com.hzs.bonus.bonus.param.BdWaresBonusItemsParam;
import com.hzs.bonus.bonus.param.BdWaresBonusParam;
import com.hzs.bonus.bonus.service.IBdWaresBonusService;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.core.utils.ComputeUtil;
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.bonus.BdWaresBonus;
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.math.BigDecimal;

/**
 * 特殊商品分配奖金 前端控制器
 */
@RestController
@RequestMapping("/wares-bonus/special")
public class BdWaresBonusController extends BaseController {

    @Autowired
    private IBdWaresBonusService bdWaresBonusService;

    /**
     * 在管理页面修改保存4-n参数
     *
     * @return AjaxResult
     */
    @PostMapping("/insert")
    public AjaxResult insertBdWaresBonus(@RequestBody BdWaresBonusParam waresBonusParam) {
        if (waresBonusParam.getPkId() != null) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY));
        }
        String msg = validateParam(waresBonusParam);
        if (msg != null) {
            return AjaxResult.error(msg);
        }
        waresBonusParam.setPkCountry(SecurityUtils.getPkCountry());
        bdWaresBonusService.insertOrUpdateBdWaresBonusByParam(waresBonusParam, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

    private String validateParam(BdWaresBonusParam waresBonusParam) {
        if (waresBonusParam.getPkWares() == null ||
                waresBonusParam.getTotalAlgebra() == null || waresBonusParam.getOrderAssAchieve() == null ||
                waresBonusParam.getWaresBonusItemsParamList() == null || waresBonusParam.getWaresBonusItemsParamList().size() == 0) {
            return TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY);
        }
        BigDecimal totalAssAchieve = BigDecimal.ZERO;
        for (BdWaresBonusItemsParam bdWaresBonusItemsParam : waresBonusParam.getWaresBonusItemsParamList()) {
            if (bdWaresBonusItemsParam.getAlgebra() == null || bdWaresBonusItemsParam.getAssAchieve() == null) {
                return TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY);
            }
            totalAssAchieve = ComputeUtil.computeAdd(totalAssAchieve, bdWaresBonusItemsParam.getAssAchieve());
        }
        if (!ComputeUtil.compareEqual(totalAssAchieve, waresBonusParam.getOrderAssAchieve())) {
            return "分配总ev跟各子ev合计不相等！";
        }
        BdWaresBonus bdWaresBonus = bdWaresBonusService.queryWaresBonusById(waresBonusParam);
        if (bdWaresBonus != null && bdWaresBonus.getPkId() != null) {
            return "该商品已经配置过，不能再次配置！";
        }
        return null;
    }

    /**
     * 在管理页面修改保存4-n参数
     *
     * @return AjaxResult
     */
    @PostMapping("/update")
    public AjaxResult updateBdWaresBonus(@RequestBody BdWaresBonusParam waresBonusParam) {
        if (waresBonusParam.getPkId() == null) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY));
        }
        String msg = validateParam(waresBonusParam);
        if (msg != null) {
            return AjaxResult.error(msg);
        }
        waresBonusParam.setPkCountry(SecurityUtils.getPkCountry());
        bdWaresBonusService.insertOrUpdateBdWaresBonusByParam(waresBonusParam, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

    /**
     * 在管理页面修改保存4-n参数
     *
     * @return AjaxResult
     */
    @DeleteMapping("/delete/{pkId}")
    public AjaxResult deleteBdWaresBonus(@PathVariable Long pkId) {
        bdWaresBonusService.deleteSpecialWaresBonus(pkId, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

    /**
     * 分页查询会员某个时间段内的奖金汇总
     */
    @AccessPermissions("WaresBonus")
    @PostMapping("/query")
    public TableDataInfo listWaresBonusParam(@RequestBody BdWaresBonusParam waresBonusParam) {
        waresBonusParam.setPkCountry(SecurityUtils.getPkCountry());
        startPage();
        return getDataTable(bdWaresBonusService.querySpecialWaresBonus(waresBonusParam));
    }

    /**
     * 极差团队特殊商品配置ev的分配规则
     */
    @GetMapping("/ev-query")
    public TableDataInfo querySpecialWaresBonus() {
        return getDataTable(bdWaresBonusService.querySpecialWaresBonus());
    }
}
