package com.chinacreator.video.restful.service;

import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.dao.VivoProductDao;
import com.chinacreator.video.service.CommonService;
import com.chinacreator.video.service.HandleService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;

@Service
public class VivoProductService {

    @Resource
    private VivoProductDao vivoProductDao;
    @Resource
    private CommonService commonService;
    private Logger logger = Logger.getLogger("vivoProduct");
    @Resource
    private HandleService handleService;


    /**
     * vivo 主产品列表查询
     *
     * @param appId
     * @return
     * @author 王瑞
     * @date 2023/4/19
     */
    public List<HashMap> queryMainProductList(String appId) {
        return vivoProductDao.queryMainProductList(appId);
    }

    public void preCheckedProduct( OrderInfo orderInfo ) throws Exception {
        String userid = orderInfo.getUserid();
        String cpid = orderInfo.getCpid();
        String spid = orderInfo.getSpid();

        //1.验证黑白名单  指什么的黑白名单,ip 吗?
        //	是黑名单不让订购
        if (vivoProductDao.isBlackUser(userid, cpid) > 0) {
            throw new BussinessException("4", "用户为黑名单用户");
        }
//        2.验证订购关系
        int count = vivoProductDao.isActive(userid, spid);
        if (count < 1) {
            throw new BussinessException("8002", "未订购指定产品，无抽奖资格");
        }
        //3.验证渠道省份限制
//        渠道省份限制具体方法：CommonService.valiChannelSpidLimit

//        commonService.setAreaInfo(orderInfo);
        try {
            commonService.valiChannelSpidLimit(orderInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //4.合约产品验证 是在产品表里看产品的 status 状态吗
        //	合约产品当前有个限制，合约期内不让订购
        //	这个在后面的流程里也有判断  看你们需要

        //todo 先不做了
    }

    public String getCpid(String spid) {
        List<HashMap> spInfo = vivoProductDao.findSpInfo(spid);
        String cpid = (String) spInfo.get(0).get("cpid");
        return cpid;
    }

    @Async
    public void handle(OrderInfo orderInfo) throws Exception {
        handleService.handle(orderInfo);
    }
}
