package com.chinacreator.video.restful.service;

import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.CommConfDao;
import com.chinacreator.video.restful.dao.TbActivityDao;
import com.chinacreator.video.service.CommonService;
import com.chinacreator.video.validate.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TbActivityService {

    @Autowired
    private TbActivityDao tbActivityDao;

    @Autowired
    private ChannelValidate channelValidate;

    @Autowired
    private IpValidate ipValidate;

    @Autowired
    private MobValidate mobValidate;

    @Autowired
    private TimetampValidate timetampValidate;

    @Autowired
    private SignValidate signValidate;

    @Autowired
    private ParamValidate paramValidate;

    @Autowired
    private CommonService commonService;


    public void insertTb(OrderInfo orderInfo) throws BussinessException {
        try {
            tbActivityDao.insertTb(orderInfo);
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getMessage()!=null && e.getMessage().contains("ORA-00001")){
                throw new BussinessException("80001","订单号重复");
            }else{
                throw new BussinessException("80002","数据保存出现异常-----"+e.getMessage());
            }
        }
    }

    public void busiCheck(OrderInfo orderInfo) throws Exception{

        paramValidate.validate(orderInfo,null);

        //timetamp验证
        timetampValidate.validate(orderInfo.getTimestamp());
        //渠道验证
        channelValidate.validate(orderInfo.getChannel());
        //ip验证
        ipValidate.volidate(orderInfo);
        //签名验证
        signValidate.validate(orderInfo);
        //手机号码验证
        mobValidate.validate(orderInfo);
        //并发访问控制
        vistVali(orderInfo);
    }
    /**
     * 验证访问限制，1分钟内同一业务只能办理一次
     * @param orderInfo
     * @throws Exception
     */
    private void vistVali(OrderInfo orderInfo) throws Exception {
        String [] params = new String [3];
        params[0] = orderInfo.getChannel();
        params[1] = orderInfo.getUserid();
        params[2] = orderInfo.getSpid();
        if(commonService.vistVali2(params, "tbActivity")){
            throw new BussinessException("80003", "指定时间内重复请求");
        }
    }
}
