package com.chinacreator.video.restful.controller;

import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.DictionaryDao;
import com.chinacreator.video.restful.service.QueryEcbService;
import com.chinacreator.video.util.URLUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author yu.su
 * @Date 20210316
 */
@RestController
public class QueryEcbController {

    @Autowired
    private DictionaryDao dictionaryDao;

    @Autowired
    private QueryEcbService queryEcbService;

    private Logger logger=Logger.getLogger("queryEcb");
    /**
     * 查询ecb用户状态
     * @param orderInfo
     * @return
     */
    @PostMapping("/queryUserInfoEcb")
    public Map<String,Object> queryUserInfoEcb(@RequestBody  OrderInfo orderInfo){
//        System.out.println(orderInfo);
        String resultCode="0";
        String errorInfo="";
        String userid ="";
        String cpid = orderInfo.getCpid();
        String spid = orderInfo.getSpid();
        String channel = orderInfo.getChannel();
        String result = null;
        Map<String,Object> resp=new HashMap<>();
        logger.info("请求参数:===="+orderInfo);
        cpid="qryecb";
        spid="*";
        orderInfo.setCpid(cpid);
        orderInfo.setSpid(spid);
        try {
            orderInfo.setInvokeface("queryUserInfoEcb");
            queryEcbService.busiCheck(orderInfo);
            userid = orderInfo.getUserid();
            result = URLUtil.get(
                    this.dictionaryDao.getValue("ecbUrl") + "?userid=" + userid + "&cpid=" + cpid + "&spid=" + spid,
                    NumberUtils.toInt(this.dictionaryDao.getValue("ecbTimeout")));
            if (StringUtils.isNotEmpty(result)){
                Map<String,Object> map = JSONObject.parseObject(result, Map.class);
                resultCode= (String) map.get("resultCode");
                errorInfo= (String) map.get("errorInfo");
                resp.put("pacakge",map.get("pacakge"));
                resp.put("brand",map.get("brand"));
                resp.put("productList",map.get("productList"));
            }else {
                resultCode="9001";
                errorInfo="三户接口返回为空";
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException){
                resultCode=((BussinessException) e).getCode();
                errorInfo=e.getMessage();
            }else {
                resultCode="8000";
                errorInfo=e.getMessage();
            }
        }finally {
            resp.put("resultCode",resultCode);
            resp.put("errorInfo",errorInfo);
            logger.info("userid:"+userid+"=======chaneel:"+channel+"=======日志信息===="+resp);
        }
        return resp;
    }
}
