package com.jeequan.jeepay.mgr.ctrl.merchant;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.core.utils.JsonKit;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 设备厂商参数接口
 *
 * @author xiaoyu
 *
 * @date 2023/7/19 16:11
 */
@Slf4j
@RestController
@RequestMapping("/api/provider/device")
public class MchDeviceCommonController extends CommonCtrl {

	/**
	 * @author: xiaoyu
	 * @date: 2023/7/19 16:13
	 * @describe: 获取智网厂商列表
	 */
	@GetMapping("/getZwCompanyList")
	public ApiRes getZwCompanyList() {
		JSONArray result = new JSONArray();
		try {
			String reqUrl = "https://open.smartlinkall.com/magicbox/GetCompanyList";
			log.info("[智网]获取厂商信息列表，请求地址：{}", reqUrl);
			String response = HttpUtil.createGet(reqUrl).execute().body();
			log.info("[智网]获取厂商信息列表，响应结果：{}", response);
			JSONObject respJson = JSONObject.parseObject(response);
			JSONObject respData = respJson.getJSONObject("respData");
			if (respData != null){
				result = respData.getJSONArray("list");
			}
		}catch (BizException e) {
			log.error("获取智网厂商列表异常" + e);
			throw new BizException(e.getMessage());
		}
		return ApiRes.ok(result);
	}

	/**
	 * @author: xiaoyu
	 * @date: 2023/7/19 16:13
	 * @describe: 获取智网厂家软件列表
	 */
	@GetMapping("/getZwSoftCodeList/{code}")
	public ApiRes getZwSoftCodeList(@PathVariable("code") String code) {
		JSONArray result = new JSONArray();
		try {
			String reqUrl = "https://open.smartlinkall.com/magicbox/GetPosList";
			JSONObject bodyParam = JsonKit.newJson("code", code);
			log.info("[智网]获取软件信息列表，请求地址：{}, bodyParam：{}", reqUrl, bodyParam);
			String response = HttpUtil.createPost(reqUrl)
					.header("Content-type", "application/json")
					.body(bodyParam.toJSONString())
					.execute().body();
			log.info("[智网]获取软件信息列表，响应结果：{}", response);
			JSONObject respJson = JSONObject.parseObject(response);
			JSONObject respData = respJson.getJSONObject("respData");
			if (respData != null){
				result = respData.getJSONArray("list");
			}
		}catch (BizException e) {
			log.error("获取智网软件列表异常" + e);
			throw new BizException(e.getMessage());
		}
		return ApiRes.ok(result);
	}

	/**
	 * @author: xiaoyu
	 * @date: 2023/7/19 16:13
	 * @describe: 获取智网厂家软件列表
	 */
	@GetMapping("/getZwConfigInfo/{code}")
	public ApiRes getZwConfigInfo(@PathVariable("code") String code) {
		JSONObject result = new JSONObject();
		try {
			String reqUrl = "https://open.smartlinkall.com/magicbox/GetPosPara";
			JSONObject bodyParam = JsonKit.newJson("code", code);
			log.info("[智网]获取软件配置信息，请求地址：{}, bodyParam：{}", reqUrl, bodyParam);
			String response = HttpUtil.createPost(reqUrl)
					.header("Content-type", "application/json")
					.body(bodyParam.toJSONString())
					.execute().body();
			log.info("[智网]获取软件配置信息，响应结果：{}", response);
			JSONObject respJson = JSONObject.parseObject(response);
			result = respJson.getJSONObject("respData");

		}catch (BizException e) {
			log.error("获取智网软件配置异常" + e);
			throw new BizException(e.getMessage());
		}
		return ApiRes.ok(result);
	}


}
