package com.chinacreator.video.restful.service;


import com.chinacreator.video.bean.ChannelBean;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.ChannelDao;
import com.chinacreator.video.restful.dao.CdnSendSmsDao;
import com.chinacreator.video.util.DesUtil;
import com.chinacreator.video.util.MD5;
import com.chinacreator.video.validate.ChannelValidate;
import com.chinacreator.video.validate.IpValidate;
import com.chinacreator.video.validate.TimetampValidate;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.chinacreator.video.util.UsermobUtil.isValid;

@Service
public class CdnSendSmsService {


    @Autowired
    private CdnSendSmsDao cdnSendSmsDao;

    @Autowired
    private ChannelValidate channelValidate;

    @Autowired
    private IpValidate ipValidate;

    @Autowired
    private TimetampValidate timetampValidate;

    @Autowired
    private ChannelDao channelDao;
    /**
     * 判断是否在白名单中
     * @param userid
     * @param channel
     * @return
     */
    public boolean findByUserid(String userid, String channel) {
        Integer count=cdnSendSmsDao.findByUserid(userid,channel);
        if (count==null||count == 0){
            return false;
        }else {
            return true;
        }
    }


    /***
     * 校验
     * @param orderInfo
     * @throws Exception
     */
    public void busiCheck(OrderInfo orderInfo) throws Exception{

        //timetamp验证
        timetampValidate.validate(orderInfo.getTimestamp());
        //渠道验证
        channelValidate.validate(orderInfo.getChannel());
        //ip验证
        ipValidate.volidate(orderInfo,"1");;
        //签名验证
        validate(orderInfo);
        //手机号码验证
        validatePhone(orderInfo);
    }


    public void validate(OrderInfo orderInfo) throws Exception {
        String lcalsign = getSign(orderInfo);
        lcalsign += channelDao.queryChannel(orderInfo.getChannel()).getPassword();
        lcalsign = MD5.MD5Encode(lcalsign);
        if(!lcalsign.equals(orderInfo.getSignature())) {
            throw new BussinessException("10006", "签名校验失败");
        }
    }

    /**
     * 拼接签名信息 cpid+spid+userid+type+channel+timestamp
     * @param orderInfo
     * @return
     */
    private String getSign(OrderInfo orderInfo) {
        if (orderInfo == null) {
            return "";
        }
        StringBuilder strb = new StringBuilder();
        if(orderInfo.getOrderid() != null && !"".equals(orderInfo.getOrderid())){
            strb.append(orderInfo.getOrderid());
        }
        strb.append(orderInfo.getUserid());
        strb.append(orderInfo.getChannel());
        strb.append(orderInfo.getTimestamp());
        return strb.toString();
    }

    public void validatePhone(OrderInfo orderInfo) throws Exception {
        String userid = orderInfo.getUserid();
        if(StringUtils.isEmpty(userid)) {
            throw new BussinessException("10007", "手机号不能为空");
        }
        ChannelBean channelBean = channelDao.queryChannel(orderInfo.getChannel());
        try {
            userid = DesUtil.decode(userid, channelBean.getPassword());
            if(isValid(userid) && userid.length() == 11){
                orderInfo.setUserid(userid);
            }else{
                throw new BussinessException("10009", "手机号错误");
            }
        } catch (Exception e) {
            if(e instanceof BussinessException) {
                throw e;
            }else {
                throw new BussinessException("10008", "手机号加密错误");
            }
        }
    }
}
