/***文档注释***********************************************
 * 作者               :                   邓彬
 * 创建日期      :                   2018.2.6
 * 描述               :        			生产产线与工序点对应表
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Qms.metallurgy.impl;

import java.util.Date;

import market.Com.mapper.ComBaseMachineRStationMapper;
import market.Com.mapper.ComBaseStationMapper;
import market.Com.vo.ComBaseMachineRStation;
import market.Com.vo.ComBasePline;
import market.Com.vo.ComBaseStation;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.metallurgy.mapper.ComMscStationRPlineMapper;
import market.Qms.metallurgy.service.ComMscStationRPlineService;
import market.Qms.metallurgy.vo.ComMscPlineVo;
import market.Qms.metallurgy.vo.ComMscStationRPline;

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="ComMscStationRPlineService")
public class ComMscStationRPlineServiceImpl extends BaseServiceImpl<ComMscStationRPline, Object> implements ComMscStationRPlineService {

	/****
	 * 根据产线信息中选中记录工序代码查询工序点信息
	 * @param cp
	 * @return
	 */
	public ResultModel doQueryStationWeb(CommonPage<ComBasePline> cp){
		ResultModel rm = new ResultModel();
		int pageIndex = cp.getPageIndex();
		int pageSize = cp.getPageSize();
		PageModel<ComBaseStation> pm = new PageModel<ComBaseStation>(pageIndex,pageSize);
		try {
			ComBasePline cbp = cp.getObject();
			//设置查询条件
			String condition = "1=1 and VALIDFLAG = '1'";
			if (cbp.getProcess_code() != null && !"".equals(cbp.getProcess_code())) {
				condition += " and PROCESS_CODE = '"+cbp.getProcess_code()+"'";
			}
			pm.setRows(this.getMapper(ComBaseStationMapper.class).findWithCondition(null, condition, "PROCESS_CODE",
							(pageIndex - 1) * pageSize, pageSize));
			pm.setTotal(this.getMapper(ComBaseStationMapper.class).countWithCondition(null, condition));
			rm.setData(pm);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/****
	 * 根据工序点信息中选中记录工序点代码查询机台信息
	 * @param cp
	 * @return
	 */
	public ResultModel doQueryMachineWeb(CommonPage<ComBaseStation> cp){
		ResultModel rm = new ResultModel();
		int pageIndex = cp.getPageIndex();
		int pageSize = cp.getPageSize();
		PageModel<ComBaseMachineRStation> pm = new PageModel<ComBaseMachineRStation>(pageIndex,pageSize);
		try {
			ComBaseStation cbs = cp.getObject();
			//设置查询条件
			String condition = "1=1 and VALIDFLAG = '1'";
			if (cbs.getProcess_point_code() != null &&!"".equals(cbs.getProcess_point_code())) {
				condition += " and PROCESS_POINT_CODE = '"+cbs.getProcess_point_code()+"'";
			}
			pm.setRows(this.getMapper(ComBaseMachineRStationMapper.class).findWithCondition(null, condition, "PROCESS_POINT_CODE",
							(pageIndex - 1) * pageSize, pageSize));
			pm.setTotal(this.getMapper(ComBaseMachineRStationMapper.class).countWithCondition(null, condition));
			rm.setData(pm);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/***
	 * 验证是否勾选
	 * @param cp
	 * @return
	 */
	public ResultModel doFindStationRPline(CommonPage<ComMscStationRPline> cp){
		ResultModel rm =new ResultModel();
		ComMscStationRPline cmsrp = cp.getObject();
		try {
			rm.setData(this.getMapper(ComMscStationRPlineMapper.class).find(cmsrp, 0, 0));
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败："+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	
//	/****
//	 * memo1:选中产线代码
//	 * @param cp
//	 * @return
//	 * @throws Exception 
//	 */
//	@Transactional(value="tm", rollbackFor=Exception.class)
//	public ResultModel doSave(CommonPage<ComMscPlineVo> cp) throws Exception{
//		ResultModel rm = new ResultModel();
//		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
//		ComMscPlineVo cmpvo = cp.getObject();
//		ComBasePline cbpr = cmpvo.getCbp();
//		
//		if (cmpvo.getCbsList() == null || "".equals(cmpvo.getCbsList())) {
//			throw new Exception("请勾选工序点信息！");
//		}
//		for(ComBaseStation cbsr:cmpvo.getCbsList()){
////			if(cbsr.getCbmList()==null || "".equals(cbsr.getCbmList())){
////				throw new Exception("请勾选机台信息！");
////			}
//			//根据工序点代码获取数据库中工序点信息
//			ComBaseStation cbs = this.getMapper(ComBaseStationMapper.class).findById(cbsr.getProcess_point_code());
//			ComMscStationRPline msrp = new ComMscStationRPline();
//			msrp.setPlineCode(cbpr.getPline_code());
//			msrp.setProcessPointCode(cbs.getProcess_point_code());
//			this.getMapper(ComMscStationRPlineMapper.class).doDelete(msrp);
//			if(cbsr.getCbmList()==null || "".equals(cbsr.getCbmList())){
//				return rm;
//				}
//			
//			for(ComBaseMachineRStation cbmr:cbsr.getCbmList()){
//				cbmr.setProcess_point_code(cbsr.getProcess_point_code());
//				ComBaseMachineRStation cbm = (ComBaseMachineRStation) this.getMapper(ComBaseMachineRStationMapper.class).doFind(cbmr);
//				ComBasePline cbp = this.getMapper(ComBasePlineMapper.class).findById(cbpr.getPline_code());
//				ComMscStationRPline msrpc = new ComMscStationRPline();
//				msrpc.setCreateName(username);
//				msrpc.setCreateTime(new Date());
//				msrpc.setPlineCode(cbp.getPline_code());
//				msrpc.setPlineCodeOld(cbp.getPline_code_old());
//				msrpc.setProcessPointCode(cbs.getProcess_point_code());
//				msrpc.setStationCodeOld(cbs.getStation_code_old());
//				if (cbs.getProcess_point_seq() != null && !"".equals(cbs.getProcess_point_seq())) {
//					msrpc.setProcessPointSeq(Integer.valueOf(cbs.getProcess_point_seq().intValue()));
//				}else {
//					msrpc.setProcessPointSeq(null);
//				}
//				msrpc.setMachineCode(cbm.getMachine_code());
//				msrpc.setValidflag("1");
//				
//				this.getMapper(ComMscStationRPlineMapper.class).doinsert(msrpc);
//			}
//		}
//		rm.setState("200");
//		return rm;
//	}
	
	/****
	 * memo1:选中工序点代码
	 * @param cp
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSave(CommonPage<ComMscPlineVo> cp) throws Exception{
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ComMscPlineVo cmpvo = cp.getObject();
		ComBasePline cbpr = cmpvo.getCbp();//获取产线对象
		//删除产线信息
		this.getMapper(ComMscStationRPlineMapper.class).doDeletePline(cbpr.getPline_code());
		
		if(cmpvo.getCbsList().size()>0){
			ComMscStationRPlineMapper stationRPlineMapper=this.getMapper(ComMscStationRPlineMapper.class);
			for (ComBaseStation station : cmpvo.getCbsList()) {
				ComMscStationRPline rPline=new ComMscStationRPline();
				rPline.setCreateName(username);
				rPline.setCreateTime(new Date());
				rPline.setPlineCode(cbpr.getPline_code());
				rPline.setProcessPointCode(station.getProcess_point_code());
				rPline.setProcessPointSeq(station.getProcess_point_seq());
				rPline.setValidflag("1");
				stationRPlineMapper.insert(rPline);
			}
			
		}
		rm.setState("200");
		return rm;
	}
	
}