/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：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.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.ServiceManagement.mapper.SlmServiceViewMapper;
import market.Slm.ServiceManagement.service.SlmServiceViewService;
import market.Slm.ServiceManagement.vo.SlmServiceView;

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="SlmServiceViewService")
public class SlmServiceViewServiceImpl extends BaseServiceImpl<SlmServiceView,Object> implements SlmServiceViewService {

	/**
	 * 意见反馈查询
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmServiceView> sum){
		ResultModel rm=new ResultModel();
		SlmServiceView view=sum.getObject();
		String startTime=sum.getStartTime();
		String endTime=sum.getEndTime();
		try{
			String sWhere=" 1=1 ";
			if(startTime!=null&&!"".equals(startTime)){		
				sWhere+=" and to_char(feedback_time, 'yyyy-mm-dd hh24:mi:ss') >='"+startTime+"'";
			}
			if(endTime!=null&&!"".equals(endTime)){
				sWhere+=" and to_char(feedback_time, 'yyyy-mm-dd hh24:mi:ss') <= '"+endTime+"'";
			}
			List<SlmServiceView> list=this.getMapper(SlmServiceViewMapper.class).findWithCondition(view, sWhere, " view_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<SlmServiceView> sum){
		ResultModel rm=new ResultModel();
		SlmServiceView view=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);
			view.setView_no(id);
			if(view.getUser_id()==null)
				view.setUser_id(userId);
			if(view.getUser_name()==null)
				view.setUser_name(userName);
			view.setFeedback_time(new Date());
			view.setValid_flag("0");
			this.getMapper(SlmServiceViewMapper.class).insert(view);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("新增失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 提交 待提交0→1待处理
	 * */
	public ResultModel doSubmitWeb(CommonPage<SlmServiceView> sum){
		ResultModel rm=new ResultModel();
		SlmServiceView view=sum.getObject();
		try{
			SlmServiceView check=this.getMapper(SlmServiceViewMapper.class).findById(view.getView_no());
			if(check==null){
				rm.setState("500");
				rm.setMsgInfo("请传入参数view_no");
				return rm;
			}
			if("0".equals(check.getValid_flag())){
				view.setValid_flag("1");
				this.getMapper(SlmServiceViewMapper.class).update(view);
			}else{
				rm.setState("500");
				rm.setMsgInfo("请选择待提单的数据进行操作");
				return rm;
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}

	/**
	 * 撤回 待处理1→0待提交
	 * */
	public ResultModel doRollbackWeb(CommonPage<SlmServiceView> sum){
		ResultModel rm=new ResultModel();
		SlmServiceView view=sum.getObject();
		try{
			SlmServiceView check=this.getMapper(SlmServiceViewMapper.class).findById(view.getView_no());
			if(check==null){
				rm.setState("500");
				rm.setMsgInfo("请传入参数view_no");
				return rm;
			}
			if("1".equals(check.getValid_flag())){
				view.setValid_flag("0");
				this.getMapper(SlmServiceViewMapper.class).update(view);
			}else{
				rm.setState("500");
				rm.setMsgInfo("请选择待处理的数据进行撤回");
				return rm;
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}

	/**
	 * 处理中 待处理1→2处理中
	 * */
	public ResultModel doProcessingWeb(CommonPage<SlmServiceView> sum){
		ResultModel rm=new ResultModel();
		SlmServiceView view=sum.getObject();
		try{
			SlmServiceView check=this.getMapper(SlmServiceViewMapper.class).findById(view.getView_no());
			if(check==null){
				rm.setState("500");
				rm.setMsgInfo("请传入参数view_no");
				return rm;
			}
			if("1".equals(check.getValid_flag())){
				view.setValid_flag("2");
				this.getMapper(SlmServiceViewMapper.class).update(view);
			}else{
				rm.setState("500");
				rm.setMsgInfo("请选择处理中的数据进行处理");
				return rm;
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}

	/**
	 * 处理 处理中2→3已处理
	 * */
	public ResultModel doHandleWeb(CommonPage<SlmServiceView> sum){
		ResultModel rm=new ResultModel();
		SlmServiceView view=sum.getObject();
		try{
			SlmServiceView check=this.getMapper(SlmServiceViewMapper.class).findById(view.getView_no());
			if(check==null){
				rm.setState("500");
				rm.setMsgInfo("请传入参数view_no");
				return rm;
			}
			if("2".equals(check.getValid_flag())){
				view.setValid_flag("3");
				view.setDetail_time(new Date());
				this.getMapper(SlmServiceViewMapper.class).update(view);
			}else{
				rm.setState("500");
				rm.setMsgInfo("请选择待处理的数据进行处理");
				return rm;
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}

	/**
	 * 修改
	 * */
	public ResultModel doUpdateWeb(CommonPage<SlmServiceView> sum){
		ResultModel rm=new ResultModel();
		SlmServiceView view=sum.getObject();
		try{
			SlmServiceView check=this.getMapper(SlmServiceViewMapper.class).findById(view.getView_no());
			if(check==null){
				rm.setState("500");
				rm.setMsgInfo("请传入参数view_no");
				return rm;
			}
			if("02".contains(check.getValid_flag())){
				this.getMapper(SlmServiceViewMapper.class).update(view);
			}else{
				rm.setState("500");
				rm.setMsgInfo("只允许修改待提交的数据");
				return rm;
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}

	/**
	 * 删除 逻辑删除 4作废
	 * */
	public ResultModel doDeleteWeb(CommonPage<SlmServiceView> sum){
		ResultModel rm=new ResultModel();
		SlmServiceView view=sum.getObject();
		try{
			SlmServiceView check=this.getMapper(SlmServiceViewMapper.class).findById(view.getView_no());
			if(check==null){
				rm.setState("500");
				rm.setMsgInfo("请传入参数view_no");
				return rm;
			}
			if("0".equals(check.getValid_flag())){
				check.setValid_flag("4"); 		//作废
				this.getMapper(SlmServiceViewMapper.class).update(check);
			}else{
				rm.setState("500");
				rm.setMsgInfo("请废除待提交的数据");
				return rm;
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}

	/**
	 * 启用
	 * */
	public ResultModel doEnableWeb(CommonPage<SlmServiceView> sum){
		ResultModel rm=new ResultModel();
		SlmServiceView view=sum.getObject();
		try{
			SlmServiceView check=this.getMapper(SlmServiceViewMapper.class).findById(view.getView_no());
			if(check==null){
				rm.setState("500");
				rm.setMsgInfo("请传入参数view_no");
				return rm;
			}
			if("4".equals(check.getValid_flag())){
				check.setValid_flag("0"); 		//作废
				this.getMapper(SlmServiceViewMapper.class).update(check);
			}else{
				rm.setState("500");
				rm.setMsgInfo("请启用已废除的数据");
				return rm;
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}
	
	//====================eason add 2019-07-17==================
	/**
	 * 新增
	 * */
	public ResultModel doAddApp(CommonPage<SlmServiceView> sum){
		ResultModel rm=new ResultModel();
		SlmServiceView view=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);
			view.setView_no(id);
			if(view.getUser_id()==null)
				view.setUser_id(userId);
			if(view.getUser_name()==null)
				view.setUser_name(userName);
			view.setFeedback_time(new Date());
			view.setValid_flag("0");
			view.setCustomer_no((String) this.getSession().getAttribute(HomeService.SESSION_DEPT_ID));
			view.setCustomer_name((String) this.getSession().getAttribute(HomeService.SESSION_DEPT_NAME));
			this.getMapper(SlmServiceViewMapper.class).insert(view);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("新增失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
}