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.LmsFixedAssetsScrapInfoMapper;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsScrapLineMapper;
import market.Lms.FixedAssets.service.LmsFixedAssetsScrapSumService;
import market.Lms.FixedAssets.vo.LmsFixedAssetsAnnex;
import market.Lms.FixedAssets.vo.LmsFixedAssetsInfo;
import market.Lms.FixedAssets.vo.LmsFixedAssetsScrapInfo;
import market.Lms.FixedAssets.vo.LmsFixedAssetsScrapLine;
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="LmsFixedAssetsScrapSum")
public class LmsFixedAssetsScrapSumServiceImpl extends BaseServiceImpl<LmsFixedAssetsSum,Object> implements LmsFixedAssetsScrapSumService{

	//==============================================web==========================================
	/**
	 * web端提交
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSubmitWeb(LmsFixedAssetsSum sum) throws Exception{
		ResultModel rm = new ResultModel();
		String scrapNo = "";
		try{
			//1报废主表新增
			LmsFixedAssetsScrapInfo scrapInfo = sum.getScrapInfo();
			if(scrapInfo != null){
				scrapInfo.setValid_flag("1");
				scrapNo = this.getBean(LmsFixedAssetsScrapInfoServiceImpl.class).doAdd(scrapInfo);
			}

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

					//报废副表新增
					LmsFixedAssetsScrapLine line = new LmsFixedAssetsScrapLine();
					line.setScrap_no(scrapNo);
					line.setSn_code(assets.getSn_code());
					//副表中需要存储该资产的当前使用人信息 
					if(assets.getUse_dept_name() != null)
						line.setUse_dept_name(assets.getUse_dept_name());
					if(assets.getUse_dept_no() != null)
						line.setUse_dept_no(assets.getUse_dept_no());
					if(assets.getUser_name() != null)
						line.setUser_name(assets.getUser_name());
					if(assets.getUser_no() != null)
						line.setUser_no(assets.getUser_no());
					this.getBean(LmsFixedAssetsScrapLineServiceImpl.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 doCancelWeb(LmsFixedAssetsSum sum) throws Exception{
		ResultModel rm = new ResultModel();
		try{
			//1报废主表修改
			LmsFixedAssetsScrapInfo scrapInfo = sum.getScrapInfo();
			if(scrapInfo != null){
				scrapInfo.setValid_flag("2");		//无效
				this.getBean(LmsFixedAssetsScrapInfoServiceImpl.class).doUpdate(scrapInfo);
			}

			//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<LmsFixedAssetsScrapInfo> sum){
		ResultModel rm = new ResultModel();
		LmsFixedAssetsScrapInfo scrap = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		String beginTime = sum.getStartTime();
		String endTime = sum.getEndTime();
		List<LmsFixedAssetsScrapInfo> scrapList = new ArrayList<LmsFixedAssetsScrapInfo>();
		PageModel<LmsFixedAssetsScrapInfo> pm=new PageModel<LmsFixedAssetsScrapInfo>(pageIndex, pageSize);
		int count = 0;
		try{
			scrapList = this.getMapper(LmsFixedAssetsScrapInfoMapper.class).doQueryList(scrap,beginTime,endTime,(pageIndex-1)*pageSize, pageIndex*pageSize);
			count = this.getMapper(LmsFixedAssetsScrapInfoMapper.class).countList(scrap,beginTime,endTime);
			pm.setRows(scrapList);
			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<LmsFixedAssetsScrapInfo> sum){
		ResultModel rm = new ResultModel();
		String scrapNo = sum.getMemo1();
		LmsFixedAssetsSum receiveSum = new LmsFixedAssetsSum();
		try{
			//1 主表信息
			LmsFixedAssetsScrapInfo scrap = this.getMapper(LmsFixedAssetsScrapInfoMapper.class).findById(scrapNo);

			//2 资产表信息
			List<LmsFixedAssetsInfo> assetsList = new ArrayList<LmsFixedAssetsInfo>();
			LmsFixedAssetsScrapLine line = new LmsFixedAssetsScrapLine();
			line.setScrap_no(scrapNo);
			List<LmsFixedAssetsScrapLine> lineList = this.getMapper(LmsFixedAssetsScrapLineMapper.class).find(line, 0, 0);
			if(lineList!=null){
				for(LmsFixedAssetsScrapLine 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.setScrapInfo(scrap);
			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 scrapNo = "";
		try{
			//1报废主表新增
			LmsFixedAssetsScrapInfo scrapInfo = sum.getScrapInfo();
			if(scrapInfo != null){
				scrapInfo.setValid_flag("1");
				scrapNo = this.getBean(LmsFixedAssetsScrapInfoServiceImpl.class).doAdd(scrapInfo);
			}

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

					//报废副表新增
					LmsFixedAssetsScrapLine line = new LmsFixedAssetsScrapLine();
					line.setScrap_no(scrapNo);
					line.setSn_code(assets.getSn_code());
					//副表中需要存储该资产的当前使用人信息 
					if(assets.getUse_dept_name() != null)
						line.setUse_dept_name(assets.getUse_dept_name());
					if(assets.getUse_dept_no() != null)
						line.setUse_dept_no(assets.getUse_dept_no());
					if(assets.getUser_name() != null)
						line.setUser_name(assets.getUser_name());
					if(assets.getUser_no() != null)
						line.setUser_no(assets.getUser_no());
					this.getBean(LmsFixedAssetsScrapLineServiceImpl.class).doAdd(line);
				}
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
		}
		return rm;
	}
	
	/**
	 * 取消报废
	 * */
	public ResultModel doCancelApp(LmsFixedAssetsSum sum){
		ResultModel rm = new ResultModel();
		try{
			//1报废主表修改
			LmsFixedAssetsScrapInfo scrapInfo = sum.getScrapInfo();
			if(scrapInfo != null){
				scrapInfo.setValid_flag("2");		//无效
				this.getBean(LmsFixedAssetsScrapInfoServiceImpl.class).doUpdate(scrapInfo);
			}

			//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;
	}
	/**Web端根据条件查询（1处理人  2业务时间  3状态 ）
	 * 暂定人员模糊查询
	 * @return
	 */
	public List<LmsFixedAssetsScrapInfo> doQueryApp(CommonPage<LmsFixedAssetsScrapInfo> sum){
		LmsFixedAssetsScrapInfo scrap = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		String beginTime = sum.getStartTime();
		String endTime = sum.getEndTime();
		List<LmsFixedAssetsScrapInfo> scrapList = new ArrayList<LmsFixedAssetsScrapInfo>();
		try{
			scrapList = this.getMapper(LmsFixedAssetsScrapInfoMapper.class).doQueryList(scrap,beginTime,endTime,pageIndex, pageSize);
		}catch(Exception e){
			return new ArrayList<LmsFixedAssetsScrapInfo>();
		}
		return scrapList;
	}

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

			//2 资产表信息
			List<LmsFixedAssetsInfo> assetsList = new ArrayList<LmsFixedAssetsInfo>();
			LmsFixedAssetsScrapLine line = new LmsFixedAssetsScrapLine();
			line.setScrap_no(scrapNo);
			List<LmsFixedAssetsScrapLine> lineList = this.getMapper(LmsFixedAssetsScrapLineMapper.class).find(line, 0, 0);
			if(lineList!=null){
				for(LmsFixedAssetsScrapLine 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.setScrapInfo(scrap);
			receiveSum.setAssetsList(assetsList);
		}catch(Exception e){
			return new LmsFixedAssetsSum();
		}
		return receiveSum;
	}
}
