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.BsGasChannelConfig;
import com.hfkj.model.ResponseData;
import com.hfkj.service.order.BsGasChannelConfigService;
import com.hfkj.sysenum.merchant.MerchantSourceTypeEnum;
import com.hfkj.sysenum.order.OrderPayChannelEnum;
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;

@Controller
@RequestMapping(value = "/gasChannelConfig")
@Api(value = "加油渠道配置")
public class BsGasChannelConfigController {
    private static Logger log = LoggerFactory.getLogger(BsGasChannelConfigController.class);
    @Resource
    private BsGasChannelConfigService gasChannelConfigService;

    @RequestMapping(value = "/editConfig", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑配置")
    public ResponseData editConfig(@RequestBody BsGasChannelConfig body) {
        try {
            if (body == null
                    || body.getGasChannelType() == null
                    || StringUtils.isBlank(body.getChannelMerNo())
                    || StringUtils.isBlank(body.getChannelMerKey())
                    || body.getTradeStatus() == null
                    || body.getShowStatus() == null
                    || body.getLedgerAccountFlag() == null
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 查询配置
            BsGasChannelConfig config = gasChannelConfigService.getDetail(MerchantSourceTypeEnum.getDataByType(body.getGasChannelType()));
            if (config == null) {
                config = new BsGasChannelConfig();
                config.setGasChannelType(body.getGasChannelType());
                config.setChannelType(OrderPayChannelEnum.type1.getCode());
                config.setStatus(1);
            }
            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.setShowStatus(body.getShowStatus());
            config.setTradeStatus(body.getTradeStatus());
            config.setChannelMerNo(body.getChannelMerNo());
            config.setChannelMerKey(body.getChannelMerKey());
            config.setLedgerAccountFlag(body.getLedgerAccountFlag());
            gasChannelConfigService.editData(config);

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

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


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

            return ResponseMsgUtil.success(gasChannelConfigService.getDetail(MerchantSourceTypeEnum.getDataByType(gasChannelType)));

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


}
