package com.chinacreator.video.restful.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.bean.JiyueOrderBean;
import com.chinacreator.video.restful.dao.KcReverseDao;
import com.chinacreator.video.restful.util.AesUtilByIntensive;
import com.chinacreator.video.validate.IpValidate;
import org.apache.commons.lang.StringUtils;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class KcReverseService {

    @Autowired
    private IpValidate ipValidate;

    @Autowired
    private KcReverseDao kcReverseDao;

    private static Logger log = Logger.getLogger("kcOrder");

    public void busiVali(JiyueOrderBean bean, String inputParams) throws Exception {
        try {
            String data = JSON.parseObject(inputParams).getString("data");
            Map invoke = kcReverseDao.getInvokeUrl("univideo", "kcOrder");
            JSONObject object = JSON.parseObject(invoke.get("PARAM1").toString());
            String aespwd = object.getString("aespwd");
            data = AesUtilByIntensive.decrypt(data, aespwd);
            log.info("解密后data"+data);
            JSONObject obj = JSON.parseObject(data);
            bean.setSerialid(obj.getString("serialid"));//
            bean.setType(obj.getString("type"));//
            bean.setUsermob(obj.getString("usermob"));//
            bean.setProductid(obj.getString("productid"));//
            bean.setOrdertime(obj.getString("ordertime"));//
            bean.setCanceltime(obj.getString("canceltime")==null?"":obj.getString("canceltime"));
            bean.setStarttime(obj.getString("starttime"));//
            bean.setEndtime(obj.getString("endtime")==null?"":obj.getString("endtime"));
            bean.setChannel(obj.getString("channel")==null?"":obj.getString("channel"));
            bean.setStaffid(obj.getString("staffid")==null?"":obj.getString("staffid"));
            bean.setFirstserialid(obj.getString("firstserialid")==null?"":obj.getString("firstserialid"));
            bean.setPrmproductid(obj.getString("prmproductid")==null?"":obj.getString("prmproductid"));//
            bean.setPrmspid(obj.getString("prmspid")==null?"":obj.getString("prmspid"));//
            valiMob(bean);
        } catch (Exception e1) {
            e1.printStackTrace();
            log.error("参数不合法，解析异常，"+e1);
            throw new BussinessException("80015", "参数不合法，解析异常，"+e1.getMessage());
        }
        if(bean.getProductid() == null){
            throw new BussinessException("80015", "参数为空");
        }
    }

    public void operBusi(JiyueOrderBean bean) throws Exception {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setChannel("kcOrder");
        orderInfo.setCpid("kcOrder");
        orderInfo.setInvokeface("kcOrder");
        //验证IP
        ipValidate.volidate(orderInfo);
        kcReverseDao.addKc(bean);
    }

    /**
     * 验证手机号码

     */
    private void valiMob(JiyueOrderBean bean)throws Exception{
        String userid = bean.getUsermob();
        if(StringUtils.isEmpty(userid) //为空
                || !isValid(userid)  //非全数字
                || userid.length() != 11) { //不是11位
            throw new BussinessException("10009", "手机号错误");
        }
    }

    private boolean isValid(String usermob){
        if (usermob == null) {
            return false;
        }
        usermob = usermob.replaceAll("[\\s-]", "");
        return usermob.matches(".*(1\\d{10}).*");
    }
}
