/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-01-09
 * 描述            ：加工计划信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
 ***********************************************************/
package market.Slm.OrderManagement.impl;
import java.util.Date;
import java.util.List;

import market.Com.pb.PbMethod;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.OrderManagement.mapper.SlmOrderInfoMapper;
import market.Slm.OrderManagement.mapper.SlmOrderSplitInfoMapper;
import market.Slm.OrderManagement.service.SlmOrderSplitInfoService;
import market.Slm.OrderManagement.vo.SlmOrderInfo;
import market.Slm.OrderManagement.vo.SlmOrderSplitInfo;

import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;
import com.ibm.icu.text.SimpleDateFormat;

@RemoteService(path="SlmOrderSplitInfoService")
public class SlmOrderSplitInfoServiceImpl extends BaseServiceImpl<SlmOrderSplitInfo,Object> implements SlmOrderSplitInfoService {

	/**
	 * 查询
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmOrderSplitInfo> sum){
		ResultModel rm=new ResultModel();
		SlmOrderSplitInfo split=sum.getObject();
		try{
			String sWhere=" valid_flag=1";
			List<SlmOrderSplitInfo> list=this.getMapper(SlmOrderSplitInfoMapper.class).findWithCondition(split, sWhere, " status,create_time desc",0,0);
			rm.setData(list);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 新增
	 * @throws Exception 
	 * */
	public ResultModel doAddWeb(CommonPage<SlmOrderSplitInfo> sum) throws Exception{
		ResultModel rm=new ResultModel();
		SlmOrderSplitInfo split=sum.getObject();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			if(split.getEstimate_weight()>0){
				if(split.getOrder_no().contains("-")) throw new Exception("拆分出来的订单不允许再次拆分");
				SlmOrderInfo old=this.getMapper(SlmOrderInfoMapper.class).findById(split.getOrder_no());
				if(old==null) throw new Exception("原订单已删除");
				String id=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
				split.setId(id);
				split.setCreate_man_no(userId);
				split.setCreate_man_name(userName);
				split.setCreate_time(new Date());
				split.setValid_flag("1");
				split.setStatus("0");		//0未生效
				this.getMapper(SlmOrderSplitInfoMapper.class).insert(split);
			}else{
				throw new Exception("请输入拆分重量");
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("新增失败"+e.getMessage());
			e.printStackTrace();
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}

	/**
	 * 删除
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doDeleteWeb(CommonPage<SlmOrderSplitInfo> sum) throws Exception{
		ResultModel rm=new ResultModel();
		SlmOrderSplitInfo split=sum.getObject();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			split=this.getMapper(SlmOrderSplitInfoMapper.class).findById(split.getId());
			if(split!=null){
				if("1".equals(split.getStatus())) throw new Exception("已拆分信息无法删除");
				split.setValid_flag("0"); 
				split.setDelete_man_no(userId);
				split.setDelete_man_name(userName);
				split.setDelete_time(new Date());
				this.getMapper(SlmOrderSplitInfoMapper.class).update(split);
			}else{
				throw new Exception("请检查参数[{\"object\":{\"id\":\"\"}}]");
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("删除失败"+e.getMessage());
			e.printStackTrace();
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}

}
