/***文档注释***********************************************
 * 作者               :                   朱泽鹏
 * 创建日期      :                   2018.2.5
 * 描述               :        			材质项目补充接口
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Com.impl;

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

import market.Com.mapper.ComBasePhyBMapper;
import market.Com.mapper.ComBasePhyItemMapper;
import market.Com.mapper.ComBasePhyRPosMapper;
import market.Com.service.ComBasePhyItemService;
import market.Com.vo.ComBasePhyItem;
import market.Com.vo.ComBasePhyRPos;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;

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

@RemoteService(path="ComBasePhyItemService")
public class ComBasePhyItemServiceImpl extends BaseServiceImpl<ComBasePhyItem,Object> implements ComBasePhyItemService {
	
	/**
	 * WEB 检验项补充基础管理：查询 
	 * @param cp 
	 * memo1  传入包含无效是否打钩。不打钩："0";打钩："1"
	 * @return
	 */
	public ResultModel doQueryPhyBWeb(CommonPage<ComBasePhyItem> cp) {
		ResultModel rm = new ResultModel();
		int pageIndex = cp.getPageIndex();
		int pageSize = cp.getPageSize();
		PageModel<ComBasePhyItem> pm = new PageModel<ComBasePhyItem>(pageIndex,pageSize);
		try {
			ComBasePhyItem cbp = cp.getObject();
			String condition = "1=1";
			if ("0".equals(cp.getMemo1())) {
				condition += " and VALIDFLAG = '1'";
				cbp.setValidflag("1");
			}
			else
			{
			cbp.setValidflag(null);
			}
			if (cbp.getItem_name() != null && !"".equals(cbp.getItem_name())) {
				condition += " and upper(ITEM_name) like '%" + cbp.getItem_name().toUpperCase() + "%'";
				
			}

			List<ComBasePhyItem> phyi = this.getMapper(ComBasePhyItemMapper.class).findWithCondition(null, condition, "item_code",
					(pageIndex - 1) * pageSize, pageSize);
			
			pm.setRows(phyi);
			pm.setTotal(this.getMapper(ComBasePhyItemMapper.class).countWithCondition(null,condition));
			rm.setData(pm);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	
	/**
	 * WEB 检验项补充基础管理界面：新增
	 * @param cp
	 * @return
	 */
	public ResultModel doInsertPhyBWeb(CommonPage<ComBasePhyItem> cp){
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
			
			ComBasePhyItem cbpc = new ComBasePhyItem(); 
			 cbpc=cp.getObject();
			 if(cbpc.getItem_name() == null || "".equals(cbpc.getItem_name()) ||cbpc.getItem_style() == null || "".equals(cbpc.getItem_style()))
			 {
				 rm.setState("500");
					rm.setMsgInfo("检验名称、试验类型不能为空!"); 
					return rm;
			 }
			
			 String condition = "  Item_name = '" + cbpc.getItem_name() + "' and ITEM_STYLE = '" + cbpc.getItem_style() + "'";
				int num = this.getMapper(ComBasePhyItemMapper.class).countWithCondition(null, condition);

				if (num != 0) {
					rm.setState("500");
					rm.setMsgInfo("存在相同项目名称、不允许新增!");
					return rm;
				}
			 
			 
		String Phid = this.getMapper(ComBasePhyBMapper.class).findMaxPHyCodeI();
		
			cbpc.setItem_code(Phid);
			cbpc.setCreate_name(username);
			cbpc.setCreate_time(new Date());
			cbpc.setValidflag("1");//有效标志(1:有效，0:无效)
			

			
			this.getMapper(ComBasePhyItemMapper.class).insert(cbpc);
			rm.setState("200");
			 
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("新增失败!");
			ex.printStackTrace(); 
		}
		return rm;
	}
	
	
	
	/**
	 * WEB 检验项补充基础管理界面：修改
	 * @param cp
	 * @return
	 */
	public ResultModel doUpdatePhyBWeb(CommonPage<ComBasePhyItem> cp){
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String	Phy = cp.getObject().getItem_code();
		try {
			
			ComBasePhyItem cbpc = new ComBasePhyItem(); 
			 cbpc=cp.getObject();
			 String code = cbpc.getItem_code();
			 ComBasePhyRPos phy = new ComBasePhyRPos();
			 phy.setItemCodeT(code);
			 if(this.getMapper(ComBasePhyRPosMapper.class).count(phy) > 0)
			 {
				 rm.setState("500");
				 rm.setMsgInfo("检验项与位置、温度关系表存在关联子项，不能修改！");
				 return rm;
			 }
			 if (cbpc.getItem_name() != null && !"".equals(cbpc.getItem_name())) {
					String condition = "  Item_name = '" + cbpc.getItem_name() + "' and Item_code <> '" + cbpc.getItem_code() + "' and ITEM_STYLE = '" + cbpc.getItem_style() + "'";
					int num = this.getMapper(ComBasePhyItemMapper.class).countWithCondition(null, condition);

					if (num != 0) {
						rm.setState("500");
						rm.setMsgInfo("存在相同项目名称、不允许修改!");
						return rm;
					}
				}
			 
			 
				ComBasePhyItem cbp = this.getMapper(ComBasePhyItemMapper.class).findById(Phy);
				if (cbp!=null) {
					if (cbp.getValidflag().equals("1")) {
						cbpc.setUpdate_name(username);
						cbpc.setUpdate_time(new Date());
						cbpc.setValidflag("1");//有效标志(1:有效，0:无效)

						this.getMapper(ComBasePhyItemMapper.class).update(cbpc);
						rm.setState("200");
					}else {
						rm.setState("500");
						rm.setMsgInfo("选择的检验项补充基础管理的状态不是有效，无法修改!"); 
					}
				}else {
					rm.setState("500");
					rm.setMsgInfo("修改失败,选择的检验项补充基础管理在数据库中不存在!"); 
				} 
			//cbpc.setItem_code(Phid);
		
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("修改失败!");
			ex.printStackTrace(); 
		}
		return rm;
	}
	




/**
 * WEB 检验项补充基础管理界面：作废 
 * @param Phyode 检验项补充基础管理代码(主键)
 * @return
 */
public ResultModel doCancelPhyWeb(CommonPage<ComBasePhyItem> cp) {
	ResultModel rm = new ResultModel();
	String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
	String	Phy = cp.getObject().getItem_code();
	try {
		 ComBasePhyRPos phy = new ComBasePhyRPos();
		 phy.setItemCodeT(Phy);
		 if(this.getMapper(ComBasePhyRPosMapper.class).count(phy) > 0)
		 {
			 rm.setState("500");
			 rm.setMsgInfo("检验项与位置、温度关系表存在关联子项，不能修改！");
			 return rm;
		 }
		ComBasePhyItem cbp = this.getMapper(ComBasePhyItemMapper.class).findById(Phy);
		if (cbp!=null) {
			if (cbp.getValidflag().equals("1")) {
				cbp.setItem_code(Phy);
				cbp.setValidflag("0");
				cbp.setDelete_name(username);
				cbp.setDelete_time(new Date());
				this.getMapper(ComBasePhyItemMapper.class).update(cbp);
				rm.setState("200");
			}else {
				rm.setState("500");
				rm.setMsgInfo("选择的检验项补充基础管理的状态不是有效，无法作废!"); 
			}
		}else {
			rm.setState("500");
			rm.setMsgInfo("作废失败,选择的检验项补充基础管理在数据库中不存在!"); 
		} 
	} catch (Exception ex) {
		rm.setState("500");
		rm.setMsgInfo("作废失败!");
		ex.printStackTrace(); 
	}
	return rm;
}

/**
 * WEB 检验项补充基础管理界面：恢复 
 * @param Phyode 检验项补充基础管理代码(主键)
 * @return
 */
public ResultModel doRecoverPhyWeb(CommonPage<ComBasePhyItem> cp) {
	ResultModel rm = new ResultModel();
	
 String	Phy = cp.getObject().getItem_code();
	try { 
		ComBasePhyItem cbp = this.getMapper(ComBasePhyItemMapper.class).findById(Phy);
		if (cbp!=null) {
			if (cbp.getValidflag().equals("0")) {
				this.getMapper(ComBasePhyBMapper.class).DoRecoverPhyI(Phy);
				rm.setState("200");
			}else {
				rm.setState("500");
				rm.setMsgInfo("选择的检验项补充基础管理的状态不是无效，无法恢复!"); 
			}
		}else {
			rm.setState("500");
			rm.setMsgInfo("恢复失败,选择的检验项补充基础管理在数据库中不存在!"); 
		}  
	} catch (Exception ex) {
		rm.setState("500");
		rm.setMsgInfo("恢复失败!");
		ex.printStackTrace(); 
	}
	return rm;
}
	
	
}


