package market.Lms.FixedAssets.impl;

import java.util.ArrayList;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsAnnexMapper;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsInfoMapper;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsMaintainInfoMapper;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsMaintainLineMapper;
import market.Lms.FixedAssets.service.LmsFixedAssetsMaintainSumService;
import market.Lms.FixedAssets.vo.LmsFixedAssetsAnnex;
import market.Lms.FixedAssets.vo.LmsFixedAssetsInfo;
import market.Lms.FixedAssets.vo.LmsFixedAssetsMaintainInfo;
import market.Lms.FixedAssets.vo.LmsFixedAssetsMaintainLine;
import market.Lms.FixedAssets.vo.LmsFixedAssetsSum;

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;

@RemoteService(path="LmsFixedAssetsMaintainSum")
public class LmsFixedAssetsMaintainSumServiceImpl extends BaseServiceImpl<LmsFixedAssetsSum,Object> implements LmsFixedAssetsMaintainSumService{

	//==============================================web==========================================
	/**
	 * web端维修新增提交
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSubmitWeb(LmsFixedAssetsSum sum) throws Exception{
		ResultModel rm = new ResultModel();
		String maintainNo = "";
		try{
			//1维修主表新增
			LmsFixedAssetsMaintainInfo maintainInfo = sum.getMaintainInfo();
			if(maintainInfo != null){
				maintainInfo.setValid_flag("1");//维修状态
				maintainNo = this.getBean(LmsFixedAssetsMaintainInfoServiceImpl.class).doAdd(maintainInfo);
			}

			//2维修副表新增 并更改对应资产的使用信息及状态
			List<LmsFixedAssetsInfo> assetsList = sum.getAssetsList();
			if(assetsList!=null){
				for(LmsFixedAssetsInfo assets : assetsList){
					assets.setValid_flag("5");//暂定维修状态值为5
					this.getBean(LmsFixedAssetsInfoServiceImpl.class).doUpdate(assets);

					//维修副表新增
					LmsFixedAssetsMaintainLine line = new LmsFixedAssetsMaintainLine();
					line.setMaintain_no(maintainNo);
					line.setSn_code(assets.getSn_code());
					this.getBean(LmsFixedAssetsMaintainLineServiceImpl.class).doAdd(line);
				}
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			e.printStackTrace();
			throw e;
		}
		return rm;
	}
	/**
	 * 维修归还
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doBackWeb(LmsFixedAssetsSum sum) throws Exception{
		ResultModel rm = new ResultModel();
		try{
			//1维修主表修改
			LmsFixedAssetsMaintainInfo maintainInfo = sum.getMaintainInfo();
			if(maintainInfo != null){
				maintainInfo.setValid_flag("2");//维修完毕
				this.getBean(LmsFixedAssetsMaintainInfoServiceImpl.class).doUpdate(maintainInfo);
			}

			//2修改资产的使用信息及状态
			List<LmsFixedAssetsInfo> assetsList = sum.getAssetsList();
			if(assetsList!=null){
				for(LmsFixedAssetsInfo assets : assetsList){
					assets.setValid_flag("1");//维修完毕 ，空闲
					this.getBean(LmsFixedAssetsInfoServiceImpl.class).doUpdate(assets);
				}
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			e.printStackTrace();
			throw e;
		}
		return rm;
	}

	/**Web端根据条件查询（1维修人  2业务时间  3状态 ）
	 * 暂定人员模糊查询
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<LmsFixedAssetsMaintainInfo> sum){
		ResultModel rm = new ResultModel();
		LmsFixedAssetsMaintainInfo maintain = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		String beginTime = sum.getStartTime();
		String endTime = sum.getEndTime();
		List<LmsFixedAssetsMaintainInfo> maintainList = new ArrayList<LmsFixedAssetsMaintainInfo>();
		PageModel<LmsFixedAssetsMaintainInfo> pm=new PageModel<LmsFixedAssetsMaintainInfo>(pageIndex, pageSize);
		int count = 0;
		try{
			maintainList = this.getMapper(LmsFixedAssetsMaintainInfoMapper.class).doQueryList(maintain,beginTime,endTime,(pageIndex-1)*pageSize, pageIndex*pageSize);
			count = this.getMapper(LmsFixedAssetsMaintainInfoMapper.class).countList(maintain,beginTime,endTime);
			pm.setRows(maintainList);
			pm.setTotal(count);
			rm.setData(pm);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
		}
		return rm;
	}

	/**web端查询详情
	 * @param sum
	 * @return
	 */
	public ResultModel doQueryDetailWeb(CommonPage<LmsFixedAssetsMaintainInfo> sum){
		ResultModel rm = new ResultModel();
		String maintainNo = sum.getMemo1();
		LmsFixedAssetsSum receiveSum = new LmsFixedAssetsSum();
		try{
			//1 主表信息
			LmsFixedAssetsMaintainInfo maintain = this.getMapper(LmsFixedAssetsMaintainInfoMapper.class).findById(maintainNo);

			//2 资产表信息
			List<LmsFixedAssetsInfo> assetsList = new ArrayList<LmsFixedAssetsInfo>();
			LmsFixedAssetsMaintainLine line = new LmsFixedAssetsMaintainLine();
			line.setMaintain_no(maintainNo);
			List<LmsFixedAssetsMaintainLine> lineList = this.getMapper(LmsFixedAssetsMaintainLineMapper.class).find(line, 0, 0);
			if(lineList!=null){
				for(LmsFixedAssetsMaintainLine ofa : lineList){
					LmsFixedAssetsInfo assets = this.getMapper(LmsFixedAssetsInfoMapper.class).findById(ofa.getSn_code());

					//资产附件
					LmsFixedAssetsAnnex annex = new LmsFixedAssetsAnnex();
					annex.setNews_no(assets.getSn_code());
					List<LmsFixedAssetsAnnex> annexList = this.getMapper(LmsFixedAssetsAnnexMapper.class).find(annex, 0, 0);
					assets.setAnnexList(annexList);

					assetsList.add(assets);
				}
			}
			receiveSum.setMaintainInfo(maintain);
			receiveSum.setAssetsList(assetsList);
			rm.setData(receiveSum);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
		}
		return rm;
	}

	//==============================================app==========================================
	//app端维修新增提交
	public ResultModel doSubmitApp(LmsFixedAssetsSum sum){
		ResultModel rm = new ResultModel();
		String maintainNo = "";
		try{
			//1维修主表新增
			LmsFixedAssetsMaintainInfo maintainInfo = sum.getMaintainInfo();
			if(maintainInfo != null){
				maintainInfo.setValid_flag("1");//维修状态
				maintainNo = this.getBean(LmsFixedAssetsMaintainInfoServiceImpl.class).doAdd(maintainInfo);
			}

			//2维修副表新增 并更改对应资产的使用信息及状态
			List<LmsFixedAssetsInfo> assetsList = sum.getAssetsList();
			if(assetsList!=null){
				for(LmsFixedAssetsInfo assets : assetsList){
					assets.setValid_flag("5");//暂定维修状态值为5
					this.getBean(LmsFixedAssetsInfoServiceImpl.class).doUpdate(assets);

					//维修副表新增
					LmsFixedAssetsMaintainLine line = new LmsFixedAssetsMaintainLine();
					line.setMaintain_no(maintainNo);
					line.setSn_code(assets.getSn_code());
					this.getBean(LmsFixedAssetsMaintainLineServiceImpl.class).doAdd(line);
				}
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
		}
		return rm;
	}
	
	/**
	 * 维修归还
	 * */
	public ResultModel doBackApp(LmsFixedAssetsSum sum){
		ResultModel rm = new ResultModel();
		try{
			//1维修主表修改
			LmsFixedAssetsMaintainInfo maintainInfo = sum.getMaintainInfo();
			if(maintainInfo != null){
				maintainInfo.setValid_flag("2");//维修完毕
				this.getBean(LmsFixedAssetsMaintainInfoServiceImpl.class).doUpdate(maintainInfo);
			}

			//2修改资产的使用信息及状态
			List<LmsFixedAssetsInfo> assetsList = sum.getAssetsList();
			if(assetsList!=null){
				for(LmsFixedAssetsInfo assets : assetsList){
					assets.setValid_flag("1");//维修完毕 ，空闲
					this.getBean(LmsFixedAssetsInfoServiceImpl.class).doUpdate(assets);
				}
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
		}
		return rm;
	}

	/**app端根据条件查询（1维修人  2业务时间  3状态 ）
	 * 暂定人员模糊查询
	 * @return
	 */
	public List<LmsFixedAssetsMaintainInfo> doQueryApp(CommonPage<LmsFixedAssetsMaintainInfo> sum){
		LmsFixedAssetsMaintainInfo maintain = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		String beginTime = sum.getStartTime();
		String endTime = sum.getEndTime();
		List<LmsFixedAssetsMaintainInfo> maintainList = new ArrayList<LmsFixedAssetsMaintainInfo>();
		try{
			maintainList = this.getMapper(LmsFixedAssetsMaintainInfoMapper.class).doQueryList(maintain,beginTime,endTime,pageIndex, pageSize);
		}catch(Exception e){
			return new ArrayList<LmsFixedAssetsMaintainInfo>();
		}
		return maintainList;
	}

	/**app端查询详情
	 * @param sum
	 * @return
	 */
	public LmsFixedAssetsSum doQueryDetailApp(String maintainNo){
		LmsFixedAssetsSum receiveSum = new LmsFixedAssetsSum();
		try{
			//1 主表信息
			LmsFixedAssetsMaintainInfo maintain = this.getMapper(LmsFixedAssetsMaintainInfoMapper.class).findById(maintainNo);

			//2 资产表信息
			List<LmsFixedAssetsInfo> assetsList = new ArrayList<LmsFixedAssetsInfo>();
			LmsFixedAssetsMaintainLine line = new LmsFixedAssetsMaintainLine();
			line.setMaintain_no(maintainNo);
			List<LmsFixedAssetsMaintainLine> lineList = this.getMapper(LmsFixedAssetsMaintainLineMapper.class).find(line, 0, 0);
			if(lineList!=null){
				for(LmsFixedAssetsMaintainLine ofa : lineList){
					LmsFixedAssetsInfo assets = this.getMapper(LmsFixedAssetsInfoMapper.class).findById(ofa.getSn_code());

					//资产附件
					LmsFixedAssetsAnnex annex = new LmsFixedAssetsAnnex();
					annex.setNews_no(assets.getSn_code());
					List<LmsFixedAssetsAnnex> annexList = this.getMapper(LmsFixedAssetsAnnexMapper.class).find(annex, 0, 0);
					assets.setAnnexList(annexList);

					assetsList.add(assets);
				}
			}
			receiveSum.setMaintainInfo(maintain);
			receiveSum.setAssetsList(assetsList);
		}catch(Exception e){
			return new LmsFixedAssetsSum();
		}
		return receiveSum;
	}
}
