package com.chinacreator.video.web;


import com.alibaba.fastjson.JSON;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.restful.bean.ShangHaiObject;
import com.chinacreator.video.service.UniMobileService;
import com.chinacreator.video.util.Base64;
import com.chinacreator.video.util.DesUtil;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.chinacreator.video.restful.service.ShangHaiOrderService;
import org.springframework.web.bind.annotation.RequestBody;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.util.AESUtil;

import javax.crypto.Cipher;
import java.security.Key;
import java.util.HashMap;
import java.util.Map;


@RestController
public class UniMobileControl {

    //日志文件路径
    private static final Logger log = LoggerFactory.getLogger("uniMobile");

    @Autowired
    private UniMobileService uniMobileService;
    @Autowired
    private ShangHaiOrderService shangHaiOrderService;


    /**
     *
     * @param orderInfo
     * @return
     */
	@RequestMapping(value = "/getinfo", method = RequestMethod.POST, produces = "application/json")
	private String getinfo(@RequestBody OrderInfo orderInfo) throws Exception{
//        String userid_new = DesUtil.encode("18942165270","creators20210430");
//       System.out.println("userid_new----------"+userid_new);
        String resultCode = "";  //结果识别码
        String resultMessage = "";    //结果信息
        String province = "";    //结果信息
        Map<String, Object> param = new HashMap<>();
        try {
            orderInfo.setCpid("defaultCpid"); //设置一个默认的cpid
            orderInfo.setSpid("1001");
            orderInfo.setInvokeface("getinfo");
            uniMobileService.check(orderInfo);
        }catch (BussinessException e){
            resultCode = e.getCode(); //异常码
            resultMessage = e.getMessage(); //异常信息
            param.put("resultCode",resultCode);
            param.put("resultInfo",resultMessage);
            param.put("userid","");
            log.info(orderInfo.getChannel()+"检测前结果："+JSON.toJSONString(param));
            return JSON.toJSONString(param);
        }
        try {
            Map<String,String> map = uniMobileService.getMobileInfo(orderInfo.getUserid());
            String resultInfo=map.get("resultInfo");
            province=map.get("province");
            resultCode = "1"; //默认为没有查询到信息
            resultMessage = "没有查询到相关信息";
            if (resultInfo != null && !resultInfo.equals("")) {
                if (resultInfo.equals("联通")) {//如果为联通号码
                    resultCode = "0"; //为联通号码
                }else {
                    resultCode = "1";   //为其他号码
                }
                resultMessage = resultInfo;
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultCode = "80001"; //异常码
            resultMessage = e.getMessage();

        }
        param.put("resultCode",resultCode);
        param.put("resultInfo",resultMessage);
        param.put("userid",orderInfo.getUserid());
        param.put("province",province);
        log.info(orderInfo.getChannel()+":"+orderInfo.getUserid()+"查询结果："+JSON.toJSONString(param));
        return JSON.toJSONString(param);

    }


//    public void checkBusi(ShangHaiObject inputObj, OrderInfo orderInfo) throws Exception {
//       this.paramVali(inputObj,orderInfo); 								//验证参数
//        this.valiTimetamp(orderInfo); 							//验证时间戳
//        channelValidate.validate(orderInfo.getChannel());		//验证渠道
//        this.valiMob(orderInfo);								//验证手机号码
//        this.valiSign(orderInfo);								//验证签名
//        ipValidate.volidate(orderInfo);							//验证ip
//        //this.checkMutual(orderInfo); 							//判断业务互斥
//        this.replaceSpid(orderInfo);
//    }


}
