package com.chinacreator.video.service;

import com.chinacreator.video.bean.KuaiShouHelpBean;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.KuaiShouHelpDao;
import com.chinacreator.video.mapper.QueryOrderByCpDao;
import com.chinacreator.video.util.RedisService;
import com.chinacreator.video.validate.IpValidate;
import com.chinacreator.video.validate.KuaiShouValidate;
import com.chinacreator.video.validate.OrderValidate;
import com.chinacreator.video.validate.TimetampValidate;
import com.chinacreator.video.web.KuaiShouHelpController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class KuaiShouHelpService {
    private static final Logger logger = LoggerFactory.getLogger(KuaiShouHelpController.class);

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

    @Autowired
    private KuaiShouHelpDao kuaiShouHelpDao;

    @Autowired
    RedisService redisService;

    @Autowired
    private TimetampValidate timetampValidate;

    @Autowired
    private KuaiShouValidate kuaiShouValidate;

    @Autowired
    private IpValidate ipValidate;

    public void checkOrder(KuaiShouHelpBean bean) throws Exception {
        kuaiShouValidate.valiTime(bean);//验证时间戳

        //kuaiShouValidate.valiChannel(bean);//验证渠道
        kuaiShouValidate.validate(bean);//验证参数
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setInvokeface(bean.getInvokeface());
        orderInfo.setCpid(bean.getCpid());
        orderInfo.setSpid(bean.getSpid());
        orderInfo.setChannel(bean.getChannel());
        ipValidate.volidate(orderInfo,"0");//验证ip
    }
//1
    public void checkOrder4Ip(KuaiShouHelpBean bean) throws Exception {
        kuaiShouValidate.valiTime(bean);//验证时间戳
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setInvokeface(bean.getInvokeface());
        orderInfo.setCpid(bean.getCpid());
        orderInfo.setSpid(bean.getSpid());
        orderInfo.setChannel(bean.getChannel());
        ipValidate.volidate(orderInfo,"0");//ip
        if(bean.getIp().equals("")||bean.getIp() == null){
            throw new BussinessException("10030", "ip参数不得为空");
        }
    }

    public void checkOrderIp(KuaiShouHelpBean bean) throws Exception {
        kuaiShouValidate.valiTime(bean);//验证时间戳
        kuaiShouValidate.validateChannel(bean);
        kuaiShouValidate.orderValidate(bean);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setInvokeface(bean.getInvokeface());
        orderInfo.setCpid(bean.getCpid());
        orderInfo.setSpid(bean.getSpid());
        orderInfo.setChannel(bean.getChannel());
        log.info("验证参数:"+orderInfo.toString());
        ipValidate.volidate(orderInfo,"1");//验证ip
        /*if(bean.getIp().equals("")||bean.getIp() == null){
            throw new BussinessException("10030", "ip参数不得为空");
        }*/
    }

    public void save(KuaiShouHelpBean bean) throws BussinessException{
        int i = kuaiShouHelpDao.save(bean);
        if(i == 0){
            throw new BussinessException("10025", "保存失败");
        }
    }

    public void savePv(String ip) throws BussinessException{
        int i = kuaiShouHelpDao.savePv(ip);
        if(i == 0){
            throw new BussinessException("10025", "保存失败");
        }
    }

    public int findCount() {
        return kuaiShouHelpDao.findCount();
    }

    public int findMangoCount() {
        return kuaiShouHelpDao.findMangoCount();
    }


    public int queryOrderRelation(String userid) {
        return kuaiShouHelpDao.queryOrderRelation(userid);
    }
}
