/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-01-09
 * 描述            ：加工计划信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
 ***********************************************************/
package market.Slm.ServiceManagement.impl;
import java.util.Date;
import java.util.List;

import market.Com.pb.PbMethod;
import market.Com.pb.SpeOptBaseService;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.ServiceManagement.mapper.SlmServiceViewAnnexMapper;
import market.Slm.ServiceManagement.service.SlmServiceViewAnnexService;
import market.Slm.ServiceManagement.vo.SlmServiceViewAnnex;

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

@RemoteService(path="SlmServiceViewAnnexService")
public class SlmServiceViewAnnexServiceImpl extends BaseServiceImpl<SlmServiceViewAnnex,Object> implements SlmServiceViewAnnexService {

	/**
	 * 附件
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmServiceViewAnnex> sum){
		ResultModel rm=new ResultModel();
		SlmServiceViewAnnex annex=sum.getObject();
		try{
			List<SlmServiceViewAnnex> list=this.getMapper(SlmServiceViewAnnexMapper.class).findWithCondition(annex, null, " annex_no desc", 0,0);
			rm.setData(list);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 新增
	 * */
	public ResultModel doAddWeb(CommonPage<SlmServiceViewAnnex> sum){
		ResultModel rm=new ResultModel();
		SlmServiceViewAnnex annex=sum.getObject();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			String id=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
			annex.setAnnex_no(id);
			annex.setPrvd_man_no(userId);
			annex.setPrvd_man_name(userName);
			annex.setPrvd_time(new Date());
			if(annex.getAnnex_path()==null){
				rm.setState("500");
				rm.setMsgInfo("请传入附件路径");
				return rm;
			}
			if(!annex.getAnnex_path().contains("group1/M00")){
				annex.setAnnex_path(this.getBean(SpeOptBaseService.class).doAdd(annex.getAnnex_path()));			
			}
			this.getMapper(SlmServiceViewAnnexMapper.class).insert(annex);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("新增失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 删除 物理删除
	 * */
	public ResultModel doDeleteWeb(CommonPage<SlmServiceViewAnnex> sum){
		ResultModel rm=new ResultModel();
		SlmServiceViewAnnex annex=sum.getObject();
		try{
			annex=this.getMapper(SlmServiceViewAnnexMapper.class).findById(annex.getAnnex_no());
			if(annex==null){
				rm.setState("500");
				rm.setMsgInfo("该附件已被删除");
				return rm;
			}
			if(annex!=null){
				this.getBean(SpeOptBaseService.class).doDelete(annex.getAnnex_path());
			}
			this.getMapper(SlmServiceViewAnnexMapper.class).delete(annex.getAnnex_no());
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}
	
	//=====================eason add 2019-08-02================
	public ResultModel doQueryApp(CommonPage<SlmServiceViewAnnex> sum){
		ResultModel rm=new ResultModel();
		SlmServiceViewAnnex annex=sum.getObject();
		try{
			if(annex.getView_no()!=null&&!"".equals(annex.getView_no())){
				List<SlmServiceViewAnnex> list=this.getMapper(SlmServiceViewAnnexMapper.class).findWithCondition(annex, null, " annex_no desc", 0,0);
				rm.setData(list);
			}
			
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	public ResultModel doQueryOne(CommonPage<SlmServiceViewAnnex> sum){
		ResultModel rm=new ResultModel();
		SlmServiceViewAnnex annex=sum.getObject();
		try{
			if(annex.getAnnex_no()!=null&&!"".equals(annex.getAnnex_no())){
				SlmServiceViewAnnex list=this.getMapper(SlmServiceViewAnnexMapper.class).findById(annex.getAnnex_no());
				rm.setData(list);
			}
			
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	} 
	
	public ResultModel doAddApp(CommonPage<SlmServiceViewAnnex> sum){
		ResultModel rm=new ResultModel();
		SlmServiceViewAnnex annex=sum.getObject();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			String id=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
			annex.setAnnex_no(id);
			annex.setPrvd_man_no(userId);
			annex.setPrvd_man_name(userName);
			annex.setPrvd_time(new Date());
			
			if(annex.getAnnex_path()==null){
				rm.setState("500");
				rm.setMsgInfo("请传入附件路径");
				return rm;
			}
			if(!annex.getAnnex_path().contains("group1/M00")){
				annex.setAnnex_path(this.getBean(SpeOptBaseService.class).doAdd(annex.getAnnex_path()));			
			}
			
			this.getMapper(SlmServiceViewAnnexMapper.class).insert(annex);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("新增失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	} 
	
	public ResultModel doDeleteApp(CommonPage<SlmServiceViewAnnex> sum){
		ResultModel rm=new ResultModel();
		SlmServiceViewAnnex annex=sum.getObject();
		try{
			annex=this.getMapper(SlmServiceViewAnnexMapper.class).findById(annex.getAnnex_no());
			if(annex==null){
				rm.setState("500");
				rm.setMsgInfo("该附件已被删除");
				return rm;
			}
			if(annex!=null){
				this.getBean(SpeOptBaseService.class).doDelete(annex.getAnnex_path());
			}
			
			this.getMapper(SlmServiceViewAnnexMapper.class).delete(annex.getAnnex_no());
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("删除失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
}