package market.Lms.Consumable.impl;

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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Consumable.mapper.LmsConsumableLineInfoMapper;
import market.Lms.Consumable.mapper.LmsConsumableOutGoodInfoMapper;
import market.Lms.Consumable.mapper.LmsConsumableOutInfoMapper;
import market.Lms.Consumable.service.LmsConsumableOutSumService;
import market.Lms.Consumable.vo.LmsConsumableLineInfo;
import market.Lms.Consumable.vo.LmsConsumableOutGoodInfo;
import market.Lms.Consumable.vo.LmsConsumableOutInfo;
import market.Lms.Consumable.vo.LmsConsumableOutSum;

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="LmsConsumableOutSum")
public class LmsConsumableOutSumServiceImpl extends BaseServiceImpl<LmsConsumableOutSum,Object> implements LmsConsumableOutSumService{

	//=====================================APP=============================
	/**APP端耗材出库提交
	 * @param sum
	 * @return
	 */
	public ResultModel doSubmitApp(LmsConsumableOutSum sum){
		ResultModel rm = new ResultModel();
		LmsConsumableOutInfo outInfo = new LmsConsumableOutInfo();
		String outNo = "";
		try{
			//数量判断
			List<LmsConsumableOutGoodInfo> outGoodList = sum.getOutGoodList();
			if(outGoodList!=null){
				for(LmsConsumableOutGoodInfo outGood : outGoodList){
					//在耗材信息副表中修改对应批次的数量
					LmsConsumableLineInfo line = new LmsConsumableLineInfo();
					line.setObject_no(outGood.getObject_no());
					line.setBatch_no(outGood.getBatch_no());
					List<LmsConsumableLineInfo> lineList = this.getMapper(LmsConsumableLineInfoMapper.class).find(line, 0, 0);
					if(lineList.size() == 1){
						LmsConsumableLineInfo ocl = lineList.get(0);
						if(ocl.getNumber2() - outGood.getNumbers()< 0){
							rm.setState("500");
							rm.setMsgInfo("出库数量超过总数量");
							return rm;
						}
					}
				}
			}

			//主表新增
			outInfo = sum.getOutInfo();
			if(outInfo != null){
				outNo = this.getBean(LmsConsumableOutInfoServiceImpl.class).doAdd(outInfo);
			}

			if(outGoodList!=null){
				for(LmsConsumableOutGoodInfo outGood : outGoodList){
					//副表新增
					outGood.setOut_no(outNo);
					this.getBean(LmsConsumableOutGoodInfoServiceImpl.class).doAdd(outGood);

					//在耗材信息副表中修改对应批次的数量
					LmsConsumableLineInfo line = new LmsConsumableLineInfo();
					line.setObject_no(outGood.getObject_no());
					line.setBatch_no(outGood.getBatch_no());
					List<LmsConsumableLineInfo> lineList = this.getMapper(LmsConsumableLineInfoMapper.class).find(line, 0, 0);
					if(lineList.size() >0){
						LmsConsumableLineInfo ocl = lineList.get(0);
						ocl.setNumber2(ocl.getNumber2() - outGood.getNumbers());//将余量修改
						this.getBean(LmsConsumableLineInfoServiceImpl.class).doUpdate(ocl);
					}

					//耗材主表信息的更新
					String objectNo = outGood.getObject_no();//得到物品编号
					this.getBean(LmsConsumableHeatInfoServiceImpl.class).doUpdateByLine(objectNo);
				}
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
		}
		return rm;
	}

	/**APP端查询（1仓库名称 2 经办人 3业务时间）
	 * @param sum
	 * @return
	 */
	public List<LmsConsumableOutInfo> doQueryApp(CommonPage<LmsConsumableOutInfo> sum){
		LmsConsumableOutInfo outInfo = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		String beginTime = sum.getStartTime();
		String endTime = sum.getEndTime();
		List<LmsConsumableOutInfo> outList = new ArrayList<LmsConsumableOutInfo>();
		try{
			outList = this.getMapper(LmsConsumableOutInfoMapper.class).doQueryList(outInfo,beginTime,endTime,pageIndex, pageSize);
		}catch(Exception e){
			e.printStackTrace();
			return new ArrayList<LmsConsumableOutInfo>();
		}
		return outList;
	}

	/**app端查询详情
	 * @param sum
	 * @return
	 */
	public LmsConsumableOutSum doQueryDetailApp(String outNo){
		LmsConsumableOutSum outSum = new LmsConsumableOutSum();
		try{
			//1 主表信息
			LmsConsumableOutInfo outInfo = this.getMapper(LmsConsumableOutInfoMapper.class).findById(outNo);

			//2 副表信息
			LmsConsumableOutGoodInfo good = new LmsConsumableOutGoodInfo();
			good.setOut_no(outNo);
			List<LmsConsumableOutGoodInfo> outGoodList = this.getMapper(LmsConsumableOutGoodInfoMapper.class).find(good, 0, 0);

			outSum.setOutGoodList(outGoodList);
			outSum.setOutInfo(outInfo);
		}catch(Exception e){
			e.printStackTrace();
			return new LmsConsumableOutSum();
		}
		return outSum;
	}

	//=====================================WEB=============================
	/**WEB端耗材出库提交
	 * @param sum
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSubmitWeb(LmsConsumableOutSum sum) throws Exception{
		ResultModel rm = new ResultModel();
		LmsConsumableOutInfo outInfo = new LmsConsumableOutInfo();
		String outNo = "";
		try{
			//数量判断
			List<LmsConsumableOutGoodInfo> outGoodList = sum.getOutGoodList();
			if(outGoodList!=null){
				for(LmsConsumableOutGoodInfo outGood : outGoodList){
					//在耗材信息副表中修改对应批次的数量
					LmsConsumableLineInfo line = new LmsConsumableLineInfo();
					line.setObject_no(outGood.getObject_no());
					line.setBatch_no(outGood.getBatch_no());
					List<LmsConsumableLineInfo> lineList = this.getMapper(LmsConsumableLineInfoMapper.class).find(line, 0, 0);
					if(lineList.size() == 1){
						LmsConsumableLineInfo ocl = lineList.get(0);
						if(ocl.getNumber2() - outGood.getNumbers()< 0){
							rm.setState("500");
							rm.setMsgInfo("出库数量超过总数量");
							return rm;
						}
					}
				}
			}

			//主表新增
			outInfo = sum.getOutInfo();
			if(outInfo != null){
				outNo = this.getBean(LmsConsumableOutInfoServiceImpl.class).doAdd(outInfo);
			}

			if(outGoodList!=null){
				for(LmsConsumableOutGoodInfo outGood : outGoodList){
					//副表新增
					outGood.setOut_no(outNo);
					this.getBean(LmsConsumableOutGoodInfoServiceImpl.class).doAdd(outGood);

					//在耗材信息副表中修改对应批次的数量
					LmsConsumableLineInfo line = new LmsConsumableLineInfo();
					line.setObject_no(outGood.getObject_no());
					line.setBatch_no(outGood.getBatch_no());
					List<LmsConsumableLineInfo> lineList = this.getMapper(LmsConsumableLineInfoMapper.class).find(line, 0, 0);
					if(lineList.size() >0){
						LmsConsumableLineInfo ocl = lineList.get(0);
						ocl.setNumber2(ocl.getNumber2() - outGood.getNumbers());//将余量修改
						this.getBean(LmsConsumableLineInfoServiceImpl.class).doUpdate(ocl);
					}

					//耗材主表信息的更新
					String objectNo = outGood.getObject_no();//得到物品编号
					this.getBean(LmsConsumableHeatInfoServiceImpl.class).doUpdateByLine(objectNo);
				}
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			e.printStackTrace();
			throw e;
		}
		return rm;
	}

	/**WEB端查询（1仓库名称 2 经办人 3业务时间）
	 * @param sum
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<LmsConsumableOutInfo> sum){
		ResultModel rm = new ResultModel();
		LmsConsumableOutInfo outInfo = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		String beginTime = sum.getStartTime();
		String endTime = sum.getEndTime();
		List<LmsConsumableOutInfo> outList = new ArrayList<LmsConsumableOutInfo>();
		PageModel<LmsConsumableOutInfo> pm=new PageModel<LmsConsumableOutInfo>(pageIndex, pageSize);
		int count = 0;
		try{
			outList = this.getMapper(LmsConsumableOutInfoMapper.class).doQueryList(outInfo,beginTime,endTime,(pageIndex-1)*pageSize,pageIndex*pageSize);
			count = this.getMapper(LmsConsumableOutInfoMapper.class).countList(outInfo,beginTime,endTime);
			pm.setRows(outList);
			pm.setTotal(count);
			rm.setData(pm);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			e.printStackTrace();
		}
		return rm;
	}

	/**WEB端查询详情
	 * @param sum
	 * @return
	 */
	public ResultModel doQueryDetailWeb(CommonPage<LmsConsumableOutSum> sum){
		ResultModel rm = new ResultModel();
		String outNo = sum.getMemo1();
		LmsConsumableOutSum outSum = new LmsConsumableOutSum();
		try{
			//1 主表信息
			LmsConsumableOutInfo outInfo = this.getMapper(LmsConsumableOutInfoMapper.class).findById(outNo);

			//2 副表信息
			LmsConsumableOutGoodInfo good = new LmsConsumableOutGoodInfo();
			good.setOut_no(outNo);
			List<LmsConsumableOutGoodInfo> outGoodList = this.getMapper(LmsConsumableOutGoodInfoMapper.class).find(good, 0, 0);

			outSum.setOutGoodList(outGoodList);
			outSum.setOutInfo(outInfo);
			rm.setData(outSum);
			rm.setState("200");
		}catch(Exception e){
			e.printStackTrace();
			rm.setState("500");
		}
		return rm;
	}
}
