package com.bweb.controller;

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.BsPayMer;
import com.hfkj.model.ResponseData;
import com.hfkj.service.pay.BsPayMerService;
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;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @className: BsPayMerController
 * @author: HuRui
 * @date: 2025/5/22
 **/
@Controller
@RequestMapping(value="/payMer")
@Api(value="支付商户号")
public class BsPayMerController {
    Logger log = LoggerFactory.getLogger(BsPayMerController.class);
    @Resource
    private BsPayMerService payMerService;

    @RequestMapping(value="/edit",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑")
    public ResponseData edit(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || StringUtils.isBlank(body.getString("payMerName"))
                    || StringUtils.isBlank(body.getString("payMerNo"))
                    || StringUtils.isBlank(body.getString("payMerKey"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            BsPayMer payMer;
            if (body.getLong("id") != null) {
                // 支付商户号
                 payMer = payMerService.getDetail(body.getLong("id"));
                if (payMer == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
            } else {
                payMer = new BsPayMer();
            }
            payMer.setPayMerName(body.getString("payMerName"));
            payMer.setPayMerNo(body.getString("payMerNo"));
            payMer.setPayMerKey(body.getString("payMerKey"));
            payMerService.edit(payMer);

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

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

    @RequestMapping(value="/del",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除")
    public ResponseData del(@RequestBody JSONObject body) {
        try {
            if (body == null || body.getLong("id") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 支付商户号
            BsPayMer payMer = payMerService.getDetail(body.getLong("id"));
            if (payMer == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            payMer.setStatus(0);
            payMerService.edit(payMer);

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

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


    @RequestMapping(value="/getDetail",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询详情")
    public ResponseData getDetail(@RequestParam(value = "id" , required = true) Long id) {
        try {

            return ResponseMsgUtil.success(payMerService.getDetail(id));

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

    @RequestMapping(value="/getList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData getList() {
        try {
            Map<String,Object> param = new HashMap<>();
            return ResponseMsgUtil.success(payMerService.getList(param));

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