/***文档注释***********************************************
 * 作者               :                   邓彬
 * 创建日期      :                   2018.2.5
 * 描述               :        			工序点与工艺参数
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Com.impl;

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

import market.Com.mapper.ComBaseCraftMapper;
import market.Com.mapper.ComBaseStationMapper;
import market.Com.mapper.ComBaseStationRCraftMapper;
import market.Com.service.ComBaseStationRCraftService;
import market.Com.vo.ComBaseCraft;
import market.Com.vo.ComBaseStation;
import market.Com.vo.ComBaseStationRCraft;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.qcm.mapper.ComBaseStdCraftTicItemMapper;
import market.Qms.qcm.mapper.ComBaseStdCraftTicMapper;
import market.Qms.qcm.vo.ComBaseStdCraftTic;
import market.Qms.qcm.vo.ComBaseStdCraftTicItem;

import org.springframework.transaction.annotation.Transactional;

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

@RemoteService(path = "ComBaseStationRCraftService")
public class ComBaseStationRCraftServiceImpl extends BaseServiceImpl<ComBaseStationRCraft, Object> implements ComBaseStationRCraftService {
	/**
	 * 工序点信息和工艺参数信息 查询
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ComBaseStationRCraft doQueryStationAndCraftTemp()
	{
		ComBaseStationRCraft cr = new ComBaseStationRCraft();
		List<ComBaseStation> cbss = new ArrayList<ComBaseStation>();
		List<ComBaseCraft> cbcs = new ArrayList<ComBaseCraft>();
		try 
		{
			ComBaseStation ComBaseStation = new ComBaseStation();
			ComBaseStation.setValidflag(States.VALID.getValue());
			List<ComBaseStation> list = this.getMapper(ComBaseStationMapper.class).find(ComBaseStation, 0, 0);
			cbss = list;
			if (cbss.size() > 0) 
			{
				ComBaseCraft ComBaseCraft = new ComBaseCraft();
				ComBaseCraft.setValidflag(States.VALID.getValue());
				List<ComBaseCraft> cbct = this.getMapper(ComBaseCraftMapper.class).find(ComBaseCraft, 0, 0);
				cbcs=cbct;
			}
			// 设置集合
			cr.setCbss(cbss);
			cr.setCbcs(cbcs);
		} catch (Exception ex) {
			throw (ex);
		}
		return cr;
	}

	public ResultModel doQueryStationAndCraft() 
	{
		ResultModel rm = new ResultModel();
		try 
		{
			ComBaseStationRCraft cr = doQueryStationAndCraftTemp();
			if (cr != null) 
			{   
				rm.setData(cr);
				rm.setState("200");
				rm.setMsgInfo("查询成功!");
			} else {
				rm.setState("500");
				rm.setMsgInfo("查询失败!");
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("服务器内部异常!");
		}
		return rm;
	}
	
	/*****
	 * 工序点与工艺参数对应 保存
	 * @param cp
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInsertComBaseStationRCraft(CommonPage<ComBaseStationRCraft> cp) throws Exception {
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String pointCode = cp.getMemo1();
		List<ComBaseStationRCraft> crs = cp.getObjectList();
		try {
			this.getMapper(ComBaseStationRCraftMapper.class).doDelete(pointCode);
			// 删除COM_BASE_STD_CRAFT_TIC_ITEM对应的项目
			List<ComBaseStdCraftTic> tics = this.getMapper(ComBaseStdCraftTicMapper.class).findWithCondition(null,
					"PROCESS_POINT_CODE='" + pointCode + "'", null, 0, 0);
			for (ComBaseStdCraftTic tic : tics) {
				List<ComBaseStdCraftTicItem> items = this.getMapper(ComBaseStdCraftTicItemMapper.class)
						.findWithCondition(null, "TIC='" + tic.getTic() + "'", "INDEX_SEQ desc", 0, 0);

				if (items != null && items.size() > 0) {
					Integer index_seq = items.get(0).getIndex_seq() + 1;

					if (crs != null && crs.size() > 0) {
						for (ComBaseStdCraftTicItem item : items) {
							for (int i = 0; i < crs.size(); i++) {
								if (item.getCraft_param_code().equals(crs.get(i).getCraft_param_code())) {
									break;
								}

								if (i == crs.size() - 1) {
									// 删除项目
									this.getMapper(ComBaseStdCraftTicItemMapper.class).delete(item.getTic(),
											item.getIndex_seq(), item.getCraft_param_code());

								}

							}

						}

						// 反向验证
						for (ComBaseStationRCraft cr : crs) {

							for (int i = 0; i < items.size(); i++) {
								if (items.get(i).getCraft_param_code().equals(cr.getCraft_param_code())) {
									break;
								}

								if (i == items.size() - 1) {
									ComBaseStdCraftTicItem item = new ComBaseStdCraftTicItem();
									item.setTic(tic.getTic());
									item.setCraft_param_code(cr.getCraft_param_code());
									item.setCraft_param_desc(cr.getCraft_param_desc());
									item.setIndex_seq(index_seq++);
									item.setCreate_name(userName);
									item.setCreate_time(new Date());
									this.getMapper(ComBaseStdCraftTicItemMapper.class).insert(item);
								}

							}

						}
					} else {
						// 删除项目
						for (ComBaseStdCraftTicItem item : items) {
							this.getMapper(ComBaseStdCraftTicItemMapper.class).delete(item.getTic(),
									item.getIndex_seq(), item.getCraft_param_code());
						}

					}

				} else {
					int index_seq = 1;
					for (ComBaseStationRCraft t : crs) {
						ComBaseStdCraftTicItem item = new ComBaseStdCraftTicItem();
						item.setTic(tic.getTic());
						item.setCraft_param_code(t.getCraft_param_code());
						item.setCraft_param_desc(t.getCraft_param_desc());
						item.setIndex_seq(index_seq++);
						item.setCreate_name(userName);
						item.setCreate_time(new Date());
						this.getMapper(ComBaseStdCraftTicItemMapper.class).insert(item);

					}

				}
			}

			if (crs != null && crs.size() > 0) {
				for (ComBaseStationRCraft cr : crs) {
					cr.setValidflag(States.VALID.getValue());
					cr.setCreate_name(userName);
					cr.setCreate_time(new Date());
					this.getMapper(ComBaseStationRCraftMapper.class).insert(cr);
					rm.setState("200");
				}
				rm.setState("200");
			}
		} catch (

		Exception e)

		{
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			e.printStackTrace();
			throw new Exception("操作失败!" + e.getMessage());
		}
		return rm;

	}
	public ResultModel doInsertComBaseStationRCraft1(CommonPage<ComBaseStationRCraft> cp) {
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String pointCode = cp.getMemo1();
		List<ComBaseStationRCraft> crs = cp.getObjectList();
		try {
			this.getMapper(ComBaseStationRCraftMapper.class).doDelete(pointCode);
			if (crs != null) {
				for (ComBaseStationRCraft cr : crs) {
					cr.setValidflag(States.VALID.getValue());
					cr.setCreate_name(userName);
					cr.setCreate_time(new Date());
					this.getMapper(ComBaseStationRCraftMapper.class).insert(cr);
					rm.setState("200");
				}
				rm.setState("200");
			}
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			e.printStackTrace();
		}
		return rm;
	}
	
	
//	/**
//	 * 工序点与工艺参数对应 保存
//	 * @param mc
//	 * @return
//	 */
//	@Transactional(value = "tm", rollbackFor = Exception.class)
//	public ResultModel doInsertComBaseStationRCraft(List<ComBaseStationRCraft> crs) 
//	{
//		ResultModel rm = new ResultModel();
//		ComBaseStationRCraft cbs = new ComBaseStationRCraft();
//		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
//		try {
//			if (crs != null) 
//			{
//				for (ComBaseStationRCraft cr : crs) 
//				{   
//					if( cr.getProcess_code() != null && cr.getProcess_point_code() != null && cr.getCraft_param_code() != null)
//					{
//					ComBaseStationRCraft comb = this.getMapper(ComBaseStationRCraftMapper.class).doQueryOneBySql(
//	                        cr.getProcess_code(), cr.getProcess_point_code(),cr.getCraft_param_code());
//					if (comb != null) 
//					{
//						// 物理删除
//						this.getMapper(ComBaseStationRCraftMapper.class).delete(cr.getProcess_point_code());
//						cbs.setProcess_code(comb.getProcess_code());
//						cbs.setProcess_point_code(comb.getProcess_point_code());
//						cbs.setCraft_param_code(comb.getCraft_param_code());
//						int cnt = this.getMapper(ComBaseStationRCraftMapper.class).countWithCondition(cbs, null);
//						if (cnt == 0) 
//						{
//							cr.setValidflag(States.VALID.getValue());
//							cr.setUpdate_name(userName);
//							cr.setUpdate_time(new Date());
//							this.getMapper(ComBaseStationRCraftMapper.class).insert(cr);
//							rm.setState("200");
//							rm.setMsgInfo("旧记录删除,新纪录新增成功!");
//						}
//					}else {
//						cr.setValidflag(States.VALID.getValue());
//						cr.setCreate_name(userName);
//						cr.setCreate_time(new Date());
//						this.getMapper(ComBaseStationRCraftMapper.class).insert(cr);
//						rm.setState("200");
//						rm.setMsgInfo("新增成功!");
//					}
//				}
//			}
//		}
//	} catch (Exception ex) {
//			rm.setState("500");
//			rm.setMsgInfo("操作失败!");
//			ex.printStackTrace();
//		}
//		return rm;
//
//	}

	/**
	 * 查询关联记录
	 * @param processpointcode
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryComBaseStationRCraft(String processpointcode) throws Exception 
	{   
		ResultModel rm = new ResultModel();
		List<ComBaseStationRCraft> list = new ArrayList<ComBaseStationRCraft>();
		try {
			if (processpointcode != null) 
			{
				list = this.getMapper(ComBaseStationRCraftMapper.class).doQueryBySql(processpointcode);
				rm.setState("200");
				rm.setData(list);
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
		}
		return rm;
	}
}
enum States {
	INIT("0"), VALID("1");
	private String value;
	private States(String val) {
		this.value = val;
	}
	public String getValue() {
		return this.value;
	}
}
