package com.chinacreator.process.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chinacreator.common.util.DESUtil;
import com.chinacreator.common.util.URLUtil;
import com.chinacreator.process.dao.DictionaryDao;
import com.chinacreator.process.util.JsonUtil;
import com.chinacreator.video.queue.MessageService;
import com.chinacreator.video.queue.bean.MessagePipe;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class CShandleSms5GJob {

	private Logger logger = Logger.getLogger("sms5g");
	private Logger log = Logger.getLogger(CShandleSms5GJob.class);

	@Autowired
	private DictionaryDao dictionaryDao;

	@Autowired
	private MessageService messageService;

    private final static String KEY = "22fdse33";



	public void doProcess() throws Exception {
		log.info("超时，生效短信job开始执行");
		if ("0".equals(dictionaryDao.getValue("recivemq"))) {
			List<MessagePipe> list = messageService.reciveBatchMessage("sms5g", 500);
			if(list != null && list.size()>0){
				log.info("超时，5g生效短信需要发送："+list.size());
				log.info("5g号码");
				for (MessagePipe messagePipe : list) {
					String result ="";
					String resultcode =(String) messagePipe.getBody().get("result");
					String userid = (String) messagePipe.getBody().get("userid");
					String times = (String) messagePipe.getBody().get("times");
					String busiType = (String) messagePipe.getBody().get("busiType");
					int style = NumberUtils.toInt((String)messagePipe.getBody().get("style"));
                    boolean bool=false;
                    String content = "";
                    logger.info(userid+"==="+busiType+"===="+style);
					try{
					    if (busiType.contains("tran_succ")||style==7){
                            String  rspJson= URLUtil.get("http://10.199.99.51:809/caporder/qry5g?userid=" + userid);
                            JSONObject jsondata = JSONObject.parseObject(rspJson);
                            String code = jsondata.getString("resultcode");
                            String errorinfo = jsondata.getString("errorinfo");
                            if (!"0".equals(code)){
                                log.error(userid+"===查询三户失败==="+errorinfo);
                            }else{
                                JSONObject jsonObj = jsondata.getJSONObject("data");
                                if (jsonObj!=null){
                                    JSONObject bodyObj  = (JSONObject)jsonObj.get("UNI_BSS_BODY");
                                    JSONObject osrspObj = (JSONObject)bodyObj.get("QRY_USER_SERVICE_RSP");
                                    JSONObject rspObj = (JSONObject)osrspObj.get("RSP");
                                    if(rspObj != null && rspObj.get("DATA") != null){
                                        JSONArray jsonArray = (JSONArray)rspObj.get("DATA");
                                        for(int i=0; i<jsonArray.size(); i++){
                                            JSONObject obj = (JSONObject)jsonArray.get(i);
                                            logger.info(userid+"=============="+obj.getString("BRAND_CODE"));
                                            if("5G00".equals(obj.getString("BRAND_CODE"))){
                                                bool=true;
                                                content="【中国联通】5G用户您好，XX、XX省的联通5G网络当前加速建设中，如您漫游到以上省分定向流量可能无法正常使用，给您带来的不便敬请谅解";
                                                break;
                                            }
                                        }
                                    }
                                }

                            }
                        }
					if(StringUtils.isNotEmpty(content)&&bool){
						result = URLUtil.get("http://10.199.99.147:8092/videoif/channelSmsSend.do?userid="+URLEncoder.encode(DESUtil.encode(userid, KEY),"utf-8")+"&content="+URLEncoder.encode(content,"UTF-8")+"&channel=cssms", 2000);
					}
					}catch(Exception e){
						log.error(userid+"短信发送失败",e);
					}finally {
						Map<String,String> map = new HashMap<String, String>();
						map.put("userid", userid);
						map.put("style", style+"");
						map.put("result", result);
						map.put("times", times);
						map.put("busiType", busiType);
						logger.info(JsonUtil.objectToJson(map));
					}                                                                                                                                  
				}
			}
		}
	}

	
}
