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.LmsFixedAssetsBorrowInfoMapper;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsBorrowLineMapper;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsInfoMapper;
import market.Lms.FixedAssets.service.LmsFixedAssetsBorrowSumService;
import market.Lms.FixedAssets.vo.LmsFixedAssetsAnnex;
import market.Lms.FixedAssets.vo.LmsFixedAssetsBorrowInfo;
import market.Lms.FixedAssets.vo.LmsFixedAssetsBorrowLine;
import market.Lms.FixedAssets.vo.LmsFixedAssetsInfo;
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;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="LmsFixedAssetsBorrowSum")
public class LmsFixedAssetsBorrowSumServiceImpl extends BaseServiceImpl<LmsFixedAssetsSum,Object> implements LmsFixedAssetsBorrowSumService{

	//==============================================web==========================================
	/**
	 * web端借用新增提交
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSubmitWeb(LmsFixedAssetsSum sum) throws Exception{
		ResultModel rm = new ResultModel();
		String borrowNo = "";
		try{
			//1借用主表新增
			LmsFixedAssetsBorrowInfo borrowInfo = sum.getBorrowInfo();
			if(borrowInfo != null){
				borrowInfo.setValid_flag("1");//借用状态
				borrowNo = this.getBean(LmsFixedAssetsBorrowInfoServiceImpl.class).doAdd(borrowInfo);
			}

			//2借用副表新增 并更改对应资产的使用信息及状态
			List<LmsFixedAssetsInfo> assetsList = sum.getAssetsList();
			if(assetsList!=null){
				for(LmsFixedAssetsInfo assets : assetsList){
					//借用副表中存放被借用人的信息
					LmsFixedAssetsBorrowLine line = new LmsFixedAssetsBorrowLine();
					line.setBorrow_no(borrowNo);
					line.setSn_code(assets.getSn_code());
					line.setUser_no(assets.getUser_no());
					line.setUser_name(assets.getUser_name());
					line.setUse_dept_no(assets.getUse_dept_no());
					line.setUse_dept_name(assets.getUse_dept_name());
					this.getBean(LmsFixedAssetsBorrowLineServiceImpl.class).doAdd(line);

					//将借用人的信息存放到借用资产里面
					assets.setUser_no(borrowInfo.getBorrower_no());
					assets.setUser_name(borrowInfo.getBorrower_name());
					assets.setUse_dept_no(borrowInfo.getUse_dept_no());
					assets.setUse_dept_name(borrowInfo.getUse_dept_name());
					assets.setValid_flag("4");
					this.getBean(LmsFixedAssetsInfoServiceImpl.class).doUpdate(assets);
				}
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			throw e;
		}
		return rm;
	}

	/**web端归还（传递归还的借用编号）（达到目的：将资产的使用信息还原并且状态设置为使用）
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doBackWeb(LmsFixedAssetsBorrowInfo borrow) throws Exception{
		ResultModel rm = new ResultModel();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String borrowNo = borrow.getBorrow_no();
		try{
			if(!borrowNo.isEmpty() && null != borrowNo){
				LmsFixedAssetsBorrowLine line = new LmsFixedAssetsBorrowLine();
				line.setBorrow_no(borrowNo);
				List<LmsFixedAssetsBorrowLine> lineList = this.getMapper(LmsFixedAssetsBorrowLineMapper.class).find(line, 0, 0);
				if(lineList!=null){
					//还原资产状态
					for(LmsFixedAssetsBorrowLine ofab : lineList){
						LmsFixedAssetsInfo assets = this.getMapper(LmsFixedAssetsInfoMapper.class).findById(ofab.getSn_code());
						if(assets != null){
							assets.setUser_no(ofab.getUser_no());
							assets.setUser_name(ofab.getUser_name());
							assets.setUse_dept_no(ofab.getUse_dept_no());
							assets.setUse_dept_name(ofab.getUse_dept_name());
							assets.setValid_flag("2");
							this.getBean(LmsFixedAssetsInfoServiceImpl.class).doUpdate(assets);
						}
					}
				}
				//主表状态修改为归还
				borrow.setValid_flag("2");
				borrow.setReturner_no(userId);
				borrow.setReturner_name(userName);
				this.getBean(LmsFixedAssetsBorrowInfoServiceImpl.class).doUpdate(borrow);
			} 
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			throw e;
		}
		return rm;
	}

	/**Web端根据条件查询（1借用人  2借用时间  3状态 ）
	 * 暂定人员模糊查询
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<LmsFixedAssetsBorrowInfo> sum){
		ResultModel rm = new ResultModel();
		LmsFixedAssetsBorrowInfo borrow = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		String beginTime = sum.getStartTime();
		String endTime = sum.getEndTime();
		List<LmsFixedAssetsBorrowInfo> borrowList = new ArrayList<LmsFixedAssetsBorrowInfo>();
		PageModel<LmsFixedAssetsBorrowInfo> pm=new PageModel<LmsFixedAssetsBorrowInfo>(pageIndex, pageSize);
		int count = 0;
		try{
			borrowList = this.getMapper(LmsFixedAssetsBorrowInfoMapper.class).doQueryList(borrow,beginTime,endTime,(pageIndex-1)*pageSize, pageIndex*pageSize);
			count = this.getMapper(LmsFixedAssetsBorrowInfoMapper.class).countList(borrow,beginTime,endTime);
			pm.setRows(borrowList);
			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<LmsFixedAssetsBorrowInfo> sum){
		ResultModel rm = new ResultModel();
		String borrowNo = sum.getMemo1();
		LmsFixedAssetsSum receiveSum = new LmsFixedAssetsSum();
		try{
			//1 主表信息
			LmsFixedAssetsBorrowInfo borrow = this.getMapper(LmsFixedAssetsBorrowInfoMapper.class).findById(borrowNo);

			//2 资产表信息
			List<LmsFixedAssetsInfo> assetsList = new ArrayList<LmsFixedAssetsInfo>();
			LmsFixedAssetsBorrowLine line = new LmsFixedAssetsBorrowLine();
			line.setBorrow_no(borrowNo);
			List<LmsFixedAssetsBorrowLine> lineList = this.getMapper(LmsFixedAssetsBorrowLineMapper.class).find(line, 0, 0);
			if(lineList!=null){
				for(LmsFixedAssetsBorrowLine 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.setBorrowInfo(borrow);
			receiveSum.setAssetsList(assetsList);
			rm.setData(receiveSum);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
		}
		return rm;
	}

	//==============================================app==========================================
	//web端借用新增提交
	public ResultModel doSubmitApp(LmsFixedAssetsSum sum){
		ResultModel rm = new ResultModel();
		String borrowNo = "";
		try{
			//1借用主表新增
			LmsFixedAssetsBorrowInfo borrowInfo = sum.getBorrowInfo();
			if(borrowInfo != null){
				borrowInfo.setValid_flag("1");//借用状态
				borrowNo = this.getBean(LmsFixedAssetsBorrowInfoServiceImpl.class).doAdd(borrowInfo);
			}

			//2借用副表新增 并更改对应资产的使用信息及状态
			List<LmsFixedAssetsInfo> assetsList = sum.getAssetsList();
			if(assetsList!=null){
				for(LmsFixedAssetsInfo assets : assetsList){
					//借用副表中存放被借用人的信息
					LmsFixedAssetsBorrowLine line = new LmsFixedAssetsBorrowLine();
					line.setBorrow_no(borrowNo);
					line.setSn_code(assets.getSn_code());
					line.setUser_no(assets.getUser_no());
					line.setUser_name(assets.getUser_name());
					line.setUse_dept_no(assets.getUse_dept_no());
					line.setUse_dept_name(assets.getUse_dept_name());
					this.getBean(LmsFixedAssetsBorrowLineServiceImpl.class).doAdd(line);

					//将借用人的信息存放到借用资产里面
					assets.setUser_no(borrowInfo.getBorrower_no());
					assets.setUser_name(borrowInfo.getBorrower_name());
					assets.setUse_dept_no(borrowInfo.getUse_dept_no());
					assets.setUse_dept_name(borrowInfo.getUse_dept_name());
					assets.setValid_flag("4");
					this.getBean(LmsFixedAssetsInfoServiceImpl.class).doUpdate(assets);
				}
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
		}
		return rm;
	}

	/**App端归还（传递归还的借用编号）（达到目的：将资产的使用信息还原并且状态设置为使用）
	 * @return
	 */
	public ResultModel doBackApp(LmsFixedAssetsBorrowInfo borrow){
		ResultModel rm = new ResultModel();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String borrowNo = borrow.getBorrow_no();
		try{
			if(!borrowNo.isEmpty() && null != borrowNo){
				LmsFixedAssetsBorrowLine line = new LmsFixedAssetsBorrowLine();
				line.setBorrow_no(borrowNo);
				List<LmsFixedAssetsBorrowLine> lineList = this.getMapper(LmsFixedAssetsBorrowLineMapper.class).find(line, 0, 0);
				if(lineList!=null){
					//还原资产状态
					for(LmsFixedAssetsBorrowLine ofab : lineList){
						LmsFixedAssetsInfo assets = this.getMapper(LmsFixedAssetsInfoMapper.class).findById(ofab.getSn_code());
						if(assets != null){
							assets.setUser_no(ofab.getUser_no());
							assets.setUser_name(ofab.getUser_name());
							assets.setUse_dept_no(ofab.getUse_dept_no());
							assets.setUse_dept_name(ofab.getUse_dept_name());
							assets.setValid_flag("2");
							this.getBean(LmsFixedAssetsInfoServiceImpl.class).doUpdate(assets);
						}
					}
				}
				//主表状态修改为归还
				borrow.setValid_flag("2");
				borrow.setReturner_no(userId);
				borrow.setReturner_name(userName);
				this.getBean(LmsFixedAssetsBorrowInfoServiceImpl.class).doUpdate(borrow);
			} 
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
		}
		return rm;
	}

	/**Web端根据条件查询（1借用人  2借用时间  3状态 ）
	 * 暂定人员模糊查询
	 * @return
	 */
	public List<LmsFixedAssetsBorrowInfo> doQueryApp(CommonPage<LmsFixedAssetsBorrowInfo> sum){
		LmsFixedAssetsBorrowInfo borrow = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		String beginTime = sum.getStartTime();
		String endTime = sum.getEndTime();
		List<LmsFixedAssetsBorrowInfo> borrowList = new ArrayList<LmsFixedAssetsBorrowInfo>();
		try{
			borrowList = this.getMapper(LmsFixedAssetsBorrowInfoMapper.class).doQueryList(borrow,beginTime,endTime,pageIndex, pageSize);
		}catch(Exception e){
			return new ArrayList<LmsFixedAssetsBorrowInfo>();
		}
		return borrowList;
	}

	/**web端查询详情
	 * @param sum
	 * @return
	 */
	public LmsFixedAssetsSum doQueryDetailApp(String borrowNo){
		LmsFixedAssetsSum receiveSum = new LmsFixedAssetsSum();
		try{
			//1 主表信息
			LmsFixedAssetsBorrowInfo borrow = this.getMapper(LmsFixedAssetsBorrowInfoMapper.class).findById(borrowNo);

			//2 资产表信息
			List<LmsFixedAssetsInfo> assetsList = new ArrayList<LmsFixedAssetsInfo>();
			LmsFixedAssetsBorrowLine line = new LmsFixedAssetsBorrowLine();
			line.setBorrow_no(borrowNo);
			List<LmsFixedAssetsBorrowLine> lineList = this.getMapper(LmsFixedAssetsBorrowLineMapper.class).find(line, 0, 0);
			if(lineList!=null){
				for(LmsFixedAssetsBorrowLine 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.setBorrowInfo(borrow);
			receiveSum.setAssetsList(assetsList);
		}catch(Exception e){
			return new LmsFixedAssetsSum();
		}
		return receiveSum;
	}
}
