/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-07-12
 * 描述            ：工艺参数项目标准表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package  market.Qms.qcm.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.qcm.mapper.ComBaseStdCraftTicItemMapper;
import market.Qms.qcm.service.ComBaseStdCraftTicItemService;
import market.Qms.qcm.vo.ComBaseStdCraftTic;
import market.Qms.qcm.vo.ComBaseStdCraftTicItem;

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

@RemoteService(path="ComBaseStdCraftTicItemService")
public class ComBaseStdCraftTicItemServiceImpl extends BaseServiceImpl<ComBaseStdCraftTicItem,Object> implements ComBaseStdCraftTicItemService {
	 public ResultModel getCraftTicItemInfo(CommonPage<ComBaseStdCraftTic> sum){
    	 ResultModel rm=new ResultModel();
    	 boolean flag = true;
 		int count = 0;
 		int seq=1;
 		ComBaseStdCraftTic cbsc=sum.getObject();
 		List<ComBaseStdCraftTicItem> result = new ArrayList<ComBaseStdCraftTicItem>();
 		try {
 			//String userneme=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
 			//获取COM_BASE_STATION_R_CRAFT表中的项目
 		//	List<ComBaseStationRCraft> listcbsr = this.getMapper(ComBaseStationRCraftMapper.class).findWithCondition(null, "PROCESS_POINT_CODE ='"+cbsc.getProcess_point_code()+"'", null, 0, 0);
 			//COM_BASE_STD_CRAFT_TIC_ITEM表中的项目
 			List<ComBaseStdCraftTicItem> listcbsct = this.getMapper(ComBaseStdCraftTicItemMapper.class).findWithCondition(null, "tic='"+cbsc.getTic()+"' and VALIDFLAG='1'", null, 0, 0);
 			//判断：如果listcbsr比listcbsct多则在COM_BASE_STD_CRAFT_TIC_ITEM中增加项目，否则删除项目
// 			List<ComBaseStdCraftTicItem> list = this.getMapper(ComBaseStdCraftTicItemMapper.class).findWithCondition(null, null, "INDEX_SEQ desc", 0, 0);
// 			ComBaseStdCraftTicItem temp = new ComBaseStdCraftTicItem();
// 			if (list.size()>0) {
//					seq=list.get(0).getIndex_seq();
//			}
 			
 			//for (ComBaseStationRCraft a : listcbsr) {
 				//for (ComBaseStdCraftTicItem b : listcbsct) {
 					//if (a.getCraft_param_code().equals(b.getCraft_param_code())) {
// 						result.add(b);
// 						flag = false;
// 						break;
 					//	a.setMin_value(b.getMin_value());
 					//	a.setMax_value(b.getMax_value());
 					//}
// 						else {
// 						flag=true;
// 						temp.setMax_value(b.getMax_value());
// 						temp.setMin_value(b.getMin_value());
// 					}
 				//}
// 				if (flag) {
//// 					new yitiao   b jilu
// 					temp.setCraft_param_code(a.getCraft_param_code());
// 					temp.setCraft_param_desc(a.getCraft_param_desc());
// 					
// 					temp.setCreate_name(userneme);
// 					temp.setCreate_time(new Date());
// 					temp.setTic(cbsc.getTic());
// 					temp.setIndex_seq(seq);
// 					seq++;
// 					result.add(temp);
// 				}
 			//}
 		
// 			listcbsct.retainAll(listcbsr);//取交集
// 			listcbsr.removeAll(listcbsct);//取listcbsr与listcbsct不相同的数据
// 			List<ComBaseStdCraftTicItem> list=new ArrayList<>();
// 			list.addAll(listcbsct);
// 			int seq=list.size();
// 			if (listcbsr.size()>0) {
// 				List<ComBaseStationRCraft> listft=new ArrayList<ComBaseStationRCraft>(); 
// 				listft.addAll(listcbsr);
// 				for (ComBaseStationRCraft c : listft) {
// 					ComBaseStdCraftTicItem cbsi=new ComBaseStdCraftTicItem();
// 			    	cbsi.setTic(cbsc.getTic());
// 			    	cbsi.setCraft_param_code(c.getCraft_param_code());
// 			    	cbsi.setCraft_param_desc(c.getCraft_param_desc());
// 			    	cbsi.setCreate_name(userneme);
// 			    	cbsi.setCreate_time(new Date());
// 			    	cbsi.setIndex_seq(seq);
// 			    	seq++;
// 					this.getMapper(ComBaseStdCraftTicItemMapper.class).insert(cbsi);
// 				}
// 			}
 			rm.setData(listcbsct);
 			rm.setState("200");
 		} catch (Exception e) {
 			// TODO: handle exception
 			rm.setState("500");
 			rm.setMsgInfo("查询信息失败");
 			rm.setErrorInfo(e.getMessage());
 		}
    	 return rm;
     }
     /**
      * 保存信息
      * @param sum
      * @return
      */
     public ResultModel saveCraftTicItemInfo(CommonPage<ComBaseStdCraftTicItem> sum){
    	 ResultModel rm=new ResultModel();
    	 String userneme=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
    	   List<ComBaseStdCraftTicItem> listcbsci = sum.getObjectList();
//    	   this.getMapper(ComBaseStdCraftTicItemMapper.class).deleteByTic(listcbsci.get(0).getTic());
    	   for (ComBaseStdCraftTicItem cbsci : listcbsci) {
    		   List<ComBaseStdCraftTicItem> list = this.getMapper(ComBaseStdCraftTicItemMapper.class).findWithCondition(null, "tic='"+cbsci.getTic()+"' and craft_param_code='"+cbsci.getCraft_param_code()+"'", null, 0, 0);
    		   //如果list>0就进行修改操作，否则进行新增操作
    		   if (list.size()>0) {
    			   cbsci.setUpdate_name(userneme);
        		   cbsci.setUpdate_time(new Date());
        		   this.getMapper(ComBaseStdCraftTicItemMapper.class).update(cbsci);
			}else {
			  cbsci.setCreate_name(userneme);
      		   cbsci.setCreate_time(new Date());
      		   this.getMapper(ComBaseStdCraftTicItemMapper.class).insert(cbsci);
			}
    		   
		}
    	 return rm;
     }
}
