/***文档注释***********************************************
 * 作者            ：  周浴超
 * 创建日期     ：
 * 描述            ：  二级工序与工序点对应接口
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：                  2018.2.5
 * 修改人员     ：                  
 * 修改内容     ：                   
***********************************************************/
package market.Com.impl;

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

import market.Com.mapper.ComBaseProcessRStationMapper;
import market.Com.mapper.ComBaseStationMapper;
import market.Com.service.ComBaseProcessRStationService;
import market.Com.vo.ComBaseProcessC;
import market.Com.vo.ComBaseProcessRStation;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.metallurgy.vo.ComBaseAskRStation;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path = "ComBaseProcessRStationService")
public class ComBaseProcessRStationServiceImpl extends BaseServiceImpl<ComBaseProcessRStation,Object> implements ComBaseProcessRStationService {

	/**
	 * 二级工序与工序点配置——从ComBaseProcessRStation中查询：确认勾选
	 * @param cp
	 * @return
	 */
	public ResultModel doQueryProcessRStationWeb(CommonPage<ComBaseProcessC> cp){
		ResultModel rm = new ResultModel();
		ComBaseProcessC cbpc = cp.getObject();
//		String condition = "1=1 and VALIDFLAG = '1'";
//		if (cbpc.getProcessCdoeC() !=null && !"".equals(cbpc.getProcessCdoeC())) {
//			condition += " and PROCESS_CODE_C = '"+cbpc.getProcessCdoeC()+"'";
//		}
//		
//		if (cbpc.getProcessCdoe() !=null && !"".equals(cbpc.getProcessCdoe())) {
//			condition += " and PROCESS_CODE = '"+cbpc.getProcessCdoe()+"'";
//		}
		try {
			rm.setData(this.getMapper(ComBaseProcessRStationMapper.class).doQueryProcessRStationWeb(cbpc.getProcessCdoeC(),cbpc.getProcessCdoe()));
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！");
			e.printStackTrace();
		}
		return rm;
	}
	
	/*****
	 * 二级工序与工序点配置——与二级工序对应工序名称的工序点信息查询
	 * @param cp
	 * @return
	 */
	public ResultModel doQueryStationWeb(CommonPage<ComBaseProcessC> cp){
		ResultModel rm = new ResultModel();
		ComBaseProcessC cbpc = cp.getObject();
		String condition = "1=1 and VALIDFLAG = '1'";
		if (cbpc.getProcessCdoe() !=null && !"".equals(cbpc.getProcessCdoe())) {
			condition += " and PROCESS_CODE = '"+cbpc.getProcessCdoe()+"'";
		}
		try {
			rm.setData(this.getMapper(ComBaseStationMapper.class).findWithCondition(null, condition, null, 0, 0));
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！");
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 保存信息
	 * memo1:传入主表查询的二级工序代码
	 * @param cp
	 * @return
	 */
	public ResultModel doSaveProcessRStationWeb(CommonPage<ComBaseProcessRStation> cp){
		ResultModel rm = new ResultModel();
		List<ComBaseAskRStation> list = new ArrayList<>();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		List<ComBaseProcessRStation> cbprsList = new ArrayList<ComBaseProcessRStation>();
		cbprsList = cp.getObjectList();//获得数据集
		String condition = cp.getMemo1();//查询条件是二级工序代码
		try {
			//先删除数据库中数据
			
//			ComBaseProcessRStation crs = new ComBaseProcessRStation();
//			crs.setProcess_code_c(condition);
//			List<ComBaseProcessRStation> list2 = this.getMapper(ComBaseProcessRStationMapper.class).find(crs, 0, 0);
//			for(ComBaseProcessRStation c1:list2)
//			{
//				for(ComBaseProcessRStation c2:cbprsList)
//				{if(c1.getProcess_code().equals(c2.getProcess_code()))
//					{
//					if(c1.getProcess_code_c().equals(c2.getProcess_code_c()))
//					{
//						if(c1.getProcess_point_code().equals(c2.getProcess_point_code()))
//						{
//							break;
//						}
//					}
//					}
//				if(c2 == cbprsList.get(cbprsList.size()-1)){	
//				 list = this.getMapper(ComBaseProcessRStationMapper.class).doFind(c1.getProcess_point_code());
//					if(list != null && list.size() > 0)
//					{
//						rm.setState("500");
//						rm.setMsgInfo("附加要求存在相关联的记录!");
//						return rm;
//					}
//				}
//				}
//			}
			
			
			this.getMapper(ComBaseProcessRStationMapper.class).deleteWithCondition(condition);
			if (cbprsList!=null) {
				//逐条插入勾选的数据
				for (ComBaseProcessRStation comBaseProcessRStation : cbprsList) {
					
					comBaseProcessRStation.setProcess_code_c(condition);
					if(comBaseProcessRStation.getCreate_name() == null){//判断是否为新增
						comBaseProcessRStation.setCreate_name(username);
						comBaseProcessRStation.setCreate_time(new Date());
						comBaseProcessRStation.setValidflag("1");
					}else {//修改数据
						comBaseProcessRStation.setUpdate_name(username);
						comBaseProcessRStation.setUpdate_time(new Date());
					}
					this.getMapper(ComBaseProcessRStationMapper.class).insert(comBaseProcessRStation);
					rm.setState("200");
				}
			}
			rm.setState("200");
			rm.setMsgInfo("保存成功");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("保存失败！");
			e.printStackTrace();
		}
		return rm;
	}
}
