package com.chinacreator.video.restful.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.druid.util.StringUtils;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.dao.CbErrInfoQryDao;
import com.chinacreator.video.service.CommonService;
import com.chinacreator.video.service.QryBusiService;
import com.chinacreator.video.validate.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订购错误信息查询
 * @author xu.zhou
 * @date 20220411
 */
@Service
public class CbErrInfoQryService {
	
    private Logger logger = LoggerFactory.getLogger("tendraw");

    @Autowired
    private TimetampValidate timetampValidate;

    @Autowired
    private IpValidate ipValidate;

    @Autowired
    private SignValidate signValidate;

    @Autowired
    private ChannelValidate channelValidate;

    @Autowired
    private MobValidate mobValidate;
    
	@Autowired
	private CbErrInfoQryDao cbErrInfoQryDao;

	@Autowired
	CommonService commonService;

    public void check(OrderInfo orderInfo) throws Exception {
    	//参数验证
    	paramValidate(orderInfo);
        //timetamp验证
        timetampValidate.validate(orderInfo.getTimestamp());
        //渠道验证
        channelValidate.validate(orderInfo.getChannel());
        //签名验证
        signValidate.validate(orderInfo);
        //IP验证
        orderInfo.setCpid("qrycberrinfo"); //用于IP白名单验证
        orderInfo.setSpid("qrycberrinfo"); //用于IP白名单验证
        ipValidate.volidate(orderInfo,"1");
        //手机号码验证
        mobValidate.validateByChannel(orderInfo);   
        //访问次数验证
        commonService.valiVisitLimit(orderInfo);
        //设置访问次数
        commonService.setVisitcount(orderInfo);
    }

    /**
     * 参数验证
     * @param orderInfo
     * @throws Exception
     */
    private void paramValidate(OrderInfo orderInfo) throws Exception {  
    	String errorMsg = "";
    	//公共参数是否为空
        if (StringUtils.isEmpty(orderInfo.getUserid()) 
        		|| StringUtils.isEmpty(orderInfo.getChannel())
        		|| StringUtils.isEmpty(orderInfo.getSignature())
        		|| StringUtils.isEmpty(orderInfo.getTimestamp())
        		) {
        	errorMsg = "参数异常";
        }

        //设置为空串，用于签名验证
        orderInfo.setType("");
        orderInfo.setCpid("");
        orderInfo.setSpid("");
        //验证未通过
        if(!"".equals(errorMsg)){
            throw new BussinessException("80015", errorMsg);
        }
    }
    
    /**
     * 业务处理
     * @param orderInfo
     * @throws Exception
     */
    public List<Map<String, String>> process(OrderInfo orderInfo) throws Exception {   	
		List<Map<String, String>> data = cbErrInfoQryDao.qryErrInfoByUserid(orderInfo.getUserid());
    	return data;
    }
}
