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

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

import market.Com.mapper.ComBasePhyBMapper;
import market.Com.mapper.ComBasePhyCMapper;
import market.Com.mapper.ComBasePhyMapper;
import market.Com.service.ComBasePhyCService;
import market.Com.vo.ComBasePhy;
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 = "ComBasePhyCService")
public class ComBasePhyCServiceImpl extends BaseServiceImpl<ComBasePhyC, Object> implements ComBasePhyCService {

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

			}

			List<ComBasePhyC> phyc = this.getMapper(ComBasePhyCMapper.class).findWithCondition(null, condition,
					"phy_code_m", (pageIndex - 1) * pageSize, pageSize);

			pm.setRows(phyc);
			pm.setTotal(this.getMapper(ComBasePhyCMapper.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 doInsertPhyCWeb(CommonPage<ComBasePhyC> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {

			ComBasePhyC cbpc = new ComBasePhyC();
			cbpc = cp.getObject();

			if (cbpc.getPhy_name_m() == null || "".equals(cbpc.getPhy_name_m()) || cbpc.getPhy_code_l() == null
					|| "".equals(cbpc.getPhy_code_l())) {
				rm.setState("500");
				rm.setMsgInfo("检验大项、试样组名称不能为空!");
				return rm;
			}

			String condition = "  PHY_name_m = '" + cbpc.getPhy_name_m() + "'";
			int num = this.getMapper(ComBasePhyCMapper.class).countWithCondition(null, condition);

			if (num != 0) {
				rm.setState("500");
				rm.setMsgInfo("存在相同试样组名称、不允许新增!");
				return rm;
			}

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

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

			this.getMapper(ComBasePhyCMapper.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<ComBasePhyC> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String Phy = cp.getObject().getPhy_code_m();
		try {

			ComBasePhyC cbpc = new ComBasePhyC();
			cbpc = cp.getObject();
			ComBasePhy phy = new ComBasePhy();
			phy.setPhy_code_m(cp.getObject().getPhy_code_m());
			if(this.getMapper(ComBasePhyMapper.class).count(phy) > 0)
			{
				rm.setMsgInfo("材质项目基础表存在关联记录，不能修改！");
				rm.setState("500");
				return rm;
			}
			if (cbpc.getPhy_name_m() != null && !"".equals(cbpc.getPhy_name_m())) {
				String condition = "  PHY_name_m = '" + cbpc.getPhy_name_m() + "' and PHY_code_m <> '" + cbpc.getPhy_code_m() + "'";
				int num = this.getMapper(ComBasePhyCMapper.class).countWithCondition(null, condition);

				if (num != 0) {
					rm.setState("500");
					rm.setMsgInfo("存在相同试样组名称、不允许修改!");
					return rm;
				}
			}

			ComBasePhyC cbp = this.getMapper(ComBasePhyCMapper.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(ComBasePhyCMapper.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 Phyode
	 *            试样组管理代码(主键)
	 * @return
	 */
	public ResultModel doCancelPhyWeb(CommonPage<ComBasePhyC> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String Phy = cp.getObject().getPhy_code_m();
		try {
			ComBasePhy phy = new ComBasePhy();
			phy.setPhy_code_m(cp.getObject().getPhy_code_m());
			if(this.getMapper(ComBasePhyMapper.class).count(phy) > 0)
			{
				rm.setMsgInfo("材质项目基础表存在关联记录，不能修改！");
				rm.setState("500");
				return rm;
			}
			ComBasePhyC cbp = this.getMapper(ComBasePhyCMapper.class).findById(Phy);
			if (cbp != null) {
				if (cbp.getValidflag().equals("1")) {
					cbp.setPhy_code_m(Phy);
					cbp.setValidflag("0");
					cbp.setDelete_name(username);
					cbp.setDelete_time(new Date());
					this.getMapper(ComBasePhyCMapper.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<ComBasePhyC> cp) {
		ResultModel rm = new ResultModel();

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

}
