package com.hzs.system.config.controller.manage;

import cn.hutool.core.collection.CollUtil;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.system.config.service.IBdTradeConfigService;
import com.hzs.system.config.vo.BdTradeConfigVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 交易配置表 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("/manage/trade-config")
public class BdTradeConfigController extends BaseController {

    @Autowired
    private IBdTradeConfigService iBdTradeConfigService;

    /**
     * 保存交易配置
     *
     * @param tradeConfigVO 入参
     * @return AjaxResult
     */
    @Log(module = EOperationModule.TRADE_CONFIG, business = EOperationBusiness.WITHDRAWAL_CONFIG, method = EOperationMethod.INSERT)
    @PostMapping("/add")
    public AjaxResult addTradeConfig(@RequestBody BdTradeConfigVO tradeConfigVO) {
        AjaxResult ajaxResult = parametersVerify(tradeConfigVO);
        if (ajaxResult.equals(AjaxResult.error())) {
            return ajaxResult;
        }
        tradeConfigVO.setPkMember(SecurityUtils.getUserId());
        tradeConfigVO.setPkCountry(SecurityUtils.getPkCountry());
        return toAjax(iBdTradeConfigService.addTradeConfig(tradeConfigVO));
    }

    /**
     * 交易配置列表显示
     *
     * @param tradeConfigVO 入参
     * @return AjaxResult
     */
    @Log(module = EOperationModule.TRADE_CONFIG, business = EOperationBusiness.WITHDRAWAL_CONFIG, method = EOperationMethod.SELECT)
    @PostMapping("/list")
    public AjaxResult showTradeConfigList(@RequestBody BdTradeConfigVO tradeConfigVO) {
        tradeConfigVO.setPkCountry(SecurityUtils.getPkCountry());
        return AjaxResult.success(iBdTradeConfigService.showTradeConfigList(tradeConfigVO));
    }

    /**
     * 更新交易配置信息
     *
     * @param tradeConfigVO 入参
     * @return AjaxResult
     */
    @Log(module = EOperationModule.TRADE_CONFIG, business = EOperationBusiness.WITHDRAWAL_CONFIG, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult updateTradeConfig(@RequestBody BdTradeConfigVO tradeConfigVO) {
        AjaxResult ajaxResult = parametersVerify(tradeConfigVO);
        if (ajaxResult.equals(AjaxResult.error())) {
            return ajaxResult;
        }
        tradeConfigVO.setPkMember(SecurityUtils.getUserId());
        tradeConfigVO.setPkCountry(SecurityUtils.getPkCountry());
        return toAjax(iBdTradeConfigService.updateTradeConfig(tradeConfigVO));
    }

    /**
     * 删除交易配置
     *
     * @param pkId 主键ID
     * @return AjaxResult
     */
    @Log(module = EOperationModule.TRADE_CONFIG, business = EOperationBusiness.WITHDRAWAL_CONFIG, method = EOperationMethod.DELETE)
    @GetMapping("/delete")
    public AjaxResult deleteTradeConfig(Integer pkId) {
        return toAjax(iBdTradeConfigService.deleteTradeConfig(pkId, SecurityUtils.getUserId()));
    }


    /**
     * 查看详情
     *
     * @param pkId 主键ID
     * @return AjaxResult
     */
    @Log(module = EOperationModule.TRADE_CONFIG, business = EOperationBusiness.WITHDRAWAL_CONFIG, method = EOperationMethod.DELETE)
    @GetMapping("/detail")
    public AjaxResult selectTradeConfigDetail(Integer pkId) {
        return AjaxResult.success(iBdTradeConfigService.selectTradeConfigDetail(pkId, SecurityUtils.getPkCountry()));
    }

    /**
     * 公共参数校验
     *
     * @param tradeConfigVO 入参
     * @return AjaxResult
     */
    private AjaxResult parametersVerify(BdTradeConfigVO tradeConfigVO) {
        if (CollUtil.isEmpty(tradeConfigVO.getPkAwardsList())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.AWARDS_NAME_NOT_NULL));
        }
        if (null == tradeConfigVO.getLicenseState()) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.LICENSE_STATE_NOT_EMPTY));
        }
        if (null == tradeConfigVO.getBusinessModule()) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.BUSINESS_MODULE));
        }
        return AjaxResult.success();
    }

}
