/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-11-20
 * 描述            ：产品改判顺序配置表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.qcm.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import market.Com.mapper.ComPscMapper;
import market.Com.mapper.ComPscRDeliveryMapper;
import market.Com.vo.ComPsc;
import market.Com.vo.ComPscRDelivery;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.common.RequestModel;
import market.Qms.common.ReturnMsg;
import market.Qms.qcm.mapper.ComPscRPriMapper;
import market.Qms.qcm.service.ComPscRPriService;
import market.Qms.qcm.vo.ComPscRPri;

import org.springframework.transaction.annotation.Transactional;

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

@RemoteService(path = "ComPscRPriService")
public class ComPscRPriServiceImpl extends BaseServiceImpl<ComPscRPri, Object> implements ComPscRPriService {

	public ReturnMsg<List<ComPsc>> doQueryPhyBWeb(RequestModel<ComPsc> cp) {

		ResultModel rm = new ResultModel();
		ReturnMsg<List<ComPsc>> msg = new ReturnMsg<>();
		try {
			if (cp == null) {
				msg.setSuccess(false);
				msg.setMessage("请传入参数");
				return msg;
			}

			int pageIndex = cp.getPageIndex();
			int pageSize = cp.getPageSize();
			// PageModel<List<ComPsc>> pm = new
			// PageModel<List<ComPsc>>(pageIndex, pageSize);

			String condition = "VALIDFLAG='1'";
			if (cp.getModel().getSteel_name() != null && !"".equals(cp.getModel().getSteel_name())) {
				condition += " and steel_name like '%" + cp.getModel().getSteel_name() + "%'";
			}

			List<ComPsc> psc = this.getMapper(ComPscMapper.class).findWithCondition(null, condition, "psc",
					(pageIndex - 1) * pageSize, pageSize);
			msg.setTotal(this.getMapper(ComPscMapper.class).countWithCondition(null, condition));
			msg.setResult(psc);
			msg.setMessage("获取数据成功");
			msg.setSuccess(true);

		} catch (Exception e) {
			msg.setSuccess(false);
			msg.setMessage("查询失败");
			return msg;
		}

		return msg;

	}

	public ResultModel doQuerryRPsc(CommonPage<ComPsc> cp) {

		ResultModel rm = new ResultModel();

		try {

			if (cp == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数");
				return rm;
			}

			ComPsc psc = cp.getObject();

			if (psc == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入产品码");
				return rm;

			}
			if (psc.getPsc() == null || "".equals(psc.getPsc())) {
				rm.setState("500");
				rm.setMsgInfo("psc为空，请确认");
				return rm;

			}

			List<ComPsc> pscs = this.getMapper(ComPscMapper.class).findWithCondition(null,
					"VALIDFLAG='1' and psc !='" + psc.getPsc() + "'", null, 0, 0);

			for (ComPsc comPsc : pscs) {

				List<ComPscRPri> pri = this.getMapper(ComPscRPriMapper.class).findWithCondition(null,
						"PSC='" + psc.getPsc() + "' and PSC_PRI='" + comPsc.getPsc() + "'", null, 0, 0);
				if (pri != null && pri.size() > 0) {
					comPsc.setPsc_seq(pri.get(0).getPsc_seq());
				}
			}
			rm.setState("200");
			rm.setData(pscs);
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询异常，请确认");
			return rm;

		}

		return rm;
	}

	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel addRPsc(CommonPage<ComPscRPri> cp) throws Exception {

		ResultModel rm = new ResultModel();

		try {

			if (cp == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数");
				return rm;
			}

			List<ComPscRPri> pri = cp.getObjectList();
			if (pri == null || pri.size() < 1) {
				rm.setState("500");
				rm.setMsgInfo("请传入需要关联的psc");
				return rm;
			}

			// 数据检验
			Set<Long> seq = new HashSet<Long>();
			for (ComPscRPri pi : pri) {
				if (pi.getPsc() == null || "".equals(pi.getPsc())) {
					rm.setState("500");
					rm.setMsgInfo("需要关联的主psc为空，请确认");
					return rm;
				}

				if (pi.getPsc_pri() == null || "".equals(pi.getPsc_pri())) {
					rm.setState("500");
					rm.setMsgInfo("需要关联的副psc为空，请确认");
					return rm;
				}

				if (pi.getPsc_seq() == null) {
					rm.setState("500");
					rm.setMsgInfo("需要关联的副的psc顺序号不存在");
					return rm;
				}
				seq.add(pi.getPsc_seq());
			}

			if (seq.size() != pri.size()) {
				rm.setState("500");
				rm.setMsgInfo("需要关联的psc顺序号存在重复，请确认");
				return rm;
			}

			// 查询表看石否主psc有关联信息
			List<ComPscRPri> old = this.getMapper(ComPscRPriMapper.class).findWithCondition(null,
					"psc='" + pri.get(0).getPsc() + "'", null, 0, 0);

			if (old != null && old.size() > 0) {
				this.getMapper(ComPscRPriMapper.class).deleteByPsc(pri.get(0).getPsc());
			}

			this.getMapper(ComPscRPriMapper.class).insertBatch(pri);
			rm.setState("200");
			rm.setMsgInfo("保存成功");

		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("添加失败");
			throw new Exception(rm.getMsgInfo());
		}

		return rm;
	}

	/**
	 * 通过psc
	 * 
	 * @param cp
	 * @return
	 * @throws Exception
	 */

	public ResultModel selectByPsc(CommonPage<ComPsc> cp) {

		ResultModel rm = new ResultModel();
		List<ComPscRPri> pri = new ArrayList<>();
		if (cp != null && cp.getObject() != null && cp.getObject().getPsc() != null
				&& !"".equals(cp.getObject().getPsc())) {
			pri = this.getMapper(ComPscRPriMapper.class).findWithCondition(null,
					"PSC='" + cp.getObject().getPsc() + "' ", "SEQ", 0, 0);

			for (ComPscRPri comPscRPri : pri) {
				ComPsc psc = this.getMapper(ComPscMapper.class).findById(comPscRPri.getPsc_pri());
				comPscRPri.setPsc_desc(psc.getPsc_desc());
				comPscRPri.setProd_code(psc.getProd_code());
				comPscRPri.setProd_name(psc.getProd_name());
				comPscRPri.setStd_code(psc.getStd_code());
				comPscRPri.setStd_name(psc.getStd_name());
				comPscRPri.setSteel_code(psc.getSteel_code());
				comPscRPri.setSteel_name(psc.getSteel_name());

			}
			rm.setData(pri);

		}else{
			rm.setMsgInfo("订单原产品码数据为空，请确认");
		}
		rm.setState("200");

		return rm;

	}
	/**
	 * 改判用
	 * 
	 * @param cp
	 * @return
	 * @throws Exception
	 */

	public ResultModel selectByPscForJudge(CommonPage<ComPsc> cp) {
		ResultModel rm=new ResultModel();
		List<ComPscRPri>  pri=new ArrayList<>();
		List<ComPscRPri>  cprList=new ArrayList<>();
		List<ComPsc> pscList = new ArrayList<>();
		if (cp!=null && cp.getObject()!=null && cp.getObject().getPsc()!=null && !"".equals(cp.getObject().getPsc())) {
			
		    pri=this.getMapper(ComPscRPriMapper.class).findWithCondition(null, "PSC='"+ cp.getObject().getPsc()+"' ", "seq", 0, 0);
			if(pri != null && pri.size() > 0) {
				String pscStr = "(";
			    for(ComPscRPri cpr : pri) {
					if("(".equals(pscStr)) {
						pscStr += "'"+cpr.getPsc_pri()+"'";
					} else {
						pscStr += ",'"+cpr.getPsc_pri()+"'";
					}
				}
			    pscStr += ")";
			    pscList =this.getMapper(ComPscMapper.class).findWithCondition(null, "PSC not in "+pscStr, null, 0, 0);
			} else {
				pscList =this.getMapper(ComPscMapper.class).findWithCondition(null, "", null, 0, 0);
			}
		    for (ComPscRPri comPscRPri : pri) {
				ComPsc psc=this.getMapper(ComPscMapper.class).findById(comPscRPri.getPsc_pri());
				comPscRPri.setPsc_desc(psc.getPsc_desc());
				comPscRPri.setProd_code(psc.getProd_code());
				comPscRPri.setProd_name(psc.getProd_name());
				comPscRPri.setStd_code(psc.getStd_code());
				comPscRPri.setStd_name(psc.getStd_name());
				comPscRPri.setSteel_code(psc.getSteel_code());
				comPscRPri.setSteel_name(psc.getSteel_name());
				cprList.add(comPscRPri);
			}
		    for (ComPsc psc : pscList) {
		    	ComPscRPri cpr = new ComPscRPri();
		    	cpr.setPsc(psc.getPsc());
		    	cpr.setPsc_pri(psc.getPsc());
		    	cpr.setDelivery_state_code(psc.getDelivery_state_code());
		    	cpr.setProd_code(psc.getProd_code());
		    	cpr.setProd_name(psc.getProd_name());
		    	cpr.setStd_code(psc.getStd_code());
		    	cpr.setStd_name(psc.getStd_name());
		    	cpr.setSteel_code(psc.getSteel_code());
		    	cpr.setSteel_name(psc.getSteel_name());
		    	cprList.add(cpr);
		    }
		    rm.setData(cprList);
		}
		rm.setState("200");
		return rm;
	}
	

	public ReturnMsg<List<ComPsc>> doFindPage(RequestModel<ComPsc> cp) {

		ResultModel rm = new ResultModel();
		ReturnMsg<List<ComPsc>> msg = new ReturnMsg<>();
		try {
			if (cp == null) {
				msg.setSuccess(false);
				msg.setMessage("请传入参数");
				return msg;
			}

			int pageIndex = cp.getPageIndex();
			int pageSize = cp.getPageSize();
			ComPsc pscs = cp.getModel();

			// PageModel<List<ComPsc>> pm = new
			// PageModel<List<ComPsc>>(pageIndex, pageSize);

			String condition = "VALIDFLAG='1' and PSC!='" + pscs.getPsc() + "'";
			if (cp.getModel().getSteel_name() != null && !"".equals(cp.getModel().getSteel_name())) {
				condition += " and LOWER(STEEL_NAME) like '%'||LOWER('" + cp.getModel().getSteel_name() + "')||'%'";
			}

			List<ComPsc> psc = this.getMapper(ComPscMapper.class).findWithCondition(null, condition, "psc",
					(pageIndex - 1) * pageSize, pageSize);
			msg.setTotal(this.getMapper(ComPscMapper.class).countWithCondition(null, condition));
			msg.setResult(psc);
			msg.setMessage("获取数据成功");
			msg.setSuccess(true);

		} catch (Exception e) {
			msg.setSuccess(false);
			msg.setMessage("查询失败");
			return msg;
		}

		return msg;

	}

	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddPri(CommonPage<ComPscRPri> cp) throws Exception {

		ResultModel rm = new ResultModel();
		try {

			if (cp == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数");
				return rm;
			}

			List<ComPscRPri> pris = cp.getObjectList();

			if (pris == null || pris.size() < 1) {
				return rm;

			}
			List<ComPscRPri> tar = new ArrayList<ComPscRPri>();
		
			for (ComPscRPri pri : pris) {

				// 查询交货状态
				List<ComPscRPri> deliverys = pri.getDeliverys();
				// 查询上限值
				List<ComPscRPri> specH = pri.getSpecH();
				if (deliverys != null && deliverys.size() > 0) {

					if (specH != null && specH.size() > 0) {
						for (ComPscRPri d : deliverys) {

							for (ComPscRPri s : specH) {
								ComPscRPri random = new ComPscRPri();
								random.setPsc(pri.getPsc());
								random.setPsc_pri(pri.getPsc_pri());
								random.setDelivery_state_code(d.getDelivery_state_code());
								random.setDelivery_state_desc(d.getDelivery_state_desc());
								random.setSpec_max_h(s.getSpec_max_h());
								tar.add(random);
							}

						}

					} else {
						for (ComPscRPri d : deliverys) {
			
								ComPscRPri random = new ComPscRPri();
								random.setPsc(pri.getPsc());
								random.setPsc_pri(pri.getPsc_pri());
								random.setDelivery_state_code(d.getDelivery_state_code());
								random.setDelivery_state_desc(d.getDelivery_state_desc());
								tar.add(random);
							

						}

					}

				} else {
					for (ComPscRPri s : specH) {
						ComPscRPri random = new ComPscRPri();
						random.setPsc(pri.getPsc());
						random.setPsc_pri(pri.getPsc_pri());
						random.setSpec_max_h(s.getSpec_max_h());
						tar.add(random);
					}

				}

			}
			
			for (ComPscRPri comPscRPri : tar) {
				List<ComPscRPri> p=this.getMapper(ComPscRPriMapper.class).find(comPscRPri, 0, 0);
				if (p!=null && p.size()>0) {
					rm.setState("500");
					rm.setMsgInfo("psc["+comPscRPri.getPsc()+"]关联的记录存在重复，请确认");
				}
				
			}
			
			int seq = 0;	
			Long psc_seq = 0l;
			List<ComPscRPri> all = this.getMapper(ComPscRPriMapper.class).findWithCondition(null, null, "PSC_SEQ desc",
					0, 0);
			if (all != null && all.size() > 0) {
				psc_seq = all.get(0).getPsc_seq() + 1;
			} else {
				psc_seq = 1l;
			}
			
		
			
			
			
			//查询最大的seq
			List<ComPscRPri> big = this.getMapper(ComPscRPriMapper.class).findWithCondition(null, "PSC='"+pris.get(0).getPsc()+"'", "SEQ desc",
					0, 0);
			if (big==null || big.size()<1) {
				seq=1;
			}else{
				seq=big.get(0).getSeq()+1;
			}
		
			
			for (ComPscRPri ComPscRPri : tar) {
				ComPscRPri.setSeq(seq++);
				ComPscRPri.setPsc_seq(psc_seq++);
				this.getMapper(ComPscRPriMapper.class).insert(ComPscRPri);
				
			}
			
			rm.setState("200");
			rm.setMsgInfo("保存成功");
			

		} catch (Exception e) {

			throw new Exception("添加失败");
		}

		return rm;

	}
	
	
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDeletePri(CommonPage<ComPscRPri> cp) throws Exception {
		ResultModel rm=new ResultModel();
		
		try {
			if (cp == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数");
				return rm;
			}
			
			String psc=cp.getMemo1();
			if (psc==null || "".equals(psc)) {
				rm.setState("500");
				rm.setMsgInfo("请传入psc");
				return rm;
			}
			
			List<ComPscRPri> pris=cp.getObjectList();
			
			List<ComPscRPri> old=this.getMapper(ComPscRPriMapper.class).findWithCondition(null, "PSC='"+psc+"'", null, 0, 0);
			
			if ((pris==null || pris.size()<1) &&(old!=null && old.size()>0) ) {
				
				for (ComPscRPri comPscRPri : old) {
					this.getMapper(ComPscRPriMapper.class).delete(comPscRPri.getPsc(),comPscRPri.getPsc_pri(),comPscRPri.getPsc_seq());
				}
						
				
			}else if ((pris!=null && pris.size()>0) &&(old!=null && old.size()>0)) {
				
				
				
				for (ComPscRPri comPscRPri : old) {
					
					for (int i = 0; i < pris.size(); i++) {
						
						if (comPscRPri.getPsc().equals(pris.get(i).getPsc()) && comPscRPri.getPsc_pri().equals(pris.get(i).getPsc_pri()) &&comPscRPri.getPsc_seq().equals(pris.get(i).getPsc_seq())) {
							this.getMapper(ComPscRPriMapper.class).update(pris.get(i));
							break;
						}
						
						if (i==pris.size()-1) {
							this.getMapper(ComPscRPriMapper.class).delete(comPscRPri.getPsc(),comPscRPri.getPsc_pri(),comPscRPri.getPsc_seq());		
						}
						
					}
					
				}
				
			}
						
			
		} catch (Exception e) {
			throw new Exception("删除失败");
		}	
		
		return rm;
		
		
		
	}
	
	
	
	/**
	 * 根据psc查询交货状态
	 */
	
	public ResultModel getPscDelivery(String psc){
		
		ResultModel rm=new ResultModel();
		
		List<ComPscRDelivery> delivery=new ArrayList<ComPscRDelivery>();
		
		try {
			if (psc!=null && !"".equals(psc)) {
				delivery=this.getMapper(ComPscRDeliveryMapper.class).findWithCondition(null, "PSC='"+psc+"' and VALIDFLAG='1'", null, 0, 0);
			}
			
			
			rm.setData(delivery);
			rm.setState("200");
			return rm;
			
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败");
			return rm;
		}
		
		
		
	}
	

}
