/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-08-07
 * 描述            ：合同行精度等级表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
 ***********************************************************/
package market.Slm.ContractManagement.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.ContractManagement.mapper.SlmContractLineInfoMapper;
import market.Slm.ContractManagement.mapper.SlmContractLineLvMapper;
import market.Slm.ContractManagement.service.SlmContractLineLvService;
import market.Slm.ContractManagement.vo.SlmContractLineInfo;
import market.Slm.ContractManagement.vo.SlmContractLineLv;
import market.Slm.OrderManagement.mapper.SlmOrderInfoMapper;
import market.Slm.OrderManagement.mapper.SlmProcessPlanMapper;
import market.Slm.OrderManagement.vo.SlmOrderInfo;
import market.Slm.OrderManagement.vo.SlmProcessPlan;

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="SlmContractLineLvService")
public class SlmContractLineLvServiceImpl extends BaseServiceImpl<SlmContractLineLv,Object> implements SlmContractLineLvService {

	/**
	 * 合同行-更多-精度等级-查询精度等级编辑区
	 * contract_line_no 合同行号
	 * */
	public ResultModel doQueryEditWeb(){
		ResultModel rm=new ResultModel();
		try {
			rm.setData(this.getMapper(SlmContractLineLvMapper.class).doQueryEditWeb());
			rm.setState("200");
			rm.setMsgInfo("查询成功！");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！"+e.getMessage());
		}
		return rm;
	}
	/**
	 * 合同行-更多-精度等级-查询精度等级
	 * contract_line_no 合同行号
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmContractLineLv> sum){
		ResultModel rm=new ResultModel();
		try {
			SlmContractLineLv lv=sum.getObject();
			if(lv==null){
				rm.setState("500");
				rm.setMsgInfo("请传入参数！");
				return rm;
			}
			rm.setData(this.getMapper(SlmContractLineLvMapper.class).find(lv, 0, 0));
			rm.setState("200");
			rm.setMsgInfo("查询成功！");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！"+e.getMessage());
		}
		return rm;
	}

	/**
	 * 保存精度等级
	 * [{"objectList":[{"lv_code":"精度等级编号","lv_desc":"精度等级名称","tol_code":"公差项目代码","tol_name":"公差项目描述"}],"memo1":"contract_pk","memo2":"contract_line_no","memo3":"contract_line_seq"}]
	 * */
	@Transactional(value="tm",rollbackFor=Exception.class)
	public ResultModel doSaveWeb(CommonPage<SlmContractLineLv> sum) throws Exception{
		ResultModel rm=new ResultModel();
		String userName=(String)this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String userId=(String)this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		try {
			List<SlmContractLineLv> lvList=sum.getObjectList();
			//			if(!(lvList!=null&&lvList.size()>0)){
			//				throw new Exception("请传入参数！");
			//			}
			if(sum.getMemo2()!=null){
				SlmContractLineLv param=new SlmContractLineLv();
				param.setContract_line_no(sum.getMemo2());
				param.setIs_flag("0");
				if(sum.getMemo4()!=null&&!"".equals(sum.getMemo4())){
					param.setIs_flag("1");
					param.setOrder_no(sum.getMemo4());		
					param.setContract_line_no(null);
				}
				List<SlmContractLineLv> lv_list=this.getMapper(SlmContractLineLvMapper.class).find(param,0,0);
				for(SlmContractLineLv lv:lv_list){
					this.getMapper(SlmContractLineLvMapper.class).delete(lv.getSeq());
				}
			}
			String code="";
			String desc="";
			String contract_line_no="";
			if(lvList!=null){
				for(SlmContractLineLv lv:lvList){
					//				SlmContractLineLv lvt=new SlmContractLineLv();
					//				lvt.setContract_line_no(lv.getContract_line_no());
					//				lvt.setTol_code(lv.getTol_code());
					//				lvt.setLv_code(lv.getLv_code());
					//				lvt.setOrder_no(lv.getOrder_no());
					//				if(this.getMapper(SlmContractLineLvMapper.class).count(lvt)>0){
					//				}else{
					code+=lv.getTol_code()+","+lv.getLv_code()+";";
					desc+=lv.getTol_name()+","+lv.getLv_desc()+";";
					if(sum.getMemo1()!=null&&lv.getContract_pk()==null)
						lv.setContract_pk(sum.getMemo1());
					if(sum.getMemo2()!=null&&lv.getContract_line_no()==null)
						lv.setContract_line_no(sum.getMemo2());
					if(sum.getMemo3()!=null&&lv.getContract_line_seq()==null)
						lv.setContract_line_seq(sum.getMemo3());
					if(sum.getMemo4()!=null&&lv.getOrder_no()==null){
						SlmOrderInfo order=this.getMapper(SlmOrderInfoMapper.class).findById(sum.getMemo4());
						if(order!=null){
							lv.setOrder_no(order.getOrder_no());
							lv.setOrder_seq(order.getOrder_seq());
						}
					}
					lv.setSeq(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3));
					lv.setCreate_man_name(userName);
					lv.setCreate_man_no(userId);
					lv.setCreate_time(new Date());
					lv.setIs_flag("0");
					if(lv.getOrder_no()!=null&&!"".equals(lv.getOrder_no()))
						lv.setIs_flag("1");
					this.getMapper(SlmContractLineLvMapper.class).insert(lv);
					contract_line_no=lv.getContract_line_no();
					//				}
				}
			}
			if(sum.getMemo4()!=null&&!"".equals(sum.getMemo4())){
				SlmOrderInfo order=new SlmOrderInfo();
				order.setOrder_no(sum.getMemo4());
				order.setLv_code(code);
				order.setLv_desc(desc);
				this.getMapper(SlmOrderInfoMapper.class).update(order);
			}else{
				SlmContractLineInfo line=new SlmContractLineInfo();
				line.setLv_code(code);
				line.setLv_desc(desc);
				line.setContract_line_no(contract_line_no);
				this.getMapper(SlmContractLineInfoMapper.class).update(line);
			}
			rm.setState("200");
			rm.setMsgInfo("保存成功！");
		} catch (Exception e) {
			throw new Exception("保存精度等级失败！"+e.getMessage());
		}
		return rm;
	}
	/**
	 * 删除精度等级
	 * */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDeleteWeb(CommonPage<SlmContractLineLv> sum) throws Exception {
		ResultModel rm = new ResultModel();
		try {
			SlmContractLineLv lv=sum.getObject();
			if(!(lv!=null&&!"".equals(lv.getSeq()))){
				throw new Exception("请传入参数！");
			}
			this.getMapper(SlmContractLineLvMapper.class).delete(lv.getSeq());
			rm.setState("200");
			rm.setMsgInfo("删除成功！");
		} catch (Exception e) {
			throw new Exception("删除精度等级失败！" + e.getMessage());
		}
		return rm;
	}

	/**
	 * 通过加工计划号查询精度等级
	 * */
	public ResultModel doQueryByPlanNoWeb(SlmProcessPlan plan){
		ResultModel rm=new ResultModel();
		try {
			if(plan==null||plan.getPlan_no()==null){
				rm.setState("500");
				rm.setMsgInfo("请传入参数！");
				return rm;
			}
			plan=this.getMapper(SlmProcessPlanMapper.class).findById(plan.getPlan_no());
			if(plan==null){
				rm.setState("500");
				rm.setMsgInfo("未找到对应的加工计划，请确认参数格式[{'plan_no':''}]");
				return rm;
			}
			SlmContractLineLv lv=new SlmContractLineLv();
			lv.setContract_line_no(plan.getContract_line_no());
			lv.setContract_line_seq(plan.getContract_line_seq());
			lv.setOrder_seq(plan.getOrder_seq());
			lv.setOrder_no(plan.getOrder_no());
			lv.setIs_flag("1");
			List<SlmContractLineLv> lv_list=this.getMapper(SlmContractLineLvMapper.class).find(lv, 0, 0);
			rm.setData(lv_list);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！"+e.getMessage());
		}
		return rm;
	}
}
