/***文档注释***********************************************
 * 作者               :                   朱泽鹏
 * 创建日期      :                   2018.2.5
 * 描述               :        			检验项与位置、温度接口
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Com.impl;

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

import market.Com.mapper.ComBasePhyItemMapper;
import market.Com.mapper.ComBasePhyMapper;
import market.Com.mapper.ComBasePhyRPosMapper;
import market.Com.service.ComBasePhyRPosService;
import market.Com.vo.ComBasePhy;
import market.Com.vo.ComBasePhyItem;
import market.Com.vo.ComBasePhyRPos;
import market.Com.vo.ComBasePhyadd;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;

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;

@RemoteService(path = "ComBasePhyRPosService")
public class ComBasePhyRPosServiceImpl extends BaseServiceImpl<ComBasePhyRPos, Object>
		implements ComBasePhyRPosService {

	public ResultModel doQueryinitialization() {
		ResultModel rm = new ResultModel();

		ComBasePhyItem arg = new ComBasePhyItem();
		try {

			arg.setValidflag("1");
			arg.setItem_style("B");

			rm.setData(this.getMapper(ComBasePhyItemMapper.class).find(arg, 0, 0));
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
		}

		return rm;
	}

	// 主记录查询
	public ResultModel doQueryPhy() {
		ResultModel rm = new ResultModel();

		ComBasePhy arg = new ComBasePhy();
		try {

			arg.setValidflag("1");

			rm.setData(this.getMapper(ComBasePhyMapper.class).QuerybySqlPos());
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
		}

		return rm;
	}

	public ResultModel doQueryPhyPos(CommonPage<ComBasePhyRPos> cp) {
		ResultModel rm = new ResultModel();

		ComBasePhyRPos arg = new ComBasePhyRPos();
		arg = cp.getObject();

		try {

			List<ComBasePhyadd> arg1 = new ArrayList<ComBasePhyadd>();
			arg1 = this.getMapper(ComBasePhyRPosMapper.class).doselect(arg.getPhyCodeS(), arg.getItemCodeT(),
					arg.getValidflag());

			rm.setData(arg1);
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
		}

		return rm;
	}
	
	/**
	 * 获取基础温度数据
	 * @param cp
	 * @return
	 */
	public ResultModel doGetPhy(CommonPage<ComBasePhyRPos> cp) {
		ResultModel rm = new ResultModel();

		ComBasePhyRPos arg = new ComBasePhyRPos();
		arg = cp.getObject();

		try {

			List<ComBasePhyItem> items=new ArrayList<ComBasePhyItem>();
			
			if ("1".equals(arg.getValidflag())) {
				items=this.getMapper(ComBasePhyItemMapper.class).findWithCondition(null, "item_style='B' and validflag='1'", "ITEM_CODE", 0, 0);
				
			}else {
				items=this.getMapper(ComBasePhyItemMapper.class).findWithCondition(null, "item_style='B'", "ITEM_CODE", 0, 0);

			}
			rm.setData(items);
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
		}

		return rm;
	}
	
	/**
	 * 获取关联的pos
	 * @param cp
	 * @return
	 */
	public ResultModel getPhyRPos(CommonPage<ComBasePhyRPos> cp) {
		
		ResultModel rm=new ResultModel();
		ComBasePhyRPos arg = new ComBasePhyRPos();
		arg = cp.getObject();
		List<ComBasePhyRPos>  pos=new ArrayList<>();
		if (arg.getPhyCodeS()!=null && !"".equals(arg.getPhyCodeS())) {
			if ("1".equals(arg.getValidflag())) {
			pos=this.getMapper(ComBasePhyRPosMapper.class).findWithCondition(null, "PHY_CODE_S='"+arg.getPhyCodeS()+"' and VALIDFLAG='1'", null, 0, 0);	
			  }else{
				  pos=this.getMapper(ComBasePhyRPosMapper.class).findWithCondition(null, "PHY_CODE_S='"+arg.getPhyCodeS()+"'", null, 0, 0);	
					  
			  }
			
			}
		rm.setState("200");
		rm.setData(pos);
		return rm;
			
	}
	

	public ResultModel doSavePos(CommonPage<ComBasePhyRPos> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ComBasePhyRPos arg = new ComBasePhyRPos();
		try {
			arg = cp.getObject();
			arg.setValidflag("1");
			arg.setCreateName(username);
			arg.setCreateTime(new Date());
			List<ComBasePhyadd> arg1 = new ArrayList<ComBasePhyadd>();
			arg1 = this.getMapper(ComBasePhyRPosMapper.class).doselect(arg.getPhyCodeS(), arg.getItemCodeT(), null);
			if (arg1.size() == 0) {
				this.getMapper(ComBasePhyRPosMapper.class).doinsert(arg);
				rm.setState("200");
			} else {
				rm.setState("500");
				rm.setMsgInfo("关系已存在，或已作废不能重复添加!");

			}

		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
		}

		return rm;
	}
	
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSavePos2(CommonPage<ComBasePhyRPos> cp) throws Exception {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		List<ComBasePhyRPos>  arg=new ArrayList<ComBasePhyRPos>();
		
		try {
			if (cp.getMemo1()==null) {
				rm.setMsgInfo("请传入检验项");
				rm.setState("500");
				return rm;
			}
			String phy_code_s=cp.getMemo1();
			
			//查询检验项目是否存在已关联信息
			List<ComBasePhyRPos> old=this.getMapper(ComBasePhyRPosMapper.class).findWithCondition(null, "PHY_CODE_S='"+phy_code_s+"'", null, 0, 0);
		    
			
			if (cp!=null) {
				//传入集合
				arg = cp.getObjectList();
				
					
				if (old!=null && old.size()>0) {
					//判断老关联的数据是否取消关联
					
					if (arg!=null && arg.size()>0) {
						
						for (ComBasePhyRPos cs : old) {
							
							
							for (int i = 0; i <arg.size(); i++) {
								//老数据如果与新数据匹配，更新一波
								if (arg.get(i).getItemCodeT().equals(cs.getItemCodeT())) {
									arg.get(i).setUpdateName(username);
									arg.get(i).setUpdateTime(new Date());
									this.getMapper(ComBasePhyRPosMapper.class).update(arg.get(i));
									break;
								}
								
								if (i==arg.size()-1) {
									//如果未找到，作废信息
									cs.setValidflag("0");
									cs.setUpdateName(username);
									cs.setUpdateTime(new Date());
									this.getMapper(ComBasePhyRPosMapper.class).update(cs);
								}
							}
							
						}
						
						//存在没有的关系，添加关系
						for (ComBasePhyRPos cs : arg) {
							
							for (int i = 0; i < old.size(); i++) {
								
								if (cs.getItemCodeT().equals(old.get(i).getItemCodeT())) {
									/*cs.setUpdateName(username);
									cs.setUpdateTime(new Date());
									this.getMapper(ComBasePhyRPosMapper.class).update(cs);*/
									break;
									
								}
								
								if (i== old.size()-1) {
									cs.setCreateName(username);
									cs.setCreateTime(new Date());
									this.getMapper(ComBasePhyRPosMapper.class).insert(cs);
								}
								
							}
							
						}
						
						
					}else {
						
						for (ComBasePhyRPos cs : old) {
							
							cs.setValidflag("0");
							cs.setUpdateName(username);
							cs.setUpdateTime(new Date());
							this.getMapper(ComBasePhyRPosMapper.class).update(cs);
						}
						
					}
					
					
				}else {
					
					for (ComBasePhyRPos cs : arg) {
						cs.setCreateName(username);
						cs.setCreateTime(new Date());
						this.getMapper(ComBasePhyRPosMapper.class).insert(cs);
					}			
					
				}	
				
			}
			
			
			
			rm.setState("200");
			
		} catch (Exception e) {
			
			rm.setState("500");
			rm.setMsgInfo("保存失败");
			throw new Exception(rm.getMsgInfo());
		}

		return rm;
	}

	
	
	
	

	public ResultModel doRecoverPos(CommonPage<ComBasePhyRPos> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		// ComBasePhyRPos arg = new ComBasePhyRPos();
		try {
			for (ComBasePhyRPos arg : cp.getObjectList()) {
				// arg = cp.getObject();
				arg.setValidflag("1");
				if (arg.getPhyCodeS() == null || "".equals(arg.getPhyCodeS()) || arg.getItemCodeT() == null
						|| "".equals(arg.getItemCodeT())) {
					rm.setState("500");
					rm.setMsgInfo("检验项、温度不能为空!");
				} else {

					List<ComBasePhyadd> arg1 = new ArrayList<ComBasePhyadd>();
					arg1 = this.getMapper(ComBasePhyRPosMapper.class).doselect(arg.getPhyCodeS(), arg.getItemCodeT(),
							null);

					if (arg1.size() != 0) {
						if (arg1.get(0).getValidflag().equals("0")) {
							arg.setUpdateName(username);
							arg.setUpdateTime(new Date());
							this.getMapper(ComBasePhyRPosMapper.class).doUpdate(arg);
							rm.setState("200");
						} else {
							rm.setState("500");
							rm.setMsgInfo("选择的关系数据的状态不是无效，无法恢复!");
						}
					} else {
						rm.setState("500");
						rm.setMsgInfo("恢复失败,选择的关系数据在数据库中不存在!");
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
		}

		return rm;
	}

	public ResultModel doCancelPos(CommonPage<ComBasePhyRPos> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		// ComBasePhyRPos arg = new ComBasePhyRPos();

		try {

			for (ComBasePhyRPos arg : cp.getObjectList()) {
				// arg = cp.getObject();
				if (arg.getPhyCodeS() == null || "".equals(arg.getPhyCodeS()) || arg.getItemCodeT() == null
						|| "".equals(arg.getItemCodeT())) {
					rm.setState("500");
					rm.setMsgInfo("检验项、温度不能为空!");
				} else {
					arg.setValidflag("0");
					List<ComBasePhyadd> arg1 = new ArrayList<ComBasePhyadd>();
					arg1 = this.getMapper(ComBasePhyRPosMapper.class).doselect(arg.getPhyCodeS(), arg.getItemCodeT(),
							null);

					if (arg1.size() != 0) {
						if (arg1.get(0).getValidflag().equals("1")) {

							arg.setDeleteName(username);
							arg.setDeleteTime(new Date());

							this.getMapper(ComBasePhyRPosMapper.class).doUpdateDele(arg);
							rm.setState("200");
						} else {
							rm.setState("500");
							rm.setMsgInfo("选择的关系数据的状态不是有效，无法作废!");
						}
					} else {
						rm.setState("500");
						rm.setMsgInfo("作废失败,选择的关系数据在数据库中不存在!");
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
		}

		return rm;
	}

}
