package com.bweb.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsMerchant;
import com.hfkj.entity.BsMerchantPayConfig;
import com.hfkj.model.ResponseData;
import com.hfkj.service.merchant.BsMerchantPayConfigService;
import com.hfkj.service.merchant.BsMerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @className: BsMerchantPayConfigController
 * @author: HuRui
 * @date: 2024/3/13
 **/
@Controller
@RequestMapping(value = "/merchantPayConfig")
@Api(value = "商户支付配置")
public class BsMerchantPayConfigController {

    private static Logger log = LoggerFactory.getLogger(BsMerchantPayConfigController.class);

    @Resource
    private BsMerchantPayConfigService merchantPayConfigService;
    @Resource
    private BsMerchantService merchantService;

    @RequestMapping(value = "/editConfig", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑配置")
    public ResponseData editConfig(@RequestBody BsMerchantPayConfig body) {
        try {
            if (body == null
                    || body.getMerNo() == null
                    || StringUtils.isBlank(body.getChannelMerNo())
                    || StringUtils.isBlank(body.getChannelMerKey())
                    || body.getLedgerAccountFlag() == null
            ) {
                log.error("BsMerchantPayConfigController --> editConfig() error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 查询商户
            BsMerchant merchant = merchantService.getMerchant(body.getMerNo());
            if (merchant == null) {
                log.error("BsMerchantPayConfigController --> editConfig() error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 查询配置
            BsMerchantPayConfig config = merchantPayConfigService.getConfig(body.getMerNo());
            if (config == null) {
                config = new BsMerchantPayConfig();
            }
            if(body.getLedgerAccountFlag().equals(true)) {
                if (StringUtils.isBlank(body.getLedgerAccountData())) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                JSONArray ledgerAccountArray = JSONArray.parseArray(body.getLedgerAccountData());
                for (Object obj : ledgerAccountArray) {
                    JSONObject ledgerAccount = (JSONObject) obj;
                    if (StringUtils.isBlank(ledgerAccount.getString("receiverMerNo"))
                            || StringUtils.isBlank(ledgerAccount.getString("receiverMerName"))
                            || ledgerAccount.getBigDecimal("receiverRatio") == null) {
                        throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                    }
                }
                config.setLedgerAccountData(body.getLedgerAccountData());
            } else {
                config.setLedgerAccountData(null);
            }
            config.setMerId(merchant.getId());
            config.setMerNo(merchant.getMerNo());
            config.setMerName(merchant.getMerName());
            config.setChannelName("惠支付");
            config.setChannelCode("HUI_PAY");
            config.setChannelMerNo(body.getChannelMerNo());
            config.setChannelMerKey(body.getChannelMerKey());
            config.setLedgerAccountFlag(body.getLedgerAccountFlag());
            merchantPayConfigService.editData(config);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("BsMerchantPayConfigController --> editConfig() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value = "/queryConfig", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取配置")
    public ResponseData queryConfig(@RequestParam(value = "merNo", required = true) String merNo) {
        try {

            return ResponseMsgUtil.success(merchantPayConfigService.getConfig(merNo));

        } catch (Exception e) {
            log.error("BsMerchantPayConfigController --> queryConfig() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
