/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-07-04
 * 描述            ：资材备件库-申报信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Lms.Equipment.impl;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Equipment.mapper.YdmEquipmentApplylistMapper;
import market.Lms.Equipment.mapper.YdmEquipmentDetailMapper;
import market.Lms.Equipment.service.YdmEquipmentApplylistService;
import market.Lms.Equipment.vo.YdmEquipmentApplylist;
import market.Lms.Equipment.vo.YdmEquipmentDetail;
import market.Pms.Mat.mapper.PurMatClassMapper;
import market.Pms.Mat.mapper.PurMatMMapper;
import market.Pms.Mat.vo.PurMatClass;
import market.Pms.Mat.vo.PurMatM;

import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;

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="YdmEquipmentApplylistService")
public class YdmEquipmentApplylistServiceImpl extends BaseServiceImpl<YdmEquipmentApplylist,Object> implements YdmEquipmentApplylistService {


	/**
	 * 初始化功能/获取当前日期
	 * @param cpp
	 * @return
	 */
	public ResultModel doQueryPurMatClassInitialization(CommonPage<PurMatClass> cppc){
		ResultModel rm =new ResultModel();
		
		//获取当前的时间作为默认时间
		String nowDate=new SimpleDateFormat("yyyyMMdd").format(new Date());
		rm.setData(nowDate);
		
		//获取产品分类信息
		List<PurMatClass> lpmc=this.getMapper(PurMatClassMapper.class).find(null, 0, 0);
		rm.setData(lpmc);
//		//存储不同状态到list中;未完成		
		return rm;
	}

	/**
	 * 资材备件的库存查询
	 * @param cp
	 * @return
	 */
	public ResultModel doQuerySortCode(CommonPage<YdmEquipmentDetail> cp){
		ResultModel rm =new ResultModel();
		try{		
			YdmEquipmentDetail ymd = cp.getObject();
				List<YdmEquipmentDetail> yed=this.getMapper(YdmEquipmentDetailMapper.class).doQuerySortCode(ymd.getSrot_code(),ymd.getMat_code(),cp.getMemo1());
				rm.setData(yed);
				rm.setState("200");	
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败！"+ex.getMessage());
		}		
		return rm;
	}
	
	/**
	 *计划申报- 申报查询
	 * @param cp
	 * @return
	 */
	public ResultModel doQueryApplyList(CommonPage<YdmEquipmentApplylist> cp){
		ResultModel rm =new ResultModel();
		try{
			String v_man = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String sTime=cp.getStartTime();
			String eTime=cp.getEndTime();
			String v_storage=cp.getMemo1();			
			if(cp!=null&&cp.getObject()!=null){
				PageModel<YdmEquipmentApplylist> pm =new PageModel<YdmEquipmentApplylist>(cp.getPageIndex(),cp.getPageSize());
				pm.setRows(this.getMapper(YdmEquipmentApplylistMapper.class).doQueryApplyList(cp.getObject(),v_man,sTime,eTime,v_storage,(cp.getPageIndex()-1)*cp.getPageSize(), cp.getPageIndex()*cp.getPageSize()));				
				pm.setTotal(this.getMapper(YdmEquipmentApplylistMapper.class).doQueryApplyListCount(cp.getObject(),v_man,sTime,eTime,v_storage));								
				rm.setData(pm);
				rm.setState("200");
			}else{
				rm.setState("500");
			}			
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败！"+ex.getMessage());
		}		
		return rm;
	}
	
	
	/**
	 * 通过产品分类，获取分类下的具体产品
	 * @param cp
	 * @return
	 */
	public ResultModel doQueryGetProdudtName(CommonPage<PurMatM> cp){
		ResultModel rm =new ResultModel();
		try{
			if(cp!=null&&cp.getObject()!=null){
				List<PurMatM> lpmc=this.getMapper(PurMatMMapper.class).doQueryGetProdudtName(cp.getObject());
				rm.setData(lpmc);
				rm.setState("200");
			}else{
				rm.setState("500");
				System.out.println("参数错误");
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败！"+ex.getMessage());
		}		
		return rm;
	}
	
	
	/**
	 * 计划申报--确认申报--新增
	 * 
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)	
	public ResultModel doAddFinishedProductReturnWeb(CommonPage<YdmEquipmentApplylist> cp) throws Exception{
		ResultModel rm =new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String dept_name = (String) this.getSession().getAttribute(HomeService.SESSION_DEPT_NAME);
		Date currentTime = new Date();
		try{
			YdmEquipmentApplylist yeal = cp.getObject();//赋值操作
			if(yeal.getApply_type().equals("500510")&&(yeal.getPlan_return_date()==null||"".equals(yeal.getPlan_return_date()))){
				rm.setState("500");
				rm.setMsgInfo("申报类型为借用，必须输入归还日期");
			}else{
				if (StringUtils.isEmpty(yeal.getMachine_code())|| StringUtils.isEmpty(yeal.getMachine_name())) {
					throw new Exception("申报机台不能为空");
				}
				
				String apply_bill_no=this.getMapper(YdmEquipmentApplylistMapper.class).getNo("SB"+new SimpleDateFormat("yyyyMMdd").format(new Date()));
				if(cp!=null&&cp.getObject()!=null){
					yeal.setApply_bill_no(apply_bill_no);
					yeal.setState("0");
					yeal.setApply_dept(dept_name);
					yeal.setApply_time(currentTime);
					yeal.setOpr_man(username);
					yeal.setCreate_man_no(userId);
					yeal.setCreate_man_name(username);
					yeal.setCreate_time(currentTime);
					//插入新增预约信息中的计量预约信息，每次measure_no不同
					this.getMapper(YdmEquipmentApplylistMapper.class).insert(yeal);			
					rm.setState("200");	
					rm.setMsgInfo("新增申报成功 !");
				}
			}	
		}catch(Exception ex){
			rm.setState("500");	
			rm.setMsgInfo("参数传入错误！"+ex.getMessage());
		}			
		return rm;
	}
	
	/**
	 * 计划申报--修改申报
	 * 
	 */
	
	public ResultModel doUpdateApplylist(CommonPage<YdmEquipmentApplylist> cp){
		ResultModel rm =new ResultModel();
		try{
			YdmEquipmentApplylist yeal=this.getMapper(YdmEquipmentApplylistMapper.class).findById(cp.getObject().getApply_bill_no());
			if(yeal==null){
				rm.setState("500");
				rm.setMsgInfo("不存在该申报记录，请刷新页面重新查看！");
				return rm;
			}
			String myState=yeal.getState().trim();//去掉所取的值里面包含的空格
			
			if(cp!=null&&cp.getObject()!=null&&cp.getObject().getApply_bill_no()!=null){
				if(myState.equals("0")){		
					if (StringUtils.isEmpty(cp.getObject().getMachine_code())|| StringUtils.isEmpty(cp.getObject().getMachine_name())) {
						throw new Exception("申报机台不能为空");
					}
					this.getMapper(YdmEquipmentApplylistMapper.class).doUpdateApplylist(cp.getObject());
					rm.setState("200");	
					rm.setMsgInfo("修改成功！");
				}else{
					rm.setState("500");
					rm.setMsgInfo("该申报记录的状态不是【申报】的状态，不允许修改，谢谢！");
				}
			}else{
				rm.setState("500");
				rm.setMsgInfo("修改失败，参数传入错误!");
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("修改失败!"+ex.getMessage());
		}		
		return rm;	
	}
	
	/**
	 * 计划申报--删除申报
	 * 
	 */
	
	public ResultModel doDeleteApplylist(CommonPage<YdmEquipmentApplylist> cp){
		ResultModel rm =new ResultModel();
		try{
			YdmEquipmentApplylist yeal=this.getMapper(YdmEquipmentApplylistMapper.class).findById(cp.getObject().getApply_bill_no());
			if(yeal==null){
				rm.setState("500");
				rm.setMsgInfo("不存在该申报记录，请刷新页面重新查看！");
				return rm;
			}
			String myState=yeal.getState().trim();//去掉所取的值里面包含的空格
			
			if(cp!=null&&cp.getObject()!=null&&cp.getObject().getApply_bill_no()!=null){
				if(myState.equals("0")){				
					this.getMapper(YdmEquipmentApplylistMapper.class).doDelete(cp.getObject());
					rm.setState("200");	
					rm.setMsgInfo("撤销成功！");
				}else{
					rm.setState("500");
					rm.setMsgInfo("该申报记录的状态不是【申报】的状态，不允许撤销，谢谢！");
					return rm;
				}
			}else{
				rm.setState("500");
				rm.setMsgInfo("撤销失败，参数传入错误!");
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("撤销申报失败!"+ex.getMessage());
		}		
		return rm;	
	}
	
	
	/**
	 * 计划审批--申报查询
	 * String sWhere=" state in('0','1') ";
	 */
	
	public ResultModel doQueryApplyListApproval(CommonPage<YdmEquipmentApplylist> cp){
		ResultModel rm =new ResultModel();
		String sWhere = " t.state in('0','1') ";
		try{
			String sTime=cp.getStartTime();
			String eTime=cp.getEndTime();			
			List<YdmEquipmentApplylist> yeaList = this.getMapper(YdmEquipmentApplylistMapper.class).doQueryApplyListApproval(cp.getObject(),sTime,eTime,sWhere);
			rm.setData(yeaList);
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败！"+ex.getMessage());
		}		
		return rm;
	}
	
	
	/**
	 * 计划审批--审批申报
	 * 
	 */
	
	public ResultModel doUpdateApplylistApproval(CommonPage<YdmEquipmentApplylist> cp){
		ResultModel rm =new ResultModel();
		try{
			String man = (String) this.getSession().getAttribute(
					HomeService.SESSION_USER_NAME);
			
			if (man == null || "".equals(man)) {
				throw new Exception("登录信息过期,请重新登录 !");
			}
			
			String dept = (String) this.getSession().getAttribute(
					HomeService.SESSION_DEPT_NAME);
			
			YdmEquipmentApplylist yeal=this.getMapper(YdmEquipmentApplylistMapper.class).findById(cp.getObject().getApply_bill_no());
			String myState=yeal.getState().trim();//去掉所取的值里面包含的空格
			
			if(cp!=null&&cp.getObject()!=null&&cp.getObject().getApply_bill_no()!=null){
				YdmEquipmentApplylist yealGetState =new YdmEquipmentApplylist();
				yealGetState=cp.getObject();
				yealGetState.setState("1");//状态设置为1审批
				yealGetState.setApproval_quantity(cp.getObject().getApproval_quantity());
				yealGetState.setApproval_time(new Timestamp(new Date().getTime()));
				yealGetState.setApproval_dept(dept);
				yealGetState.setApproval_man(man);
				yealGetState.setGrant_quantity(0d); // 发放数量默认为0
				
				if(myState.equals("0")){				
					this.getMapper(YdmEquipmentApplylistMapper.class).doUpdateApplylistApproval(yealGetState);
					rm.setState("200");			
				}else{
					rm.setState("500");
					rm.setMsgInfo("申报已审批 , 不允许重复审批");
				} 
			}else{
				rm.setState("500");
				rm.setMsgInfo("参数传入错误!");
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("参数传入错误!"+ex.getMessage());
		}		
		return rm;	
	}
	
	/**
	 * 计划审批--撤销申报
	 * 
	 */
	
	public ResultModel doUpdateApplylistRecall(CommonPage<YdmEquipmentApplylist> cp){
		ResultModel rm =new ResultModel();
		try{
			YdmEquipmentApplylist yeal=this.getMapper(YdmEquipmentApplylistMapper.class).findById(cp.getObject().getApply_bill_no());
			String myState=yeal.getState().trim();//去掉所取的值里面包含的空格
			
			if(cp!=null&&cp.getObject()!=null&&cp.getObject().getApply_bill_no()!=null){
				YdmEquipmentApplylist yealGetState =new YdmEquipmentApplylist();
				yealGetState=cp.getObject();
				yealGetState.setState("0");//状态设置为0申报
				if(myState.equals("1")){				
					this.getMapper(YdmEquipmentApplylistMapper.class).doUpdateApplylistRecall(yealGetState);
					rm.setState("200");			
				}else{
					rm.setState("500");
					rm.setMsgInfo("此状态不允许撤销 !");
				}
			}else{
				rm.setState("500");
				rm.setMsgInfo("参数传入错误!");
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("参数传入错误!"+ex.getMessage());
		}		
		return rm;	
	}
}
