/***文档注释***********************************************
 * 作者            ：      邓彬
 * 创建日期     ：2018-02-6
 * 描述            ：附加要求项与工序点对应关系
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.metallurgy.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.mapper.SlmBaseAddAskMapper;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Com.vo.SlmBaseAddAsk;
import market.Qms.metallurgy.mapper.ComBaseAskRStationMapper;
import market.Qms.metallurgy.mapper.ComBaseProcessRStationMapper;
import market.Qms.metallurgy.service.ComBaseAskRStationService;
import market.Qms.metallurgy.vo.ComBaseAskRStation;
import market.Qms.metallurgy.vo.ComBasePRS;
import market.Qms.metallurgy.vo.ComBaseProcessRStation;
import market.Qms.metallurgy.vo.SlmBaseA;

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="ComBaseAskRStationService")
public class ComBaseAskRStationServiceImpl extends BaseServiceImpl<ComBaseAskRStation,Object> implements ComBaseAskRStationService {

	public ResultModel doFindC(CommonPage<ComBasePRS> sum) {
		ComBasePRS oci = sum.getObject();
		ComBaseProcessRStation com = new ComBaseProcessRStation();
		com.setProcess_code_c(oci.getProcess_cdoe_c());
		com.setValidflag("1");
		ResultModel rm =new ResultModel();
		List<ComBasePRS> li = new ArrayList<ComBasePRS>();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		PageModel<ComBasePRS> pm = new PageModel<ComBasePRS>(pageIndex,pageSize);
		try {
			List<ComBaseProcessRStation> list = this.getMapper(ComBaseProcessRStationMapper.class).find(com, 0, 0);
			for(ComBaseProcessRStation c : list)
			{
//				SlmBaseAddAsk s = new SlmBaseAddAsk();
//				s.setStation_code(c.getProcess_point_code());
//				s.setValidflag("1");
//				int count = this.getMapper(SlmBaseAddAskMapper.class).count(s);	
				//2018年1月30日修改，关联数目
				ComBaseAskRStation s = new ComBaseAskRStation();
				s.setProcess_point_code(c.getProcess_point_code());
				s.setValidflag("1");
				int count = this.getMapper(ComBaseAskRStationMapper.class).count(s);
				ComBasePRS cs = new ComBasePRS(c);
				cs.setCount(count);
				li.add(cs);
			}
			pm.setRows(li);
			pm.setTotal(li.size());
			rm.setData(pm);
			rm.setMsgInfo("查询成功！");
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("查询失败");
		}
		return rm;
	}

	public ResultModel doSavePos(CommonPage<SlmBaseA> cp) {
		SlmBaseA s = cp.getObject();
		String point =cp.getMemo1();
		String code = cp.getMemo2();
		String codec = cp.getMemo3();
		List<SlmBaseA> list = cp.getObjectList();
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			Date date = new Date();
			//2018年1月30日修改，没有勾选关联则删除所有该工序点下的关联关系
			if (list == null) {
				this.getMapper(ComBaseAskRStationMapper.class).deleteBy(point);
			}
			else {
				for(SlmBaseA c:list){
					ComBaseAskRStation com = new ComBaseAskRStation();
					com.setProcess_point_code(point);
					com.setAsk_sub_itme(c.getAsk_item_no());
					List<ComBaseAskRStation> list2 = this.getMapper(ComBaseAskRStationMapper.class).find(com, 0, 0);
					if(list2.size() != 0){
						ComBaseAskRStation cs = list2.get(0);
						ComBaseAskRStation cas  = new ComBaseAskRStation();
						this.getMapper(ComBaseAskRStationMapper.class).deleteBy(point);
						cas.setAsk_sub_itme_desc(c.getAsk_item_desc());
						cas.setAsk_sub_itme(c.getAsk_item_no());
						cas.setCreate_time(cs.getCreate_time());
						cas.setCreate_name(cs.getCreate_name());
						cas.setMemo(cs.getMemo());
						cas.setProcess_cdoe(cs.getProcess_cdoe());
						cas.setProcess_cdoe_c(cs.getProcess_cdoe_c());
						cas.setProcess_desc(cs.getProcess_desc());
						cas.setProcess_desc_c(cs.getProcess_desc_c());
						cas.setProcess_point_code(cs.getProcess_point_code());
						cas.setProcess_point_desc(cs.getProcess_point_desc());
						cas.setValidflag(cs.getValidflag());
						cas.setDelete_name(cs.getDelete_name());
						cas.setDelete_time(cs.getDelete_time());
						cas.setUpdate_name(userName);
						cas.setUpdate_time(date);
						this.getMapper(ComBaseAskRStationMapper.class).insert(cas);
					}else {
						ComBaseAskRStation cba = new ComBaseAskRStation();
						ComBaseProcessRStation cbp = new ComBaseProcessRStation();
						cbp.setProcess_code(code);
						cbp.setProcess_code_c(codec);
						cbp.setProcess_point_code(point);
						ComBaseProcessRStation station = this.getMapper(ComBaseProcessRStationMapper.class).find(cbp, 0, 0).get(0);
						cba.setAsk_sub_itme_desc(c.getAsk_item_desc());
						cba.setAsk_sub_itme(c.getAsk_item_no());
						cba.setCreate_name(userName);
						cba.setCreate_time(date);
						cba.setMemo(cbp.getMemo());
						cba.setProcess_cdoe(station.getProcess_code());
						cba.setProcess_cdoe_c(station.getProcess_code_c());
						cba.setProcess_desc(station.getProcess_desc());
						cba.setProcess_desc_c(station.getProcess_desc_c());
						cba.setProcess_point_code(station.getProcess_point_code());
						cba.setProcess_point_desc(station.getProcess_point_desc());
						this.getMapper(ComBaseAskRStationMapper.class).insert(cba);
					}
				}
			}
			rm.setState("200");
			rm.setMsgInfo("保存成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("新增失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	public ResultModel doFindS(CommonPage<SlmBaseA> sum) {
		ResultModel rm =new ResultModel();
		List<SlmBaseAddAsk> list = new ArrayList<SlmBaseAddAsk>();
		List<SlmBaseA> lis = new ArrayList<SlmBaseA>();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		PageModel<SlmBaseA> pm = new PageModel<SlmBaseA>(pageIndex,pageSize);
		try{
			SlmBaseAddAsk sk = new SlmBaseAddAsk();
			list = this.getMapper(SlmBaseAddAskMapper.class).find(sk,0,0);
			for(SlmBaseAddAsk s:list)
			{
				SlmBaseAddAsk sbad = new SlmBaseAddAsk();
				sbad.setAsk_p_item_no(s.getAsk_item_no());
				sbad.setValidflag("1");
				List<SlmBaseAddAsk> find = this.getMapper(SlmBaseAddAskMapper.class).find(sbad, 0, 0);
				for(SlmBaseAddAsk sd:find)
				{
					SlmBaseA sa = new SlmBaseA();
					sa.setStation_code("999999999");
					sa.setAsk_desc(sd.getAsk_item_desc());
					sa.setAsk_item_desc(s.getAsk_item_desc());
					sa.setAsk_item_no(sd.getAsk_item_no());
					lis.add(sa);
				}
			}
			pm.setRows(lis);
			pm.setTotal(lis.size());
			rm.setState("200");
			rm.setMsgInfo("查询成功！");
			rm.setData(pm);
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败");
			rm.setErrorInfo(e.getMessage());
		}
		return rm;
	}

	public ResultModel doFindSY(CommonPage<SlmBaseA> sum) {
		SlmBaseA oci = sum.getObject();
		ResultModel rm =new ResultModel();
		List<ComBaseAskRStation> list = new ArrayList<ComBaseAskRStation>();
		List<SlmBaseA> lis = new ArrayList<SlmBaseA>();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		PageModel<SlmBaseA> pm = new PageModel<SlmBaseA>(pageIndex,pageSize);
		try{
			if(oci.getStation_code() != null){
				ComBaseAskRStation cst = new ComBaseAskRStation();
				cst.setProcess_point_code(oci.getStation_code());
				list = this.getMapper(ComBaseAskRStationMapper.class).find(cst, 0, 0);
				for(ComBaseAskRStation c:list)
				{
					SlmBaseA sla = new SlmBaseA();
					//sla.setAsk_desc(c.getAsk_sub_itme_desc());
					sla.setAsk_item_desc(c.getAsk_sub_itme_desc());
					sla.setAsk_item_no(c.getAsk_sub_itme());
					sla.setStation_code(oci.getStation_code());
					lis.add(sla);
				}
			}else {
				rm.setState("500");
				rm.setMsgInfo("工序点代码为空！");
			}
			pm.setRows(lis);
			pm.setTotal(lis.size());
			rm.setState("200");
			rm.setMsgInfo("查询成功！");
			rm.setData(pm);
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("查询失败");
		}
		return rm;

	}
	
	/**
	 * 获取附加要求项及分项
	 * @return
	 */
	public ResultModel GetAskLeve2Info(){
		ResultModel rm=new ResultModel("200","获取数据成功");
		try{
			List<SlmBaseAddAsk> listAddAsk=this.getMapper(SlmBaseAddAskMapper.class).GetAskLeve2Info();
			rm.setData(listAddAsk);
			rm.setState("200");
			rm.setMsgInfo("获取数据成功");
		}catch(Exception ex){
			rm.setState("500");
			rm.setErrorInfo(ex.getMessage());
			rm.setMsgInfo("获取附加要求项失败");
		}
		return rm;
	}
	
}
