package cn.superdesk.uniorder.controller._openapi.unified;

import cn.superdesk.uniorder.common.ResponseResult;
import cn.superdesk.uniorder.common.enums.UnifiedCode;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedLnssOrderAdd;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedOrderAppraiseAdd;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedOrderAppraiseSMAdd;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedOrderGoodsAppraiseAdd;
import cn.superdesk.uniorder.dubbo.consumer.unified.UnifiedOrderAppraiseDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.unified.UnifiedOrderDubboConsumerService;
import cn.superdesk.uniorder.utils.MyTools;
import cn.superdesk.uniorder.utils.UnifiedSignUtils;
import cn.superdesk.uniorder.utils.exception.ExceptionUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
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.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: TODO 专门给楼闪送用的?
 * @Author: linjinyu 补充 修改
 * @CreateDate: 5/7/20 5:00 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 5/7/20 5:00 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/unifiedorder")
public class UnifiedOrderController extends BaseController {


	@Autowired
	UnifiedOrderDubboConsumerService         unifiedOrderDubboConsumerService;
	@Autowired
	UnifiedOrderAppraiseDubboConsumerService unifiedOrderAppraiseDubboConsumerService;

	/**
	 * TODO 楼闪送(只有) ? 新增订单
	 *
	 * @return
	 */
	@PostMapping("/add")
	@ResponseBody
	public ResponseResult<String> add() {

		ResponseResult<String> result = new ResponseResult<>();

		try {
			String jsonStr = MyTools.readInputStream(request.getInputStream(), Charset.forName("utf-8"));

			JSONObject jasonObject = JSONObject.parseObject(jsonStr);

			if (jasonObject == null) {

				result.setSuccess(false);
				result.setCode(UnifiedCode.JSON_ERROR.getCode());
				result.setMessage(UnifiedCode.JSON_ERROR.getMsg());

				return result;
			}

			UnifiedLnssOrderAdd unifiedLnssOrderAdd = jasonObject.toJavaObject(UnifiedLnssOrderAdd.class);

			log.info("order_add:" + jsonStr);

			String signParam = "version=1.0&timestamp=" + unifiedLnssOrderAdd.getTimestamp() + "&charset=utf-8&nonce_str=" + unifiedLnssOrderAdd.getNonce_str();

			if (!UnifiedSignUtils.checkSign(signParam, unifiedLnssOrderAdd.getSign())) {

				result.setSuccess(false);
				result.setCode(UnifiedCode.SIGN_ERROR.getCode());
				result.setMessage(UnifiedCode.SIGN_ERROR.getMsg());

				return result;
			}

			Map<String, Object> returnMap = unifiedOrderDubboConsumerService.addObjTran(unifiedLnssOrderAdd);

			if (returnMap.get("code").equals(1)) {

				result.setData(returnMap.get("sn").toString());
				result.setSuccess(true);

			} else {

				result.setCode(Integer.parseInt(returnMap.get("code").toString()));
				result.setMessage(returnMap.get("msg").toString());
				result.setSuccess(false);

			}

		} catch (Exception e) {

			result.setSuccess(false);
			result.setCode(UnifiedCode.OTHER_ERROR.getCode());
			result.setMessage(e.getMessage());

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

			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 新增评价
	 *
	 * @return
	 */
	@PostMapping("/appraise")
	@ResponseBody
	public ResponseResult<String> appraise() {
		ResponseResult<String> result = new ResponseResult<>();
		try {
			String instr = MyTools.readInputStream(request.getInputStream(), Charset.forName("utf-8"));

			JSONObject jasonObject = JSONObject.parseObject(instr);
			if (jasonObject == null) {
				result.setSuccess(false);
				result.setCode(UnifiedCode.JSON_ERROR.getCode());
				result.setMessage(UnifiedCode.JSON_ERROR.getMsg());
				return result;
			}

			UnifiedOrderAppraiseAdd appraiseAdd = jasonObject.toJavaObject(UnifiedOrderAppraiseAdd.class);

			Map<String, Object> returnMap = unifiedOrderAppraiseDubboConsumerService.addObj(appraiseAdd);
			if (returnMap.get("code").equals(1)) {
				result.setCode(1);
				result.setSuccess(true);
			} else {
				result.setCode(Integer.parseInt(returnMap.get("code").toString()));
				result.setMessage(returnMap.get("msg").toString());
				result.setSuccess(false);
			}

		} catch (Exception e) {

			result.setSuccess(false);
			result.setCode(UnifiedCode.OTHER_ERROR.getCode());
			result.setMessage(e.getMessage());
			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 新增评价-商城专用
	 *
	 * @return
	 */
	@PostMapping("/appraisesm")
	@ResponseBody
	public ResponseResult<String> appraisesm(UnifiedOrderAppraiseSMAdd appraiseAdd) {
		ResponseResult<String> result = new ResponseResult<>();
		try {
			//String instr= MyTools.readInputStream(request.getInputStream(), Charset.forName("utf-8"));

//			JSONObject  jasonObject = JSONObject.parseObject(instr);
//			if(jasonObject==null) {
//				result.setSuccess(false);
//				result.setCode(UnifiedCode.JSON_ERROR.getCode());
//				result.setMessage(UnifiedCode.JSON_ERROR.getMsg());
//				return result;
//			}

			//UnifiedOrderAppraiseSMAdd appraiseAdd  = jasonObject.toJavaObject(UnifiedOrderAppraiseSMAdd.class);
			JSONArray                          goodsJsonArray = JSONArray.parseArray(appraiseAdd.getGoods_json());
			List<UnifiedOrderGoodsAppraiseAdd> goodsLs        = goodsJsonArray.toJavaList(UnifiedOrderGoodsAppraiseAdd.class);

			Map<String, Object> returnMap = unifiedOrderAppraiseDubboConsumerService.addObjSM(appraiseAdd, goodsLs);
			if (returnMap.get("code").equals(1)) {
				result.setCode(1);
				result.setSuccess(true);
			} else {
				result.setCode(Integer.parseInt(returnMap.get("code").toString()));
				result.setMessage(returnMap.get("msg").toString());
				result.setSuccess(false);
			}

		} catch (Exception e) {

			result.setSuccess(false);
			result.setCode(UnifiedCode.OTHER_ERROR.getCode());
			result.setMessage(e.getMessage());
			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}
		return result;
	}

	/***
	 * kafka测试 ---- 楼闪送 专用
	 */
	@Autowired
	private        KafkaTemplate<String, String> kafkaTemplate;
	private static Gson                          gson = new GsonBuilder().create();

	@PostMapping("/kfktest")
	@ResponseBody
	public ResponseResult<String> kfktest() {
		ResponseResult<String> result = new ResponseResult<>();
		try {
			String              topTic = "unifiedorderupdatedev";
			Map<String, Object> map    = new HashMap<>();
			map.put("sn", "LS2019120400001");
			map.put("pay_state", 1);
			kafkaTemplate.send(topTic, gson.toJson(map));

		} catch (Exception e) {

			result.setSuccess(false);
			result.setCode(UnifiedCode.OTHER_ERROR.getCode());
			result.setMessage(e.getMessage());
			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}
		return result;
	}


}
