package com.chinacreator.process.service;

import com.chinacreator.process.bean.VipBlackBean;
import com.chinacreator.process.dao.BlackDao;
import com.chinacreator.process.util.DesUtil;
import com.chinacreator.process.util.SHAUtil;
import com.chinacreator.process.util.URLUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Component
public class VipBlackService {
    @Autowired
    private BlackDao blackDao;

    //prod

    private static String ORDER_CLEAN_URL="http://114.255.201.238:8090/video-activity/cacheOper?cacheName=ECBConfig&ip=all";
    private static String VIP_CLEAN_URL="http://114.255.201.238:8090/video-activity/cacheOper?cacheName=ECBJoinConfig&ip=all";

//    private static String ORDER_CLEAN_URL="http://114.255.201.228:86/video-activity/cacheOper?cacheName=ECBConfig&ip=all";
//    private static String VIP_CLEAN_URL="http://114.255.201.228:86/video-activity/cacheOper?cacheName=ECBJoinConfig&ip=all";

    private static String MSG_SEND_URL="http://111.206.133.54/smsorder/channelSmsSend.do?";
    private static String CHANNEL="kclyl02";
    private static String PWD="xhs82dks";

    private Logger logger = Logger.getLogger("intoBlack");

    public void intoBlack(String cpid,String spid,List<String> userids)throws Exception{
        List<VipBlackBean> orderbean=blackDao.getAllOrderCount(cpid,spid);
        VipBlackBean vipBlackBean;
        double percent=0;

        if(orderbean!=null && orderbean.size()>0){
            for(VipBlackBean vip:orderbean){
                logger.info("findProvince"+vip.getProvince());
                //根据省份产品查询套餐订购量
                vipBlackBean=  blackDao.getOrderCountBy(cpid,spid,vip.getProvince());

                if(vipBlackBean!=null){
                    percent=(Math.round(vipBlackBean.getCount()*10000/vip.getCount())/100.0);
                    logger.info("percent"+percent);


                    if(percent>60 && !blackDao.isOrderWhiteUser(cpid,spid,vip.getProvince(),vipBlackBean.getLimit())){
                        logger.info("findPackage"+vipBlackBean.getLimit());
                        int flag=0;
                        if(!blackDao.isOrderBlackUser(cpid,spid,vip.getProvince(),vipBlackBean.getLimit())){
                            blackDao.insertOrderBlack(vipBlackBean);
                            flag=1;
                            logger.info("加入订购黑名单成功");

                        }
                        if(!blackDao.isVipBlackUser(cpid,spid,vip.getProvince(),vipBlackBean.getLimit())){
                            vipBlackBean.setActivetype(blackDao.getActiveType(vipBlackBean));
                            blackDao.insertVipBlack(vipBlackBean);
                            flag=1;
                            logger.info("加入领取黑名单成功");

                        }



                        logger.info(flag);
                        if(flag>0){
                            //刷新缓存

                            for(int i=0;i<3;i++){
                                URLUtil.get(ORDER_CLEAN_URL);
                                URLUtil.get(VIP_CLEAN_URL);
                            }

                            if(userids!=null && userids.size()>0) {
                                //发送短信

                                String content=vipBlackBean.getProvince()+"省 "+vipBlackBean.getLimit()+"套餐 腾讯20元产品最近10小时占比为"+percent+"%，已加入黑名单";
                                logger.info(content);
                                for (String userid : userids) {
                                    sendMsg(CHANNEL, userid, PWD, content);
                                }
                            }
                        }

                    }
                }else{
                    logger.info("无超额套餐");
                }

            }
        }else{
            logger.info("无超额省份");
        }
    }


    public void sendMsg(String channel,String userid,String pwd,String content) {
        String result="";
        try {

            String timestamp = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
            String sign = SHAUtil.shaEncode(channel + userid + timestamp + pwd).toLowerCase();
            userid = URLEncoder.encode(DesUtil.encode(userid, pwd), "utf-8");
            content = URLEncoder.encode(content, "utf-8");
            String url = MSG_SEND_URL + "userid=" + userid + "&channel=" + channel + "&timestamp=" + timestamp + "&sign=" + sign + "&content=" + content;
            result=URLUtil.get(url);
            logger.info(url+"result="+result);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("短信发送失败"+result);
        }
    }




}
