package cn.superdesk.uniorder.controller.unified;

import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.common.HttpClientUtils;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


/**
 * @Description: 外部接口 封闭调用
 * @Author: linjinyu 补充 修改
 * @CreateDate: 4/28/20 6:23 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 4/28/20 6:23 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/unifiedoutapi")
public class UnifiedOutApiController extends BaseController {


	@Value("${unified.out.saleurl}")
	private String SALEURL;

	/***
	 * 商户列表
	 * TODO 这个商户 信息是没有同步过来的?
	 * @return
	 */
	@PostMapping("/getsales")
	@ResponseBody
	public R<Object> getSales() {

		R<Object> result = new R<>();

		try {
			Map<String, String> params = new HashMap<String, String>();
			params.put("id", getString("id"));
			params.put("name", getString("name"));

			String jsonstr = HttpClientUtils.doPost(SALEURL, params);
			Map    jsonMap = JSON.parseObject(jsonstr, Map.class);

			if (!jsonMap.get("code").toString().equals("200")) {
				result.setSuccess(false);
				result.setMessage("数据获取失败!");
				return result;
			}

			result.setSuccess(true);
			result.setData(jsonMap.get("data"));

		} catch (Exception e) {

			result.setCode(String.valueOf(e.hashCode()));
			result.setSuccess(false);
			result.setMessage("商户接口异常:" + e.getMessage());

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}

		return result;
	}
}
