package com.chinacreator.video.web;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.bean.EcreditBean;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.EcreditDao;
import com.chinacreator.video.service.handle.EcreditService;
import com.chinacreator.video.util.HttpUtils;
import com.chinacreator.video.util.MD5;
import com.chinacreator.video.util.URLUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author czw
 */
@RestController
public class EcreditControl {
    private static final Logger log = LoggerFactory.getLogger("ecredit");

    @Autowired
    private EcreditDao ecreditDao;
    @Autowired
    private EcreditService service;

    @RequestMapping("/getEcredit")
    public DataOutPipe<?> getEcredit(OrderInfo orderInfo) throws Exception {
        orderInfo.setCpid("getEcredit");
        orderInfo.setSpid("getEcredit");
        Map<String,Object> logmap = new HashMap<String,Object>();
        DataOutPipe outpipe = new DataOutPipe();
        String resultcode = "";
        String errorinfo = "";
        orderInfo.setInvokeface("getEcredit");
        log.info("传入参数===========================》："+orderInfo.toString());
        try {
            logmap.put("reqData", orderInfo);
            service.check(orderInfo);//业务验证


            EcreditBean ecredit = ecreditDao.getEcredit();

            Map map = new HashMap(12);
            map.put("number", orderInfo.getUserid());
            log.info("参数详情：=============================>" + JSON.toJSONString(map));
            String s = HttpUtil.get(ecredit.getInvokeurl(), map);
            log.info("结果详情：=============================>" + s);
            JSONObject jsonObject = JSON.parseObject(s);
            resultcode = jsonObject.getString("resultcode");
            errorinfo = jsonObject.getString("errorinfo");
            outpipe.setData(jsonObject.getString("data"));
        }catch (Exception e) {
            e.printStackTrace();
            resultcode = "80003";
            errorinfo = "出现异常，"+e.getMessage();
            if(e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
                throw e;
            }else {
                throw new BussinessException(resultcode, errorinfo);
            }
        }finally {
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
            logmap.put("rspData", outpipe.getData());
            logmap.put("resultcode", resultcode);
            logmap.put("errorinfo", errorinfo);
            log.info(JSON.toJSONString(logmap)); //写日志文件
        }
        return outpipe;
    }
}
