/***文档注释***********************************************
 * 作者               :                   刘学
 * 创建日期      :                   2017.12.4
 * 描述               :        			质量检验项产品标准与取样码试验标准关系
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Com.impl;

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

import market.Com.mapper.ComBaseStdRTestMapper;
import market.Com.service.ComBaseStdRTestService;
import market.Com.vo.ComBaseStdRTest;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.common.DataHelper;
import market.Qms.metallurgy.mapper.ComBaseStdMapper;
import market.Qms.metallurgy.vo.ComBaseStd;

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 = "ComBaseStdRTestService")
public class ComBaseStdRTestServiceImpl extends BaseServiceImpl<ComBaseStdRTest, String> implements ComBaseStdRTestService {

	/****
	 * web 试样组与试验标准配置界面：查询
	 * @param cp
	 * Memo1 传入试样组是否打钩，不打勾："0";打勾"1"
	 * Memo2 传入试验标准是否打钩，不打勾："0";打勾"1"
	 * @return
	 */
	public ResultModel doQueryStdRTestWeb(CommonPage<ComBaseStdRTest> cp) {
		ResultModel rm = new ResultModel();
		int pageIndex = cp.getPageIndex();
		int pageSize = cp.getPageSize();
		PageModel<ComBaseStdRTest> pm = new PageModel<ComBaseStdRTest>(pageIndex, pageSize);
		try {
			ComBaseStdRTest cbs = cp.getObject();
			String condition = "1=1";
//			if ("0".equals(cp.getMemo1())) {
//				condition += " and VALIDFLAG = '1'";
//				cbs.setValidflag("1");
//			}
			if ("1".equals(cp.getMemo2()) && cbs.getStdNameTest() != null && !"".equals(cbs.getStdNameTest())) {
				condition += " and upper(STD_NAME_TEST) like '%" + cbs.getStdNameTest().toUpperCase() + "%'";
			}
			if ("1".equals(cp.getMemo1()) && cbs.getPhyNameM() != null && !"".equals(cbs.getPhyNameM())) {
				condition += " and upper(PHY_NAME_M) like '%" + cbs.getPhyNameM().toUpperCase() + "%'";
			}

			pm.setRows(this.getMapper(ComBaseStdRTestMapper.class).findWithCondition(null, condition, "CREATE_TIME desc",
							(pageIndex - 1) * pageSize, pageSize));
			pm.setTotal(this.getMapper(ComBaseStdRTestMapper.class).countWithCondition(null, condition));
			rm.setData(pm);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败："+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	/*****
	 * 1月5日上午9点修改
	 * 试验标准信息查询
	 * @param cp
	 * @return
	 */
	public ResultModel doQueryStd(CommonPage<ComBaseStd> cp){
		ResultModel rm = new ResultModel();
		int pageIndex = cp.getPageIndex();
		int pageSize = cp.getPageSize();
		PageModel<ComBaseStd> pm = new PageModel<ComBaseStd>(pageIndex, pageSize);
		try {
			String condition = "1=1 and STD_TYPE = 'S' and VALIDFLAG = '1'";
			pm.setRows(this.getMapper(ComBaseStdMapper.class).findWithCondition(null, condition, "CREATE_TIME desc",
					(pageIndex - 1) * pageSize, pageSize));
			pm.setTotal(this.getMapper(ComBaseStdMapper.class).countWithCondition(null, condition));
			rm.setData(pm);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败："+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	
	/*****
	 * @param cp
	 * 
	 * 
	 * @return
	 */
	public ResultModel doQueryStdGorX(CommonPage<ComBaseStd> cp){
		ResultModel rm = new ResultModel();
		int pageIndex = cp.getPageIndex();
		int pageSize = cp.getPageSize();
		PageModel<ComBaseStd> pm = new PageModel<ComBaseStd>(pageIndex, pageSize);
		try {
			String condition = "1=1 and STD_TYPE in ('G','X') and VALIDFLAG = '1'";
			pm.setRows(this.getMapper(ComBaseStdMapper.class).findWithCondition(null, condition, "CREATE_TIME desc",
					(pageIndex - 1) * pageSize, pageSize));
			pm.setTotal(this.getMapper(ComBaseStdMapper.class).countWithCondition(null, condition));
			rm.setData(pm);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败："+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	
	/*****
	 * web 试样组与试验标准配置界面：新增
	 * @param cp
	 * @return
	 */
	public ResultModel doInsertStdRTestWeb(CommonPage<ComBaseStdRTest> cp){
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ComBaseStdRTest cb = cp.getObject();
		String aa[] = cb.getStdCodeTest().split(";");
		try {
			//判断试样组是否存在相同试验标准
/*			for (String STD_CODE_TEST : aa) {
				String condition ="1=1";
				condition += " and PHY_CODE_M ='" + cb.getPhyCodeM() + "' and STD_CODE_TEST like '%"+ STD_CODE_TEST +"%'";
				int num = this.getMapper(ComBaseStdRTestMapper.class).countWithCondition(null, condition);
				if (num > 0) {
					rm.setState("500");
					rm.setMsgInfo("该试样组已经存在相同标准，新增失败");
					return rm;
				}
			}*/
			ComBaseStdRTest test=new ComBaseStdRTest();
			test.setPhyCodeM(cb.getPhyCodeM());
			List<ComBaseStdRTest> listRTest=this.getMapper(ComBaseStdRTestMapper.class).find(test,0, 0);
			if(listRTest.size()>0){
				rm.setState("500");
				rm.setMsgInfo("只能有一条试样组数据做关联请查看是否已经有选择的试样组关联记录存在");
				return rm;
			}
			if (cb.getLevelTest() == null || "".equals(cb.getLevelTest())) {
				cb.setLevelTest(1);
			}
			cb.setCreateName(username);
			cb.setCreateTime(new Date());
			cb.setValidflag("1");
			//设置pk码
			String pk = this.getMapper(ComBaseStdRTestMapper.class).GetPk();
			cb.setPk(pk);
			this.getMapper(ComBaseStdRTestMapper.class).insert(cb);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("新增失败："+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	/****
	 * web 试样组与试验标准配置界面：修改 
	 * @param cp
	 * @return
	 */
	public ResultModel doUpdateStdRTestWeb(CommonPage<ComBaseStdRTest> cp){
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ComBaseStdRTest cb = cp.getObject();
		String aa[] = cb.getStdCodeTest().split(";");
		try {
			ComBaseStdRTest cbs = this.getMapper(ComBaseStdRTestMapper.class).findById(cb.getPk());
			//数据修改
			if ("1".equals(cbs.getValidflag())) {
				//判断试样组是否存在相同试验标准
/*				for (String STD_CODE_TEST : aa) {
					String condition ="1=1";
					condition += " and PHY_CODE_M ='" + cb.getPhyCodeM() + 
							"' and STD_CODE_TEST like '%"+ STD_CODE_TEST +"%' and PK <> '" +cbs.getPk()+ "'";
					int num = this.getMapper(ComBaseStdRTestMapper.class).countWithCondition(null, condition);
					if (num > 0) {
						rm.setState("500");
						rm.setMsgInfo("该试样组已经存在相同标准，新增失败");
						return rm;
					}
				}*/
				ComBaseStdRTest test=new ComBaseStdRTest();
				test.setPhyCodeM(cb.getPhyCodeM());
				List<ComBaseStdRTest> listRTest=this.getMapper(ComBaseStdRTestMapper.class).find(test,0, 0);
				if(listRTest.size()>0&&DataHelper.IsNullOrSpace(cb.getPk())){
					rm.setState("500");
					rm.setMsgInfo("只能有一条试样组数据做关联请查看是否已经有选择的试样组关联记录存在");
					return rm;
				}
				if (cb.getLevelTest() == null || "".equals(cb.getLevelTest())) {
					cb.setLevelTest(1);
				}
				cb.setUpdateName(username);
				cb.setUpdateTime(new Date());
				this.getMapper(ComBaseStdRTestMapper.class).update(cb);
				rm.setState("200");
			}else {
				rm.setState("500");
				rm.setMsgInfo("选择的数据不是有效的，无法修改");
			}
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("修改失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	
	/***
	 * web 试样组与试验标准配置界面：作废
	 * @param cp
	 * @return
	 */
	public ResultModel doCancelStdRTestWeb(CommonPage<ComBaseStdRTest> cp){
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
			ComBaseStdRTest cb = cp.getObject();
			ComBaseStdRTest cbs = this.getMapper(ComBaseStdRTestMapper.class).findById(cb.getPk());
			if (cbs!=null) {
				if ("1".equals(cbs.getValidflag())) {
					cbs.setValidflag("0");
					cbs.setDeleteName(username);
					cbs.setDeleteTime(new Date());
					this.getMapper(ComBaseStdRTestMapper.class).update(cbs);
					rm.setState("200");
				}else {
					rm.setState("500");
					rm.setMsgInfo("请选择无效记录");
				}
			}else {
				rm.setState("500");
				rm.setMsgInfo("作废失败，选择的试验标准在数据库中不存在");
			}
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("作废失败："+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	/***
	 * web 试样组与试验标准配置：恢复
	 * @param cp
	 * @return
	 */
	public ResultModel doRecoverStdRTestWeb(CommonPage<ComBaseStdRTest> cp){
		ResultModel rm = new ResultModel();
		try {
			ComBaseStdRTest cb = cp.getObject();
			ComBaseStdRTest cbs = this.getMapper(ComBaseStdRTestMapper.class).findById(cb.getPk());
			if (cbs!=null) {
				if ("0".equals(cbs.getValidflag())) {
					this.getMapper(ComBaseStdRTestMapper.class).DoRecoverStdRTest(cbs.getPk());
					rm.setState("200");
				}else {
					rm.setState("500");
					rm.setMsgInfo("请选择有效记录");
				}
			}else {
				rm.setState("500");
				rm.setMsgInfo("恢复失败，选择的试验标准在数据库中不存在");
			}
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("恢复失败："+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	/*
	 * 材质检验弹框查询
	 * 
	 * 
	 */
	public ResultModel doQueryPhyCodeM(CommonPage<ComBaseStdRTest> cb) {
		ResultModel rm = new ResultModel();
		int pageIndex = cb.getPageIndex();
		int pageSize = cb.getPageSize();
		ComBaseStdRTest object = cb.getObject();
		PageModel<ComBaseStdRTest> pm = new PageModel<ComBaseStdRTest>(pageIndex, pageSize);
		try{
			ComBaseStdRTest csr = new ComBaseStdRTest();
			csr.setPhyCodeM(object.getPhyCodeM());
			csr.setValidflag("1");
			List<ComBaseStdRTest> list = this.getMapper(ComBaseStdRTestMapper.class).find(csr, 0, 0);
			if (list.size()<1) {
				pm.setRows(null);
				pm.setTotal(0);
				rm.setData(pm);
				rm.setState("500");
				rm.setMsgInfo("未查询到试样组"+object.getPhyCodeM()+"适用的试验标准");
				return rm;
				
			}
			
			ComBaseStdRTest stdRTest= list.get(0);
			String[] testCodes=stdRTest.getStdCodeTest().split(";");
			String condition="STD_CODE IN (";
			for (String code : testCodes) {
				condition+="'"+code+"',";
			}   
			condition=condition.substring(0, condition.length()-1)+")";
			List<ComBaseStd> listStd=this.getMapper(ComBaseStdMapper.class).findWithCondition(null, condition, null, 0, 0);
			List<ComBaseStdRTest> listRTest=new ArrayList<>();
			for (ComBaseStd baseStd : listStd) {
				ComBaseStdRTest _test=new ComBaseStdRTest();
				_test.setStdCodeTest(baseStd.getStdCode());
				_test.setStdNameTest(baseStd.getStdName());
				_test.setMemo(baseStd.getMemo());
				listRTest.add(_test);
			}
			
			pm.setRows(listRTest);
			pm.setTotal(listRTest.size());
			rm.setData(pm);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		
	} catch (Exception ex) {
		rm.setState("500");
		rm.setMsgInfo("查询失败："+ex.getMessage());
		ex.printStackTrace();
	}
		return rm;
	}
	
	
	
	/*****
	 * web 试样组与试验标准配置界面：保存
	 * @param cp
	 * @return
	 */
	public ResultModel doAddStdRTestWeb(CommonPage<ComBaseStdRTest> cp){
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		//试样组代码
		
		if (cp==null) {
			rm.setState("500");
			rm.setMsgInfo("请传入参数");
			return rm;
			
		}
		String phy_code_m=cp.getMemo1();
		if (phy_code_m==null || "".equals(phy_code_m)) {
			rm.setState("500");
			rm.setMsgInfo("请传入试样组代码");
			return rm;
		}
		//删除原试样组代码
		this.getMapper(ComBaseStdRTestMapper.class).deleteByPhyCode(phy_code_m);
		List<ComBaseStdRTest> cbs = cp.getObjectList();
		try {

			for (ComBaseStdRTest cb : cbs) {
				if (cb.getLevelTest() == null || "".equals(cb.getLevelTest())) {
					cb.setLevelTest(1);
				}
				cb.setCreateName(username);
				cb.setCreateTime(new Date());
				cb.setValidflag("1");
				String pk = this.getMapper(ComBaseStdRTestMapper.class).GetPk();
				cb.setPk(pk);
				this.getMapper(ComBaseStdRTestMapper.class).insert(cb);
			}
			
			
			rm.setState("200");
			rm.setMsgInfo("保存成功");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("新增失败："+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	/*****
	 * web 试样组与试验标准配置界面：查询关联了的标准
	 * @param cp
	 * @return
	 */
	public ResultModel selectStdRTestWeb(CommonPage<ComBaseStdRTest> cp){
		ResultModel rm = new ResultModel();
		//试样组代码
		
		if (cp==null) {
			rm.setState("500");
			rm.setMsgInfo("请传入参数");
			return rm;
			
		}
		String phy_code_m=cp.getMemo1();
		if (phy_code_m==null || "".equals(phy_code_m)) {
			rm.setState("500");
			rm.setMsgInfo("请传入试样组代码");
			return rm;
		}
		try {
			List<ComBaseStdRTest> cbs = this.getMapper(ComBaseStdRTestMapper.class).findWithCondition(null, "PHY_CODE_M='"+phy_code_m+"'", null, 0, 0);	
			rm.setData(cbs);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败："+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	
}
