package com.chinacreator.process.service;

import com.chinacreator.common.exception.BusinessException;
import com.chinacreator.process.bean.*;
import com.chinacreator.process.dao.*;
import com.chinacreator.process.util.*;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@Component
public class TencentVacOrderService {

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

    private Logger log = Logger.getLogger(TencentVacOrderService.class);

    @Autowired
    private VacSyncOrderDao vacSyncOrderDao;

    @Autowired
    private NetOrderDao netOrderDao;

    @Autowired
    private WhiteDao whiteDao;

    @Autowired
    private ActivityConfigDao activityConfigDao;

    @Autowired
    private VacActiveDao vacActiveDao;

    @Autowired
    private PreOrderDao preOrderDao;

    @Autowired
    private DictionaryDao dictionaryDao;

    public int handle(VacOrderBean vacOrderBean) {
        String errorcode = "0";
        String errorinfo="";
        NetOrderBean netOrderBean = null;
        int savelog = 0;
        try{
            netOrderBean = getNetOrderInfo(vacOrderBean);
            check(vacOrderBean);
            if (vacOrderBean.getOrderstatus().equals("0")) {
                savelog = order(netOrderBean, vacOrderBean);
            } else if (vacOrderBean.getOrderstatus().equals("1")) {
                cancel(netOrderBean, vacOrderBean);
            }
        }catch(Exception e){
            if (e instanceof BusinessException) {
                errorcode = ((BusinessException) e).getCode();
                errorinfo=e.getMessage();
            }else{
                errorcode = "8000";
                errorinfo="系统错误";
            }
            log.error(vacOrderBean.getUserid(),e);
        }finally {
            try {
                if(savelog == 0){
                    saveNetLog(netOrderBean, errorcode, errorinfo);
                }
            } catch (SQLException e) {
                log.error(vacOrderBean.getUserid(),e);
            }
            Map<String, String> map = new HashMap<String, String>();
            map.put("userid", vacOrderBean.getUserid());
            map.put("orderstatus", vacOrderBean.getOrderstatus());
            map.put("ordertype", vacOrderBean.getOrdertype());
            if(netOrderBean != null){
                map.put("cpid", netOrderBean.getCpid());
                map.put("spid", netOrderBean.getSpid());
            }
            map.put("errorcode", errorcode);
            map.put("errorinfo", errorinfo);
            map.put("type", "0");
            logger.info(JsonUtil.objectToJson(map));
        }
        return errorcode.equals("0") ? 0 :1;
    }

    public void check(VacOrderBean vacOrderBean) throws SQLException, BusinessException {
        if (StringUtils.isNotEmpty(vacOrderBean.getProvince()) && vacOrderBean.getOrderstatus().equals("0")) {
            if (vacOrderBean.getOrdertype().equals("1")) {// 0元
                if (vacSyncOrderDao.canOrder(vacOrderBean.getProvince(), "8000652500")) {
                    throw new BusinessException("9069", vacOrderBean.getProvince() + "省份被限制");
                }
            } else if (vacOrderBean.getOrdertype().equals("2")) {// 20包月
                if (vacSyncOrderDao.canOrder(vacOrderBean.getProvince(), "8000656500")) {
                    throw new BusinessException("9069", vacOrderBean.getProvince() + "省份被限制");
                }
            } else if (vacOrderBean.getOrdertype().equals("0")) {// 20元按次
                if (vacSyncOrderDao.canOrder(vacOrderBean.getProvince(), "8000650900")) {
                    throw new BusinessException("9069", vacOrderBean.getProvince() + "省份被限制");
                }
            }
        }
    }

    public int order(NetOrderBean netOrderBean, VacOrderBean vacOrderBean) throws Exception {
        boolean ispre = false;

        if("84".equals(netOrderBean.getSpid())){
            throw new BusinessException("9719", "84产品订购不处理", new String[0]);
        }

        if (vacOrderBean.getOrdertype().equals("0")) {// 20元按次
            NetOrderBean orderBeanfor101 = this.netOrderDao.findOrder(netOrderBean.getCpid(), "101",
                    netOrderBean.getUserid());
            NetOrderBean orderBeanfor84 = this.netOrderDao.findOrder(netOrderBean.getCpid(), netOrderBean.getSpid(),
                    netOrderBean.getUserid());
            if (orderBeanfor101 != null && orderBeanfor84 == null) {// 有包月订购关系，20元按次请求
                if (orderBeanfor101.getStatus().equals("0")) {
                    throw new BusinessException("9710", "重复订购", new String[0]);
                } else if (orderBeanfor101.getStatus().equals("1")) {
                    throw new BusinessException("9711", "已有有效包月订购，按次不处理", new String[0]);
                }else if(orderBeanfor101.getStatus().equals("2")){
                    if (NumberUtils.toLong(orderBeanfor101.getCanceltimestr()) > NumberUtils
                            .toLong(netOrderBean.getOrdertimestr())) {
                        throw new BusinessException("9902", "vac订购时间早于本地退订时间", new String[0]);
                    }
                }
            } else if(orderBeanfor101 == null && orderBeanfor84 != null){// 无包月订购关系，20元按次请求
                if (orderBeanfor84.getStatus().equals("0")) {
                    throw new BusinessException("9901", "vac重复订购", new String[0]);
                } else if (orderBeanfor84.getStatus().equals("1") || orderBeanfor84.getStatus().equals("2")) {
                    if (NumberUtils.toLong(orderBeanfor84.getCanceltimestr()) > NumberUtils
                            .toLong(netOrderBean.getOrdertimestr())) {
                        throw new BusinessException("9902", "vac订购时间晚于本地退订时间", new String[0]);
                    }
                }
            }else if(orderBeanfor101 != null && orderBeanfor84 != null){//既有腾讯20元包月，也有20元按次
                if(orderBeanfor84.getStatus().equals("2") && orderBeanfor101.getStatus().equals("0")){
                    throw new BusinessException("9710", "重复订购", new String[0]);
                }else if(orderBeanfor84.getStatus().equals("2") && orderBeanfor101.getStatus().equals("1")){
                    throw new BusinessException("9711", "已有有效包月订购，按次不处理", new String[0]);
                }else if(orderBeanfor84.getStatus().equals("2") && orderBeanfor101.getStatus().equals("2")){
                    if (NumberUtils.toLong(orderBeanfor101.getCanceltimestr()) > NumberUtils
                            .toLong(netOrderBean.getOrdertimestr()) || NumberUtils.toLong(orderBeanfor84.getCanceltimestr()) > NumberUtils
                            .toLong(netOrderBean.getOrdertimestr())) {
                        throw new BusinessException("9902", "vac订购时间早于本地退订时间", new String[0]);
                    }
                }else if(orderBeanfor84.getStatus().equals("0") && orderBeanfor101.getStatus().equals("2")){
                    throw new BusinessException("9901", "vac重复订购", new String[0]);
                }else if(orderBeanfor84.getStatus().equals("1") && orderBeanfor101.getStatus().equals("2")){
                    if (NumberUtils.toLong(orderBeanfor84.getCanceltimestr()) > NumberUtils
                            .toLong(netOrderBean.getOrdertimestr())) {
                        throw new BusinessException("9902", "vac订购时间晚于本地退订时间", new String[0]);
                    }
                }else {
                    throw new BusinessException("8001", "数据异常", new String[0]);
                }
            }
        } else if (vacOrderBean.getOrdertype().equals("1") || vacOrderBean.getOrdertype().equals("2")) {// 包月订购请求
            if(vacOrderBean.getOrdertype().equals("1")){
                throw new BusinessException("9718", "0元预受理产品不订购", new String[0]);
            }
            NetOrderBean orderBeanfor84 = this.netOrderDao.findOrder(netOrderBean.getCpid(),"84",
                    netOrderBean.getUserid());
            NetOrderBean orderBeanfor101 = this.netOrderDao.findOrder(netOrderBean.getCpid(), "101",
                    netOrderBean.getUserid());
            if (orderBeanfor84 != null && orderBeanfor101 == null) {// 有按次的订购关系，包月订购请求
                if (orderBeanfor84.getStatus().equals("0")) {
                    throw new BusinessException("9713", "已有按次订购，0元包月重复订购", new String[0]);
                } else if (orderBeanfor84.getStatus().equals("1")) {// 生成预定购关系
                    if (NumberUtils.toLong(orderBeanfor84.getCanceltimestr()) > NumberUtils
                            .toLong(netOrderBean.getOrdertimestr())) {
                        throw new BusinessException("9902", "vac包月订购时间早于按次本地退订时间", new String[0]);
                    }
                    ispre = true;
                    preOrder(netOrderBean);
                }else if (orderBeanfor84.getStatus().equals("2")){
                    if (NumberUtils.toLong(orderBeanfor84.getCanceltimestr()) > NumberUtils
                            .toLong(netOrderBean.getOrdertimestr())) {
                        throw new BusinessException("9902", "vac订购时间早于本地退订时间", new String[0]);
                    }
                }
            } else if(orderBeanfor84 == null && orderBeanfor101 != null){// 无按次的订购关系，包月订购请求
                if (orderBeanfor101.getStatus().equals("0")) {
                    throw new BusinessException("9714", "已包月订购，重复订购", new String[0]);
                } else if (orderBeanfor101.getStatus().equals("1") || orderBeanfor101.getStatus().equals("2")) {
                    if (NumberUtils.toLong(orderBeanfor101.getCanceltimestr()) > NumberUtils
                            .toLong(netOrderBean.getOrdertimestr())) {
                        throw new BusinessException("9902", "vac订购时间早于本地退订时间", new String[0]);
                    }
                }
            }else if(orderBeanfor84 != null && orderBeanfor101 != null){
                if(orderBeanfor84.getStatus().equals("2")   && !orderBeanfor101.getStatus().equals("2")){
                    if (orderBeanfor101.getStatus().equals("0")) {
                        throw new BusinessException("9714", "已包月订购，重复订购", new String[0]);
                    } else if (orderBeanfor101.getStatus().equals("1")) {
                        if (NumberUtils.toLong(orderBeanfor101.getCanceltimestr()) > NumberUtils
                                .toLong(netOrderBean.getOrdertimestr())) {
                            throw new BusinessException("9902", "vac订购时间早于本地退订时间", new String[0]);
                        }
                    }
                }else if(orderBeanfor84.getStatus().equals("2")  && orderBeanfor101.getStatus().equals("2")){
                    if (NumberUtils.toLong(orderBeanfor101.getCanceltimestr()) > NumberUtils
                            .toLong(netOrderBean.getOrdertimestr()) || NumberUtils.toLong(orderBeanfor84.getCanceltimestr()) > NumberUtils
                            .toLong(netOrderBean.getOrdertimestr())) {
                        throw new BusinessException("9902", "包月订购时间早于本地退订时间", new String[0]);
                    }
                }else if(!orderBeanfor84.getStatus().equals("2")  && orderBeanfor101.getStatus().equals("2")){
                    if (orderBeanfor84.getStatus().equals("0")) {
                        throw new BusinessException("9713", "已有按次订购，0元包月重复订购", new String[0]);
                    } else if (orderBeanfor84.getStatus().equals("1")) {// 生成预定购关系
                        if (NumberUtils.toLong(orderBeanfor84.getCanceltimestr()) > NumberUtils
                                .toLong(netOrderBean.getOrdertimestr())) {
                            throw new BusinessException("9902", "vac包月订购时间早于按次本地退订时间", new String[0]);
                        }
                        ispre = true;
                        preOrder(netOrderBean);
                    }else if (orderBeanfor84.getStatus().equals("2")){
                        if (NumberUtils.toLong(orderBeanfor84.getCanceltimestr()) > NumberUtils
                                .toLong(netOrderBean.getOrdertimestr())) {
                            throw new BusinessException("9902", "vac订购时间早于本地退订时间", new String[0]);
                        }
                    }
                }else {
                    throw new BusinessException("8001", "数据异常", new String[0]);
                }
            }
        }
        if (!ispre) {
            if (vacOrderBean.getOrdertype().equals("1")) {
                EcbUtils.queryEcb(netOrderBean.getCpid(), netOrderBean.getSpid(), netOrderBean.getProvince(),
                        netOrderBean.getUserid());
                vac(netOrderBean, "1");
            }
            netOrderDao.order(netOrderBean);
            inserSmstMq(netOrderBean, "0");
            activite(netOrderBean);
            if(vacOrderBean.getOrdertype().equals("0") && vacOrderBean.getOrderstatus().equals("0")){
                inserOrderMq(netOrderBean,"0");
            }
        }
        return ispre ? 1 :0;
    }

    public void cancel(NetOrderBean netOrderBean, VacOrderBean vacOrderBean) throws Exception {
        boolean ispre = false;
        boolean needvaccancel = false;
        NetOrderBean orderBeanfor84 = this.netOrderDao.findOrder(netOrderBean.getCpid(), "84",
                netOrderBean.getUserid());
        NetOrderBean orderBeanfor101 = this.netOrderDao.findOrder(netOrderBean.getCpid(), "101",
                netOrderBean.getUserid());
        if ((orderBeanfor84 == null || orderBeanfor84.getStatus().equals("2")) && (orderBeanfor101 == null || orderBeanfor101.getStatus().equals("2"))) {
            throw new BusinessException("9716", "退订请求，无订购不处理", new String[0]);
        }
        if((orderBeanfor84 != null && !orderBeanfor84.getStatus().equals("2")) ){
            if(orderBeanfor84.getStatus().equals("0")){
                if(vacOrderBean.getOrdertype().equals("2")){
                    preCancel(netOrderBean,1);
                }
                netOrderBean.setSpid("84");
                if (NumberUtils.toLong(orderBeanfor84.getOrdertimestr()) >= NumberUtils
                        .toLong(netOrderBean.getCanceltimestr())) {
                    throw new BusinessException("9902", "vac退订时间早于本地订购时间", new String[0]);
                }
                needvaccancel =true;
            }else{
                ispre = true;
                preCancel(netOrderBean,0);
            }
        }else{
            if(orderBeanfor101 != null && !orderBeanfor101.getStatus().equals("2")){
                if(orderBeanfor101.getStatus().equals("0")){
                    if (NumberUtils.toLong(orderBeanfor101.getOrdertimestr()) >= NumberUtils
                            .toLong(netOrderBean.getCanceltimestr())) {
                        throw new BusinessException("9902", "vac退订时间早于本地订购时间", new String[0]);
                    }
                }else{
                    throw new BusinessException("9902", "本地已退订", new String[0]);
                }
            }
        }
        logger.info("没进入");
        if(!ispre){
            if(vacOrderBean.getOrdertype().equals("1")){
                logger.info("走cap退订===="+vacOrderBean);
                cancel(netOrderBean);    //退  资费id   101
            }
            logger.info("本地退订");
            netOrderDao.cancelOrder(netOrderBean);
            cancelActivite(netOrderBean);
            inserSmstMq(netOrderBean, "1");
            if(vacOrderBean.getOrdertype().equals("2") && needvaccancel){
                inserOrderMq(netOrderBean,"1");
            }
        }
    }
    /**
     * 退订
     * @param netOrderBean
     * @return
     * @throws Exception
     */
    public String cancel(NetOrderBean netOrderBean) throws Exception{
        String result = "-1";
        String errorinfo = "cap退订失败";
        if (!whiteDao.isWhiteUser(netOrderBean.getUserid(), "tencent")) {
            String resJson = CapUtil.cap(netOrderBean.getCpid(),netOrderBean.getSpid(),netOrderBean.getUserid(), "88226", "8000656500", "1",
                    "", "", "", "");
            logger.info("调AP返回结果："+resJson);
            if (resJson != null) {
                Map<?,?> map = JsonUtil.jsonToMap(resJson);
                result = map.get("resultcode") != null ? map.get("resultcode").toString() : "-1";
                errorinfo = map.get("errorinfo") != null ? map.get("errorinfo").toString() : "cap退订失败";
            }
        }
        /**
         业务返回编码
         0000 处理成功
         0007 订单同步失败
         0009 参数异常
         0008 非4G用户,鉴权失败
         0001 订购鉴权失败/退订鉴权失败
         0004 话单缓存失败
         0003 查询用户归属省编码失败
         0002 cbss产品订购异常
         0006 cbss产品退订异常
         */
        if (!result.equals("0")) {
            if(result.equals("-1")){
                throw new BusinessException(result, "cap退订失败:接口调用失败，服务暂不可用", new String[0]);
            }else{
                //errorinfo = handelErrorinfo(result,errorinfo);
                throw new BusinessException(result, "cap退订失败:"+errorinfo, new String[0]);

            }
        }
        return result;
    }

    public void vac(NetOrderBean netOrderBean, String type) throws Exception {
        String result = "0";
        try {
            if (!whiteDao.isWhiteUser(netOrderBean.getUserid(), "tencent")) {
                result = VacUtil.vac(netOrderBean.getUserid(), "88226", "8000656500", type);
                if (result.equals("1200") && type.equals("1")) {
                    result = "0";
                } else if (result.equals("1201") && type.equals("2")) {
                    result = "0";
                }
            }
        } catch (Exception e) {
            throw new BusinessException("9715", "vac操作异常", new String[0]);
        }
        if (!result.equals("0")) {
            throw new BusinessException(result, "vac计费失败", new String[0]);
        }
    }

    public void preOrder(NetOrderBean netOrderBean) throws Exception {
        String errorcode = "0";
        String errorinfo = "";
        try{
            NetOrderBean preOrderBean = preOrderDao.existUser(netOrderBean.getUserid(), netOrderBean.getCpid(),
                    netOrderBean.getSpid());
            if (preOrderBean != null && preOrderBean.getStatus().equals("0") && netOrderBean.getStatus().equals("0")) {
                throw new BusinessException("9905", "腾讯20元产品，已预订购", new String[0]);
            }
            if (preOrderBean != null && preOrderBean.getStatus().equals("1") && netOrderBean.getStatus().equals("0")
                    && NumberUtils.toLong(netOrderBean.getOrdertimestr()) < NumberUtils
                    .toLong(preOrderBean.getCanceltimestr())) {
                throw new BusinessException("9907", "腾讯20元产品，预订购晚于本地预退订", new String[0]);
            }
            preOrderDao.order(netOrderBean);
        }catch(Exception e){
            if (e instanceof BusinessException) {
                errorcode = ((BusinessException) e).getCode();
                errorinfo=e.getMessage();
            }else{
                errorcode = "8000";
                errorinfo="系统错误";
            }
            throw e;
        }finally {
            netOrderBean.setOrderchannel("vacpreorder");
            saveNetLog(netOrderBean, errorcode, errorinfo);
            netOrderBean.setOrderchannel("vac");
            Map<String, String> map = new HashMap<String, String>();
            map.put("userid", netOrderBean.getUserid());
            if(netOrderBean != null){
                map.put("cpid", netOrderBean.getCpid());
                map.put("spid", netOrderBean.getSpid());
            }
            map.put("errorcode", errorcode);
            map.put("errorinfo", errorinfo);
            map.put("type", "2");
            logger.info(JsonUtil.objectToJson(map));
        }
    }

    public void preCancel(NetOrderBean netOrderBean,int isgo) throws Exception {
        String errorcode = "0";
        String errorinfo = "";
        try{
            NetOrderBean preOrderBean = preOrderDao.existUser(netOrderBean.getUserid(), netOrderBean.getCpid(),
                    netOrderBean.getSpid());
            if(preOrderBean == null){
                throw new BusinessException("9905", "无预订购，不能退订", new String[0]);
            }
            if (preOrderBean != null && preOrderBean.getStatus().equals("1") && netOrderBean.getStatus().equals("1")) {
                throw new BusinessException("9905", "腾讯20元产品，已预退订", new String[0]);
            }
            if (preOrderBean != null && preOrderBean.getStatus().equals("0") && netOrderBean.getStatus().equals("1")
                    && NumberUtils.toLong(netOrderBean.getCanceltimestr()) < NumberUtils
                    .toLong(preOrderBean.getOrdertimestr())) {
                throw new BusinessException("9907", "腾讯20元产品，预退订早于本地预订购", new String[0]);
            }
            preOrderDao.cancel(netOrderBean);
        }catch(Exception e){
            if (e instanceof BusinessException) {
                errorcode = ((BusinessException) e).getCode();
                errorinfo=e.getMessage();
            }else{
                errorcode = "8000";
                errorinfo="系统错误";
            }
            if(isgo ==0){
                throw e;
            }
        }finally {
            netOrderBean.setCancelchannel("vacprecancel");
            saveNetLog(netOrderBean, errorcode, errorinfo);
            netOrderBean.setCancelchannel("vac");
            Map<String, String> map = new HashMap<String, String>();
            map.put("userid", netOrderBean.getUserid());
            if(netOrderBean != null){
                map.put("cpid", netOrderBean.getCpid());
                map.put("spid", netOrderBean.getSpid());
            }
            map.put("errorcode", errorcode);
            map.put("errorinfo", errorinfo);
            map.put("type", "1");
            logger.info(JsonUtil.objectToJson(map));
        }

    }

    public NetOrderBean getNetOrderInfo(VacOrderBean vacOrderBean) throws Exception {
        NetOrderBean netOrderBean = new NetOrderBean();
        netOrderBean.setUserid(vacOrderBean.getUserid());
        netOrderBean.setCpid(vacOrderBean.getCpid());
        netOrderBean.setSpid(vacOrderBean.getSpid());
        netOrderBean.setProvince(vacOrderBean.getProvince());
        netOrderBean.setArea(vacOrderBean.getArea());
        netOrderBean.setChannel2(vacOrderBean.getChannel2());
        netOrderBean.setStaffid(vacOrderBean.getStaffid());
        netOrderBean.setDepartid(vacOrderBean.getDepartid());
        if (vacOrderBean.getOrderstatus().equals("0")) {
            netOrderBean.setStatus("0");
            netOrderBean
                    .setOrdertime(DateUtils.parseDate(vacOrderBean.getOrdertime(), new String[] { "yyyyMMddHHmmss" }));
            if("vacnewfile".equals(vacOrderBean.getChannel()) || "vac".equals(vacOrderBean.getChannel())){
                //从vac反向通知推送过来的数据（VacOrderJob）
                netOrderBean.setOrderchannel("vac");
            }else{
                //FTP手厅反向通知推送过来的数据（ChannelOrderJob），使用原渠道信息
                netOrderBean.setOrderchannel(vacOrderBean.getChannel());
            }

            netOrderBean.setOrderstatus(3);
        } else {
            netOrderBean.setStatus("1");
            netOrderBean.setCanceltime(
                    DateUtils.parseDate(vacOrderBean.getCanceltime(), new String[] { "yyyyMMddHHmmss" }));
            if("vacnewfile".equals(vacOrderBean.getChannel()) || "vac".equals(vacOrderBean.getChannel())){
                netOrderBean.setCancelchannel("vac");
            }else{
                //FTP手厅反向通知推送过来的数据（ChannelOrderJob），使用原渠道信息
                netOrderBean.setCancelchannel(vacOrderBean.getChannel());
            }

            netOrderBean.setOrderstatus(5);
        }
        return netOrderBean;
    }

    public void activite(NetOrderBean netOrderBean) throws Exception {
        ActivityConfigBean activityConfigBean = activityConfigDao.findByCallerhead(netOrderBean.getCpid(),
                netOrderBean.getSpid(), "");
        if (activityConfigBean == null) {
            throw new BusinessException("9030", "非活动产品或者活动结束");
        }
        OrderBean oldbean = vacActiveDao.existUser(netOrderBean.getUserid(), netOrderBean.getCpid(),
                netOrderBean.getSpid(),netOrderBean.getSpid().equals("101") ? "11" : "6");
        if (oldbean == null) {
            vacActiveDao.insertBeans(getBusBean(netOrderBean, null));
        } else if (oldbean.getOrderstatus().equals("0")) {
            throw new BusinessException("9031", "腾讯20元产品,活动表已订购");
        } else {
            if (NumberUtils.toLong(oldbean.getCanceltime()) > NumberUtils.toLong(netOrderBean.getOrdertimestr())) {
                throw new BusinessException("9032", "腾讯20元产品,活动表退订>订购");
            }
            netOrderBean.setId(oldbean.getId());
            vacActiveDao.update(getBusBean(netOrderBean, oldbean));
        }
    }

    public void cancelActivite(NetOrderBean netOrderBean) throws Exception {
        ActivityConfigBean activityConfigBean = activityConfigDao.findByCallerhead(netOrderBean.getCpid(),
                netOrderBean.getSpid(), "");
        if (activityConfigBean == null) {
            throw new BusinessException("9030", "非活动产品或者活动结束");
        }
        OrderBean oldbean = vacActiveDao.existUser(netOrderBean.getUserid(), netOrderBean.getCpid(),netOrderBean.getSpid(),
                netOrderBean.getSpid().equals("101") ? "11" : "6");
        if (oldbean == null) {
            throw new BusinessException("9035", "腾讯20元产品,活动表无订购");
        } else if (oldbean.getOrderstatus().equals("1")) {
            throw new BusinessException("9031", "腾讯20元产品,活动表已退订");
        } else {
            if (NumberUtils.toLong(oldbean.getOrdertime()) > NumberUtils.toLong(netOrderBean.getCanceltimestr())) {
                throw new BusinessException("9032", "腾讯20元产品,活动表订购>退订");
            }
            netOrderBean.setId(oldbean.getId());
            vacActiveDao.cancelUpdate(getBusBean(netOrderBean, null));
        }
    }

    public OrderBean getBusBean(NetOrderBean netOrderBean, OrderBean oldbean) {
        OrderBean bean = new OrderBean();
        bean.setId(netOrderBean.getId());
        bean.setUserid(netOrderBean.getUserid());
        bean.setCpid(netOrderBean.getCpid());
        bean.setSpid(netOrderBean.getSpid());
        bean.setProvince(netOrderBean.getProvince());
        bean.setArea(netOrderBean.getArea());
        bean.setOrdertime(netOrderBean.getOrdertimestr());
        bean.setChannel(netOrderBean.getOrderchannel());
        bean.setCanceltime(netOrderBean.getCanceltimestr());
        if(netOrderBean.getSpid().equals("101")){
            bean.setActiveType(11);
        }else{
            bean.setActiveType(6);
        }
        if (oldbean != null) {
            bean.setVipstatus(oldbean.getVipstatus());
            bean.setViptime(oldbean.getViptime());
        }
        return bean;
    }

    public void inserOrderMq(NetOrderBean netOrderBean,String type) {
        try {
            Map<String, String> map = new HashMap<String, String>();
            map.put("userid", netOrderBean.getUserid());
            map.put("cpid", netOrderBean.getCpid());
            map.put("spid", netOrderBean.getSpid());
            if (type.equals("0")) {
                map.put("status", "0");
                map.put("ordertime",netOrderBean.getOrdertimestr());
                map.put("channel", netOrderBean.getOrderchannel());
            } else {
                map.put("status", "1");
                map.put("canceltime", netOrderBean.getCanceltimestr());
                map.put("channel", netOrderBean.getCancelchannel());
            }
            map.put("type", "vacorder");
            //URLUtil.post("http://10.199.99.144:8090/mq-service/recive.do", JsonUtil.objectToJson(map))
            URLUtil.post(dictionaryDao.getValue("mqReciveUrl"), JsonUtil.objectToJson(map));
//			URLUtil.post("http://10.199.99.158:8030/mq-service/recive.do", JsonUtil.objectToJson(map));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void inserSmstMq(NetOrderBean netOrderBean, String type) {
        try {
            if (netOrderBean.getCpid().equals("tencent")) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("userid", netOrderBean.getUserid());
                map.put("cpid", netOrderBean.getCpid());
                map.put("spid", netOrderBean.getSpid());
                map.put("result", "0");
                map.put("channel", "");
                if (type.equals("0")) {
                    map.put("style", "7");
                    map.put("busiType", "tran_succ");
                } else {
                    map.put("style", "8");
                    map.put("busiType", "cancel_succ");
                }
                map.put("times", "");
                map.put("orderType", "");
                map.put("type", "cssms");
                map.put("staffid", netOrderBean.getStaffid()); //工号
                //URLUtil.post("http://10.199.99.144:8090/mq-service/recive.do", JsonUtil.objectToJson(map));
                URLUtil.post(dictionaryDao.getValue("mqReciveUrl"), JsonUtil.objectToJson(map));
//				URLUtil.post("http://10.199.99.158:8030/mq-service/recive.do", JsonUtil.objectToJson(map));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void saveNetLog(NetOrderBean orderInfo, String errorcode, String errorinfo)
            throws SQLException {
        OrderLog orderLog = new OrderLog();
        orderLog.setApptype(orderInfo.getApptype());
        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);
        if (orderInfo.getStatus().equals("0")) {
            orderLog.setChannel(orderInfo.getOrderchannel());
        } else {
            orderLog.setChannel(orderInfo.getCancelchannel());
        }
        this.netOrderDao.addOrderLog(orderLog);
    }
}