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.LmsFixedAssetsCancleInfoMapper;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsCancleLineMapper;
import market.Lms.FixedAssets.mapper.LmsFixedAssetsInfoMapper;
import market.Lms.FixedAssets.service.LmsFixedAssetsCancleSumService;
import market.Lms.FixedAssets.vo.LmsFixedAssetsAnnex;
import market.Lms.FixedAssets.vo.LmsFixedAssetsCancleInfo;
import market.Lms.FixedAssets.vo.LmsFixedAssetsCancleLine;
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;

@RemoteService(path="LmsFixedAssetsCancleSum")
public class LmsFixedAssetsCancleSumServiceImpl extends BaseServiceImpl<LmsFixedAssetsSum,Object> implements LmsFixedAssetsCancleSumService{

	//==============================================web==========================================
	/**
	 * web端提交
	 * @throws Exception 
	 * */
	 @Transactional(value="tm", rollbackFor=Exception.class)
	 public ResultModel doSubmitWeb(LmsFixedAssetsSum sum) throws Exception{
		ResultModel rm = new ResultModel();
		String cancleNo = "";
		try{
			//1退库主表新增
			LmsFixedAssetsCancleInfo cancleInfo = sum.getCancleInfo();
			if(cancleInfo != null){
				cancleNo = this.getBean(LmsFixedAssetsCancleInfoServiceImpl.class).doAdd(cancleInfo);
			}

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

					LmsFixedAssetsCancleLine line = new LmsFixedAssetsCancleLine();
					line.setCancle_no(cancleNo);
					line.setSn_code(assets.getSn_code());
					this.getBean(LmsFixedAssetsCancleLineServiceImpl.class).doAdd(line);
				}
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			throw e;
		}
		return rm;
	}

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

			//2 资产表信息
			List<LmsFixedAssetsInfo> assetsList = new ArrayList<LmsFixedAssetsInfo>();
			LmsFixedAssetsCancleLine line = new LmsFixedAssetsCancleLine();
			line.setCancle_no(cancleNo);
			List<LmsFixedAssetsCancleLine> lineList = this.getMapper(LmsFixedAssetsCancleLineMapper.class).find(line, 0, 0);
			if(lineList!=null){
				for(LmsFixedAssetsCancleLine 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.setCancleInfo(cancle);
			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 cancleNo = "";
		try{
			//1退库主表新增
			LmsFixedAssetsCancleInfo cancleInfo = sum.getCancleInfo();
			if(cancleInfo != null){
				cancleNo = this.getBean(LmsFixedAssetsCancleInfoServiceImpl.class).doAdd(cancleInfo);
			}

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

					LmsFixedAssetsCancleLine line = new LmsFixedAssetsCancleLine();
					line.setCancle_no(cancleNo);
					line.setSn_code(assets.getSn_code());
					this.getBean(LmsFixedAssetsCancleLineServiceImpl.class).doAdd(line);
				}
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
		}
		return rm;
	}

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

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

			//2 资产表信息
			List<LmsFixedAssetsInfo> assetsList = new ArrayList<LmsFixedAssetsInfo>();
			LmsFixedAssetsCancleLine line = new LmsFixedAssetsCancleLine();
			line.setCancle_no(cancleNo);
			List<LmsFixedAssetsCancleLine> lineList = this.getMapper(LmsFixedAssetsCancleLineMapper.class).find(line, 0, 0);
			if(lineList!=null){
				for(LmsFixedAssetsCancleLine 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.setCancleInfo(cancle);
			receiveSum.setAssetsList(assetsList);
		}catch(Exception e){
			return new LmsFixedAssetsSum();
		}
		return receiveSum;
	}
}
