package com.chinacreator.video.service;

import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.ChannelDao;
import com.chinacreator.video.mapper.QueryBackOrderDao;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class BackBusiQueryService {

    private static final Logger logger = LoggerFactory.getLogger(BackBusiOrderService.class);

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

    @Autowired
    private ChannelDao channelDao;

    @Autowired
    private QueryBackOrderDao queryBackOrderDao;

    @Autowired
    private ChannelValidate channelValidate;

    @Autowired
    private IpValidate ipValidate;

    @Autowired
    private TimetampValidate timetampValidate;

    /**
     * 基础检验
     * @param orderInfo
     * @throws Exception
     */
    public void busiCheck(OrderInfo orderInfo) throws Exception {

        //参数验证
        valiParams(orderInfo);
        //渠道验证
        channelValidate.validate(orderInfo.getChannel());
        //ip验证
        ipValidate.volidate(orderInfo,"1");
        //时间戳验证
        timetampValidate.validate(orderInfo.getTimestamp());
        //签名验证
        signValidate(orderInfo);


    }

    /**
     * 签名验证
     * @param orderInfo
     * @throws Exception
     */
    public void signValidate(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", "签名校验失败");
        }
    }

    /**
     * 参数验证
     * @param orderInfo
     * @throws Exception
     */

    public void valiParams(OrderInfo orderInfo) throws Exception{
        String orderid = orderInfo.getOrderid();
        if(StringUtils.isEmpty(orderid)){
            throw new BussinessException("80015", "orderid参数不能为空");
        }

    }


    /**
     * 拼接签名信息 orderid+channel+timestamp
     * @param orderInfo
     * @return
     */

    public String getSign(OrderInfo orderInfo){
        if (orderInfo == null) {
            return "";
        }
        StringBuilder strb = new StringBuilder();
        strb.append(orderInfo.getChannel());
        strb.append(orderInfo.getOrderid());
        strb.append(orderInfo.getTimestamp());
        return strb.toString();

    }

    /**
     * 查询订单状态
     * @param orderInfo
     * @throws Exception
     */

    public Map queryOrderStatus(OrderInfo orderInfo) throws Exception{

        Map map = queryBackOrderDao.getBackOrderStatus(orderInfo.getOrderid());
        return map;
    }




}
