/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-09-04
 * 描述            ：询价/报价主表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Pms.Quote.impl;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import market.Com.pb.DoubleUtil;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Mat.mapper.PurMatClassMapper;
import market.Pms.Mat.vo.PurMatClass;
import market.Pms.Order.mapper.PurOrderCMapper;
import market.Pms.Order.mapper.PurOrderClauseDMapper;
import market.Pms.Order.mapper.PurOrderClauseMapper;
import market.Pms.Order.mapper.PurOrderMMapper;
import market.Pms.Order.vo.PurOrderC;
import market.Pms.Order.vo.PurOrderClause;
import market.Pms.Order.vo.PurOrderClauseD;
import market.Pms.Order.vo.PurOrderM;
import market.Pms.Quote.mapper.PurInquiryCMapper;
import market.Pms.Quote.mapper.PurInquiryMMapper;
import market.Pms.Quote.mapper.PurPurchaseCMapper;
import market.Pms.Quote.service.PurInquiryMService;
import market.Pms.Quote.vo.PurInquiryC;
import market.Pms.Quote.vo.PurInquiryM;
import market.Pms.Quote.vo.PurPurchaseC;

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.mapper.UserMapper;
import com.hnshituo.core.auth.service.HomeService;
import com.hnshituo.core.auth.vo.User;

@RemoteService(path="PurInquiryMService")
public class PurInquiryMServiceImpl extends BaseServiceImpl<PurInquiryM,Object> implements PurInquiryMService {
	/**
	 * 报价单编制-报价单信息
		object：
		报价单：inquiry_no
		供应商：supp_name
		数据状态：validflag
		评审状态：status
		startTime:开始时间
		endTime:结束时间
	 * @param sum
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurInquiryM> sum) {
		ResultModel rm = new ResultModel();
		PurInquiryM purInquiryM = sum.getObject();
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
//		String userName = (String) this.getSession().getAttribute(
//				HomeService.SESSION_USER_NAME);
		try {	
			User user2=this.getMapper(UserMapper.class).findById(userId);
			String str="";
			if("资材备件科".equals(user2.getDept_name())||"原料科".equals(user2.getDept_name())){
				str+=user2.getDept_name();
			} 
			rm.setData(this.getMapper(PurInquiryMMapper.class)
					.doQueryWeb(purInquiryM.getInquiry_no(),purInquiryM.getSupp_name(),purInquiryM.getValidflag(),
							purInquiryM.getStatus(),sum.getStartTime(),sum.getEndTime(),str));
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！" + ex.getMessage());
		}
		return rm;
	}
	/**
	 * 采购报价单信息新增
	 * 
	 * @param oci
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddWeb(CommonPage<PurInquiryM> oci) throws Exception {
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		PurInquiryM ppm=oci.getObject();
		User user2=this.getMapper(UserMapper.class).findById(userId);
		try {
				String INQUIRY_NO = "XJ"
						+ new SimpleDateFormat("yyyyMMdd").format(new Date());
			String maxD = this.getMapper(PurInquiryMMapper.class)
						.findMaxInquiryNo(INQUIRY_NO);
				if (maxD != null && maxD.length() > 4) {
					String seq = maxD.substring(maxD.length() - 4);
					int iseq = Integer.parseInt(seq) + 1;
					INQUIRY_NO += String.format("%04d", iseq);
				} else {
					INQUIRY_NO += "0001";
				}
				ppm.setBuyer_dept_code(user2.getDept_id());//设置部门
				ppm.setBuyer_dept_desc(user2.getDept_name());
				
				ppm.setBuyer_unit_code(user2.getDept_id());//资材备件科
				ppm.setBuyer_unit_desc(user2.getDept_name());
				
				ppm.setInquiry_no(INQUIRY_NO);
				ppm.setInquiry_desc(ppm.getInquiry_desc());//报价单描述
				ppm.setPurchase_desc(ppm.getPurchase_desc());//采购单描述
				ppm.setSupp_code(ppm.getSupp_code());
				ppm.setSupp_name(ppm.getSupp_name());//供应商信息
				ppm.setPurchase_no(ppm.getPurchase_no());
				ppm.setValidflag("10");// 默认10草稿
				ppm.setStatus("00"); // 默认00代提报
				ppm.setArchive_flag("0");//默认0未归档
				ppm.setCreate_userid(userId);
				ppm.setCreate_name(userName);
				ppm.setCreate_time(new Date());
				this.getMapper(PurInquiryMMapper.class).insert(ppm);
			rm.setState("200");
			rm.setMsgInfo("新增成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("新增失败，请联系管理员");
			throw ex;
		}
		return rm;
	}
	/**
	 * 修改
	 * 报价单编制-报价单信息
	 * @param sum
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doUpdateWeb(CommonPage<PurInquiryM> sum) 
			throws Exception {
		ResultModel rm = new ResultModel();
		try {
			PurInquiryM purM = sum.getObject();
			if(purM.getInquiry_no()==null || "".equals(purM.getInquiry_no())){
				throw new Exception("报价单号为空");
			}
			int count = this.getMapper(PurInquiryMMapper.class).countWithCondition(null, "inquiry_no='"+purM.getInquiry_no()+"'");
			if(count<=0){
				throw new Exception("报价单号【"+purM.getInquiry_no()+"】，在系统中不存在！");
			}
			this.getMapper(PurInquiryMMapper.class).update(purM);
			rm.setState("修改成功！");
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("修改失败！" + ex.getMessage());
		}
		return rm;
	}
	/**
	 * 报价单信息提报
	 * 
	 * @param oci
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel upPurDemandM(CommonPage<PurInquiryM> oci)
			throws Exception {
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if (oci.getObjectList() == null || oci.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}
		try {
			for (PurInquiryM pdm : oci.getObjectList()) {
				PurInquiryM pdm2=this.getMapper(PurInquiryMMapper.class).findById(pdm.getInquiry_no());
				pdm.setRemark(pdm2.getRemark());
				if ("10".equals(pdm2.getValidflag())
						&& ("00".equals(pdm2.getStatus()) || "40".equals(pdm2
								.getStatus()))) {
					// 主表
					pdm2.setStatus("10"); // 暂时提报
					pdm2.setValidflag("20"); // 暂时生效
					pdm2.setUpdate_userid(userId);
					pdm2.setUpdate_name(userName);
					pdm2.setUpdate_time(new Date());
					this.getMapper(PurInquiryMMapper.class).update(pdm2);
					// 从表
					PurInquiryC pdc = new PurInquiryC();
					pdc.setInquiry_no(pdm2.getInquiry_no());
					List<PurInquiryC> pdcList = this.getMapper(
							PurInquiryCMapper.class).find(pdc, 0, 0);
					if (pdcList.size() > 0) {
						for (PurInquiryC pDc : pdcList) {
							pDc.setStatus("10"); // 暂时提报
							pDc.setValidflag("20"); // 暂时生效
							pDc.setUpdate_userid(userId);
							pDc.setUpdate_name(userName);
							pDc.setUpdate_time(new Date());
							this.getMapper(PurInquiryCMapper.class).update(pDc);
						}
					}
				} else {
					rm.setState("500");
					rm.setMsgInfo("提报失败，请联系管理员");
					return rm;
				}
			}

			rm.setState("200");
			rm.setMsgInfo("提报成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("提报失败，请联系管理员");
			throw ex;
		}
		return rm;
	}
	
	/**
	 * 报价单信息提报撤销
	 * 
	 * @param oci
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel disPurInquiryM(CommonPage<PurInquiryM> oci)
			throws Exception {
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if (oci.getObjectList() == null || oci.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}
		try {
			for (PurInquiryM ppm : oci.getObjectList()) {
				PurInquiryM ppm2=this.getMapper(PurInquiryMMapper.class).findById(ppm.getInquiry_no());
				ppm.setRemark(ppm2.getRemark());
				if ("20".equals(ppm2.getValidflag())
						&& ("10".equals(ppm2.getStatus()) || "40".equals(ppm2
								.getStatus()))) {
					// 主表
					ppm2.setStatus("00"); // 置为待提报
					ppm2.setValidflag("10"); // 置为草稿状态
					ppm2.setUpdate_userid(userId);
					ppm2.setUpdate_name(userName);
					ppm2.setUpdate_time(new Date());
					this.getMapper(PurInquiryMMapper.class).update(ppm2);
					// 从表
					PurInquiryC ppc = new PurInquiryC();
					ppc.setInquiry_no(ppm2.getInquiry_no());
					List<PurInquiryC> ppcList = this.getMapper(
							PurInquiryCMapper.class).find(ppc, 0, 0);
					if (ppcList.size() > 0) {
						for (PurInquiryC ppc2: ppcList) {
							ppc2.setStatus("00"); //  置为待提报
							ppc2.setValidflag("10"); // 置为草稿状态
							ppc2.setUpdate_userid(userId);
							ppc2.setUpdate_name(userName);
							ppc2.setUpdate_time(new Date());
							this.getMapper(PurInquiryCMapper.class).update(ppc2);
						}
					}
				} else {
					rm.setState("500");
					rm.setMsgInfo("当前数据不能提报撤销！");
					return rm;
				}
			}

			rm.setState("200");
			rm.setMsgInfo("提报撤销成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("提报撤销失败，请联系管理员");
			throw ex;
		}
		return rm;
	}
	/**
	 * 报价单信息删除
	 * 
	 * @param oci
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDelete(CommonPage<PurInquiryM> oci) throws Exception {
		ResultModel rm = new ResultModel();
		if (oci.getObjectList() == null || oci.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}
		try {
			for (PurInquiryM pdc : oci.getObjectList()) {
				//数据状态无效或草稿状态。审批状态待提报、拒绝、退回。可进行删除操作。
				if (("00".equals(pdc.getValidflag())||"10".equals(pdc.getValidflag()))
						&& ("00".equals(pdc.getStatus()) || "30".equals(pdc
								.getStatus())|| "40".equals(pdc
								.getStatus()))) {
				//报价单信息主表
				this.getMapper(PurInquiryMMapper.class).delete(pdc.getInquiry_no());	
				//报价单信息从表
				this.getMapper(PurInquiryCMapper.class).deleteByInquiryMNo(pdc.getInquiry_no());	
				}else{
					rm.setState("500");
					rm.setMsgInfo("该数据不能删除！");
					return rm;
				}		
			}
			rm.setState("200");
			rm.setMsgInfo("删除成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("删除失败，请联系管理员");
			throw ex;
		}
		return rm;
	}
	
	/**
	 * 报价单信息审批
	 * 
	 * @param oci
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel appPurInquiryM(CommonPage<PurInquiryM> oci)
			throws Exception {
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if (oci.getObjectList() == null || oci.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}
		String memo1=oci.getMemo1();
		if(memo1==null&&"".equals(memo1)){
			rm.setState("500");
			rm.setMsgInfo("请选择操作！");
			return rm;
		}
		try {
			Date now =new Date();
			for (PurInquiryM ppm2 : oci.getObjectList()) {
				PurInquiryM pdm=this.getMapper(PurInquiryMMapper.class).findById(ppm2.getInquiry_no());
				pdm.setRemark(ppm2.getRemark());
				if ("10".equals(pdm.getStatus()) ) {
					if("1".equals(memo1)){
						// 修改报价单主表
						pdm.setStatus("50"); //审批通过
						pdm.setUpdate_userid(userId);
						pdm.setUpdate_name(userName);
						pdm.setUpdate_time(now);
						this.getMapper(PurInquiryMMapper.class).update(pdm);
						// 从表
						PurInquiryC pdc = new PurInquiryC();
						pdc.setInquiry_no(ppm2.getInquiry_no());
						pdc.setStatus("10");
						List<PurInquiryC> pdcList = this.getMapper(
								PurInquiryCMapper.class).find(pdc, 0, 0);
						
						//此处为资材备件信息审批通过之后新增合同信息所用
						if (pdcList!=null && pdcList.size() > 0) {
							boolean  b=false;//合同条款信息新增标志符，默认为false
							
							// 处理采购合同主表 PurOrderM
							PurOrderM  pom =new  PurOrderM();
							pom.setContract_pk(UUID.randomUUID().toString());
							String contract_no = "ZC"+ new SimpleDateFormat("yyyyMMdd").format(now);
							String maxD1 = this.getMapper(PurOrderMMapper.class).findContractNo(contract_no);
							if (maxD1 != null && maxD1.length() > 4) {
								String seq = maxD1.substring(maxD1.length() - 4);
								int iseq = Integer.parseInt(seq) + 1;
								contract_no += String.format("%04d", iseq);
							} else {
								contract_no += "0001";
							}
							pom.setContract_no(contract_no);
							pom.setSupp_contract_no(pom.getContract_no());
							pom.setSupp_code(pdm.getSupp_code());
							pom.setSupp_name(pdm.getSupp_name());//供应商
							pom.setContract_class("601103");//合同分类默认场内合同
							pom.setContract_type("601001");//合同类别默认资材备件
							pom.setMode_code("OM2018071001");
							pom.setMode_name("通用模板");//默认模板
							pom.setTitle_code("TT2018070601");
							pom.setTitle_name("新余新钢优特钢带有限公司");//默认抬头公司
							pom.setReceiver_code("DZ0010");
							pom.setReceiver_name("优特钢带");//默认收货地点
							pom.setCurrency_code("600501");//货币代码
							pom.setCurrency_name("人民币");//默认货币
							pom.setPayment_code("602007");
							pom.setPayment_name("其它支付");//默认支付方式
							pom.setTrans_clause_code("601303");
							pom.setTrans_clause_name("运输方责任");//默认运输方责任
							pom.setValidflag("10");// 默认10草稿
							pom.setStatus("00"); // 默认00代提报
							pom.setSign_date(now);// 默认日期
							pom.setBal_year_month(new SimpleDateFormat("yyyyMM").format(now));
							//日期处理格式
							Calendar calendar2 = Calendar.getInstance();
							calendar2.add(Calendar.DATE, 7);
							Date date = calendar2.getTime();
							pom.setStart_date(date); // 默认日期
							pom.setCreate_userid(userId);
							pom.setCreate_name(userName);
							pom.setCreate_time(now);
							
							for (PurInquiryC pDc : pdcList) {
								//判断物料是否属于资材备件科
								PurMatClass pmc=this.getMapper(PurMatClassMapper.class).findById(pDc.getMat_class_code());
								if(pmc!=null && pmc.getMat_class_code()!=null 
										&& pmc.getMat_class_code().contains("W02")){
									//  Start*************属于资材备件******************
									// 1. 修改报价单子表信息
									pDc.setStatus("50"); // 审批通过
									if(pDc.getBid_qty()==null){
										pDc.setBid_qty(pDc.getQty());
									}//中标数量
									if(pDc.getBid_win_without_tax_price()==null){
										pDc.setBid_win_without_tax_price(pDc.getRes_without_tax_price());
									}//不含税单价
									if(pDc.getBid_win_with_tax_price()==null){
										pDc.setBid_win_with_tax_price(pDc.getRes_with_tax_price());
									}//含税单价
									if(pDc.getBid_without_tax_amt()==null){
										pDc.setBid_without_tax_amt(pDc.getRes_without_tax_amt());
									}//不含税总额
									if(pDc.getBid_with_tax_amt()==null){
										pDc.setBid_with_tax_amt(pDc.getRes_with_tax_amt());
									}//含税总额
									pDc.setUpdate_userid(userId);
									pDc.setUpdate_name(userName);
									pDc.setUpdate_time(now);
									this.getMapper(PurInquiryCMapper.class).update(pDc);
									// 2. 新增采购合同主信息
									PurOrderM  pom2=this.getMapper(PurOrderMMapper.class).findById(pom.getContract_pk());
									if(pom2==null){
										this.getMapper(PurOrderMMapper.class).insert(pom);
										b=true;//标志符变为true；
									}
									// 3. 新增采购合同行信息
									PurOrderC poc =new PurOrderC();
									poc.setOrder_pk(UUID.randomUUID().toString());
									String order_no=pom.getContract_no();
									String maxD = this.getMapper(PurOrderCMapper.class).getMaxNo(pom.getContract_no());
									if (maxD != null && maxD.length() > 4) {
										String seq = maxD.substring(maxD.length() - 3);
										int iseq = Integer.parseInt(seq) + 1;
										order_no += String.format("%03d", iseq);
									} else {
										order_no += "001";
									}
									poc.setOrder_no(order_no);
									poc.setContract_pk(pom.getContract_pk());//设置合同号
									poc.setContract_no(pom.getContract_no());
									poc.setSupp_code(pom.getSupp_code());
									poc.setSupp_name(pom.getSupp_name());
									poc.setValidflag("10");// 默认10草稿
									poc.setStatus("00"); // 默认00代提报
									poc.setCreate_userid(userId);
									poc.setCreate_name(userName);
									poc.setCreate_time(new Date());
									poc.setTax_amt(DoubleUtil.sub(pDc.getBid_with_tax_amt(), pDc.getBid_without_tax_amt()));//稅額
									PurInquiryC  pic=this.getMapper(PurInquiryCMapper.class).findById(pDc.getInquiry_sqe());
									poc.setInquiry_no(pic.getInquiry_no());//报价单号
									PurPurchaseC ppc=this.getMapper(PurPurchaseCMapper.class).findById(pDc.getPurchase_sqe());
									poc.setPurchase_no(ppc.getPurchase_no());//采购单号
									poc.setPlan_sqe(ppc.getPlan_sqe());//计划行号
									poc.setTax_rate_val("600604");//默认税率16
									poc.setBid_no(pDc.getBid_no());//标段号
									poc.setRemark(pic.getBid_win_reason());//默认描述
		                            poc.setPurchase_sqe(ppc.getPurchase_sqe());//采购单行号
		                            poc.setInquiry_sqe(pic.getInquiry_sqe());//报价单号
									poc.setMat_class_code(pDc.getMat_class_code());//物料分类
									poc.setMat_class_name(pDc.getMat_class_name());							
									poc.setItem_code(pDc.getItem_code());//物料编码
									poc.setItem_name(pDc.getItem_name());
									poc.setItem_desc(pDc.getItem_desc());
									poc.setItem_idx(pDc.getItem_idx());
									poc.setSpec_name(pDc.getSpec_name());//规格	
									poc.setAttr_code(pDc.getAttr_code());//属性
									poc.setAttr_name(pDc.getAttr_name());
						            poc.setStart_date(date);//默认交货日期
									poc.setDelivery_location_code(pDc.getDelivery_location_code());
									poc.setDelivery_location(pDc.getDelivery_location());//交货地址
									poc.setReceive_type(pDc.getReceive_type_name());//收货方式 
									poc.setQty(pDc.getRes_qty());//默认数量为中标数量
									poc.setUom_code(pDc.getUom_code());//计量单位
									poc.setUom_name(pDc.getUom_name());
									poc.setPrice_with_tax(pDc.getRes_with_tax_price());//默认单价为中标含税单价
									poc.setPrice_without_tax(pDc.getRes_without_tax_price());//默认不含税单价为中标不含税单价
									poc.setWith_tax_amt(pDc.getRes_with_tax_amt());//默认含税总额为中标量总额
									poc.setWithout_tax_amt(pDc.getRes_without_tax_amt());//不含税
									poc.setRemark(pDc.getRemark());
									poc.setReceiver_code(pom.getReceiver_code());
									poc.setReceiver_name(pom.getReceiver_name());
									poc.setBuyer_dept_code(pDc.getBuyer_dept_code());//得到部门
									poc.setBuyer_dept_desc(pDc.getBuyer_dept_desc());//
									poc.setBuyer_unit_code(pDc.getBuyer_unit_code());//得到科室
									poc.setBuyer_unit_desc(pDc.getBuyer_unit_desc());//
									poc.setTrans_type_code("602201");
									poc.setTrans_type_name("汽运");
									this.getMapper(PurOrderCMapper.class).insert(poc);
									//  End  *************属于资材备件****************** 
								}else {
									// 不属于 资材备件
									pDc.setStatus("50"); // 审批通过
									pDc.setUpdate_userid(userId);
									pDc.setUpdate_name(userName);
									pDc.setUpdate_time(now);
									this.getMapper(PurInquiryCMapper.class).update(pDc);
								}
							}
							// 新增  合同条款明细
							if(b){
								String mode_code = pom.getMode_code();
								List<PurOrderClause> purOrederClauseList = this.getMapper(
										PurOrderClauseMapper.class).FindBYmodeCode(mode_code);
								PurOrderM order = this.getMapper(PurOrderMMapper.class).findById(pom.getContract_pk());
								if (order != null) {
									for (PurOrderClause poc : purOrederClauseList) {
										PurOrderClauseD clause = new PurOrderClauseD();
										clause.setClause_code(poc.getClause_code());
										clause.setContract_pk(pom.getContract_pk());
										List<PurOrderClauseD> clauseList = this.getMapper(PurOrderClauseDMapper.class).find(clause, 0, 0);
										if (clauseList.size() > 0) {
											rm.setState("500");
											rm.setMsgInfo("新增失败！当前条款" + poc.getClause_code()+ "已存在该合同中！"); 
											return rm;
										}

										// 新增  合同条款明细
										PurOrderClauseD pocd = new PurOrderClauseD();
										pocd.setClause_no(this.getMapper(PurOrderClauseDMapper.class).getNo(
												"TK" + new SimpleDateFormat("yyyyMMdd")
																.format(now)));// TK+YYYYMMDD+3位流水
										pocd.setContract_pk(order.getContract_pk());
										pocd.setContract_no(order.getContract_no());
										pocd.setClause_code(poc.getClause_code());
										pocd.setClause_name(poc.getClause_name());
										pocd.setClause_desc(poc.getClause_desc());
										pocd.setClause_order_by(poc.getClause_order_by());
										pocd.setCreate_userid(userId);
										pocd.setCreate_name(userName);
										pocd.setCreate_time(now);
										this.getMapper(PurOrderClauseDMapper.class).insert(pocd); 
									}
								} else {
									rm.setState("500");
									rm.setMsgInfo("新增失败！无效的合同数据");
									return rm;
								}
							}
						} 
					}
					if("2".equals(memo1)){ 
						// 主表
						pdm.setStatus("40"); //审批退回
						pdm.setUpdate_userid(userId);
						pdm.setUpdate_name(userName);
						pdm.setUpdate_time(now);
						this.getMapper(PurInquiryMMapper.class).update(pdm);
						// 从表
						PurInquiryC pdc = new PurInquiryC();
						pdc.setInquiry_no(ppm2.getInquiry_no());
						List<PurInquiryC> pdcList = this.getMapper(
								PurInquiryCMapper.class).find(pdc, 0, 0);
						if (pdcList.size() > 0) {
							for (PurInquiryC pDc : pdcList) {
								pDc.setStatus("40"); // 审批退回
								pDc.setUpdate_userid(userId);
								pDc.setUpdate_name(userName);
								pDc.setUpdate_time(now);
								this.getMapper(PurInquiryCMapper.class).update(pDc);
							}
						} 
					}
					 if("3".equals(memo1)){
						// 主表
						pdm.setStatus("30"); //审批拒绝
						pdm.setValidflag("00");//数据状态变为无效
						pdm.setUpdate_userid(userId);
						pdm.setUpdate_name(userName);
						pdm.setUpdate_time(now);
						this.getMapper(PurInquiryMMapper.class).update(pdm);
						// 从表
						PurInquiryC pdc = new PurInquiryC();
						pdc.setInquiry_no(ppm2.getInquiry_no());
						List<PurInquiryC> pdcList = this.getMapper(PurInquiryCMapper.class).find(pdc, 0, 0);
						if (pdcList.size() > 0) {
							for (PurInquiryC pDc : pdcList) {
								pDc.setStatus("30"); // 审批拒绝
								pDc.setValidflag("00");//数据状态变为无效
								pDc.setUpdate_userid(userId);
								pDc.setUpdate_name(userName);
								pDc.setUpdate_time(now);
								this.getMapper(PurInquiryCMapper.class).update(pDc);
							}
						}
					} 
				} else {
					rm.setState("500");
					rm.setMsgInfo("数据状态不为待审批，请刷新界面！");
					return rm;
				} 
			}

			rm.setState("200");
			rm.setMsgInfo("审批成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("审批失败，请联系管理员");
			throw ex;
		}
		return rm;
	}

}
