/***文档注释***********************************************
 * 作者               :                   易鑫
 * 创建日期      :                   2017.10.24
 * 描述               :        			客户订单信息
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
 ***********************************************************/
package market.Slm.OrderManagement.impl;

import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.ContractManagement.mapper.SlmContractLineInfoMapper;
import market.Slm.ContractManagement.vo.SlmContractLineInfo;
import market.Slm.OrderManagement.mapper.SlmOrderDesignMapper;
import market.Slm.OrderManagement.mapper.SlmOrderInfoMapper;
import market.Slm.OrderManagement.service.SlmOrderDesignService;
import market.Slm.OrderManagement.vo.SlmOrderDesign;
import market.Slm.OrderManagement.vo.SlmOrderInfo;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

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

@RemoteService(path="SlmOrderDesignService")
public class SlmOrderDesignServiceImpl extends BaseServiceImpl<SlmOrderDesign,Object> implements SlmOrderDesignService {

	public ResultModel doQuerySlmOrderDesign(CommonPage<SlmOrderDesign> cp){
		ResultModel rm=new ResultModel();
		SlmOrderDesign sod = cp.getObject();		
		try{
			String condition = "";
			if (StringUtils.isNotEmpty(sod.getOrder_no())) {
				condition += " and t.order_no like '%"+sod.getOrder_no()+"%'";
			}
			List<SlmOrderDesign> sodList = this.getMapper(SlmOrderDesignMapper.class).doQuerySlmOrderDesign(sod,condition);
			rm.setData(sodList);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	
	public ResultModel doSaveSlmOrderDesign(CommonPage<SlmOrderDesign> cp) throws Exception{
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date now = new Date();
		ResultModel rm=new ResultModel();
		List<SlmOrderDesign> sodList = cp.getObjectList();
		if (CollectionUtils.isEmpty(sodList)) {
			throw new Exception("请传入参数");
		}
		try{
			for (SlmOrderDesign sod : sodList) {
				SlmOrderInfo soi = this.getMapper(SlmOrderInfoMapper.class).findById(sod.getOrder_id());
				if (soi!=null) {
					sod.setDelivery_state_code(soi.getDelivery_state_code());
					sod.setDelivery_state_desc(soi.getDelivery_state_desc());
					sod.setSteel_code(soi.getSteel_code());
					sod.setSteel_name(soi.getSteel_name());
					sod.setWeight_type_no(soi.getWeight_type_no());
					sod.setWeight_type_name(soi.getWeight_type_name());
					sod.setOrder_nature(soi.getOrder_nature());
					sod.setContract_no(soi.getContract_no());
					sod.setOrder_no(soi.getContract_no()+soi.getContract_line_seq()+soi.getOrder_seq());
					SlmContractLineInfo scli = this.getMapper(SlmContractLineInfoMapper.class).findById(soi.getContract_line_no());
					if (scli!=null) {
						sod.setOrder_thick(scli.getThick().toString());
						sod.setOrder_width(scli.getWidth().toString());
					}
				}
				//判定数据是否存在
				SlmOrderDesign SOD = this.getMapper(SlmOrderDesignMapper.class).findById(sod.getOrder_id(),sod.getSerial_no());
				if (SOD == null) {
					//不存在：  保存
					sod.setCreate_man_name(userName);
					sod.setCreate_time(now);
					this.getMapper(SlmOrderDesignMapper.class).insert(sod);
					
				}else {
					//存在： 修改
					sod.setUpdate_man_name(userName);
					sod.setUpdate_time(now);
					this.getMapper(SlmOrderDesignMapper.class).update(sod);
				}
			}
			rm.setMsgInfo("保存成功");
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("保存失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
}

