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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.com.mapper.BaseDeliverystateMapper;
import market.Qms.com.vo.BaseDeliverystate;
import market.Qms.common.DataHelper;
import market.Qms.common.ReturnMsg;
import market.Qms.metallurgy.mapper.ComBaseStdMapper;
import market.Qms.metallurgy.vo.ComBaseStd;
import market.Qms.qcm.mapper.ComBaseStdCraftMapper;
import market.Qms.qcm.mapper.ComBaseStdCraftTicMapper;
import market.Qms.qcm.service.ComBaseStdCraftService;
import market.Qms.qcm.vo.ComBaseStdCraft;

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

@RemoteService(path="ComBaseStdCraftService")
public class ComBaseStdCraftServiceImpl extends BaseServiceImpl<ComBaseStdCraft,Object> implements ComBaseStdCraftService {
	/**
	   * 查询工艺标准信息
	   * @param sum
	   * @return
	   */
		public ResultModel getProcessStandardInfo(CommonPage<ComBaseStdCraft> sum){
		      ResultModel rm=new ResultModel();
		      ComBaseStdCraft cbsc=sum.getObject();
		      
		     try {
		    	 String connect="";
				if (cbsc.getCraft_std_name()!=null&&!cbsc.getCraft_std_name().equals("")) {
					connect+=" and CRAFT_STD_NAME like '%"+cbsc.getCraft_std_name()+"%'";
				}
				if (cbsc.getValidflag()!=null&&!cbsc.getValidflag().equals("")) {
					List<ComBaseStdCraft> findWithCondition = this.getMapper(ComBaseStdCraftMapper.class).findWithCondition(null, connect, null, 0, 0);
				     rm.setData(findWithCondition);
				     rm.setState("200");
				}
				else {
					List<ComBaseStdCraft> findWithCondition = this.getMapper(ComBaseStdCraftMapper.class).findWithCondition(null, connect+" and VALIDFLAG='1'", null, 0, 0);
				     rm.setData(findWithCondition);
				     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 insertProcessStandardInfo(CommonPage<ComBaseStdCraft> sum){
		   ResultModel rm=new ResultModel();
		   ComBaseStdCraft cbsc=sum.getObject();
		   try {
			   //有效性验证
			if (cbsc.getMemo()!=null && cbsc.getMemo().length()>200) {
				rm.setState("500");
				rm.setMsgInfo("备注信息超长，请确认！");
				return rm;
			}
			if (cbsc.getCraft_std_name()==null&&cbsc.getCraft_std_name().equals("")) {
				rm.setState("500");
				rm.setMsgInfo("【工艺标准名称】不能为空，请确认！");
				return rm;
			}
			if (cbsc.getProcess_desc()==null&&cbsc.getProcess_desc().equals("")) {
				rm.setState("500");
				rm.setMsgInfo("【适用工序名称】不能为空，请确认！");
				return rm;
			}
			if (cbsc.getDept_desc()==null&&cbsc.getDept_desc().equals("")) {
				rm.setState("500");
				rm.setMsgInfo("【管理部门】不能为空，请确认！");
				return rm;
			}
			if (cbsc.getSection_name()==null&&cbsc.getSection_name().equals("")) {
				rm.setState("500");
				rm.setMsgInfo("【管理科室】不能为空，请确认！");
				return rm;
			}
			List<ComBaseStdCraft> li = this.getMapper(ComBaseStdCraftMapper.class).findWithCondition(null, "CRAFT_STD_NAME ='"+cbsc.getCraft_std_name()+"'", null, 0, 0);
		    if (li.size()>0) {
		    	rm.setState("500");
				rm.setMsgInfo("“[工艺标准名称]已存在，不能重复创建，请确认！”");
				return rm;
			}
		    int count=0;
			String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
//			获取工艺标准代码最大值
			List<ComBaseStdCraft> qrlist=this.getMapper(ComBaseStdCraftMapper.class)
					.findWithCondition(null,null, "CRAFT_STD_CODE DESC", 0, 1);
			if(qrlist!=null&&qrlist.size()>0){
				String s=qrlist.get(0).getCraft_std_code();
				count=Integer.parseInt(s.substring(s.length()-7, s.length()));
			}
			count++;
			String std_code="CSC"+String.format("%07d",count);
			//把获取的数据加入COM_BASE_STD_CRAFT表中
			 cbsc.setCraft_std_code(std_code);
			 cbsc.setCreate_name(username);
			 cbsc.setCreate_time(new Date());
			 this.getMapper(ComBaseStdCraftMapper.class).insert(cbsc);
			 rm.setState("200");
			 rm.setMsgInfo("新增工艺标准信息成功！！！");
		   } catch (Exception e) {
			// TODO: handle exception
			   rm.setState("500");
			  rm.setMsgInfo("新增工艺标准信息失败。。。");
			  rm.setErrorInfo(e.getMessage());
		}
		   return rm;
	   }
	   /**
	    * 修改工艺标准信息
	    * 
	    * @param sum
	    * @return
	    */
	   public ResultModel upProcessStandardInfo(CommonPage<ComBaseStdCraft> sum){
		   ResultModel rm=new ResultModel();
		   ComBaseStdCraft cbsc=sum.getObject();
		   try {
			   //有效性验证
		   if (cbsc.getCraft_std_code()==null&&"".equals(cbsc.getCraft_std_code())) {
				rm.setState("500");
				rm.setMsgInfo("【工艺标准号】不能为空，请确认！");
				return rm;
			}
			if (cbsc.getCraft_std_name()==null&&"".equals(cbsc.getCraft_std_name())) {
				rm.setState("500");
				rm.setMsgInfo("【工艺标准名称】不能为空，请确认！");
				return rm;
			}
			if (cbsc.getProcess_desc()==null&&cbsc.getProcess_desc().equals("")) {
				rm.setState("500");
				rm.setMsgInfo("【适用工序名称】不能为空，请确认！");
				return rm;
			}
			if (cbsc.getDept_desc()==null&&cbsc.getDept_desc().equals("")) {
				rm.setState("500");
				rm.setMsgInfo("【管理部门】不能为空，请确认！");
				return rm;
			}
			if (cbsc.getSection_name()==null&&cbsc.getSection_name().equals("")) {
				rm.setState("500");
				rm.setMsgInfo("【管理科室】不能为空，请确认！");
				return rm;
			} 
			//判断名称是否重复
		   List<ComBaseStdCraft> oldCraft=this.getMapper(ComBaseStdCraftMapper.class).findWithCondition(null, "CRAFT_STD_NAME='"+cbsc.getCraft_std_name()+"'", null, 0, 0);
		   if (DataHelper.checkIsNull(oldCraft)) {
			if (!oldCraft.get(0).getCraft_std_code().equals(cbsc.getCraft_std_code())) {
				rm.setState("500");
				rm.setMsgInfo("已存在工艺标准名称为["+cbsc.getCraft_std_name()+"]的标准号，请确认！");
				return rm;
			}
		   }
		   String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		   cbsc.setUpdate_name(username);
		   cbsc.setUpdate_time(new Date());
		   this.getMapper(ComBaseStdCraftMapper.class).upProcessStandardInfo(cbsc);
		   rm.setState("200");
		    rm.setMsgInfo("修改工艺标准信息成功！！！");
		   }catch (Exception e) {
				// TODO: handle exception
				   rm.setState("500");
				  rm.setMsgInfo("修改工艺标准信息失败。。。");
				  rm.setErrorInfo(e.getMessage());
			}
		   return rm;
	   }
	   /**
	    * 作废工艺标准信息
	    * @param sum
	    * @return
	    */
	   public ResultModel toVoidProcessStandardInfo(CommonPage<ComBaseStdCraft> sum){
		   ResultModel rm=new ResultModel();
		   List<ComBaseStdCraft> listcbsc=sum.getObjectList();
		   String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		   try {
			   for (ComBaseStdCraft cbsc : listcbsc) {
				   //修改COM_BASE_STD_CRAFT表中的状态=0
					cbsc.setValidflag("0");
					cbsc.setDelete_time(new Date());
					cbsc.setDelete_name(username);
					this.getMapper(ComBaseStdCraftMapper.class).update(cbsc);
					//修改COM_BASE_STD_CRAFT_TIC表中的状态=0
					this.getMapper(ComBaseStdCraftTicMapper.class).ToVoidOrRecover(cbsc.getCraft_std_code(),"0");
			}
			rm.setState("200");	
			rm.setMsgInfo("作废工艺标准信息成功！！！");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("作废工艺标准信息失败。。。");
			rm.setErrorInfo(e.getMessage());
		}
		   return rm;
	   }
	   /**
	    * 恢复已经作废的工艺标准信息
	    * @param sum
	    * @return
	    */
	   public ResultModel recoveryProcessStandardInfo(CommonPage<ComBaseStdCraft> sum){
		   ResultModel rm=new ResultModel();
		     List<ComBaseStdCraft> listcbsc = sum.getObjectList();
		     String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		   try {
			   for (ComBaseStdCraft cbsc : listcbsc) {
				 //修改COM_BASE_STD_CRAFT表中的状态=1
					cbsc.setValidflag("1");
					cbsc.setDelete_time(null);
					cbsc.setDelete_name(null);
					this.getMapper(ComBaseStdCraftMapper.class).tovoidSample(cbsc.getCraft_std_code());;
					//修改COM_BASE_STD_CRAFT_TIC表中的状态=0
					this.getMapper(ComBaseStdCraftTicMapper.class).ToVoidOrRecover(cbsc.getCraft_std_code(), "1");
			}
			rm.setState("200");
			rm.setMsgInfo("恢复工艺标准信息成功！！！");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("恢复工艺标准信息失败。。。");
			rm.setErrorInfo(e.getMessage());
		}
		   return rm;
	   }
	   /**
	    * 查询订货标准下拉列表
	    * @return
	    */
	   public ResultModel findOrderStandard(){
		   ResultModel rm=new ResultModel();
		   try {
			   List<ComBaseStd> listStd = this.getMapper(ComBaseStdMapper.class).findWithCondition(null, "STD_TYPE IN ('G','X') AND VALIDFLAG='1'", null, 0, 0);
		       rm.setData(listStd);
		       rm.setState("200");
		   } catch (Exception e) {
			// TODO: handle exception
			   rm.setState("500");
			   rm.setMsgInfo("查询订货标准失败");
			   rm.setErrorInfo(e.getMessage());
		}
		   return rm;
	   }
	   /**
	    * 查询订货标准下拉列表(分页)
	    * @return
	    */
	   public ReturnMsg<List<ComBaseStd>> findOrderStandardPage(CommonPage<ComBaseStd> sum){
		   ReturnMsg<List<ComBaseStd>> msg=new ReturnMsg<>();
		   ComBaseStdMapper mapper=this.getMapper(ComBaseStdMapper.class);
		   try {
			   String condition = "STD_TYPE IN ('G','X') AND VALIDFLAG='1' AND LOWER(STD_NAME) LIKE '%'||LOWER('"+sum.getObject().getStdName()+"')||'%'";
			   List<ComBaseStd> listStd = this.getMapper(ComBaseStdMapper.class).findWithCondition(null, condition, null,(sum.getPageIndex()-1)*sum.getPageSize(), sum.getPageSize());
			   sum.getObject().setStdName(null);
			   int count=mapper.countWithCondition(sum.getObject(), condition);
			   msg.setTotal(count);
			   msg.setMessage("获取数据成功");
			   msg.setSuccess(true);
			   msg.setResult(listStd);
		   } catch (Exception e) {
			// TODO: handle exception
			   msg.setSuccess(false);
			   msg.setMessage(e.getMessage());
		}
		   return msg;
	   }
	   
	// 查询交货状态
		public ReturnMsg<List<BaseDeliverystate>> GetDeliveryStatePage(CommonPage<BaseDeliverystate> sum) {
			// TODO Auto-generated method stub
			ReturnMsg<List<BaseDeliverystate>> msg=new ReturnMsg<>();
			try{
				Integer pageSize= sum.getPageSize()*(sum.getPageIndex()-1);
				Integer pageIndex=sum.getPageSize()*(sum.getPageIndex()-1)+sum.getPageSize();
				BaseDeliverystateMapper mapper=this.getMapper(BaseDeliverystateMapper.class);
				String condition="";
				if(sum.getObject()!=null&&!DataHelper.IsNullOrSpace(sum.getObject().getDeliveryStateDesc())){
					condition=" LOWER(DELIVERY_STATE_DESC) like '%'||LOWER('"+sum.getObject().getDeliveryStateDesc()+"')||'%'";
					sum.getObject().setDeliveryStateDesc(null);
				}
				
				List<BaseDeliverystate> list=mapper.findWithCondition(sum.getObject(), condition,"DELIVERY_STATE_CODE Desc", (sum.getPageIndex()-1)*sum.getPageSize(), sum.getPageSize());
				int count=mapper.countWithCondition(sum.getObject(), condition);
				msg.setTotal(count);
				msg.setMessage("获取数据成功");
				msg.setSuccess(true);
				msg.setResult(list);
			}catch(Exception ex){
				msg.setSuccess(false);
				msg.setMessage(ex.getMessage());
			}
			return msg;
		}
	   /**
	    * 获取管理部门
	    * @return
	    */
	   public ResultModel getDept(){
		   ResultModel rm=new ResultModel();
		   try {
			List<ComBaseStd> liststd = this.getMapper(ComBaseStdMapper.class).findWithCondition(null, null, null, 0, 0);
		     rm.setData(liststd);
		     rm.setState("200");
		   } catch (Exception e) {
			// TODO: handle exception
			   rm.setState("500");
			   rm.setMsgInfo("查询管理部门信息失败");
			   rm.setErrorInfo(e.getMessage());
		}
		   return rm;
	   }
	   /**
	    * 获取管理科室
	    * @return
	    */
	   public ResultModel getStationByDept(CommonPage<ComBaseStd> sum){
		   ResultModel rm=new ResultModel();
		   ComBaseStd cbs=sum.getObject();
		   try {
			List<ComBaseStd> liststd = this.getMapper(ComBaseStdMapper.class).findWithCondition(null, "DEPT_CODE='"+cbs.getDeptCode()+"'", null, 0, 0);
		     rm.setData(liststd);
		     rm.setState("200");
		   } catch (Exception e) {
			// TODO: handle exception
			   rm.setState("500");
			   rm.setMsgInfo("查询管理科室信息失败");
			   rm.setErrorInfo(e.getMessage());
		}
		   return rm;
	   }
}
