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.OrderInfo;
import com.chinacreator.video.bean.ProJinVokeUrlBean;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.ProJinVokeUrlDao;
import com.chinacreator.video.service.QryBusiService;
import com.chinacreator.video.service.UserCreditService;
import com.chinacreator.video.util.MD5;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 用户信息接口
 * @author czw
 */
@RestController
public class UserCreditControl {

    private static final Logger log = LoggerFactory.getLogger("userCredit");

    @Autowired
    private ProJinVokeUrlDao proJinVokeUrlDao;

    @Autowired
    private UserCreditService service;

    @PostMapping("/usercredit")
    public DataOutPipe usercredit(@RequestBody OrderInfo orderInfo) throws Exception {
        orderInfo.setCpid("usercredit");
        orderInfo.setSpid("usercredit");
        log.info("参数传递==============》"+orderInfo.toString());
        Map<String,Object> logmap = new HashMap<String,Object>();
        DataOutPipe outpipe = new DataOutPipe();
        String resultcode = "";
        String errorinfo = "";
        orderInfo.setInvokeface("usercredit");
        try {
            service.check(orderInfo);
            ProJinVokeUrlBean usercredit = proJinVokeUrlDao.getEcredit("usercredit");

            Map map = new HashMap(16);
            map.put("operType","1");
            map.put("infotagSet","1");
            map.put("needRoute",orderInfo.getNeedRoute());
            map.put("userobj",orderInfo.getUserid());
            map.put("productTypeCode",orderInfo.getActivitycode());
            map.put("productMode",orderInfo.getActivetype());
            map.put("productId",orderInfo.getProductid());
            log.info("参数详情：=============================>"+JSON.toJSONString(map));
            String s = HttpUtil.get(usercredit.getInvokeurl(),map);
            log.info("结果详情：=============================>"+s);
            JSONObject jsonObject = JSON.parseObject(s);
            resultcode = jsonObject.getString("resultCode");
            errorinfo = jsonObject.getString("resultInfo");
            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;
    }
}
