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.LmsFixedAssetsReceiveInfoMapper;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsReceiveLineMapper;
import market.Lms.FixedAssets.service.LmsFixedAssetsReceiveSumService;
import market.Lms.FixedAssets.vo.LmsFixedAssetsAnnex;
import market.Lms.FixedAssets.vo.LmsFixedAssetsInfo;
import market.Lms.FixedAssets.vo.LmsFixedAssetsReceiveInfo;
import market.Lms.FixedAssets.vo.LmsFixedAssetsReceiveLine;
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="LmsFixedAssetsReceiveSum")
public class LmsFixedAssetsReceiveSumServiceImpl extends BaseServiceImpl<LmsFixedAssetsSum,Object> implements LmsFixedAssetsReceiveSumService{

	//==============================================web==========================================
	/**
	 * web端提交
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSubmitWeb(LmsFixedAssetsSum sum) throws Exception{
		ResultModel rm = new ResultModel();
		String receiveNo = "";
		try{
			//1领用主表新增（获得该领用人的信息加入对应的资产信息中）
			LmsFixedAssetsReceiveInfo receiveInfo = sum.getReceiveInfo();
			if(receiveInfo != null){
				receiveNo = this.getBean(LmsFixedAssetsReceiveInfoServiceImpl.class).doAdd(receiveInfo);
			}

			//2领用副表新增 并更改对应资产的使用信息及状态
			List<LmsFixedAssetsInfo> assetsList = sum.getAssetsList();
			if(assetsList!=null){
				for(LmsFixedAssetsInfo assets : assetsList){
					assets.setUser_no(receiveInfo.getReceiver_no());
					assets.setUser_name(receiveInfo.getReceiver_name());
					assets.setUse_dept_no(receiveInfo.getUse_dept_no());
					assets.setUse_dept_name(receiveInfo.getUse_dept_name());
					assets.setArea_no(receiveInfo.getUse_area_no());
					assets.setArea_name(receiveInfo.getUse_area_name());
					assets.setAssets_address(receiveInfo.getUse_address());
					assets.setValid_flag("2");
					this.getBean(LmsFixedAssetsInfoServiceImpl.class).doUpdate(assets);

					LmsFixedAssetsReceiveLine line = new LmsFixedAssetsReceiveLine();
					line.setReceive_no(receiveNo);
					line.setSn_code(assets.getSn_code());
					this.getBean(LmsFixedAssetsReceiveLineServiceImpl.class).doAdd(line);
				}
			}

			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			e.printStackTrace();
			throw e;
		}
		return rm;
	}

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

			//2 资产表信息
			List<LmsFixedAssetsInfo> assetsList = new ArrayList<LmsFixedAssetsInfo>();
			LmsFixedAssetsReceiveLine line = new LmsFixedAssetsReceiveLine();
			line.setReceive_no(receiveNo);
			List<LmsFixedAssetsReceiveLine> lineList = this.getMapper(LmsFixedAssetsReceiveLineMapper.class).find(line, 0, 0);
			if(lineList!=null){
				for(LmsFixedAssetsReceiveLine 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.setReceiveInfo(receive);
			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 receiveNo = "";
		try{
			//1领用主表新增（获得该领用人的信息加入对应的资产信息中）
			LmsFixedAssetsReceiveInfo receiveInfo = sum.getReceiveInfo();
			if(receiveInfo != null){
				receiveNo = this.getBean(LmsFixedAssetsReceiveInfoServiceImpl.class).doAdd(receiveInfo);
			}

			//2领用副表新增 并更改对应资产的使用信息及状态
			List<LmsFixedAssetsInfo> assetsList = sum.getAssetsList();
			if(assetsList!=null){
				for(LmsFixedAssetsInfo assets : assetsList){
					assets.setUser_no(receiveInfo.getReceiver_no());
					assets.setUser_name(receiveInfo.getReceiver_name());
					assets.setUse_dept_no(receiveInfo.getUse_dept_no());
					assets.setUse_dept_name(receiveInfo.getUse_dept_name());
					assets.setArea_no(receiveInfo.getUse_area_no());
					assets.setArea_name(receiveInfo.getUse_area_name());
					assets.setAssets_address(receiveInfo.getUse_address());
					assets.setValid_flag("2");
					this.getBean(LmsFixedAssetsInfoServiceImpl.class).doUpdate(assets);

					LmsFixedAssetsReceiveLine line = new LmsFixedAssetsReceiveLine();
					line.setReceive_no(receiveNo);
					line.setSn_code(assets.getSn_code());
					this.getBean(LmsFixedAssetsReceiveLineServiceImpl.class).doAdd(line);
				}
			}

			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
		}
		return rm;
	}

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

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

			//2 资产表信息
			List<LmsFixedAssetsInfo> assetsList = new ArrayList<LmsFixedAssetsInfo>();
			LmsFixedAssetsReceiveLine line = new LmsFixedAssetsReceiveLine();
			line.setReceive_no(receiveNo);
			List<LmsFixedAssetsReceiveLine> lineList = this.getMapper(LmsFixedAssetsReceiveLineMapper.class).find(line, 0, 0);
			if(lineList!=null){
				for(LmsFixedAssetsReceiveLine 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.setReceiveInfo(receive);
			receiveSum.setAssetsList(assetsList);
		}catch(Exception e){
			return new LmsFixedAssetsSum();
		}
		return receiveSum;
	}
}
