package org.cs.mgr.order.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.cs.core.service.impl.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.ibm.wsdl.Constants;

import org.cs.util.DateUtil;
import org.cs.util.MathUtil;
import org.cs.util.Pager;
import org.cs.util.RandomUtil;
import org.cs.util.StringUtil;
import org.cs.mgr.order.service.IMgmOrderService;
import org.cs.mgr.product.dao.IMgmProductDao;
import org.cs.mgr.product.model.MgmProduct;
import org.cs.mgr.user.dao.IMgmCustomerDao;
import org.cs.mgr.user.dao.IMgmUserDao;
import org.cs.mgr.user.model.MgmCustomer;
import org.cs.mgr.user.model.MgmUser;
import org.cs.mgr.user.service.IMsgService;
import org.cs.mgr.user.service.ISmsService;
import org.cs.mgr.order.dao.IMgmOrderBreakDao;
import org.cs.mgr.order.dao.IMgmOrderCommissionDao;
import org.cs.mgr.order.dao.IMgmOrderDao;
import org.cs.mgr.order.dao.IMgmOrderProductDao;
import org.cs.mgr.order.dao.IMgmOrderScheduleDao;
import org.cs.mgr.order.dao.IMgmOrderTransferDao;
import org.cs.mgr.order.model.MgmOrder;
import org.cs.mgr.order.model.MgmOrderBreak;
import org.cs.mgr.order.model.MgmOrderCommission;
import org.cs.mgr.order.model.MgmOrderProduct;
import org.cs.mgr.order.model.MgmOrderSchedule;
import org.cs.mgr.order.model.MgmOrderTransfer;

/**
 * 
 * @ClassName: IMgmOrderService
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:29:02
 */
@Service("mgmOrderService")
public class MgmOrderService extends BaseService<MgmOrder> implements IMgmOrderService{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private IMgmOrderDao mgmOrderDao;
	@Autowired
	private  IMgmProductDao mgmProductDao;
	
	@Autowired
	private  IMgmOrderProductDao mgmOrderProductDao;
	
	@Autowired
	private  IMgmOrderScheduleDao mgmOrderScheduleDao;
			
	@Autowired
	private  IMgmUserDao mgmUserDao;
	
	@Autowired
	private  IMgmCustomerDao mgmCustomerDao;
	

	@Autowired
	private  IMgmOrderTransferDao mgmOrderTransferDao;
	
	
	@Autowired
	private  IMgmOrderBreakDao mgmOrderBreakDao;
	
	
	
	@Autowired
	private  IMgmOrderCommissionDao mgmOrderCommissionDao;
	
	@Autowired
	private  IMsgService msgService;
	
	
	@Override
	@Transactional(readOnly = true)
	public Pager getMgmOrders(Pager page){
		return this.mgmOrderDao.queryMgmOrders(page);
	}
	
	/**
	 * 自动生成
	 * @Title: delMgmOrder 
	 * @Description: TODO
	 * @param id						主键id
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean delMgmOrder(Integer id){
	
		MgmOrder mgmOrder = this.mgmOrderDao.findById(id);
		if(mgmOrder != null){
			mgmOrder.setIsDelete(true);				
			this.mgmOrderDao.update(mgmOrder);
		}
		return true;
	}
	
	
	/**
	 * 自动生成
	 * @Title: addMgmOrder 
	 * @Description: TODO
	 * @param mgmOrder						对象
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean addMgmOrder(MgmOrder mgmOrder){
	
		if(mgmOrder != null){
			this.mgmOrderDao.insert(mgmOrder);
		}
		return true;
	}
	
	
	/**
	 *  自动生成
	 * @Title: updateMgmOrder 
	 * @Description: TODO
	 * @param mgmOrder						对象
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean updateMgmOrder(MgmOrder mgmOrder){
	
		MgmOrder mgmOrder2 = this.mgmOrderDao.findById(mgmOrder.getId());
		if(mgmOrder2 != null){
			mgmOrder.mergeTo(mgmOrder2);
			this.mgmOrderDao.update(mgmOrder2);
		}
		
		return true;
	}

	@Override
	@Transactional(readOnly = false)
	public JSONObject addMgmOrder(Map<String, String> map) {
		// TODO Auto-generated method stub
		String userId = map.get("userId");//用户id
		String productIds = map.get("productId");//产品id
		String customerId = map.get("customerId");//客户id
		String numbers = map.get("numbers");    //数量
		String remark = map.get("remark");		//订单备注
		
		try {
			MgmUser user=this.mgmUserDao.findById(Integer.valueOf(userId));//用户信息
			
			//客户id
			MgmCustomer mgmCustomer = mgmCustomerDao.findById(Integer.valueOf(customerId));
			if(mgmCustomer == null) {				
				return getJson(0, "", "客户不存在");
			}
			
			map.put("name", mgmCustomer.getName());
			map.put("phone", mgmCustomer.getPhone());
			
			///生成订单
			MgmOrder mgmOrder = new MgmOrder();
			mgmOrder.setUserId(Long.valueOf(userId));//推介人id
			if(user!=null) {				
				mgmOrder.setUserName(user.getName());
			}else{
				return getJson(0, "", "未获取到推荐人信息");
			}
			
			mgmOrder.setOrderState("0");//订单状态(0,已备案)
			String orderNo=DateUtil.formatTime(new Date(),"YYYYMMDDHHMMSS")+RandomUtil.getNotSimple(6);
			mgmOrder.setOrderNo(orderNo);//订单编号
			mgmOrder.setRecommendTime(new Date());	//推荐日期
			if(StringUtil.isNotBlank(remark)) {
				mgmOrder.setRemark(remark);//订单备注
			}
			
			mgmOrder.setCustomerId(Long.valueOf(customerId));//客户id
			if(mgmCustomer!=null) {
				mgmOrder.setCustomerName(mgmCustomer.getName());
				mgmOrder.setCustomerPhone(mgmCustomer.getPhone());
			}
			
			List<MgmCustomer>  lstcst = this.mgmCustomerDao.queryMgmCustomerByMap(map);
			if(lstcst!=null) {
				if(lstcst.size()>1) {
					
						
						
					mgmOrder.setAttr1(""+lstcst.size());
				}
			}
			
			
			this.add(mgmOrder);			
			
			if(mgmOrder!=null) {
			    Integer orderId = mgmOrder.getId();
			    //订单进度
			    MgmOrderSchedule mgmOrderSchedule = new MgmOrderSchedule();
			    mgmOrderSchedule.setOrderId(orderId);
			    mgmOrderSchedule.setOrderState(mgmOrder.getOrderState());
			    mgmOrderSchedule.setFillUserid(mgmOrder.getUserId());
			    if(user!=null) {
			    	mgmOrderSchedule.setFillUsername(user.getName());	
			    }
			    if(StringUtil.isNotBlank(remark)) {			    	
			    	mgmOrderSchedule.setFillContent(remark);
			    }
			    mgmOrderScheduleDao.insert(mgmOrderSchedule);			    
			    //订单产品
			    if(StringUtil.isNotBlank(productIds)) {
				   String[] arrProductId = productIds.split(",");//多产品推荐
				   String[] arrnumbers = numbers.split(",");//树林里
				   
				   if(arrProductId.length>0) {
					   int i=0;
					   for(int k=0;k<arrProductId.length;k++) {
					
						   if(StringUtil.isNotBlank(arrProductId[k])) {							   
							   Integer productId = Integer.valueOf(arrProductId[k]);
							   MgmProduct mgrProduct = mgmProductDao.findById(productId);							   
							  if(mgrProduct!=null) {								  
								  MgmOrderProduct mgmOrderProduct = new MgmOrderProduct();
								  mgmOrderProduct.setOrderId(orderId);
								  mgmOrderProduct.setProductName(mgrProduct.getProductName());//产品名称
								  mgmOrderProduct.setPrice(mgrProduct.getProductPrice().toString());//单价
								  mgmOrderProduct.setProductImg(mgrProduct.getProductImg());//产品图片
								  mgmOrderProduct.setProductId(productId.toString());//产品ID
								  mgmOrderProduct.setUnit(mgrProduct.getUnit());//单位
								  mgmOrderProduct.setCommissionRate(mgrProduct.getCommissionRate());//佣金比例
								  //mgmOrderProduct.setCommissionRateSj(0.0);//实际佣金比例
								  //mgmOrderProduct.setCommissionRateYc(0.0);//溢出佣金比例
								  mgmOrderProduct.setWillCnt(Integer.valueOf(arrnumbers[k]));
								  mgmOrderProductDao.insert(mgmOrderProduct);
								  
								  
								  if(StringUtil.isBlank(mgmOrder.getOrderType())) {
									  mgmOrder.setOrderType(mgrProduct.getProductType());
									  this.update(mgmOrder);
								  }
								  
							  }
							   
						   }
						   i++;
					   }
				   }
			   }
			    
				if(user!=null) {	
					Map<String,Object> param = new HashMap<String,Object>();
					param.put("type", "1");
					//推送消息
					msgService.sendMsg(user.getId(),"东旭通","您推介的客户信息已备案成功，请关注后续进度推进。", user.getClientId(),param );
				}
				
			    
			   return getJson(1,orderId, "推荐成功");
			}
			return getJson(0, "", "推荐失败");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return getJson(0, "", "推荐失败");
		}		
	}

	@Override
	public JSONObject getOrderList(Map<String, String> map) {
		JSONObject json = new JSONObject();	
		Integer pageNo =1;
		if(StringUtil.isNotBlank(map.get("page"))) {			
			pageNo = Integer.parseInt(map.get("page"));
		}		
		Long userId=Long.valueOf(map.get("userId").toString());
		Pager pager = this.mgmOrderDao.queryMyMgmOrders(new Pager(pageNo), userId);
//		List<Map<String,Objeit>>
//		Map<String,Object> params = new HashMap<String,Object>();	
		
		
		json.put("results", pager.getResults());
		json.put("total", pager.getTotal());
		json.put("page", pager.getPage());
		json.put("start", pager.getStart());
		json.put("pageSize", pager.getPageSize());
		json.put("end", pager.getEnd());
		json.put("pageTotal", pager.getPageTotal());
		return getJson(1,json, "获取订单列表成功");
	}

	@Override
	public JSONObject orderDetailsCustomer(Map<String, String> map) {
		// TODO Auto-generated method stub
		String orderId = map.get("orderId");
		JSONObject json = new JSONObject();	
		if(StringUtil.isNotBlank(orderId)) {
			MgmOrder mgmOrder=this.findById(Integer.valueOf(orderId));
			if(mgmOrder != null){
				
				Long customerId = mgmOrder.getCustomerId();
				Long salerMaster = mgmOrder.getSalerId();
				
				//Integer.valueOf()
				//客户id
				MgmCustomer mgmCustomer = mgmCustomerDao.findById(Integer.valueOf(customerId.toString()));
				
				mgmCustomer.setRemarks(mgmOrder.getRemark());
				json = (JSONObject) JSONObject.toJSON(mgmCustomer);
				//销售id
				if(salerMaster != null) {
					
					MgmUser mgmUser = mgmUserDao.findById(Integer.valueOf(salerMaster.toString()));
					if(mgmUser != null) {						
						json.put("salerName", mgmUser.getName());
						json.put("salerPhone", mgmUser.getPhone());
						json.put("salerId", mgmUser.getId());
					}else {
						json.put("salerName", "");
						json.put("salerPhone", "");
						json.put("salerId", "");
					}
				}else {
					json.put("salerName", "");
					json.put("salerPhone", "");
					json.put("salerId", "");
				}
				
				
				return getJson(1,json, "获取客户详情成功");
			}else {
				return getJson(0,"", "订单不存在");
			}		
		}else {
			return getJson(0,"", "订单不存在");
		}
		
	}
	

	@Override
	public JSONObject orderPruductDetails(Map<String, String> map) {
		// TODO Auto-generated method stub
		JSONObject json = new JSONObject();	
		Integer pageNo =1;
		if(StringUtil.isNotBlank(map.get("pageNo"))) {			
			pageNo = Integer.parseInt(map.get("pageNo"));
		}		
		String orderId=map.get("orderId");
		Pager pager = this.mgmOrderProductDao.queryMgmOrderProductsbyOrderId(new Pager(pageNo), orderId);
		json.put("results", pager.getResults());
		json.put("total", pager.getTotal());
		json.put("page", pager.getPage());
		json.put("start", pager.getStart());
		json.put("pageSize", pager.getPageSize());
		json.put("end", pager.getEnd());
		return getJson(1,json, "获取产品信息成功");
	}

	@Override
	public Pager getMgmOrdersBySearch(Pager page, Map<String, String> map) {
		// TODO Auto-generated method stub
		return this.mgmOrderDao.getMgmOrdersBySearch(page, map);
	}

	@Override
	public JSONObject queryOrderList(Map<String, String> map) {
		JSONObject json = new JSONObject();	
		Integer pageNo =1;
		if(StringUtil.isNotBlank(map.get("page"))) {			
			pageNo = Integer.parseInt(map.get("page"));
		}		
		Pager pager = this.mgmOrderDao.queryOrderList(new Pager(pageNo), map);
		json.put("results", pager.getResults());
		json.put("total", pager.getTotal());
		json.put("page", pager.getPage());
		json.put("start", pager.getStart());
		json.put("pageSize", pager.getPageSize());
		json.put("end", pager.getEnd());
		return getJson(1,json, "获取订单列表成功");
	}
    
	@Override
	@Transactional(readOnly = false)
	public JSONObject transferAction(MgmOrderTransfer mgmOrderTransfer1) {
		// TODO Auto-generated method stub
		MgmOrderTransfer mgmOrderTransfer = new MgmOrderTransfer();
		mgmOrderTransfer1.mergeTo(mgmOrderTransfer);
		mgmOrderTransfer.setCreateDate(new Date());
		mgmOrderTransfer.setModifyDate(new Date());
		mgmOrderTransfer.setTransferTime(new Date());
		MgmOrder mgmOrder =null;
		
		if(mgmOrderTransfer1.getOrderId()!=null) {
			mgmOrder =  this.findById(mgmOrderTransfer1.getOrderId());
			if(mgmOrder!=null) {
				mgmOrderTransfer.setOrderState(mgmOrder.getOrderState());
				mgmOrderTransfer.setFromUserid(Integer.valueOf(mgmOrder.getSalerId().toString()));
				mgmOrderTransfer.setFromUsename(mgmOrder.getSalerName());
				mgmOrder.setSalerId(Long.valueOf(mgmOrderTransfer1.getToUserid()));
				mgmOrder.setSalerName(mgmOrderTransfer1.getToUsername());
				mgmOrder.setModifyDate(new Date());
				this.update(mgmOrder);
			}
			
		}
		this.mgmOrderTransferDao.insert(mgmOrderTransfer);
		
		
		if(mgmOrder!=null) {
			
			//推送人
			Long userTuiJieId=mgmOrder.getUserId();
			//salerId
			Long salerId=mgmOrder.getSalerId();
			MgmUser userTJ=null;
			MgmUser userSaler = null;
			
			if(userTuiJieId !=null) {
				userTJ = this.mgmUserDao.findById(userTuiJieId.intValue());
			}
			if(salerId !=null) {
				userSaler = this.mgmUserDao.findById(salerId.intValue());
			}			
						
			String jPushMsgTj="您推介的客户"+mgmOrder.getCustomerName()+"订单正在申请终止，了解详细进度请联系跟单人员或客服。";//推荐人消息推送
			String jPushMsgSal="为您指派新订单，推介人"+userTJ.getName()+"，联系电话"+userTJ.getPhone()+"，请尽快联系推介人和客户。";
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("type", "1");
			
			if(userTJ!=null) {
				msgService.sendMsg(userTJ.getId(),"东旭通",jPushMsgTj, userTJ.getClientId(),param );
			}
			if(userSaler!=null) {
				msgService.sendMsg(userSaler.getId(),"东旭通",jPushMsgSal, userSaler.getClientId(),param );
			}
		}
		return getJson(1,mgmOrderTransfer.getId(), "转单成功");
	}

	//订单终止申请
	@Override
	@Transactional(readOnly = false)
	public JSONObject orderTermination(Map<String, String> map) {
		// TODO Auto-generated method stub
		String orderId = map.get("orderId");
		String userId=map.get("userId");
		String apply_reason=map.get("apply_reason");
		MgmOrderBreak mgmOrderBreak = new MgmOrderBreak();
		mgmOrderBreak.setApplyTime(new Date());
		mgmOrderBreak.setCreateDate(new Date());
		mgmOrderBreak.setModifyDate(new Date());
		mgmOrderBreak.setApplyReason(apply_reason);
		
		if(StringUtil.isNotBlank(userId)) {
			mgmOrderBreak.setUserId(Integer.valueOf(userId));
		}
		
		//更新订单状态（终止申请时间，可以过滤重复提交终止申请）
		if(StringUtil.isNotBlank(orderId)) {
			MgmOrder mgmOrder = this.findById(Integer.valueOf(orderId));
			mgmOrderBreak.setOrderId(Integer.valueOf(orderId));
			mgmOrderBreak.setOrderState(mgmOrder.getOrderState());			
			if(mgmOrder.getBreakTime()!=null) {
				return getJson(0,mgmOrderBreak.getId(), "该订单已提交终止申请，不能重复提交！");
			}
			mgmOrder.setBreakTime(new Date());
			mgmOrder.setBreakState("30");
			this.update(mgmOrder);
		}
		
		
		mgmOrderBreak.setIsDelete(false);
		mgmOrderBreak.setApplyState("0");
		this.mgmOrderBreakDao.insert(mgmOrderBreak);
		
		if(StringUtil.isNotBlank(orderId)) {
			MgmOrder mgmOrder = this.findById(Integer.valueOf(orderId));	
		
			//推送人
			Long userTuiJieId=mgmOrder.getUserId();
			//salerId
			Long salerId=mgmOrder.getSalerId();
			MgmUser userTJ=null;
			MgmUser userSaler = null;
			
			if(userTuiJieId !=null) {
				userTJ = this.mgmUserDao.findById(userTuiJieId.intValue());
			}
			if(salerId !=null) {
				userSaler = this.mgmUserDao.findById(salerId.intValue());
			}			
						
			String jPushMsgTj="您推介的客户信息已被成功受理，跟单销售人员调整为"+userSaler.getName()+"，联系电话"+userSaler.getPhone()+"，请关注后续进度推进。";//推荐人消息推送
			String jPushMsgSal="恭喜您，您的客户"+mgmOrder.getCustomerName()+"订单正在申请终止，请跟进申请结果。";	
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("type", "1");
			if(userTJ!=null) {
				msgService.sendMsg(userTJ.getId(),"东旭通",jPushMsgTj, userTJ.getClientId(),param );
			}
			if(userSaler!=null) {
				msgService.sendMsg(userSaler.getId(),"东旭通",jPushMsgSal, userSaler.getClientId(),param );
			}
		}
		
		return getJson(1,mgmOrderBreak.getId(), "申请提交成功");
	}

	
	
	
	@Override
	@Transactional(readOnly = false)
	public JSONObject orderFillProgress(Map<String, String> map) {
		// TODO Auto-generated method stub
		String orderId = map.get("orderId");
		String userId=map.get("userId");
		String orderState=map.get("orderState");//订单状态	
		String remark=map.get("remark");//情况填写
		String imgeUrl=map.get("imgeUrl"); //f附件上传接口
		Integer orderScheduleId=-1;
		String jPushMsgTj="";//推荐人消息推送
		String jPushMsgSal="";//销售人消息推送
		MgmOrderSchedule mgmOrderSchedule=new MgmOrderSchedule();
		if(StringUtil.isNotBlank(orderId)) {
			
			MgmOrder mgmOrder = this.findById(Integer.valueOf(orderId));			
			mgmOrderSchedule.setOrderId(mgmOrder.getId());
			mgmOrderSchedule.setFillTime(new Date());
			mgmOrderSchedule.setOrderState(orderState);
			mgmOrderSchedule.setImgeUrl1(imgeUrl);
			mgmOrderSchedule.setFillContent(remark);
			
			
			if(StringUtil.isNotBlank(userId)) {				
				MgmUser mgmUser = this.mgmUserDao.findById(Integer.valueOf(userId));
			    if(mgmUser!=null) {
			    	mgmOrderSchedule.setFillUserid(Long.valueOf(userId));
			    	mgmOrderSchedule.setFillUsername(mgmUser.getName());
			    }
			}
			
			this.mgmOrderScheduleDao.insert(mgmOrderSchedule);
			orderScheduleId = mgmOrderSchedule.getId();
			//更新顶单数据
			mgmOrder.setOrderState(orderState);
			mgmOrder.setModifyDate(new Date());
			if("1".equals(orderState.trim())) {
				mgmOrder.setReceiveTime(new Date());
			}else if("2".equals(orderState.trim())) {
				mgmOrder.setInviteTime(new Date());
			}else if("3".equals(orderState.trim())) {
				mgmOrder.setVisitTime(new Date());
			}else if("4".equals(orderState.trim())) {
				mgmOrder.setSubscribeTime(new Date());
			}else if("5".equals(orderState.trim()))		{
				mgmOrder.setContractTime(new Date());
			}else if("6".equals(orderState.trim())){
				mgmOrder.setCheckTime(new Date());
			}
			
			this.update(mgmOrder);
			
			
			//推送人
			Long userTuiJieId=mgmOrder.getUserId();
			//salerId
			Long salerId=mgmOrder.getSalerId();
			MgmUser userTJ=null;
			MgmUser userSaler = null;
			
			if(userTuiJieId !=null) {
				userTJ = this.mgmUserDao.findById(userTuiJieId.intValue());
			}
			if(salerId !=null) {
				userSaler = this.mgmUserDao.findById(salerId.intValue());
			}
			

			if("0".equals(orderState.trim())) {
				jPushMsgTj="您推介的客户信息已备案成功，请关注后续进度推进";
			}else if("1".equals(orderState.trim())) {				
				jPushMsgTj="您推介的客户信息已被成功受理，跟单销售人员"+userSaler.getName()+"，联系电话"+userSaler.getPhone()+"，请关注后续进度推进。";//推荐人消息推送
				jPushMsgSal="为您指派新订单，推介人"+userTJ.getName()+"，联系电话"+userTJ.getPhone()+"，请尽快联系推介人和客户。";
			}else if("2".equals(orderState.trim())) {
				if(mgmOrder.getOrderType().equals("1")) {					
					jPushMsgTj="您推介的客户"+mgmOrder.getCustomerName()+"已邀约成功，请关注后续进度推进。";//推荐人消息推送
					jPushMsgSal="您的客户"+mgmOrder.getCustomerName()+"已邀约成功，请继续推进订单进度。";			
				}else {
					jPushMsgTj="您推介的客户"+mgmOrder.getCustomerName()+"已签约成功，请关注后续进度推进。";//推荐人消息推送
					jPushMsgSal="您的客户"+mgmOrder.getCustomerName()+"已签约成功，请关注后续进度推进。";		
				}
			}else if("3".equals(orderState.trim())) {
				if(mgmOrder.getOrderType().equals("1")) {					
					jPushMsgTj="您推介的客户"+mgmOrder.getCustomerName()+"已到访成功，请关注后续进度推进。";//推荐人消息推送
					jPushMsgSal="您的客户"+mgmOrder.getCustomerName()+"已到访成功，请继续推进订单进度。";			
				}else {
					jPushMsgTj="恭喜您，您推介的客户"+mgmOrder.getCustomerName()+"已确认签约成功，您又增加一笔佣金，佣金以线下结算为准。";//推荐人消息推送
					jPushMsgSal="恭喜您，您的客户"+mgmOrder.getCustomerName()+"已确认签约成功。";		
				}
			}else if("4".equals(orderState.trim()))		{
				jPushMsgTj="您推介的客户"+mgmOrder.getCustomerName()+"已认购成功，请关注后续进度推进。";//推荐人消息推送
				jPushMsgSal="您的客户"+mgmOrder.getCustomerName()+"已认购成功，请继续推进订单进度。";		
			}else if("5".equals(orderState.trim())){
				jPushMsgTj="恭喜您，您推介的客户"+mgmOrder.getCustomerName()+"已确认签约成功，您又增加一笔佣金，佣金以线下结算为准。";//推荐人消息推送
				jPushMsgSal="恭喜您，您的客户"+mgmOrder.getCustomerName()+"已确认签约成功。";	
			}else if("6".equals(orderState.trim())){
				jPushMsgTj="恭喜您，您推介的客户"+mgmOrder.getCustomerName()+"已确认签约成功，您又增加一笔佣金，佣金以线下结算为准。";//推荐人消息推送
				jPushMsgSal="恭喜您，您的客户"+mgmOrder.getCustomerName()+"已确认签约成功。";	
			}
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("type", "1");
			if(userTJ!=null) {
				msgService.sendMsg(userTJ.getId(),"东旭通",jPushMsgTj, userTJ.getClientId(),param );
			}
			if(userSaler!=null) {
				msgService.sendMsg(userSaler.getId(),"东旭通",jPushMsgSal, userSaler.getClientId(),param );
			}
			
		}
		
		return  getJson(1,orderScheduleId, "申请提交成功");
		
	}

	@Override
	@Transactional(readOnly = false)
	public JSONObject signAcntract(Map<String, String> map) {
		// TODO Auto-generated method stub		
		String orderId = map.get("orderId");
		String userId=map.get("userId");
		String orderState=map.get("orderState");//订单状态	
		String remark=map.get("remark");//情况填写
		String imgeUrl=map.get("imgeUrl"); //f附件上传接口
		String commissionMeans = map.get("commissionMeans");//佣金计算方式
		String productId = map.get("productId");//产品id
		String commissionRate = map.get("commissionRate");//佣金比例（多产品用'，'分开）
		String commissionRateYc = map.get("commissionRateYc");//溢出佣金比例（多产品用'，'分开）
		String numbers = map.get("numbers");//认购数量，同产品id多产品数量直接用','
		String standardAmount= map.get("standardAmount");//标准金额，同产品id多产品数量直接用','
		String volumeTransaction= map.get("volumeTransaction");//成交金额，同产品id多产品数量直接用','
		
		String contractGrandTotal= map.get("contractGrandTotal");//	合同总额
		
		String jPushMsgTj="";//推荐人消息推送
		String jPushMsgSal="";//销售人消息推送
		
		//填写佣金
		MgmOrderCommission mgmOrderCommission = new MgmOrderCommission();
		Integer orderScheduleId=-1;
		MgmOrderSchedule mgmOrderSchedule=new MgmOrderSchedule();
		if(StringUtil.isNotBlank(orderId)) {
		
			MgmOrder mgmOrder = this.findById(Integer.valueOf(orderId));		
			mgmOrderCommission.setOrderId(mgmOrder.getId());
			mgmOrderCommission.setUserId(mgmOrder.getUserId().intValue());
			
			mgmOrderSchedule.setOrderId(mgmOrder.getId());
			mgmOrderSchedule.setFillTime(new Date());
			mgmOrderSchedule.setOrderState(orderState);
			mgmOrderSchedule.setFillContent(remark);
			mgmOrderSchedule.setImgeUrl1(imgeUrl);
			if(StringUtil.isNotBlank(userId)) {				
				MgmUser mgmUser = this.mgmUserDao.findById(Integer.valueOf(userId));
			    if(mgmUser!=null) {
			    	mgmOrderSchedule.setFillUserid(Long.valueOf(userId));
			    	mgmOrderSchedule.setFillUsername(mgmUser.getName());
			    	//mgmOrderCommission.setUserId(mgmUser.getId());
			    }
			}
			this.mgmOrderScheduleDao.insert(mgmOrderSchedule);
			orderScheduleId = mgmOrderSchedule.getId();
			mgmOrder.setOrderState(orderState);
			mgmOrder.setModifyDate(new Date());
			
			if("1".equals(orderState.trim())) {
				mgmOrder.setReceiveTime(new Date());
			}else if("1".equals(orderState.trim())) {
				mgmOrder.setReceiveTime(new Date());
			}else if("2".equals(orderState.trim())) {
				mgmOrder.setInviteTime(new Date());
			}else if("3".equals(orderState.trim())) {
				mgmOrder.setVisitTime(new Date());
			}else if("4".equals(orderState.trim())) {
				mgmOrder.setSubscribeTime(new Date());
			}else if("5".equals(orderState.trim())){
				mgmOrder.setContractTime(new Date());
			}else if("6".equals(orderState.trim())){
				mgmOrder.setCheckTime(new Date());
			}
			
			
			
			mgmOrder.setAmount(Double.valueOf(contractGrandTotal));
			
			
			this.update(mgmOrder);
			
			if(StringUtil.isNotBlank(productId)) {
				String[] arrayProductId=productId.split(",");
				String[] arrycommissionRate=commissionRate.split(",");
				String[] arrycommissionRateYc=commissionRateYc.split(",");
				String[] arrnumbers=numbers.split(",");
				String[] arryvolumeTransaction=volumeTransaction.split(",");				
				String[] arrstandardAmount=standardAmount.split(",");
				Double yj=0.00;
				if(arrayProductId!=null) {
					for(int i=0; i< arrayProductId.length;i++) {
						if(StringUtil.isNotBlank(arrayProductId[i])) {
							Pager page = this.mgmOrderProductDao.queryMgmOrderProductsbyOPId(new Pager(1), orderId, arrayProductId[i]);
							if(page.getResults().size()>0) {								
								MgmOrderProduct mgmOrderProduct = (MgmOrderProduct) page.getResults().get(0);
								if(mgmOrderProduct!=null) {
									if(StringUtil.isNotBlank(arrycommissionRate[i])) {
										/** 实际佣金比例  */
										mgmOrderProduct.setCommissionRateSj(Double.valueOf(arrycommissionRate[i]));	
									}
									
									if(StringUtil.isNotBlank(arrycommissionRateYc[i])) {										
										mgmOrderProduct.setCommissionRateYc(Double.valueOf(arrycommissionRateYc[i]));
									}
									
									if(StringUtil.isNotBlank(arrnumbers[i])) {
										mgmOrderProduct.setActualCnt(Integer.valueOf(arrnumbers[i]));
									}
								
									if(StringUtil.isNotBlank(arrstandardAmount[i])) {
										mgmOrderProduct.setStandardAmount(Double.valueOf(arrstandardAmount[i]));
									}
									
									
									/** 成交价  */
									if(StringUtil.isNotBlank(arryvolumeTransaction[i])) {
										mgmOrderProduct.setDealAmount(Double.valueOf(arryvolumeTransaction[i]));
									}
									
									this.mgmOrderProductDao.insert(mgmOrderProduct);
									
									//佣金计算
									if(StringUtil.isNotBlank(commissionMeans)) {
										if("1".equals(commissionMeans)) {											
											yj = MathUtil.add(yj,MathUtil.div( MathUtil.mul(mgmOrderProduct.getDealAmount(), mgmOrderProduct.getCommissionRateSj()),100));
											
										}else {
											double sjyj = 0.0;
											if(mgmOrderProduct.getStandardAmount()!=null && mgmOrderProduct.getCommissionRateSj()!=null) {
												
												sjyj= MathUtil.div( MathUtil.mul(mgmOrderProduct.getStandardAmount(),mgmOrderProduct.getCommissionRateSj() ),100);
											}
											
											double ycyj =0.0;
											if(mgmOrderProduct.getStandardAmount()!=null && mgmOrderProduct.getDealAmount()!=null&&mgmOrderProduct.getCommissionRateYc()!=null) {
												
												ycyj = MathUtil.div(MathUtil.mul(MathUtil.sub(mgmOrderProduct.getDealAmount(), mgmOrderProduct.getStandardAmount()),mgmOrderProduct.getCommissionRateYc()),100);
											}
											
											yj =MathUtil.add( yj,MathUtil.add(sjyj, ycyj));
										}
									}
								}
							}						
						}
					
					}
			    
				}
				//填写佣金
				mgmOrderCommission.setCommission(yj);
				mgmOrderCommission.setFinishTime(new Date());
				mgmOrderCommission.setType(commissionMeans);
				mgmOrderCommission.setState("0");
				mgmOrderCommissionDao.insert(mgmOrderCommission);
				

				//推送人
				Long userTuiJieId=mgmOrder.getUserId();
				//salerId
				Long salerId=mgmOrder.getSalerId();
				MgmUser userTJ=null;
				MgmUser userSaler = null;
				
				if(userTuiJieId !=null) {
					userTJ = this.mgmUserDao.findById(userTuiJieId.intValue());
				}
				if(salerId !=null) {
					userSaler = this.mgmUserDao.findById(salerId.intValue());
				}	
				
				Map<String,Object> param = new HashMap<String,Object>();
				param.put("type", "0");
				
				jPushMsgTj="恭喜您，您推介的客户"+mgmOrder.getCustomerName()+"已签约成功，请关注后续进度推进。";//推荐人消息推送
				jPushMsgSal="恭喜您，您的客户"+mgmOrder.getCustomerName()+"已签约成功，请继续推进订单进度。";		
				if(userTJ!=null) {
					msgService.sendMsg(userTJ.getId(),"东旭通",jPushMsgTj, userTJ.getClientId(),param );
				}
				if(userSaler!=null) {
					msgService.sendMsg(userSaler.getId(),"东旭通",jPushMsgSal, userSaler.getClientId(),param );
				}
			}
		}
		
		return  getJson(1,orderScheduleId, "提交成功");
		
	}
    //指派销售人员
	@Override
	@Transactional(readOnly = false)
	public JSONObject assignAction(String userId, String orderId) {
		// TODO Auto-generated method stub
		
		if(StringUtil.isNotBlank(orderId)) {
			MgmOrder mgmOrder = this.findById(Integer.valueOf(orderId));
			if(StringUtil.isNotBlank(userId)) {				
				MgmUser mgmUser = this.mgmUserDao.findById(Integer.valueOf(userId));
			    if(mgmUser!=null) {
			    	mgmOrder.setSalerId(Long.valueOf(mgmUser.getId()));
			    	mgmOrder.setSalerName(mgmUser.getName());	
			    	mgmOrder.setModifyDate(new Date());
			    	mgmOrder.setOrderState("1");
			    	this.update(mgmOrder);
			    }
			    
			}
			
			//推送人
			Long userTuiJieId=mgmOrder.getUserId();
			//salerId
			Long salerId=mgmOrder.getSalerId();
			MgmUser userTJ=null;
			MgmUser userSaler = null;
			
			if(userTuiJieId !=null) {
				userTJ = this.mgmUserDao.findById(userTuiJieId.intValue());
			}
			if(salerId !=null) {
				userSaler = this.mgmUserDao.findById(salerId.intValue());
			}			
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("type", "1");	
			String jPushMsgTj="您推介的客户信息已被成功受理，跟单销售人员"+userSaler.getName()+"，联系电话"+userSaler.getPhone()+"，请关注后续进度推进。";//推荐人消息推送
			
			String	jPushMsgSal="为您指派新订单，推介人"+userTJ.getName()+"，联系电话"+userTJ.getPhone()+"，请尽快联系推介人和客户。";
			if(userTJ!=null) {
				msgService.sendMsg(userTJ.getId(),"东旭通",jPushMsgTj, userTJ.getClientId(),param );
			}
			if(userSaler!=null) {
				msgService.sendMsg(userSaler.getId(),"东旭通",jPushMsgSal, userSaler.getClientId(),param );
			}
			
		}		
		return  getJson(1,orderId, "指派成功");
	}
//签约确认
	@Override
	@Transactional(readOnly = false)
	public JSONObject signAction(String actionName, String orderId) {
		// TODO Auto-generated method stub
		if(StringUtil.isNotBlank(orderId)) {
			MgmOrder mgmOrder = this.findById(Integer.valueOf(orderId));
			if(mgmOrder!=null) {
				if(actionName.equals("qyqr")) {					
					if("1".equals(mgmOrder.getOrderType())) {
						mgmOrder.setOrderState("6");
					}else {
						mgmOrder.setOrderState("3");
					}					
					mgmOrder.setOrderStateName("签约成功");
					
					MgmOrderCommission mgmOrderCommission =  this.mgmOrderCommissionDao.getByPropertie(orderId,"orderId");
					
					mgmOrderCommission.setState("1");
					this.mgmOrderCommissionDao.update(mgmOrderCommission);
					
				}else {
					StringBuffer sqlbuf=new StringBuffer();
					sqlbuf.append("UPDATE MGM_ORDER_SCHEDULE t");
					sqlbuf.append("  SET t.is_delete = 1");
					sqlbuf.append(" WHERE t.is_delete = 0 ");
					sqlbuf.append(" AND t.order_id="+mgmOrder.getId());
					if("1".equals(mgmOrder.getOrderType())) {
						mgmOrder.setOrderState("4");
						sqlbuf.append(" AND t.order_state=5");
					}else {
						mgmOrder.setOrderState("1");
						sqlbuf.append(" AND t.order_state=2");
					}				
					mgmOrder.setOrderStateName("签约无效");
					this.mgmOrderDao.execute(sqlbuf.toString(), "sql");
					
					
				}
				
				mgmOrder.setModifyDate(new Date());
				mgmOrder.setCheckTime(new Date());
				
				this.update(mgmOrder);
				
				
				
				
				//推送人
				Long userTuiJieId=mgmOrder.getUserId();
				//salerId
				Long salerId=mgmOrder.getSalerId();
				MgmUser userTJ=null;
				MgmUser userSaler = null;
				
				if(userTuiJieId !=null) {
					userTJ = this.mgmUserDao.findById(userTuiJieId.intValue());
				}
				if(salerId !=null) {
					userSaler = this.mgmUserDao.findById(salerId.intValue());
				}			
				if(actionName.equals("qyqr")) 	{		
					String jPushMsgTj="恭喜您，您推介的客户"+mgmOrder.getCustomerName()+"已确认签约成功，您又增加一笔佣金，佣金以线下结算为准。";//推荐人消息推送
					String jPushMsgSal="恭喜您，您的客户"+mgmOrder.getCustomerName()+"已确认签约成功。";	
					Map<String,Object> param = new HashMap<String,Object>();
					param.put("type", "1");	
					if(userTJ!=null) {
						msgService.sendMsg(userTJ.getId(),"东旭通",jPushMsgTj, userTJ.getClientId(),param );
					}
					if(userSaler!=null) {
						msgService.sendMsg(userSaler.getId(),"东旭通",jPushMsgSal, userSaler.getClientId(),param );
					}
				}else {
					String jPushMsgTj="恭喜您，您推介的客户"+mgmOrder.getCustomerName()+"签约没有确认。";//推荐人消息推送
					String jPushMsgSal="恭喜您，您的客户"+mgmOrder.getCustomerName()+"签约没有成功，请重新填写签约内容。";	
					Map<String,Object> param = new HashMap<String,Object>();
					param.put("type", "1");	
					if(userTJ!=null) {
						msgService.sendMsg(userTJ.getId(),"东旭通",jPushMsgTj, userTJ.getClientId(),param );
					}
					if(userSaler!=null) {
						msgService.sendMsg(userSaler.getId(),"东旭通",jPushMsgSal, userSaler.getClientId(),param );
					}
				}
			}

		}		
		return  getJson(1,orderId, "确认签约成功");
	}

	@Override
	@Transactional(readOnly = false)
	public JSONObject deliveryAction(String orderId) {
		// TODO Auto-generated method stub
		

		if(StringUtil.isNotBlank(orderId)) {
			MgmOrder mgmOrder = this.findById(Integer.valueOf(orderId));
			if(mgmOrder!=null) {
								
					mgmOrder.setOrderStateName("签约成功");
				}else {
					mgmOrder.setOrderStateName("签约无效");
				}
				
				mgmOrder.setModifyDate(new Date());
				mgmOrder.setCheckTime(new Date());
				
				this.update(mgmOrder);
			}
		return  getJson(1,orderId, "确认签约成功");
	}

	@Override
	@Transactional(readOnly = false)
	public JSONObject assignActionTj(String userId, String orderId) {
		String oldUserId="";
		if(StringUtil.isNotBlank(orderId)) {
			MgmOrder mgmOrder = this.findById(Integer.valueOf(orderId));
			oldUserId = mgmOrder.getUserId().toString();
			if(StringUtil.isNotBlank(userId)) {				
				MgmUser mgmUser = this.mgmUserDao.findById(Integer.valueOf(userId));
			    if(mgmUser!=null) {
			    	mgmOrder.setUserId(Long.valueOf(mgmUser.getId()));
			    	mgmOrder.setUserName(mgmUser.getName());
			    	mgmOrder.setModifyDate(new Date());
			    	mgmOrder.setAttr1("");
			    	
			    	this.update(mgmOrder);
			    }			    
			}
			
//			//推送人
//			Long userTuiJieId=mgmOrder.getUserId();
//			//salerId
//			Long salerId=mgmOrder.getSalerId();
//			MgmUser userTJ=null;
//			MgmUser userSaler = null;
//			
//			if(userTuiJieId !=null) {
//				userTJ = this.mgmUserDao.findById(userTuiJieId.intValue());
//			}
//			if(StringUtil.isNotBlank(oldUserId) ) {
//				userSaler = this.mgmUserDao.findById(Integer.valueOf(oldUserId));
//			}	
			
			
//			
//			Map<String,Object> param = new HashMap<String,Object>();
//			param.put("type", "1");	
//			
//			String jPushMsgTj="";//推荐人消息推送
			
//			String	jPushMsgSal="为您指派新订单，推介人"+userTJ.getName()+"，联系电话"+userTJ.getPhone()+"，请尽快联系推介人和客户。";
//			if(userTJ!=null) {
//				msgService.sendMsg(userTJ.getId(),"东旭通",jPushMsgTj, userTJ.getClientId(),param );
//			}
//			if(userSaler!=null) {
//				msgService.sendMsg(userSaler.getId(),"东旭通",jPushMsgSal, userSaler.getClientId(),param );
//			}
			
		}		
		return  getJson(1,orderId, "指派推荐人成功");
	}

	//统计报表
	@Override
	public Pager getMgmOrderStatistics(Pager page) {
		// TODO Auto-generated method stub
		return this.mgmOrderDao.getMgmOrderStatistics(page);
	}

	@Override
	public void downExecl(Map<String, String> map, HttpServletResponse response) {
		// TODO Auto-generated method stub
		try {
			
			Pager pager = this.mgmOrderDao.getMgmOrderStatistics(new Pager(1, 10000) );
			
			List<Map<String, Object>> list = (List<Map<String, Object>>) pager.getResults();
			
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFCellStyle hcs = workbook.createCellStyle();
			hcs.setAlignment(HorizontalAlignment.CENTER);
			hcs.setVerticalAlignment(VerticalAlignment.CENTER);
			HSSFSheet sheet = workbook.createSheet("统计报表");

			int i = 0;
			HSSFRow row0 = sheet.createRow((short) 0);
			String[] titles = {"产品名称","所属类别","用户姓名","浏览时间","浏览次数","是否推介","推介时间","客户名称","订单数量","订单类型","推介状态","状态时间","跟单人员"};
			for (Object title : titles) {
				HSSFCell cell0 = row0.createCell((short) i);
				cell0.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell0.setCellValue(title + "");
				i++;
			}

			i = 1;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
			for (Map<String, Object> value : list) {
				if (value != null) {
					String[] values = new String[13];					
					if(value.get("product_name")!=null){						
						values[0] = value.get("product_name").toString();
					}else{
						values[0] = "";
					}
					if(value.get("gc")!=null){
						values[1] = value.get("gc").toString();
					}else{
						values[1] = "";
					}
					
					if( value.get("user_name")!=null){
						
						values[2] = value.get("user_name").toString();
					}else{
						values[2] = "";
					}
					
					if(value.get("view_time_str")!=null){
						values[3] = value.get("view_time_str").toString();
					}else{
						values[3] ="";

					}
					if(value.get("view_cont") !=null){
						
						values[4] = value.get("view_cont").toString();
					}else{
						values[4] ="";
					}
					
					if(value.get("tj")!=null){
						
						values[5] = value.get("tj").toString();					
					}else{
						values[5] = "";
					}
					if(value.get("modify_date_str")!=null){
						
						values[6] = value.get("modify_date_str").toString();
					}else{
						values[6] = "";
					}
					
					if(value.get("customer_name")!=null){
						
						values[7] = value.get("customer_name").toString();
					}else{
						values[7] = "";
					}
					
					if(value.get("will_cnt")!=null){
						
						values[8] = value.get("will_cnt").toString();
					}else{
						values[8] ="";
					}
					if(value.get("ordertype")!=null){
						
						values[9] = value.get("ordertype").toString();
					}else{
						values[9] = "";
					}
					if(value.get("state_name")!=null){
						
						values[10] = value.get("state_name").toString();
					}else{
						values[10] = "";
					}
					
					if(value.get("modify_date_str")!=null){
						
						values[11] = value.get("modify_date_str").toString();
					}else{
						values[11] ="";
					}
					
					if(value.get("saler_name")!=null){
						
						values[12] = value.get("saler_name").toString();	
					}else{
						values[12] ="";
					}
					
					
					HSSFRow row = sheet.createRow((short) i);
					for (int j = 0; j < values.length; j++) {
						HSSFCell cell = row.createCell((short) j);
						cell.setCellType(HSSFCell.CELL_TYPE_STRING);
						cell.setCellStyle(hcs);
						cell.setCellValue(values[j] + "");
					}
				}
				i++;
			}
			String date = DateUtil.getDate(new Date());

			String downFileName = "统计报表" + "_" + date + ".xls";
			try {
				downFileName = new String(downFileName.getBytes(), "ISO8859-1");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			
			
			response.setHeader("Content-Disposition", "attachment; filename="
					+ downFileName);
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			ServletOutputStream outputStream = response.getOutputStream();
			workbook.write(outputStream);
			outputStream.close();
			outputStream.flush();
		} catch (Exception e) {
			logger.info("excel导出失败，" + e.getMessage());
		}
	}

	@Override
	@Transactional(readOnly = false)
	public JSONObject orderContinuation(String orderId) {
		// TODO Auto-generated method stub
		if(StringUtil.isNotBlank(orderId)) {
			MgmOrder mgmOrder = this.findById(Integer.valueOf(orderId));
			mgmOrder.setModifyDate(new Date());
			this.update(mgmOrder);
			return  getJson(1,orderId, "操作成功");
		}
		return  getJson(0,orderId, "订单不存在");
	}

	@Override
	public JSONObject orderEnd(String orderId,Integer userId) {
	
		String apply_reason="订单停滞终止";
		MgmOrderBreak mgmOrderBreak = new MgmOrderBreak();
		mgmOrderBreak.setApplyTime(new Date());
		mgmOrderBreak.setCreateDate(new Date());
		mgmOrderBreak.setModifyDate(new Date());
		mgmOrderBreak.setApplyReason(apply_reason);
		
		if(userId != null) {
			mgmOrderBreak.setUserId(userId);
		}
				
		//更新订单状态（终止申请时间，可以过滤重复提交终止申请）
		if(StringUtil.isNotBlank(orderId)) {
			MgmOrder mgmOrder = this.findById(Integer.valueOf(orderId));
			mgmOrderBreak.setOrderId(Integer.valueOf(orderId));
			mgmOrderBreak.setOrderState(mgmOrder.getOrderState());	
			mgmOrder.setBreakTime(new Date());
			mgmOrder.setBreakState("30");
			this.update(mgmOrder);
		}
		
				
		mgmOrderBreak.setIsDelete(false);
		mgmOrderBreak.setApplyState("1");
		this.mgmOrderBreakDao.insert(mgmOrderBreak);
		
				
		return  getJson(1,orderId, "终止成功");
	}
}
