/***文档注释***********************************************
 * 作者               :                   刘学
 * 创建日期      :                   2017.12.4
 * 描述               :        			质量检验大项
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Com.impl;

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

import market.Com.mapper.ComBaseInfoMapper;
import market.Com.mapper.ComBasePhyBMapper;
import market.Com.mapper.ComBasePhyCMapper;
import market.Com.service.ComBasePhyBService;
import market.Com.vo.ComBaseInfo;
import market.Com.vo.ComBasePhyB;
import market.Com.vo.ComBasePhyC;
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 = "ComBasePhyBService")
public class ComBasePhyBServiceImpl extends BaseServiceImpl<ComBasePhyB, Object> implements ComBasePhyBService {
	
	
	/**
	 * 获取材质大项信息
	 * @param cp
	 * @return
	 */
	public ResultModel GetBasePhyB(CommonPage<ComBasePhyB> cp){
		ResultModel rm=new ResultModel();
		try{
			List<ComBasePhyB> list=this.getMapper(ComBasePhyBMapper.class).find(cp.getObject(), 0, 0);
			rm.setData(list);
			rm.setState("200");
		}catch(Exception ex){
			rm.setMsgInfo("获取材质大项出错："+ex.getMessage());
			rm.setState("500");
		}
		rm.setState("200");
		return rm;
	}
	
	public ResultModel doQueryPhyType(){
		ResultModel rm = new ResultModel();
		try{
			ComBaseInfo _info=new ComBaseInfo();
			_info.setSort_code("4106");
			_info.setValidflag("1");
			List<ComBaseInfo> listInfo=this.getMapper(ComBaseInfoMapper.class).find(_info, 0, 0);
			rm.setData(listInfo);
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询出错："+ex.getMessage());
		}
		return rm;
	}
	

	/**
	 * WEB 材质大项管理：查询
	 * 
	 * @param cp
	 *            memo1 传入包含无效是否打钩。不打钩："0";打钩："1"
	 * @return
	 */
	public ResultModel doQueryPhyBWeb(CommonPage<ComBasePhyB> cp) {
		ResultModel rm = new ResultModel();
		int pageIndex = cp.getPageIndex();
		int pageSize = cp.getPageSize();
		PageModel<ComBasePhyB> pm = new PageModel<ComBasePhyB>(pageIndex, pageSize);
		try {
			ComBasePhyB 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.getPhy_name_l() != null && !"".equals(cbp.getPhy_name_l())) {
				condition += " and upper(PHY_name_L) like '%" + cbp.getPhy_name_l().toUpperCase() + "%'";

			}

			List<ComBasePhyB> phyB = this.getMapper(ComBasePhyBMapper.class).findWithCondition(null, condition,
					"phy_code_l", (pageIndex - 1) * pageSize, pageSize);
			pm.setRows(phyB);
			pm.setTotal(this.getMapper(ComBasePhyBMapper.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<ComBasePhyB> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {

			ComBasePhyB cbpc = new ComBasePhyB();
			cbpc = cp.getObject();
			if (cbpc.getPhy_name_l() == null || "".equals(cbpc.getPhy_name_l()) || cbpc.getPhy_deptid() == null
					|| "".equals(cbpc.getPhy_deptid())) {
				rm.setState("500");
				rm.setMsgInfo("检验大项、试验科室不能为空!");
				return rm;
			} 

				String condition = "   PHY_name_L = '" + cbpc.getPhy_name_l() + "'";
				int num = this.getMapper(ComBasePhyBMapper.class).countWithCondition(null, condition);

				if (num != 0) {
					rm.setState("500");
					rm.setMsgInfo("存在相同的检验大项名称、不允许新增!");
					return rm;
				}

				String Phid = this.getMapper(ComBasePhyBMapper.class).findMaxPHyCode();

				cbpc.setPhy_code_l(Phid);
				cbpc.setCreate_name(username);
				cbpc.setCreate_time(new Date());
				cbpc.setValidflag("1");// 有效标志(1:有效，0:无效)

				this.getMapper(ComBasePhyBMapper.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<ComBasePhyB> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {

			ComBasePhyB cbpc = new ComBasePhyB();
			cbpc = cp.getObject();
			String Phy = cp.getObject().getPhy_code_l();
ComBasePhyC phy = new ComBasePhyC();
phy.setPhy_code_l(Phy);
if(this.getMapper(ComBasePhyCMapper.class).count(phy) > 0)
{
	rm.setMsgInfo("材质试样组基础表存在关联记录，不能修改！");
	rm.setState("500");
	return rm;
}
			ComBasePhyB cbp = this.getMapper(ComBasePhyBMapper.class).findById(Phy);

			if (cbpc.getPhy_name_l() != null && !"".equals(cbpc.getPhy_name_l())) {
				String condition = " and PHY_name_L = '" + cbpc.getPhy_name_l() + "' and PHY_code_l <> '" + cbpc.getPhy_code_l() + "'";
				int num = this.getMapper(ComBasePhyBMapper.class).countWithCondition(null, condition);

				if (num != 0) {
					rm.setState("500");
					rm.setMsgInfo("存在相同的检验大项名称、不允许修改!");
					return rm;
				}
			}
			if (cbp != null) {
				if (cbp.getValidflag().equals("1")) {
					cbpc.setUpdate_name(username);
					cbpc.setUpdate_time(new Date());
					cbpc.setValidflag("1");// 有效标志(1:有效，0:无效)
					this.getMapper(ComBasePhyBMapper.class).update(cbpc);
					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 PhyBode
	 *            材质大项管理代码(主键)
	 * @return
	 */
	public ResultModel doCancelPhyBWeb(CommonPage<ComBasePhyB> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String Phy = cp.getObject().getPhy_code_l();
		try {
			ComBasePhyC phy = new ComBasePhyC();
			phy.setPhy_code_l(Phy);
			if(this.getMapper(ComBasePhyCMapper.class).count(phy) > 0)
			{
				rm.setMsgInfo("材质试样组基础表存在关联记录，不能修改！");
				rm.setState("500");
				return rm;
			}
			ComBasePhyB cbp = this.getMapper(ComBasePhyBMapper.class).findById(Phy);
			if (cbp != null) {
				if (cbp.getValidflag().equals("1")) {
					cbp.setPhy_code_l(Phy);
					cbp.setValidflag("0");
					cbp.setDelete_name(username);
					cbp.setDelete_time(new Date());
					this.getMapper(ComBasePhyBMapper.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 PhyBode
	 *            材质大项管理代码(主键)
	 * @return
	 */
	public ResultModel doRecoverPhyBWeb(CommonPage<ComBasePhyB> cp) {
		ResultModel rm = new ResultModel();

		String Phy = cp.getObject().getPhy_code_l();
		try {
			ComBasePhyB cbp = this.getMapper(ComBasePhyBMapper.class).findById(Phy);
			if (cbp != null) {
				if (cbp.getValidflag().equals("0")) {
					this.getMapper(ComBasePhyBMapper.class).DoRecoverPhyB(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;
	}
}
