package com.chinacreator.process.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinacreator.common.exception.BusinessException;
import com.chinacreator.common.util.MD5;
import com.chinacreator.common.util.URLUtil;
import com.chinacreator.process.bean.*;
import com.chinacreator.process.dao.*;
import com.chinacreator.process.exception.VideoException;
import com.chinacreator.process.util.DesUtil;
import com.chinacreator.process.util.HttpInvoke;
import com.chinacreator.process.util.JsonUtil;
import com.chinacreator.video.queue.MessageService;
import com.chinacreator.video.queue.bean.MessagePipe;
import com.google.gson.Gson;
import net.sf.json.JSONArray;
import net.sf.json.JSONSerializer;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class ChannelOrderService {

    Logger logger = LoggerFactory.getLogger(ChannelOrderService.class);

    @Autowired
    private CsmakeupDao csmakeupDao;

    @Autowired
    private SdkOrderDao sdkOrderDao;

    @Autowired
    private NetOrderDao netOrderDao;

    @Autowired
    private CallerAreacodeDao callerAreacodeDao;

    @Autowired
    private SysDao sysDao;

    @Autowired
    private ChannelOrderDao channelOrderDao;

    @Autowired
    private OrderConfigDao orderConfigDao;

    @Autowired
    private MessageService messageService;

    @Autowired
    private ChangshiDao changshiDao;

    @Autowired
    private CsOrderControlDao csOrderControlDao;

    @Autowired
    private EcbProductDao ecbProductDao;

    @Autowired
    private WxcsDao wxcsDao;

    @Autowired
    private EcbDiscntDao ecbDiscntDao;

    @Autowired
    private ChangshiBlackDao changshiBlackDao;

    @Autowired
    private DictionaryDao dictionaryDao;

    @Autowired
    private SPDao spDao;
    
    @Autowired
    private BusiOperlimitConfDao busiOperlimitConfDao;
    
    @Autowired
    private NextMonthEffectService nextMonthEffectService;

    public int doLocalData(ChannelOrderBean orderBean) {
        int result = 1;
        try {
            //北京号码段订购限制
            String numberlimit = dictionaryDao.getValue("Numberlimit");
            if(numberlimit != null && numberlimit.indexOf(orderBean.getUserid().substring(0, 7)) != -1){
                this.channelOrderDao.updatestatus(orderBean.getId(), "9010");
                throw new BusinessException("9010", "用户为黑名单用户", new String[0]);
            }

            OrderConfigBean orderConfigBean = getOrderConfigBean(orderBean.getProductid());
            if (orderConfigBean == null) {
                this.channelOrderDao.updatestatus(orderBean.getId(), "2");
                throw new BusinessException("9015", "该产品不处理", new String[0]);
            }
            
            //验证业务操作限制（是否能订购，是否能退订）
            if(this.valiOperLimit(orderBean.getId(), orderConfigBean.getSpid(), orderBean.getUpdatetype())){
                this.channelOrderDao.updatestatus(orderBean.getId(), "9016", "业务办理限制，不处理");
                throw new BusinessException("9016", orderBean.getId()+"业务办理限制，不处理", new String[0]);
            }
            
            if ("1006".equals(orderConfigBean.getSpid()) && 0 == this.channelOrderDao.isOrder(orderConfigBean.getCpid(), "1009", orderBean.getUserid())) {
                this.channelOrderDao.updatestatus(orderBean.getId(), "4");
                throw new BusinessException("9001", "订购失败，芒果TV16元产品互斥,订购了合约产品", new String[0]);
            }

            getProvinceInfo(orderBean);
            if (orderBean.getUpdatetype().equals("1")) {
                String provinceOrder = sdkOrderDao.getProvinceOrder(orderConfigBean.getSpid());
                if (StringUtils.isNotEmpty(provinceOrder)){
                    String mobileInfo = getMobileInfo(orderBean.getUserid());
                    if (!provinceOrder.contains(mobileInfo)){
                        throw new VideoException("1", "订购失败，该"+mobileInfo+"地区不能订购此产品");
                    }
                }
                if (orderConfigBean.getType().equals("1")) {
                    if (orderConfigBean.getCpid().equals("tencent")
                            && (orderConfigBean.getSpid().equals("101") || orderConfigBean.getSpid().equals("84"))) {
                        result = inserttencentMq(orderBean, orderConfigBean, "0");
                    } else {
                        result = compareOrderForNet(orderBean);
                        if(result == 0){//成功发短信
                            insertSmsMq1(orderBean.getUserid(), "bss", orderConfigBean, "1",orderBean);
                        }
                    }
                } else {
                    result = compareOrderForSdk(orderBean);
                }
            } else {
                if (orderConfigBean.getType().equals("1")) {
                    if (orderConfigBean.getCpid().equals("tencent")
                            && (orderConfigBean.getSpid().equals("101") || orderConfigBean.getSpid().equals("84"))) {
                        result =inserttencentMq(orderBean, orderConfigBean, "1");
                    } else{
                        result = compareCancelForNet(orderBean);
                        if(result == 0){//成功发短信
                            insertSmsMq1(orderBean.getUserid(), "bss", orderConfigBean, "2",orderBean);
                        }
                    }
                } else {
                    result = compareCancelForSdk(orderBean);
                }
            }
            if (result == 0) {
                //insertMq(orderBean, orderConfigBean);
                insertMqOffline(orderBean, orderConfigBean);
                if (orderConfigBean.getCpid().equals("changshi") && orderConfigBean.getSpid().equals("1150")
                        && orderBean.getUpdatetype().equals("1")) {
                    WxcsBean wxcsBean = wxcsDao.getConfig(orderConfigBean.getCpid(), orderConfigBean.getSpid());
                    if (wxcsBean.getVipstatus().equals("0")
                            && NumberUtils.toLong(DateFormatUtils.format(new Date(), "yyyyMMddHHmmss")) < NumberUtils
                            .toLong(wxcsBean.getVipendtime())
                            && NumberUtils.toLong(DateFormatUtils.format(new Date(), "yyyyMMddHHmmss")) > NumberUtils
                            .toLong(wxcsBean.getVipstarttime())) {
                        insertSmsMq(orderBean.getUserid(), "ecb", orderConfigBean);
                    }
                }
                //pushMusic(orderBean,orderConfigBean);
                //调拓维C+接口，20200428，xu.zhou
                //tuoweiInvoke(orderBean);
            }
        } catch (Exception e) {
            result = 8000;
            e.printStackTrace();
            logger.error("同步订购关系出现异常，"+e.getMessage());
        }
        return result;
    }
    /**
     * 查询号码信息
     * @param userid
     * @return
     * @throws Exception
     */
    public String  getMobileInfo(String userid){
        String provice= null;
        try {
            provice= sdkOrderDao.getHmdInfo(userid.substring(0,7));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return provice;
    }

    public int inserttencentMq(ChannelOrderBean orderBean, OrderConfigBean orderConfigBean,String type) {
        try {
            Map<String, String> map = new HashMap<String, String>();
            String url = dictionaryDao.getValue("mqReciveUrl");
            map.put("userid", orderBean.getUserid());
            map.put("cpid", orderConfigBean.getCpid());
            map.put("spid", orderConfigBean.getSpid());
            map.put("province", orderBean.getProvince());
            map.put("area", orderBean.getArea());
            map.put("channel", this.convertChannel(orderBean));
            if (type.equals("0")) {
                //map.put("channel", "dqftp");
                map.put("ordertime", orderBean.getOrdertime());
            } else {
                //map.put("channel", "dqftp");
                map.put("canceltime", orderBean.getCanceltime());
            }
            map.put("endtime", "");
            if(orderConfigBean.getSpid().equals("101")){
                map.put("ordertype", "2");
            }else{
                map.put("ordertype", "0");
            }
            map.put("orderstatus", type);
            map.put("type", "vacmq");
            //测试环境
            //URLUtil.post("http://10.199.99.177:8030/mq-service/recive.do", JSONSerializer.toJSON(map).toString(),1000);
            //生产环境
            URLUtil.post(url, JSONSerializer.toJSON(map).toString(),1000);
//			URLUtil.post("http://10.199.99.144:8090/mq-service/recive.do", JSONSerializer.toJSON(map).toString(),1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.channelOrderDao.updatestatus(orderBean.getId(), "3");

        /****
         * 腾讯20元,手厅渠道覆盖
         */
        try {
            NetOrderBean newOrderBean = getNetOrderInfo(orderBean);
            NetOrderBean oldOrderBean = this.netOrderDao.findOrder(newOrderBean.getCpid(), newOrderBean.getSpid(), newOrderBean.getUserid());
            if(oldOrderBean != null){
                if("0".equals(type)){
                    /*****************************************************手厅订购，进行订购渠道更新*************************************************************/
                    if ("0".equals(oldOrderBean.getStatus())) {
                        if("sjyyt".equals(newOrderBean.getOrderchannel())){
                            try {
                                //订购渠道优先级更新
                                this.netOrderDao.orderUpdate(newOrderBean);
                                saveNetLog(newOrderBean, "9098", "[手厅订购渠道覆盖]已订购，订购渠道更新为sjyyt", orderBean.getUpdatetype());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }else{
                        if("sjyyt".equals(newOrderBean.getOrderchannel())){
                            try {
                                if(newOrderBean.getOrdertime().getTime() < oldOrderBean.getCanceltime().getTime()){
                                    //订购渠道优先级更新
                                    this.netOrderDao.orderUpdate(newOrderBean);
                                    saveNetLog(newOrderBean, "9096", "[手厅订购渠道覆盖]已退订，且手厅订购时间小于退订时间，订购渠道更新为sjyyt", orderBean.getUpdatetype());
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    /*****************************************************手厅订购，进行订购渠道更新*************************************************************/
                }else{
                    /*****************************************************手厅退订，进行退订渠道更新*************************************************************/
                    if (!"0".equals(oldOrderBean.getStatus())) {
                        if("sjyyt".equals(newOrderBean.getCancelchannel()) ){
                            try {
                                //退订渠道优先级更新
                                this.netOrderDao.cancelUpdate(newOrderBean);
                                saveNetLog(newOrderBean, "9099", "[手厅退订渠道覆盖]已退订，退订渠道更新为sjyyt", orderBean.getUpdatetype());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }else{
                        if("sjyyt".equals(newOrderBean.getCancelchannel()) ){
                            try {
                                if(newOrderBean.getCanceltime().getTime() < oldOrderBean.getOrdertime().getTime()
                                        && oldOrderBean.getCancelchannel()!=null && !"".equals(oldOrderBean.getCancelchannel().trim())){
                                    //退订渠道优先级更新
                                    this.netOrderDao.cancelUpdate(newOrderBean);
                                    saveNetLog(newOrderBean, "9097", "[手厅退订渠道覆盖]已订购，且手厅退订时间小于订购时间，退订渠道更新为sjyyt", orderBean.getUpdatetype());
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    /*****************************************************手厅退订，进行退订渠道更新*************************************************************/
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 3;
    }

    public void insertSmsMq(String userid, String channel, OrderConfigBean orderConfigBean) {
        MessagePipe mpipe = new MessagePipe();
        mpipe.setHeader("cssms");
        mpipe.addBody("userid", userid);
        mpipe.addBody("channel", channel);
        mpipe.addBody("cpid", orderConfigBean.getCpid());
        mpipe.addBody("spid", orderConfigBean.getSpid());
        mpipe.addBody("style", "5");
        mpipe.addBody("result", "");
        mpipe.addBody("times", "");
        mpipe.addBody("orderType", "0");
        this.messageService.sendMessage(mpipe);
    }

    public void insertSmsMq1(String userid, String channel, OrderConfigBean orderConfigBean, String updateType,ChannelOrderBean orderbean) {
        try {
			/*
			if ((orderConfigBean.getCpid().equals("aiqy") && orderConfigBean.getSpid().equals("1018"))
					|| (orderConfigBean.getCpid().equals("mangtv16") && orderConfigBean.getSpid().equals("1006"))
					|| (orderConfigBean.getCpid().equals("leshilt") && orderConfigBean.getSpid().equals("954"))) {*/
            //1150的另外处理，保持不动
            if ((!orderConfigBean.getCpid().equals("changshi") && !orderConfigBean.getSpid().equals("1150")) || "1218".equals(orderConfigBean.getSpid()) ){
                Map<String, String> map = new HashMap<String, String>();
                String url = dictionaryDao.getValue("mqReciveUrl");
                String spid = orderConfigBean.getSpid();
                map.put("userid", userid);
                map.put("cpid", orderConfigBean.getCpid());
                map.put("spid", orderConfigBean.getSpid());
                map.put("result", "0");
                map.put("channel", "");
				/*
				if (orderConfigBean.getSpid().equals("1018") && updateType.equals("1")) {
					map.put("style", "16");
				} else if (orderConfigBean.getSpid().equals("1018") && updateType.equals("2")) {
					map.put("style", "45");
				} else if (orderConfigBean.getSpid().equals("954") && updateType.equals("1")) {
					map.put("style", "20");
				} else if (orderConfigBean.getSpid().equals("954") && updateType.equals("2")) {
					map.put("style", "21");
				} else if (orderConfigBean.getSpid().equals("1006") && updateType.equals("1")) {
					map.put("style", "22");
				} else if (orderConfigBean.getSpid().equals("1006") && updateType.equals("2")) {
					map.put("style", "23");
				}
				*/
                map.put("times", "");
                map.put("orderType", "");
                map.put("type", "cssms");
                if("fhcp".equals(orderConfigBean.getCpid())&&"99134246".equals(orderConfigBean.getSpid())){
                    map.put("param1", timehandel(channelOrderDao.getFhcpEndtime(userid,orderConfigBean.getCpid(),orderConfigBean.getSpid())));
                }
                //20220507修改，针对云南省订购成功短信保持原来的订购成功短信。其他省份订购成功短信短信下发全部是改为：
                //快手12G流量月包省分           spid=  297
                //畅视多视频共享15G流量月包省分  spid= 1218 活动，192908845733和192908129979 渠道 订购成功短信调整为新模板
                String busiType = "";
                boolean flag = false;
                //1.先判断省份
                String province = getMobileInfo(userid);
                //2.获取活动配置
                HashMap activitymap = channelOrderDao.getCouponConfig(spid);
                if(activitymap!=null && !"云南".equals(province) && ("297".equals(spid) || "1218".equals(spid) || "299".equals(spid))){
                    String activitystarttime = activitymap.get("STARTTIME").toString();
                    String activityendtime = activitymap.get("ENDTIME").toString();
                    //订购时间小于活动开始时间，或者订购时间大于活动结束时间
                    String ordertime = orderbean.getOrdertime();
                    if(Long.parseLong(ordertime) < Long.parseLong(activitystarttime)
                            || Long.parseLong(ordertime) > Long.parseLong(activityendtime)){
                        System.out.println("订购时间未在【517电信】活动期内，不下发活动短信");
                    }else{
                        //发送短信
                        busiType = "ftp_tran_succ4";
                        System.out.println("下发【517】活动券码短信");
                        flag = true;
                    }
                }
                if(!flag) {
                    //业务类型，默认是办理成功
                    busiType = "ftp_tran_succ";
                }
                if("2".equals(updateType)){//退订
                    busiType = "ftp_cancel_succ"; //退订成功
                }
                map.put("busiType", busiType);
                map.put("style","0000");
                URLUtil.post(url, JSONSerializer.toJSON(map).toString(),

//				URLUtil.post("http://10.199.99.144:8090/mq-service/recive.do", JSONSerializer.toJSON(map).toString(),
                        1000);
                //20211214 修改 新春活动 1214~0228 活动期间内的1017,979,1005产品发送短信
                //20220412 下线，更改配置化，指定产品可发送短信
                if(!"2".equals(updateType)) {//订购下发
//                    Boolean hasSendCouponSms = channelOrderDao.hasSendCouponSms(userid,orderConfigBean.getSpid());
                    List<String> sendsmsProduct = channelOrderDao.querySendSmsProduct();
                    if(sendsmsProduct!=null && sendsmsProduct.contains(orderConfigBean.getSpid())){
                        System.out.println("下发订购后的定制短信");
//                    if(hasSendCouponSms) {
                        map.put("busiType", "ftp_tran_succ3");
                        URLUtil.post(url, JSONSerializer.toJSON(map).toString(), 1000);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String timehandel(Date time){

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
        return sdf.format(time);
    }

    public void getProvinceInfo(ChannelOrderBean bean) {
        AreaInfo areaInfo = this.callerAreacodeDao.findByCallerhead(bean.getUserid().substring(0, 7));
        if (areaInfo == null) {
            bean.setProvince("");
            bean.setArea("");
        } else {
            bean.setProvince(areaInfo.getProvince());
            bean.setArea(areaInfo.getArea());
        }
    }

    public void insertUpdateChannelMq(String userid, String channel, OrderConfigBean orderConfigBean) {
        MessagePipe mpipe = new MessagePipe();
        mpipe.setHeader("channelupdate");
        mpipe.addBody("userid", userid);
        mpipe.addBody("channel", channel);
        mpipe.addBody("cpid", orderConfigBean.getCpid());
        mpipe.addBody("spid", orderConfigBean.getSpid());
        this.messageService.sendMessage(mpipe);
    }

    /***
    public void insertMq(ChannelOrderBean bean, OrderConfigBean configBean) {
        try{
            MessagePipe mpipe = new MessagePipe();
            mpipe.setHeader("order");
            mpipe.addBody("userid", bean.getUserid());
            mpipe.addBody("province", bean.getProvince());
            mpipe.addBody("area", bean.getArea());
            if (bean.getUpdatetype().equals("1")) {
                mpipe.addBody("orderType", "0");
                mpipe.addBody("updateTime", bean.getOrdertime());
            } else {
                mpipe.addBody("orderType", "1");
                mpipe.addBody("updateTime", bean.getCanceltime());
            }
            mpipe.addBody("cpid", configBean.getCpid());
            mpipe.addBody("spid", configBean.getSpid());
            if (bean.getType().equals("1"))
                mpipe.addBody("channel", "spfbtx9");
            else if (bean.getType().equals("2"))
                mpipe.addBody("channel", "spfbtx4");
            else if (bean.getType().equals("3"))
                mpipe.addBody("channel", "spfbtx10");
            else if (bean.getType().equals("4"))
                mpipe.addBody("channel", "spfbtx6");
            else if (bean.getType().equals("5"))
                mpipe.addBody("channel", "spfbtx7");
            else if (bean.getType().equals("6"))
                mpipe.addBody("channel", "spfbtx14");
            else if (bean.getType().equals("7"))
                mpipe.addBody("channel", "spfbtx15");
            else if (bean.getType().equals("8"))
                mpipe.addBody("channel", "spfbtx16");
            else if (bean.getType().equals("9")) {
                mpipe.addBody("channel", "spfbtx17");
            } else if (bean.getType().equals("11")) {
                mpipe.addBody("channel", "csssy");
            }
            mpipe.addBody("activated", "2");
            this.messageService.sendMessage(mpipe);
        }catch(Exception e){
            logger.error("推送队列出现异常，"+e.getMessage());
        }
        //优酷送双倍会员需求
        this.inserYoutuVipMq(bean, configBean);
    }
	***/

    public int compareOrderForNet(ChannelOrderBean bean) throws Exception {
        int result = 0;
        NetOrderBean orderBean = null;
        NetOrderBean oldOrderBean = null;
        try {
            orderBean = getNetOrderInfo(bean);
            if( changshiBlackDao.isBlackUser(orderBean.getUserid(),orderBean.getCpid())){
                throw new BusinessException("9080", "黑名单用户不处理订购", new String[0]);
            }
            
            boolean hasnme = false; //是否次月生效
            if(nextMonthEffectService.hasNextMonthBusi(orderBean.getSpid())){
            	bean.setCpid(orderBean.getCpid());
            	bean.setSpid(orderBean.getSpid());
            	hasnme = true;
            	nextMonthEffectService.process(orderBean);
            	//设置同步状态值，用于判断是否生成活动关系
            	bean.setSyncstatus(orderBean.getSyncstatus());
            }else{
	            oldOrderBean = this.netOrderDao.findOrder(orderBean.getCpid(), orderBean.getSpid(), orderBean.getUserid());
	            if (oldOrderBean != null) {
	                //沃视频爱奇艺定向流量年包产品限制为当月不能重复订购
	                if("99134246".equals(orderBean.getSpid()) && valiMonth(orderBean.getOrdertime(),oldOrderBean.getOrdertime())){
	                    throw new BusinessException("9088", "订购失败，反向通知的订购时间小于或等于订购关系表的订购时间", new String[0]);
	                }
	
	                /*****************************************************手厅订购，进行订购渠道更新*************************************************************/
	                if ("0".equals(oldOrderBean.getStatus())) {
	                    if("sjyyt".equals(orderBean.getOrderchannel())){
	                        try {
	                            //订购渠道优先级更新
	                            this.netOrderDao.orderUpdate(orderBean);
	                        } catch (Exception e) {
	                            e.printStackTrace();
	                        }
	                        throw new BusinessException("9098", "[手厅订购渠道覆盖]已订购，订购渠道更新为sjyyt", new String[0]);
	                    }
	
	                    throw new BusinessException("9010", "已订购，请不要重复订购", new String[0]);
	                }else if("1".equals(oldOrderBean.getStatus()) && oldOrderBean.getSpid().equals("99134246")){
	                    orderBean.setEndtime(channelOrderDao.getLastYearDay(oldOrderBean.getEndtime()));
	                }else if("2".equals(oldOrderBean.getStatus()) && oldOrderBean.getSpid().equals("99134246")){
	                    orderBean.setEndtime(channelOrderDao.getLastYearDayFirst(orderBean.getOrdertime()));
	                }else{
	                    if("sjyyt".equals(orderBean.getOrderchannel())){
	                        try {
	                            if(orderBean.getOrdertime().getTime() < oldOrderBean.getCanceltime().getTime()){
	                                //订购渠道优先级更新
	                                this.netOrderDao.orderUpdate(orderBean);
	
	                                saveNetLog(orderBean, "9096", "[手厅订购渠道覆盖]已退订，且手厅订购时间小于退订时间，订购渠道更新为sjyyt", bean.getUpdatetype());
	                            }
	                        } catch (Exception e) {
	                            e.printStackTrace();
	                        }
	                    }
	                }
	                /*****************************************************手厅订购，进行订购渠道更新*************************************************************/
	
	                if (orderBean.getCpid().equals("changshi") && ("1150".equals(orderBean.getSpid()) || "1040".equals(orderBean.getSpid()))){
	                    if (orderBean.getSpid().equals("1040")) {
	                        throw new BusinessException("9017", "标清订购不处理", new String[0]);
	                    }
	                    String discntValue = orderBean.getSpid().equals("1150") ? "1" : "0";
	                    EcbProductInfo ecbProductInfo = ecbProductDao.queryEcb(bean.getId());
	                    List<DiscntInfo> list = changshiDao.queryDiscntInfo(orderBean.getUserid(), discntValue);
	                    for (DiscntInfo discntInfo : list) {
	                        if (discntInfo.getType().equals("0")
	                                && !discntInfo.getDiscntid().equals(ecbProductInfo.getDiscntCode())) {
	                            throw new BusinessException("9011", "不同资费有未退的订购请求，不处理", new String[0]);
	                        }
	                    }
	                    if (orderBean.getSpid().equals("1150")) {
	                        if (!csOrderControlDao.discntCodeControl(ecbProductInfo.getDiscntCode(), orderBean.getCpid(),
	                                orderBean.getSpid())) {
	                            throw new BusinessException("9903", "不在资费白名单内", new String[0]);
	                        }
	                        if (csOrderControlDao.discntFeeControl(ecbProductInfo.getDiscntCode(), orderBean.getCpid(),
	                                orderBean.getSpid())) {
	                            result = 1;
	                        }
	                        //20220104xu.zhou，只有1150的才走ecbDiscntDao.order，防止其他cpid为changshi的数据落不了订购关系
	                        ecbDiscntDao.order(ecbProductInfo, orderBean);
	                    }
	                    //ecbDiscntDao.order(ecbProductInfo, orderBean);
	                } else if (NumberUtils.toLong(bean.getOrdertime()) < NumberUtils
	                        .toLong(oldOrderBean.getCanceltimestr())) {
	                    throw new BusinessException("9023", "订购时间晚于本地退订时间不处理", new String[0]);
	                }
	                this.netOrderDao.order(orderBean);
	            } else {
	
	                if("99134246".equals(orderBean.getSpid())){
	                    orderBean.setEndtime(channelOrderDao.getLastYearDayFirst(orderBean.getOrdertime()));
	                }
	                if (orderBean.getCpid().equals("changshi") && orderBean.getSpid().equals("1150")) {
	                    EcbProductInfo ecbProductInfo = ecbProductDao.queryEcb(bean.getId());
	                    if (!csOrderControlDao.discntCodeControl(ecbProductInfo.getDiscntCode(), orderBean.getCpid(),
	                            orderBean.getSpid())) {
	                        throw new BusinessException("9903", "不在资费白名单内", new String[0]);
	                    }
	                    if (csOrderControlDao.discntFeeControl(ecbProductInfo.getDiscntCode(), orderBean.getCpid(),
	                            orderBean.getSpid())) {
	                        result = 1;
	                    }
	                    ecbDiscntDao.order(ecbProductInfo, orderBean);
	                } else if (orderBean.getCpid().equals("changshi") && orderBean.getSpid().equals("1040")) {
	                    throw new BusinessException("9017", "标清订购不处理", new String[0]);
	                    // EcbProductInfo ecbProductInfo =
	                    // ecbProductDao.queryEcb(bean.getId());
	                    // ecbDiscntDao.order(ecbProductInfo, orderBean);
	                }
	                this.netOrderDao.order(orderBean);
	            }
            }
            //不是能力平台次月生效产品，或者是能力平台产品且是正式订购关系表退订操作
            if(!hasnme || (hasnme && "5".equals(orderBean.getSyncstatus()))){
            	saveNetLog(orderBean, "0", "", bean.getUpdatetype());
            }
            if(hasnme){
            	this.channelOrderDao.updatestatus(bean.getId(), "0", "次月生效数据订购完成");
            }else{
            	this.channelOrderDao.updatestatus(bean.getId(), "0", "ok");
            }   
        } catch (Exception e) {
            result = 1;
            result = this.catchOper(e, bean, oldOrderBean);
        }
        return result;
    }

    /**
     * 验证订购月份是否与已订购月份相同
     * @param newOrdertime
     * @param oldOrdertime
     * @return 相同返回true，反之返回false
     */
    private boolean valiMonth(Date newOrdertime, Date oldOrdertime){
        boolean res = false;
        String newmonth = DateFormatUtils.format(newOrdertime, "yyyyMM");
        String oldmonth = DateFormatUtils.format(oldOrdertime, "yyyyMM");
        if(newmonth.equals(oldmonth)){
            res = true;
        }
        if(!res && Integer.parseInt(newmonth) < Integer.parseInt(oldmonth)){
            res = true;
        }
        return res;
    }
    
    /**
     * 退订
     * @param bean
     * @return
     * @throws Exception
     */
    public int compareCancelForNet(ChannelOrderBean bean) throws Exception {
        int result = 0;
        NetOrderBean orderBean = null;
        NetOrderBean oldOrderBean = null;
        try {
            orderBean = getNetOrderInfo(bean);
            if("99134246".equals(orderBean.getSpid())){
                throw new BusinessException("9098", "此产品不支持退订！", new String[0]);
            }
            if(changshiBlackDao.isBlackUser(orderBean.getUserid(),orderBean.getCpid())){
                throw new BusinessException("9081", "黑名单用户不处理退订", new String[0]);
            }
            
            boolean hasnme = false; //是否次月生效
            //能力平台次月生效产品处理流程
            if(nextMonthEffectService.hasNextMonthBusi(orderBean.getSpid())){
            	bean.setCpid(orderBean.getCpid());//用于推送活动关系时使用
            	bean.setSpid(orderBean.getSpid());//用于推送活动关系时使用
            	hasnme = true;
            	nextMonthEffectService.process(orderBean);
            	//设置同步状态值，用于判断是否生成活动关系
            	bean.setSyncstatus(orderBean.getSyncstatus());
            }else{
	            oldOrderBean = this.netOrderDao.findOrder(orderBean.getCpid(), orderBean.getSpid(), orderBean.getUserid());
	            if (oldOrderBean != null) {
	                /*****************************************************手厅退订，进行退订渠道更新*************************************************************/
	                if (!"0".equals(oldOrderBean.getStatus())) {
	                    if("sjyyt".equals(orderBean.getCancelchannel()) ){
	                        try {
	                            //退订渠道优先级更新
	                            this.netOrderDao.cancelUpdate(orderBean);
	                        } catch (Exception e) {
	                            e.printStackTrace();
	                        }
	                        throw new BusinessException("9099", "[手厅退订渠道覆盖]已退订，退订渠道更新为sjyyt", new String[0]);
	                    }
	                    throw new BusinessException("9006", "已退订，请不要重复退订", new String[0]);
	                }else{
	                    if("sjyyt".equals(orderBean.getCancelchannel()) ){
	                        try {
	                            if(orderBean.getCanceltime().getTime() < oldOrderBean.getOrdertime().getTime()
	                                    && oldOrderBean.getCancelchannel()!=null && !"".equals(oldOrderBean.getCancelchannel().trim())){
	                                //退订渠道优先级更新
	                                this.netOrderDao.cancelUpdate(orderBean);
	
	                                saveNetLog(orderBean, "9097", "[手厅退订渠道覆盖]已订购，且手厅退订时间小于订购时间，退订渠道更新为sjyyt", bean.getUpdatetype());
	                            }
	                        } catch (Exception e) {
	                            e.printStackTrace();
	                        }
	                    }
	                }
	                /*****************************************************手厅退订，进行退订渠道更新*************************************************************/
	                //只有1150和1040走ECB订购
	                if (orderBean.getCpid().equals("changshi") && ("1150".equals(orderBean.getSpid()) || "1040".equals(orderBean.getSpid()))){
	                       // && !"99144185".equals(orderBean.getSpid())
	                       // && !"99144186".equals(orderBean.getSpid())
	                       // && !"99144187".equals(orderBean.getSpid())) {
	                    String discntValue = orderBean.getSpid().equals("1150") ? "1" : "0";
	                    EcbProductInfo ecbProductInfo = ecbProductDao.queryEcb(bean.getId());
	                    List<DiscntInfo> list = changshiDao.queryDiscntInfo(orderBean.getUserid(), discntValue);
	                    for (DiscntInfo discntInfo : list) {
	                        if (discntInfo.getType().equals("0")
	                                && !discntInfo.getDiscntid().equals(ecbProductInfo.getDiscntCode())) {
	                            throw new BusinessException("9011", "不同资费有未退得订购请求，不处理", new String[0]);
	                        }
	                    }
	                    ecbDiscntDao.cancel(ecbProductInfo, orderBean);
	                } else if (NumberUtils.toLong(bean.getCanceltime()) < NumberUtils
	                        .toLong(oldOrderBean.getOrdertimestr())) {
	                    throw new BusinessException("9025", "退订时间晚于本地订购时间不处理", new String[0]);
	                }
	                this.netOrderDao.cancelOrder(orderBean);
	            } else {
	                throw new BusinessException("9026", "本地无订购", new String[0]);
	            }
            }
            //不是能力平台次月生效产品，或者是能力平台产品且是正式订购关系表退订操作
            if(!hasnme || (hasnme && "5".equals(orderBean.getSyncstatus()))){
            	saveNetLog(orderBean, "0", "", bean.getUpdatetype());
            }
            if(hasnme){
                this.channelOrderDao.updatestatus(bean.getId(), "0", "次月生效数据退订完成 ");
            }else{
                this.channelOrderDao.updatestatus(bean.getId(), "0", "ok");
            }
        } catch (Exception e) {       	
            result = 1;
            catchOper(e,bean,orderBean);
        }
        return result;
    }
    
    /**
     * 异常处理
     * @param e
     * @param bean
     * @param orderBean
     * @return
     */
    private int catchOper(Exception e, ChannelOrderBean bean, NetOrderBean orderBean){
    	int result = 1;
        if ((e instanceof BusinessException)) {
        	try {
                //this.channelOrderDao.updatestatus(bean.getId(), ((BusinessException) e).getCode());
            	String resultInfo = ((BusinessException) e).getMessage();
            	String resultcode = ((BusinessException) e).getCode();
            	if(!StringUtils.isEmpty(resultInfo) && resultInfo.length()>500){
            		resultInfo = resultInfo.substring(0, 500);
            	}
            	logger.info("resultInfo="+resultInfo+", resultcode="+resultcode);
                this.channelOrderDao.updatestatus(bean.getId(), resultcode, resultInfo);
			} catch (Exception e2) {
				e.printStackTrace();
				logger.error(bean.getId()+"ChannelOrderService更新数据出现异常"+e.getMessage());
			}
        	try {
        		 saveNetLog(orderBean, ((BusinessException) e).getCode(), e.getMessage(), bean.getUpdatetype());
			} catch (Exception e2) {
				e.printStackTrace();
				logger.error(bean.getId()+"ChannelOrderService写日志出现异常"+e.getMessage());
			}

            result = Integer.valueOf(((BusinessException) e).getCode()).intValue();
        } else {
            e.printStackTrace();
        	try {
                //this.channelOrderDao.updatestatus(bean.getId(), "8000");
            	String resultInfo = e.getMessage();
            	String resultcode = "8000";
            	if(!StringUtils.isEmpty(resultInfo) && resultInfo.length()>500){
            		resultInfo = resultInfo.substring(0, 500);
            	}
                this.channelOrderDao.updatestatus(bean.getId(), resultcode, resultInfo);
			} catch (Exception e2) {
				e.printStackTrace();
				logger.error(bean.getId()+"ChannelOrderService更新数据出现异常"+e.getMessage());
			}
        	try {
        		saveNetLog(orderBean, "8000", "系统忙！", bean.getUpdatetype());
			} catch (Exception e2) {
				e.printStackTrace();
				logger.error(bean.getId()+"ChannelOrderService写日志出现异常"+e.getMessage());
			}
        }
        return result;
    }

    public int compareOrderForSdk(ChannelOrderBean bean) throws Exception {
        int result = 0;
        SDKOrderInfo orderInfo = null;
        SdkOrderBean oldOrderBean = null;
        try {
            orderInfo = getSdkOrderInfo(bean);
            oldOrderBean = this.sdkOrderDao.queryOrderList(orderInfo.getUserid(), orderInfo.getCpid(),
                    orderInfo.getProductId());
            if (oldOrderBean != null) {
                String status = oldOrderBean.getStatus();
                if (status.equals("0")) {
                    throw new BusinessException("9010", "已订购，请不要重复订购", new String[0]);
                }

                if (NumberUtils.toLong(bean.getOrdertime()) < NumberUtils.toLong(oldOrderBean.getCanceltime())) {
                    throw new BusinessException("9023", "订购时间晚于本地退订时间不处理", new String[0]);
                }
                orderInfo.setId(oldOrderBean.getId());
                this.sdkOrderDao.order(orderInfo, true);
            } else {
                orderInfo.setId(this.sysDao.getID());
                this.sdkOrderDao.order(orderInfo, false);
            }
            saveSdkLog(orderInfo, "0", "");
            this.channelOrderDao.updatestatus(bean.getId(), "0");
        } catch (Exception e) {
            result = 1;
            e.printStackTrace();
            if ((e instanceof BusinessException)) {
                this.channelOrderDao.updatestatus(bean.getId(), ((BusinessException) e).getCode());
                saveSdkLog(orderInfo, ((BusinessException) e).getCode(), e.getMessage());
            } else {
                this.channelOrderDao.updatestatus(bean.getId(), "8000");
                saveSdkLog(orderInfo, "8000", "系统忙！");
            }
        }
        return result;
    }

    public int compareCancelForSdk(ChannelOrderBean bean) {
        int result = 0;
        SDKOrderInfo orderInfo = null;
        SdkOrderBean oldOrderBean = null;
        try {
            orderInfo = getSdkOrderInfo(bean);
            oldOrderBean = this.sdkOrderDao.queryOrderList(orderInfo.getUserid(), orderInfo.getCpid(),
                    orderInfo.getProductId());

            if (oldOrderBean != null) {
                orderInfo.setId(oldOrderBean.getId());
                String status = oldOrderBean.getStatus();
                if (!status.equals("0")) {
                    throw new BusinessException("9006", "已退订，请不要重复退订", new String[0]);
                }

                if (NumberUtils.toLong(bean.getCanceltime()) < NumberUtils.toLong(oldOrderBean.getOrdertime())) {
                    throw new BusinessException("9025", "退订时间晚于本地订购时间不处理", new String[0]);
                }
                this.sdkOrderDao.cancelOrder(orderInfo);
            } else {
                throw new BusinessException("9026", "退订失败，本地无订购", new String[0]);
            }
            saveSdkLog(orderInfo, "0", "");
            this.channelOrderDao.updatestatus(bean.getId(), "0");
        } catch (Exception e) {
            result = 1;
            e.printStackTrace();
            if ((e instanceof BusinessException)) {
                this.channelOrderDao.updatestatus(bean.getId(), ((BusinessException) e).getCode());
                saveSdkLog(orderInfo, ((BusinessException) e).getCode(), e.getMessage());
            } else {
                this.channelOrderDao.updatestatus(bean.getId(), "8000");
                saveSdkLog(orderInfo, "8000", "系统忙！");
            }
        }
        return result;
    }

    public String getDateStr(String str) throws ParseException {
        return DateFormatUtils.format(DateUtils.parseDate(str, new String[] { "yyyyMMddHHmmss" }), "yyyyMMdd");
    }

    public void saveSdkLog(SDKOrderInfo orderInfo, String errorcode, String errorinfo) {
        this.sdkOrderDao.insertOrderLog(orderInfo, errorcode, errorinfo, "");
    }

    public void saveNetLog(NetOrderBean orderInfo, String errorcode, String errorinfo, String ordertype)
            throws SQLException {
        OrderLog orderLog = new OrderLog();
        orderLog.setApptype(orderInfo.getApptype());
        orderLog.setChannel(ordertype.equals("1") ? orderInfo.getOrderchannel() : orderInfo.getCancelchannel());
        orderLog.setOrderstatus(orderInfo.getOrderstatus());
        orderLog.setStatus(Integer.valueOf(orderInfo.getStatus()).intValue());
        orderLog.setCpid(orderInfo.getCpid());
        orderLog.setIsexperience(orderInfo.getIsexperience());
        orderLog.setOrdertype(orderInfo.getOrdertype());
        orderLog.setProvince(orderInfo.getProvince());
        orderLog.setArea(orderInfo.getArea());
        orderLog.setSpid(orderInfo.getSpid());
        orderLog.setUserid(orderInfo.getUserid());
        orderLog.setErrorcode(errorcode);
        orderLog.setErrorinfo(errorinfo);
        this.netOrderDao.addOrderLog(orderLog);
    }

    public SDKOrderInfo getSdkOrderInfo(ChannelOrderBean bean) throws Exception {
        OrderConfigBean orderConfigBean = getOrderConfigBean(bean.getProductid());
        SDKOrderInfo orderInfo = new SDKOrderInfo();
        orderInfo.setUserid(bean.getUserid());
        orderInfo.setCpid(orderConfigBean.getCpid());
        orderInfo.setProductId(orderConfigBean.getSpid());
        orderInfo.setAppid("40000001");
        orderInfo.setSectionId(orderConfigBean.getSpid());
        orderInfo.setProvince(bean.getProvince());
        bean.setArea(bean.getArea());
        orderInfo.setIsExperience(0);
        orderInfo.setType(1);
        orderInfo.setAppType("");
        if (bean.getUpdatetype().equals("1")) {
            if (StringUtils.isEmpty(bean.getOrdertime())) {
                throw new BusinessException("9012", "参数错误", new String[0]);
            }
            orderInfo.setOrderTime(DateUtils.parseDate(bean.getOrdertime(), new String[] { "yyyyMMddHHmmss" }));
            orderInfo.setOrdertimestr(bean.getOrdertime());
            orderInfo.setOrderChannel(this.convertChannel(bean));
            orderInfo.setStatus("0");
            orderInfo.setOrderStatus(3);
        } else {
            if (StringUtils.isEmpty(bean.getCanceltime())) {
                throw new BusinessException("9012", "参数错误", new String[0]);
            }
            orderInfo.setCancelTime(DateUtils.parseDate(bean.getCanceltime(), new String[] { "yyyyMMddHHmmss" }));
            orderInfo.setCanceltimestr(bean.getCanceltime());
            orderInfo.setCancelChannel(this.convertChannel(bean));
            orderInfo.setStatus("1");
            orderInfo.setOrderStatus(5);
        }
        return orderInfo;
    }

    public NetOrderBean getNetOrderInfo(ChannelOrderBean bean) throws Exception {
        OrderConfigBean orderConfigBean = getOrderConfigBean(bean.getProductid());
        NetOrderBean netOrderBean = new NetOrderBean();
        netOrderBean.setUserid(bean.getUserid());
        netOrderBean.setCpid(orderConfigBean.getCpid());
        netOrderBean.setBatchid(DateFormatUtils.format(new Date(), "yyyyMMdd"));
        netOrderBean.setSpid(orderConfigBean.getSpid());
        netOrderBean.setProvince(bean.getProvince());
        netOrderBean.setArea(bean.getArea());
        if (bean.getUpdatetype().equals("1")) {
            if (StringUtils.isEmpty(bean.getOrdertime())) {
                throw new BusinessException("9012", "参数错误", new String[0]);
            }
            netOrderBean.setOrdertime(DateUtils.parseDate(bean.getOrdertime(), new String[] { "yyyyMMddHHmmss" }));
            netOrderBean.setOrderchannel(this.convertChannel(bean));
            netOrderBean.setStatus("0");
            netOrderBean.setOrderstatus(3);
        } else {
            if (StringUtils.isEmpty(bean.getCanceltime())) {
                throw new BusinessException("9012", "参数错误", new String[0]);
            }
            netOrderBean.setCanceltime(DateUtils.parseDate(bean.getCanceltime(), new String[] { "yyyyMMddHHmmss" }));
            netOrderBean.setCancelchannel(this.convertChannel(bean));
            netOrderBean.setStatus("1");
            netOrderBean.setOrderstatus(5);
        }
        
        /**
         * 计费点相同的产品SPID替换
         */
        nextMonthEffectService.replaceSameVacSpid(netOrderBean);
        
        /**
         * 次月生效产品SPID替换
         */
        nextMonthEffectService.replaceSpid(netOrderBean);
        
        //验证业务操作限制（是否能订购，是否能退订）
        if(this.valiOperLimit(bean.getId(), netOrderBean.getSpid(), bean.getUpdatetype())){
            //this.channelOrderDao.updatestatus(orderBean.getId(), "9016", "业务办理限制，不处理");
            throw new BusinessException("9016", "业务办理限制，不处理", new String[0]);
        }
        
        return netOrderBean;
    }
    
    public OrderConfigBean getOrderConfigBean(String vacproductid) throws SQLException {
        return this.orderConfigDao.findByProductid(vacproductid);
    }

    /**
     * 转换渠道名称
     * @param bean
     * @return
     */
    private String convertChannel(ChannelOrderBean bean){
        String channel = "dqftp";
        if (bean.getType().equals("1"))
            channel = "pftp";
        else if (bean.getType().equals("2"))
            channel = "dftp";
        else if (bean.getType().equals("3"))
            channel = "xxh";
        else if (bean.getType().equals("4"))
            channel = "cbss";
        else if (bean.getType().equals("5"))
            channel = "bss";
        else if (bean.getType().equals("6"))
            channel = "sjyyt";
        else if (bean.getType().equals("7"))
            channel = "wsyyt";
        else if (bean.getType().equals("8"))
            channel = "dxyyt";
        else if (bean.getType().equals("9")) {
            channel = "zzzd";
        } else if (bean.getType().equals("11")) {
            channel = "chuangf";
        }
        return channel;
    }

    public static void main(String[] args) {
        System.out.println(DateFormatUtils.format(new Date(), "yyyyMMdd"));
    }

    /*
     * 赠送会员超时异步队列推送
     * @param orderInfo
     */
    /***
    public void inserYoutuVipMq(ChannelOrderBean bean, OrderConfigBean configBean){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String currtime = sdf.format(new Date()); //当前时间
            String youtuVipEndtime = dictionaryDao.getValue("youtuVipEndtime");
            if(youtuVipEndtime == null || "".equals(youtuVipEndtime)){
                System.out.println("优酷订购赠送双会员无结束时间配置");
                return;
            }

            if(Long.parseLong(currtime) > Long.parseLong(youtuVipEndtime)){
                System.out.println("优酷订购赠送双会员活动已结束");
                return;
            }
            if (bean.getUpdatetype().equals("1") && "youtu".equals(configBean.getCpid()) && "1167".equals(configBean.getSpid())) {
                MessagePipe mpipe = new MessagePipe();
                mpipe.setHeader("youtuvip");
                mpipe.addBody("userid", bean.getUserid());
                mpipe.addBody("cpid",  configBean.getCpid());
                mpipe.addBody("spid",  configBean.getSpid());
                mpipe.addBody("sendcont","1");
                this.messageService.sendMessage(mpipe);
                System.out.println("优酷订购赠送双会员推送队列成功，userid=>"+bean.getUserid());
            }
        } catch (Exception e) {
            //e.printStackTrace();
            System.out.println("赠送会员超时异步重试队列推送出现异常，"+e.getMessage());
        }
    }
	***/

    /**
     * 20元沃派视频增加拓维C+接口
     * @param orderBean
     */
    /***
    private void tuoweiInvoke(ChannelOrderBean orderBean){
        boolean hasrec = false;
        long starttime =  System.currentTimeMillis();
        HashMap params = new HashMap();
        try {
            OrderConfigBean confBean = getOrderConfigBean(orderBean.getProductid());
            if(confBean == null) return;
            List<HashMap> confList = csmakeupDao.getBusiConf(confBean.getSpid());
            if(confList != null && confList.size()>0){
                hasrec = true;
                HashMap conf = confList.get(0);
                conf.put("SENO", csmakeupDao.getNo()); //获取业务流水号
                params.put("SENO", conf.get("SENO")); //获取业务流水号
                params.put("CHANNEL", "JOB-"+convertChannel(orderBean));
                params.put("USERID", orderBean.getUserid());
                params.put("PROVINCE", orderBean.getProvince());
                params.put("AREA", orderBean.getArea());
                params.put("VIPRECODE", "-1");
                params.put("VIPREINFO", "");
                params.put("RESULTCODE", "0");
                params.put("RESULTINFO", "ok");
                params.put("STATUS", "0");
                if("2".equals(orderBean.getUpdatetype())){//1 订购 2 退订
                    params.put("STATUS", "1");
                }
                params.put("CPID", conf.get("CPID"));
                params.put("MUSPID", conf.get("MUSPID"));
                String result = invokeTwvip(orderBean, conf); //调拓维会员接口返回结果
                if(!"".equals(result)){
                    params.put("VIPRECODE", JSON.parseObject(result).get("code").toString());
                    //params.put("VIPREINFO", JSON.parseObject(result).get("message").toString());
                    params.put("VIPREINFO", result);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("调拓维C+接口出现异常，"+e.getMessage());
            params.put("VIPRECODE", "8000");
            params.put("VIPREINFO", e.getMessage());
            params.put("RESULTCODE", "8000");
            params.put("RESULTINFO", "调拓维C+会员接口异常，"+e.getMessage());
        } finally {
            if(hasrec){
                try{
                    String vipreinfo = (String)params.get("VIPREINFO");
                    if(vipreinfo != null && vipreinfo.length()>500){
                        params.put("VIPREINFO", vipreinfo.subSequence(0, 450));
                    }
                    params.put("TIMES", System.currentTimeMillis()-starttime);
                    csmakeupDao.addOrderRec(params);
                }catch(Exception e){
                    e.printStackTrace();
                    logger.error("拓维C+接口添加订购记录出现异常，"+e.getMessage());
                }
            }
        }
    }
	***/
    
    /**
     * 调拓维会员接口
     * @param orderBean
     * @param conf
     * @return
     * @throws Exception
     */
    /***
    private String invokeTwvip(ChannelOrderBean orderBean, HashMap conf) throws Exception{
        String result = ""; //调拓维会员接口返回结果
        String vipurl = "";
        String jsonParams = "";
        if("2".equals(orderBean.getUpdatetype())){//退订
            vipurl = dictionaryDao.getValue("csmakeupvipcancelurl");
            jsonParams = this.fullCancelParams(orderBean.getUserid(), conf);
        }else{//订购
            vipurl = dictionaryDao.getValue("csmakeupviporderurl");
            jsonParams = this.fullOrderParams(orderBean.getUserid(),conf);
        }
        logger.info("vipurl=>"+vipurl+", jsonParams=>"+jsonParams);
        result = HttpInvoke.sendhttpsReq("POST", vipurl, jsonParams, getProperty(orderBean.getUpdatetype()));
        logger.info("调拓维会员接口返回结果："+result);
        //去空格、换行符号
        if(result != null) result = result.replaceAll("\r|\n", "").replaceAll("	", "").replaceAll(" ", "");

        //result = "{\"code\":0,\"message\":\"成功\",\"success\":true}";
        //result = "{\"code\":7007,\"message\":\"签名无效\",\"success\":false}";
        return result;
    }
	***/

    /**
     * 获取请求属性性
     * @return
     */
    /***
    private static Map getProperty(String updatetype){
        Map reqProperty = new HashMap();
        if("1".equals(updatetype)){//订购
            reqProperty.put("Content-type", "application/json;charset=utf-8");
        }else{//退订
            reqProperty.put("Content-type", "application/x-www-form-urlencoded");
        }

        return reqProperty;
    }
	***/
    
    /**
     * 填充退订会员参数
     * @param mobile
     * @param conf
     * @return
     * @throws Exception
     */
    /**
    private String fullCancelParams(String mobile, HashMap conf) throws Exception {
        String jsonParams = "";
        HashMap<String, String> params = new HashMap<String, String>();
        String timestamp = System.currentTimeMillis()+"";
        String spid = conf.get("VSPID").toString();
        String cpid = conf.get("VCPID").toString();
        String userType = "1";
        String password = conf.get("VPWD").toString();
        mobile = DesUtil.encode(mobile, password); //手机号码加密
        String signature = MD5.MD5Encode(spid + cpid + mobile + timestamp + userType + password);

        JSONObject json = new JSONObject();
        json.put("timestamp", timestamp);
        json.put("signature", signature);
        json.put("spid", spid);
        json.put("cpid", cpid);
        json.put("mobile", mobile);
        json.put("userType", userType);
        jsonParams = json.toJSONString();
        //去除json格式
        jsonParams = jsonParams.replaceAll(":", "=").replaceAll(",", "&")
                .replaceAll("\"", "").replaceAll("\\{", "").replaceAll("\\}", "");
        return jsonParams;
    }
	***/
    
    /**
     * 填充订购会员参数
     * @param mobile
     * @param conf
     * @return
     * @throws Exception
     */
    /***
    private String fullOrderParams(String mobile, HashMap conf)throws Exception {
        String jsonParams = "";
        String timestamp = System.currentTimeMillis()+"";
        String spid = conf.get("VSPID").toString();
        String cpid = conf.get("VCPID").toString();
        String memberUrl = "";
        String orderNumber = conf.get("SENO").toString();
        String password = conf.get("VPWD").toString();
        mobile = DesUtil.encode(mobile, password); //手机号码加密
        String signature = MD5.MD5Encode(spid + cpid + orderNumber + timestamp + password);

        JSONObject json = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(mobile);
        json.put("timestamp", timestamp);
        json.put("signature", signature);
        json.put("spid", spid);
        json.put("cpid", cpid);
        json.put("mobiles", jsonArray);
        json.put("memberUrl", memberUrl);
        json.put("orderNumber", orderNumber);
        jsonParams = json.toJSONString();

        return jsonParams;
    }
	***/
    
    /**
     * 音乐订购关系推送
     *
     * @param orderInfo
     */
    /**
    private void pushMusic(ChannelOrderBean orderInfo, OrderConfigBean capRevSyncOrder) {
        SpBean spBean =spDao.querySpByProductId(orderInfo.getProductid());
        logger.info("音乐公司==="+new Gson().toJson(orderInfo)+"===="+new Gson().toJson(capRevSyncOrder));
        if (spBean != null && "fhcp".equals(spBean.getCpid()) && ("99134250".equals(spBean.getSpid())||"99134256".equals(spBean.getSpid()))) {
            Gson gson = new Gson();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            SimpleDateFormat sdl=new SimpleDateFormat("yyyyMMddhhmmss");
            try {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("userid", orderInfo.getUserid());
                map.put("cpid", spBean.getCpid());
                map.put("spid", spBean.getSpid());
                if ("1".equals(orderInfo.getUpdatetype())) {
                    map.put("channel", this.convertChannel(orderInfo));
                    map.put("opertime", sdf.format(sdl.parse(orderInfo.getOrdertime())));
                    map.put("orderType", "0");
                    map.put("endTime", "");
                } else{
                    String dateStr = sdf.format(sdl.parse(orderInfo.getCanceltime()));
                    map.put("channel", this.convertChannel(orderInfo));
                    map.put("opertime",dateStr);
                    map.put("orderType", "1");
                    if (dateStr!=null&&dateStr.length()>7) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        int year = Integer.valueOf(dateStr.substring(0, 4));
                        int month = Integer.valueOf(dateStr.substring(5, 7));
                        Calendar calendar = Calendar.getInstance();
                        calendar.set(year,month,1);
                        calendar.add(Calendar.DATE,-1);
                        String endTime=format.format(calendar.getTime())+ " 23:59:59";
                        map.put("endTime", endTime);
                    } else{
                        map.put("endTime", "");
                    }
                }

                map.put("kafkaid", orderInfo.getId());
                map.put("type", "kafkaMusic");
                //测试环境
                String url = dictionaryDao.getValue("mqReciveUrl");
                System.out.println("音乐公司推送队列数据:" + orderInfo);
                URLUtil.post(url, gson.toJson(map), 1000 * 30);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("音乐公司推送队列出现异常，" + e.getMessage() + "， data：" + gson.toJson(orderInfo));
            }
        }
    }
    **/
    
    /**
     * 验证业务操作限制（是否能订购，是否能退订）
     * @param id
     * @param spid
     * @param updatetype 操作标识：1订购、 2退订
     * @return
     * @throws Exception
     */
    private boolean valiOperLimit(String id, String spid,String updatetype) throws Exception {
    	//数据来源
    	String datasrc = "SJYYT";
    	List<HashMap> dataList = busiOperlimitConfDao.getValue(spid);
		boolean haslimit = false;
        if(dataList != null && dataList.size()>0){   		
    		//转换为订购和退订标准标识，把1转为0，2转为1
    		updatetype = "1".equals(updatetype) ? "0" : ("2".equals(updatetype) ? "1":updatetype);
        	for(HashMap tm : dataList){
        		if(updatetype.equals(tm.get("OPERTYPE")) && datasrc.equals(tm.get("DATASRC"))){
        			haslimit = true;
        			break;
        		}
        	}     
        	//被限制抛异常
        	if(haslimit){
        		//throw new BusinessException("9016", id+"业务办理限制，不处理", new String[0]);
        	}
        }
        
        return haslimit;
    }
    
    /**
     * 生成领取会员的活动关系
     * @param bean
     * @param configBean
     */
    public void insertMqOffline(ChannelOrderBean bean, OrderConfigBean configBean) {
        try{
            //20220507增加能力平次月生效处理逻辑         
            //不是能力平台次月生效产品，或者是能力平台产品且是正式订购关系表退订操作
            if(nextMonthEffectService.hasNextMonthBusi(bean.getSpid())){
            	if("5".equals(bean.getSyncstatus())){
            		logger.info("能力平台正式表退订操作，推送到mq生成活动关系");
            	}else{
                	logger.info("能力平台非正式表退订操作，不推送到mq");
                	return;
            	}
            }
            MessagePipe mpipe = new MessagePipe();
            mpipe.setHeader("orderoffline");
            mpipe.addBody("userid", bean.getUserid());
            mpipe.addBody("province", bean.getProvince());
            mpipe.addBody("area", bean.getArea());
            if (bean.getUpdatetype().equals("1")) {
                mpipe.addBody("orderType", "0");
                mpipe.addBody("updateTime", bean.getOrdertime());
            } else {
                mpipe.addBody("orderType", "1");
                mpipe.addBody("updateTime", bean.getCanceltime());
            }
            mpipe.addBody("cpid", configBean.getCpid());
            mpipe.addBody("spid", configBean.getSpid());
            if (bean.getType().equals("1"))
                mpipe.addBody("channel", "spfbtx9");
            else if (bean.getType().equals("2"))
                mpipe.addBody("channel", "spfbtx4");
            else if (bean.getType().equals("3"))
                mpipe.addBody("channel", "spfbtx10");
            else if (bean.getType().equals("4"))
                mpipe.addBody("channel", "spfbtx6");
            else if (bean.getType().equals("5"))
                mpipe.addBody("channel", "spfbtx7");
            else if (bean.getType().equals("6"))
                mpipe.addBody("channel", "spfbtx14");
            else if (bean.getType().equals("7"))
                mpipe.addBody("channel", "spfbtx15");
            else if (bean.getType().equals("8"))
                mpipe.addBody("channel", "spfbtx16");
            else if (bean.getType().equals("9")) {
                mpipe.addBody("channel", "spfbtx17");
            } else if (bean.getType().equals("11")) {
                mpipe.addBody("channel", "csssy");
            }
            mpipe.addBody("activated", "2");
            this.messageService.sendMessage(mpipe);
        }catch(Exception e){
        	e.printStackTrace();
            logger.error("推送队列出现异常，"+e.getMessage());
        }
        //优酷送双倍会员需求
        //this.inserYoutuVipMq(bean, configBean);
    }
}
