/***文档注释***********************************************
 * 作者               :                   朱泽鹏
 * 创建日期      :                   2018.2.5
 * 描述               :        			基础信息-附加要求
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Com.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.mapper.SlmBaseAddAskMapper;
import market.Com.service.SlmBaseAddAskService;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Com.vo.SlmBaseAddAsk;
import market.Qms.common.DataHelper;
import market.Qms.metallurgy.mapper.ComBaseAskRStationMapper;
import market.Qms.metallurgy.vo.ComBaseAskRStation;

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="SlmBaseAddAskService")
public class SlmBaseAddAskServiceImpl extends BaseServiceImpl<SlmBaseAddAsk,Object> implements SlmBaseAddAskService {

	//	/**
	//	 * 初始化，获取类别
	//	 * @param sum
	//	 * @return
	//	 */
	//	public ResultModel doFindCsh(CommonPage<SlmBaseAddAsk> sum){
	//		ResultModel rm =new ResultModel();
	//		SlmBaseAddAsk ask=sum.getObject();
	//		try{
	//			if (ask.getValidflag().equals("1")) {
	//			}else {
	//				ask.setValidflag(null);
	//			}
	//			List<TreeNode> ltn=this.getMapper(SlmBaseAddAskMapper.class).findComBaseInfo(ask);
	//			TreeNodeHelper th=new TreeNodeHelper();
	//			List<TreeNode> lt = th.GetTreeJsonByLists(ltn);
	//			rm.setData(lt);
	//			rm.setState("200");
	//		}catch(Exception ex){
	//			ex.printStackTrace();
	//			rm.setState("500");
	//			rm.setMsgInfo("服务端异常，获取数据失败!");
	//		}
	//		return rm;
	//	}

	/**
	 * 查询系统中已维护且有效的所有记录
	 * @param sum
	 * @return
	 */
	public ResultModel doFindAsk(CommonPage<SlmBaseAddAsk> sum) {
		ResultModel rm =new ResultModel();
		SlmBaseAddAsk ask =sum.getObject();
		try {
			ask.setValidflag(sum.getMemo1());
			if(sum.getMemo1().equals("1")){
				ask.setValidflag(null);
			}else{
				ask.setValidflag("1");
			}
			String ask_item_desc =ask.getAsk_item_desc();
			if (ask_item_desc==""||ask_item_desc.equals("")) {
				ask_item_desc=null;
			}
			//查一级节点
			if (DataHelper.IsNullOrSpace(ask.getAsk_item_no())&&!ask.getCheckBoxFlag()){
				PageModel<SlmBaseAddAsk> pm = new PageModel<SlmBaseAddAsk>(sum.getPageIndex(),sum.getPageSize());
				String condition = "length(ASK_ITEM_NO) = 6";
				ask.setAsk_item_desc("");
				List<SlmBaseAddAsk> askList =this.getMapper(SlmBaseAddAskMapper.class).findWithCondition(ask, condition, "CREATE_TIME desc", (sum.getPageIndex() - 1) * sum.getPageSize(), sum.getPageSize());
				for (int i = 0; i<askList.size(); i++) {
					SlmBaseAddAsk tempAsk=new SlmBaseAddAsk();
					tempAsk.setAsk_p_item_no(askList.get(i).getAsk_item_no());
					int tempCount =this.getMapper(SlmBaseAddAskMapper.class).count(tempAsk);
					if (tempCount > 0) {
						askList.get(i).setIs_haveNext("1");
					} else {
						askList.get(i).setIs_haveNext("0");
					}
				}
				//int count =this.getMapper(SlmBaseAddAskMapper.class).countWithCondition(ask, condition);
				pm.setRows(askList);
				//pm.setTotal(count);
				rm.setData(pm);
				rm.setState("200");
			}
			//查二级节点
			else if(ask.getAsk_item_no().length()==6){
				PageModel<SlmBaseAddAsk> pm = new PageModel<SlmBaseAddAsk>(sum.getPageIndex(),sum.getPageSize());
				SlmBaseAddAsk asks=new SlmBaseAddAsk();
				if(sum.getMemo1().equals("1")){
					asks.setValidflag(null);
				}else{
					asks.setValidflag("1");
				}
				asks.setAsk_p_item_no(ask.getAsk_item_no());
				List<SlmBaseAddAsk> asklist =this.getMapper(SlmBaseAddAskMapper.class).find(asks,sum.getPageIndex()-1,sum.getPageSize());
				for (int i = 0; i<asklist.size(); i++) {
					int tempCount =this.getMapper(SlmBaseAddAskMapper.class).getCount(asklist.get(i).getAsk_item_no(), null, null);
					if (tempCount > 1) {
						asklist.get(i).setIs_haveNext("1");
					} else {
						asklist.get(i).setIs_haveNext("0");
					}
				}
				//int count =this.getMapper(SlmBaseAddAskMapper.class).count(asks);
				pm.setRows(asklist);
				//pm.setTotal(count);
				rm.setData(pm);
				rm.setState("200");
			}
			//查三级节点
			else if (ask.getAsk_item_no().length()==8||ask.getCheckBoxFlag()) {
				if(ask.getAsk_item_no()!=null&&ask.getAsk_item_no().equals("")){
					ask.setAsk_item_no(null);
				}
				PageModel<SlmBaseAddAsk> pm = new PageModel<SlmBaseAddAsk>(sum.getPageIndex(),sum.getPageSize());
				int index=(sum.getPageIndex()*sum.getPageSize()-sum.getPageSize());
				List<SlmBaseAddAsk> askList =this.getMapper(SlmBaseAddAskMapper.class).doFindAsks(ask.getAsk_item_no(),ask.getValidflag(),index, sum.getPageSize(),ask_item_desc," length(ASK_ITEM_NO)=11");
				//int count =this.getMapper(SlmBaseAddAskMapper.class).getCount(ask.getAsk_item_no(),ask.getValidflag(),ask_item_desc);
				pm.setRows(askList);
				//pm.setTotal(count);
				rm.setData(pm);
				rm.setState("200");
			}
		} catch (Exception e) {
			e.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败");
		}
		return rm;
	}

	/**
	 * 保存附加项基础信息主记录
	 * @param sum
	 * @return
	 */
	public ResultModel doAddAsk(CommonPage<SlmBaseAddAsk> sum) {
		ResultModel rm =new ResultModel();
		List<SlmBaseAddAsk> AddAskList =sum.getObjectList();
		try {
			for (SlmBaseAddAsk ask : AddAskList) {
				//新增
				if (DataHelper.GetNull(ask.getInsertFlag()).equals("1"))
				{
					SlmBaseAddAsk asks =new SlmBaseAddAsk();
					List<SlmBaseAddAsk> askLists = new ArrayList<SlmBaseAddAsk>();
					if (ask.getAsk_item_no() == null || "".equals(ask.getAsk_item_no()) || "1".equals(ask.getAsk_item_no())) {
						String condition = "length(ASK_ITEM_NO) = 6";
						asks.setAsk_item_desc(ask.getAsk_item_desc());
						askLists =this.getMapper(SlmBaseAddAskMapper.class).findWithCondition(asks, condition, null, 0, 0);
					} else {
						asks.setAsk_p_item_no(ask.getAsk_item_no());
						asks.setAsk_item_desc(ask.getAsk_item_desc());
						askLists =this.getMapper(SlmBaseAddAskMapper.class).find(asks, 0, 0);
					}
					if (askLists.size()>0) {
						rm.setMsgInfo("当前结点下有一样的描述");
						rm.setState("500");
					}else {
						//当前时间为创建时间
						SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmssSSS");
						Date time =null;
						time= sdf.parse(sdf.format(new Date()));
						ask.setCreate_time(time);
						ask.setCreate_name((String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME));
						String getAsk_p_item_no =ask.getAsk_p_item_no();
						if (getAsk_p_item_no.length()>=6) {
							ask.setAsk_p_item_no(getAsk_p_item_no);
						}
						ask.setAsk_p_item_no(getAsk_p_item_no);
						if(getAsk_p_item_no==null||getAsk_p_item_no.equals("1210")||getAsk_p_item_no.equals("")) {
							getAsk_p_item_no = "1210"+this.getMapper(SlmBaseAddAskMapper.class).getAsk_item_no();
						}else if(getAsk_p_item_no.length()==6) {
							ask.setAsk_p_item_no(getAsk_p_item_no);
							getAsk_p_item_no = getAsk_p_item_no+this.getMapper(SlmBaseAddAskMapper.class).getAsk_item_notwo(getAsk_p_item_no);
						}else if (getAsk_p_item_no.length()==8) {
							ask.setAsk_p_item_no(getAsk_p_item_no);
							getAsk_p_item_no = getAsk_p_item_no+this.getMapper(SlmBaseAddAskMapper.class).getAsk_item_noTh(getAsk_p_item_no);
						}
						if(DataHelper.IsNullOrSpace(ask.getAsk_item_no())){
							ask.setAsk_item_no(getAsk_p_item_no);
						}
						//验证标准说明唯一性
						List<SlmBaseAddAsk> listadd = this.getMapper(SlmBaseAddAskMapper.class).findWithCondition(null, "ASK_P_ITEM_NO='"+ask.getAsk_p_item_no()+"'", null, 0, 0);
						for (SlmBaseAddAsk ad : listadd) {
							if (ad.getAsk_item_desc().equals(ask.getAsk_item_desc())) {
								rm.setState("403");
								rm.setMsgInfo("不能重复添加相同的标准说明。。。。");
								return rm;
							}
						}


						this.getMapper(SlmBaseAddAskMapper.class).insert(ask);
						rm.setMsgInfo("新增成功");
						rm.setState("200");
					}
				} 
				//修改
				else {
					SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmssSSS");
					Date time =null;
					time= sdf.parse(sdf.format(new Date()));
					ask.setUpdate_time(time);
					ask.setUpdate_name((String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME));
					SlmBaseAddAsk asks =new SlmBaseAddAsk();
					asks.setAsk_p_item_no(ask.getAsk_item_no());
					//验证==================
					ComBaseAskRStation cbar = new ComBaseAskRStation();
					cbar.setAsk_sub_itme(ask.getAsk_item_no());
//					if(this.getMapper(ComBaseAskRStationMapper.class).count(cbar)>0)
//					{
//						rm.setState("500");
//						rm.setMsgInfo("附加要求项与工序点对应关系表存在关联记录，不能修改");
//						return rm;
//					}
					List<SlmBaseAddAsk> asklists=this.getMapper(SlmBaseAddAskMapper.class).find(asks, 0, 0); 
					SlmBaseAddAsk adds =new SlmBaseAddAsk();
					adds.setAsk_item_desc(ask.getAsk_item_desc());
					adds.setAsk_p_item_no(ask.getAsk_item_no());
					List<SlmBaseAddAsk> askList =this.getMapper(SlmBaseAddAskMapper.class).find(adds, 0, 0);
					if (asklists.size()==0) {
						if (askList.size()==0) {
								this.getMapper(SlmBaseAddAskMapper.class).update(ask);
								rm.setState("200");
								rm.setMsgInfo("修改成功");
						}else {
							rm.setState("500");
							rm.setMsgInfo("子节点下有相同描述");
							return rm;
						}
					}else {
						
						this.getMapper(SlmBaseAddAskMapper.class).update(ask);
						rm.setState("200");
						rm.setMsgInfo("修改成功");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("新增失败!");
		}
		return rm;
	}

	/**
	 * 作废
	 * @param cp
	 * @return
	 */
	public ResultModel doAskDelList(CommonPage<SlmBaseAddAsk> cp) {
		ResultModel rm = new ResultModel();
		try {
			for (SlmBaseAddAsk slm : cp.getObjectList()) {
				ComBaseAskRStation cbar = new ComBaseAskRStation();
				cbar.setAsk_sub_itme(slm.getAsk_item_no());
				if(slm.getAsk_item_no().length()==11){
					cbar.setAsk_sub_itme(slm.getAsk_item_no().substring(0, 8));
					int count = this.getMapper(ComBaseAskRStationMapper.class).count(cbar);
					if(count>0){
						rm.setState("500");
						rm.setMsgInfo(slm.getAsk_item_desc() + "的标准值存在工序点与附加要求项关联记录，不能作废");
						return rm;
					}
				}
				
				int count2 = this.getMapper(ComBaseAskRStationMapper.class).count(cbar);
				if(count2>0)
				{
					rm.setState("500");
					rm.setMsgInfo(slm.getAsk_item_desc() + "的附加要求项与工序点对应关系表存在关联记录，不能作废");
					return rm;
				}
				SlmBaseAddAsk slm2 = new SlmBaseAddAsk();
				slm2.setAsk_p_item_no(slm.getAsk_item_no());
				List<SlmBaseAddAsk> lists = this.getMapper(SlmBaseAddAskMapper.class).find(slm2, 0, 0);
				if(lists.size()>=1) {
					for (SlmBaseAddAsk slm3 : lists) {
						if (slm3.getValidflag().equals("1")) {
							rm.setMsgInfo(slm.getAsk_item_desc() + "的下级有有效数据不能作废");
							rm.setState("500");
							return rm;
						}
					}
				}else {
					String ask_item_no =slm.getAsk_item_no();
					int count = 0;
					try {
						count =this.getMapper(SlmBaseAddAskMapper.class).GexYinY(ask_item_no);
					} catch (Exception ex) {
						count = 0;
					}
					if (count>0) {
						rm.setMsgInfo(slm.getAsk_item_desc() + "为引用数据，不能作废！");
						rm.setState("500");
						return rm;
					}
				}
			}
		} catch (Exception ex) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("作废失败!");
			return rm;
		}
		for (SlmBaseAddAsk slm : cp.getObjectList()) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
			Date time = null;
			try {
				time = sdf.parse(sdf.format(new Date()));
			} catch (ParseException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			slm.setDelete_time(time);
			slm.setValidflag("0");
			this.getMapper(SlmBaseAddAskMapper.class).update(slm);
		}
		rm.setState("200");
		rm.setMsgInfo("作废成功");
		return rm;
	}

	/**
	 * 恢复
	 * @param cp
	 * @return
	 */
	public ResultModel doAskHFList(CommonPage<SlmBaseAddAsk> cp) {
		ResultModel rm = new ResultModel();
		 List <SlmBaseAddAsk> askList =cp.getObjectList();
		try {
			for (SlmBaseAddAsk slm : askList) {
				if (slm.getValidflag().equals("0")) {
					if (slm.getAsk_item_no().length() > 6) {
						SlmBaseAddAsk ask= this.getMapper(SlmBaseAddAskMapper.class).findById(slm.getAsk_p_item_no());
						if (ask.getValidflag().equals("0")) {
							rm.setState("500");
							rm.setMsgInfo(slm.getAsk_item_desc() +"的父级数据状态为无效,不能恢复！");
							return rm;
						}
					}
				}else {
					rm.setState("500");
					rm.setMsgInfo(slm.getAsk_item_desc() +"数据状态为有效");
					return rm;
				}
			}
		} catch (Exception ex) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("恢复失败!");
			return rm;
		}
		for (SlmBaseAddAsk slm : askList) {
			this.getMapper(SlmBaseAddAskMapper.class).doUpdatehuif(slm.getAsk_item_no());
		}
		rm.setState("200");
		rm.setMsgInfo("恢复成功!");
		return rm;
	}

	public ResultModel GetRootAddAsk() {
		ResultModel rm = new ResultModel();
		try{
			List<SlmBaseAddAsk> list=this.getMapper(SlmBaseAddAskMapper.class).GetRoot();
			rm.setData(list);
			rm.setState("200");
		}catch(Exception ex){
			rm.setMsgInfo(ex.getMessage());
			rm.setState("500");
		}
		return rm;
	}

	public ResultModel GetAddAskItem(String parentNo) {
		ResultModel rm = new ResultModel();
		try{
			SlmBaseAddAsk ask=new SlmBaseAddAsk();
			ask.setValidflag("1");
			ask.setAsk_p_item_no((parentNo));
			List<SlmBaseAddAsk> list=this.getMapper(SlmBaseAddAskMapper.class).find(ask, 0, 0);
			rm.setState("200");
			rm.setData(list);
		}catch(Exception ex){
			rm.setMsgInfo(ex.getMessage());
			rm.setState("500");
		}
		return rm;
	}	
}
