package com.chinacreator.video.web;


import com.alibaba.fastjson.JSON;
import com.chinacreator.video.bean.CpSpBean;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.CpSpDao;
import com.chinacreator.video.mapper.DictionaryDao;
import com.chinacreator.video.mapper.TestCapAopLogDao;
import com.chinacreator.video.util.DesUtil;
import com.chinacreator.video.util.URLUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class TestCapAopLogControl {

    private static final  Logger logger = LoggerFactory.getLogger(TestCapAopLogControl.class);

    @Autowired
    private TestCapAopLogDao testCapAopLogDao;
    @Autowired
    private DictionaryDao dictionaryDao;
    @Autowired
    private CpSpDao cpSpDao;

    /**
     * 查询cap、能力平台的日志
     * @param userid
     * @param cpid
     * @param spid
     * @return
     * @throws Exception
     */
	@RequestMapping(value = "/testCapAopLog", method = RequestMethod.GET)
	private String testCapAopLog(String userid,String cpid,String spid) throws Exception{
        List<HashMap> list= testCapAopLogDao.getCapAopLogInfo(userid,cpid,spid);
        return JSON.toJSONString(list);
    }

    /**
     * 调用能力平台接口订购
     * @param orderInfo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/testShareOrder", method = RequestMethod.GET)
    private String testShamainreOrder(OrderInfo orderInfo) {
        logger.info("测试调用能力平台开始------"+new Timestamp(System.currentTimeMillis()));
        String url = dictionaryDao.getValue("commonOrderUrl")+"order.do?";
        String result = "";
        try {
            url += this.getParams(orderInfo);
            result = URLUtil.get(url);
        }catch (Exception e) {
            e.printStackTrace();
            logger.info("测试调用能力平台异常---------------"+e.getMessage());
            return "异常2";
        }
        logger.info("测试调用能力平台结束------"+new Timestamp(System.currentTimeMillis())+"-----"+result);
//        return result;
        return JSON.toJSONString(result);
    }

    private String getParams(OrderInfo orderInfo) throws BussinessException{
        String params = "";
        try {
            String cpid = orderInfo.getCpid();
            String spid = orderInfo.getSpid();
            String userid = orderInfo.getUserid();
//          String channel = "WP_vactest";//测试，现在前端传过来了
            String channel = orderInfo.getChannel();
//          String pwd = cpSpDao.getCpSp(cpid, spid).getPwd(); //TB_CP_ACCOUNT_CONFIG表NETPWD字段
            String pwd = cpSpDao.getCpSp(orderInfo.getCpid(), orderInfo.getSpid()).getPwd(); //TB_CP_ACCOUNT_CONFIG表NETPWD字段
//           String pwd = "chsh5a1d";
            String timestamp = (System.currentTimeMillis()) / 1000 + "";
            String seqnumber = "test";  //未用
			String access = "test";  //未用
			String token = "test";  //未用
			String seqnumberParams = "";
//			if(!"".equals(seqnumber)){
//				seqnumberParams = "&seqnumber="+seqnumber;
//			}
//			var param = "cpid=" + cs.user.cpid + "&spid=" + cs.user.spid + "&orderType=0&ordertype=0&userid="
//					+ encodeURIComponent(userid) + "&channel=" + cs.channel + "&apptype=2";
            params += "ordertype=0&apptype=2&channel="+channel+"&cpid="+cpid+"&spid="+spid+"&timestamp="+timestamp;
            userid = DesUtil.encode(userid, pwd); //手机号码加密
            params += "&userid="+ URLEncoder.encode(userid, "utf-8"); //重新编码
//			params += seqnumberParams;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BussinessException("80013", "组建调用参数出现异常，"+e.getMessage());
        }
        return params;
    }

}
