/***文档注释***********************************************
 * 作者               :                   刘学
 * 创建日期      :                   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.mapper.ComBasePhyRStationMapper;
import market.Com.service.ComBasePhyService;
import market.Com.vo.ComBasePhy;
import market.Com.vo.ComBasePhyB;
import market.Com.vo.ComBasePhyC;
import market.Com.vo.ComBasePhyRStation;
import market.Com.vo.ComBasePhyadd;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.common.DataHelper;

import org.springframework.transaction.annotation.Transactional;

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 = "ComBasePhyService")
public class ComBasePhyServiceImpl extends BaseServiceImpl<ComBasePhy, Object> implements ComBasePhyService {

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

			}

			List<ComBasePhyadd> add = this.getMapper(ComBasePhyMapper.class).QuerybySql((pageIndex - 1) * pageSize,
					(pageIndex - 1) * pageSize + pageSize, cbp.getPhy_name_s(), cbp.getValidflag());
			pm.setRows(add);
			
			pm.setTotal(this.getMapper(ComBasePhyMapper.class).QuerybySql(0,0, cbp.getPhy_name_s(), cbp.getValidflag()).size());
			rm.setData(pm);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	public ResultModel doExistFlac(CommonPage<ComBasePhy> cp ){
		ResultModel model=new ResultModel();
		try{
			String phyCodeM=cp.getObject().getPhy_code_m();
			ComBasePhyC phyC=this.getMapper(ComBasePhyCMapper.class).findById(phyCodeM);
			ComBasePhyB phyB=this.getMapper(ComBasePhyBMapper.class).findById(phyC.getPhy_code_l());
			if(phyB.getPhy_type().equals("410602")){//探伤
				model.setState("200");
			}else{
				model.setState("404");
			}
		}catch(Exception ex){
			model.setState("500");
			model.setMsgInfo(ex.getMessage());
			return model;
		}
		return model;
	}
	
	/**
	 * WEB 材质项管理界面：新增
	 * 
	 * @param cp
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doInsertPhyBWeb(CommonPage<ComBasePhy> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		

			ComBasePhy cbpc = new ComBasePhy();
			cbpc = cp.getObject();
			if (cbpc.getPhy_name_s() == null || "".equals(cbpc.getPhy_name_s()) || cbpc.getPhy_code_m() == null
					|| "".equals(cbpc.getPhy_code_m())) {
				rm.setState("500");
				rm.setMsgInfo("试样组，项目名称不能为空!");
				return rm;
			} 
			
			String condition = "  PHY_name_s = '" + cbpc.getPhy_name_s() + "'";
			int num = this.getMapper(ComBasePhyMapper.class).countWithCondition(null, condition);

			if (num != 0) {
				rm.setState("500");
				rm.setMsgInfo("存在相同项目名称、不允许新增!");
				return rm;
			} 
			
			
				ComBasePhyC cc = this.getMapper(ComBasePhyCMapper.class).findById(cbpc.getPhy_code_m());

				cbpc.setPhy_code_l(cc.getPhy_code_l());

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

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

				this.getMapper(ComBasePhyMapper.class).insert(cbpc);

				if (cp.getMemo2() != null && !"".equals(cp.getMemo2())) {

					String[] aa = cp.getMemo2().split("\\,");

					String[] bb = cp.getMemo3().split("\\,");

					for (int i = 0; i < aa.length; i++) {
						ComBasePhyRStation Phyr = new ComBasePhyRStation();

						Phyr.setPhyCodeS(Phid);
						Phyr.setPhyNameS(cbpc.getPhy_name_s());
						Phyr.setProcessPointCode(aa[i].toString());
						Phyr.setProcessPointDesc(bb[i].toString());
						Phyr.setCreateName(username);
						Phyr.setCreateTime(new Date());

						this.getMapper(ComBasePhyRStationMapper.class).doInsertSTATION(Phyr);
					}
				}
				rm.setState("200");
			
	
		return rm;
	}

	/**
	 * WEB 材质项管理界面：修改
	 * 
	 * @param cp
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doUpdatePhyBWeb(CommonPage<ComBasePhy> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String Phy = cp.getObject().getPhy_code_s();
		
			// if (!"".equals(Phy)&& Phy!=null){
			ComBasePhy cbpc = new ComBasePhy();
			cbpc = cp.getObject();
			ComBasePhy cbp = this.getMapper(ComBasePhyMapper.class).findById(Phy);
			
			
			if (cbpc.getPhy_name_s() != null && !"".equals(cbpc.getPhy_name_s())) {
				String condition = "  PHY_name_s = '" + cbpc.getPhy_name_s() + "' and PHY_code_s <> '" + cbpc.getPhy_code_s() + "'";
				int num = this.getMapper(ComBasePhyMapper.class).countWithCondition(null, condition);

				if (num != 0) {
					rm.setState("500");
					rm.setMsgInfo("存在相同项目名称、不允许修改!");
					return rm;
				}
			}
			
			
			if (cbp != null) {
				if (cbp.getValidflag().equals("1")) {

					ComBasePhyC cc = this.getMapper(ComBasePhyCMapper.class).findById(cbpc.getPhy_code_m());

					cbpc.setPhy_code_l(cc.getPhy_code_l());

					cbpc.setUpdate_name(username);
					cbpc.setUpdate_time(new Date());
					cbpc.setValidflag("1");// 有效标志(1:有效，0:无效)
					// cbpc.setProcessPointCode(null);
					// cbpc.setProcessPointDesc(null);
					//
					this.getMapper(ComBasePhyMapper.class).update(cbpc);
					ComBasePhyRStationMapper rStationMapper=this.getMapper(ComBasePhyRStationMapper.class);
					if (cp.getMemo2().toString() != null && !"".equals(cp.getMemo2())) {

						this.getMapper(ComBasePhyRStationMapper.class).doDeleteSTATION(cbpc.getPhy_code_s());
						String[] aa = cp.getMemo2().split("\\,");

						String[] bb = cp.getMemo3().split("\\,");

						for (int i = 0; i < aa.length; i++) {
							ComBasePhyRStation Phyr = new ComBasePhyRStation();

							Phyr.setPhyCodeS(cbpc.getPhy_code_s());
							Phyr.setPhyNameS(cbpc.getPhy_name_s());
							Phyr.setProcessPointCode(aa[i].toString());
							Phyr.setProcessPointDesc(bb[i].toString());
							Phyr.setCreateName(username);
							Phyr.setCreateTime(new Date());

							rStationMapper.doInsertSTATION(Phyr);
						}
					}else if(DataHelper.IsNullOrSpace(cp.getMemo2())){
							rStationMapper.doDeleteSTATION(Phy);

					}

					rm.setState("200");
				} else {
					rm.setState("500");
					rm.setMsgInfo("选择的材质项管理的状态不是有效，无法修改!");
				}
			} else {
				rm.setState("500");
				rm.setMsgInfo("修改失败,选择的材质项管理在数据库中不存在!");
			}
			// }
			// else
			// {
			// rm.setState("500");
			// rm.setMsgInfo("修改失败,选择的材质项编码不能为空!");
			// }

		
		return rm;
	}

	/**
	 * WEB 材质项管理界面：作废
	 * 
	 * @param Phyode
	 *            材质项管理代码(主键)
	 * @return
	 */
	public ResultModel doCancelPhyWeb(CommonPage<ComBasePhy> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String Phy = cp.getObject().getPhy_code_s();
		try {
			ComBasePhy cbp = this.getMapper(ComBasePhyMapper.class).findById(Phy);
			if (cbp != null) {
				if (cbp.getValidflag().equals("1")) {
					cbp.setPhy_code_s(Phy);
					cbp.setValidflag("0");
					cbp.setDelete_name(username);
					cbp.setDelete_time(new Date());
					this.getMapper(ComBasePhyMapper.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<ComBasePhy> cp) {
		ResultModel rm = new ResultModel();

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

	
	
}
