package com.ym.mec.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.huifu.adapay.Adapay;
import com.huifu.adapay.model.MerConfig;
import com.ym.mec.biz.service.HfMerchantConfigService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.page.QueryInfo;
import com.ym.mec.thirdparty.adapay.entity.HfMerchantConfig;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (HfMerchantConfig)表控制层
 *
 * @author hgw
 * @since 2022-07-13 10:07:36
 */
@RestController
@RequestMapping("/hfMerchantConfig")
public class HfMerchantConfigController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(HfMerchantConfigController.class);
    /**
     * 服务对象
     */
    @Resource
    private HfMerchantConfigService hfMerchantConfigService;

    @ApiOperation("创建汇付商户配置")
    @PostMapping("/add")
    public HttpResponseResult<Object> add(@RequestBody HfMerchantConfig dto) throws Exception {
        long insert = hfMerchantConfigService.insert(dto);
        if (insert == 1) {
            MerConfig merConfig = new MerConfig();
            merConfig.setApiKey(dto.getApiKey());
            merConfig.setApiMockKey(dto.getMockApiKey());
            merConfig.setRSAPrivateKey(dto.getRsaPrivateKey());
            Adapay.addMerConfig(merConfig, dto.getMerKey());
            MerConfig config = Adapay.getConfig(dto.getMerKey());
            log.info("HfMerchantConfig in config:{}", JSONObject.toJSONString(config));
            return succeed();
        }
        return failed("添加失败");
    }

    @ApiOperation("创建汇付商户配置")
    @PostMapping("/update")
    public HttpResponseResult<Object> update(@RequestBody HfMerchantConfig dto) throws Exception {
        int update = hfMerchantConfigService.update(dto);
        if (update == 1) {
            MerConfig merConfig = new MerConfig();
            merConfig.setApiKey(dto.getApiKey());
            merConfig.setApiMockKey(dto.getMockApiKey());
            merConfig.setRSAPrivateKey(dto.getRsaPrivateKey());
            Adapay.addMerConfig(merConfig, dto.getMerKey());
            MerConfig config = Adapay.getConfig(dto.getMerKey());
            log.info("HfMerchantConfig up config:{}", JSONObject.toJSONString(config));
            return succeed();
        }
        return failed("修改失败");
    }

    @ApiOperation("根据机构id查询汇付商户配置")
    @GetMapping(value = "/queryByTenantId/{id}")
    public HttpResponseResult<HfMerchantConfig> queryByTenantId(@ApiParam(value = "机构ID", required = true) @PathVariable("id") Integer id) {
        return succeed(hfMerchantConfigService.queryByTenantId(id));
    }

    @ApiOperation("分页查询汇付商户配置")
    @PostMapping(value = "/queryPage")
    public HttpResponseResult<PageInfo<HfMerchantConfig>> queryPage(@RequestBody QueryInfo dto) {
        return succeed(hfMerchantConfigService.queryPage(dto));
    }

}

