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.LmsConsumableOveroutGoodInfoMapper;
import market.Lms.Consumable.mapper.LmsConsumableOveroutInfoMapper;
import market.Lms.Consumable.service.LmsConsumableOveroutSumService;
import market.Lms.Consumable.vo.LmsConsumableLineInfo;
import market.Lms.Consumable.vo.LmsConsumableOveroutGoodInfo;
import market.Lms.Consumable.vo.LmsConsumableOveroutInfo;
import market.Lms.Consumable.vo.LmsConsumableOveroutSum;

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="LmsConsumableOveroutSum")
public class LmsConsumableOveroutSumServiceImpl extends BaseServiceImpl<LmsConsumableOveroutSum,Object> implements LmsConsumableOveroutSumService{

	//=====================================APP=============================
	/**APP端耗材盘亏出库提交
	 * @param sum
	 * @return
	 */
	public ResultModel doSubmitApp(LmsConsumableOveroutSum sum){
		ResultModel rm = new ResultModel();
		LmsConsumableOveroutInfo overoutInfo = new LmsConsumableOveroutInfo();
		String overoutNo = "";
		try{
			//数量判断
			List<LmsConsumableOveroutGoodInfo> overoutGoodList = sum.getOveroutGoodList();
			if(overoutGoodList!=null){
				for(LmsConsumableOveroutGoodInfo overoutGood : overoutGoodList){
					//在耗材信息副表中修改对应批次的数量
					LmsConsumableLineInfo line = new LmsConsumableLineInfo();
					line.setObject_no(overoutGood.getObject_no());
					line.setBatch_no(overoutGood.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() - overoutGood.getNumbers() < 0){
							rm.setState("500");
							rm.setMsgInfo("盘亏出库数量大于余量");
							return rm;
						}
					}
				}
			}
			
			
			//主表新增
			overoutInfo = sum.getOveroutInfo();
			if(overoutInfo != null){
				overoutNo = this.getBean(LmsConsumableOveroutInfoServiceImpl.class).doAdd(overoutInfo);
			}

			if(overoutGoodList!=null){
				for(LmsConsumableOveroutGoodInfo overoutGood : overoutGoodList){
					//副表新增
					overoutGood.setOverout_no(overoutNo);
					this.getBean(LmsConsumableOveroutGoodInfoServiceImpl.class).doAdd(overoutGood);

					//在耗材信息副表中修改对应批次的数量
					LmsConsumableLineInfo line = new LmsConsumableLineInfo();
					line.setObject_no(overoutGood.getObject_no());
					line.setBatch_no(overoutGood.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() - overoutGood.getNumbers());//将余量修改
						this.getBean(LmsConsumableLineInfoServiceImpl.class).doUpdate(ocl);
					}

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

	/**APP端查询（1仓库名称 2 经办人）
	 * @param sum
	 * @return
	 */
	public List<LmsConsumableOveroutInfo> doQueryApp(CommonPage<LmsConsumableOveroutInfo> sum){
		LmsConsumableOveroutInfo overoutInfo = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		List<LmsConsumableOveroutInfo> overoutList = new ArrayList<LmsConsumableOveroutInfo>();
		try{
			overoutList = this.getMapper(LmsConsumableOveroutInfoMapper.class).doQueryList(overoutInfo, pageIndex, pageSize);
		}catch(Exception e){
			e.printStackTrace();
			return new ArrayList<LmsConsumableOveroutInfo>();
		}
		return overoutList;
	}

	/**app端查询详情
	 * @param sum
	 * @return
	 */
	public LmsConsumableOveroutSum doQueryDetailApp(String overoutNo){
		LmsConsumableOveroutSum overoutSum = new LmsConsumableOveroutSum();
		try{
			//1 主表信息
			LmsConsumableOveroutInfo overoutInfo = this.getMapper(LmsConsumableOveroutInfoMapper.class).findById(overoutNo);

			//2 副表信息
			LmsConsumableOveroutGoodInfo good = new LmsConsumableOveroutGoodInfo();
			good.setOverout_no(overoutNo);
			List<LmsConsumableOveroutGoodInfo> overoutGoodList = this.getMapper(LmsConsumableOveroutGoodInfoMapper.class).find(good, 0, 0);

			overoutSum.setOveroutInfo(overoutInfo);
			overoutSum.setOveroutGoodList(overoutGoodList);
		}catch(Exception e){
			e.printStackTrace();
			return new LmsConsumableOveroutSum();
		}
		return overoutSum;
	}

	//=====================================WEB=============================
	/**WEB端耗材盘亏出库提交
	 * @param sum
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSubmitWeb(LmsConsumableOveroutSum sum) throws Exception{
		ResultModel rm = new ResultModel();
		LmsConsumableOveroutInfo overoutInfo = new LmsConsumableOveroutInfo();
		String overoutNo = "";
		try{
			//数量判断
			List<LmsConsumableOveroutGoodInfo> overoutGoodList = sum.getOveroutGoodList();
			if(overoutGoodList!=null){
				for(LmsConsumableOveroutGoodInfo overoutGood : overoutGoodList){
					//在耗材信息副表中修改对应批次的数量
					LmsConsumableLineInfo line = new LmsConsumableLineInfo();
					line.setObject_no(overoutGood.getObject_no());
					line.setBatch_no(overoutGood.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() - overoutGood.getNumbers() < 0){
							rm.setState("500");
							rm.setMsgInfo("盘亏出库数量大于余量");
							return rm;
						}
					}
				}
			}
			
			//主表新增
			overoutInfo = sum.getOveroutInfo();
			if(overoutInfo != null){
				overoutNo = this.getBean(LmsConsumableOveroutInfoServiceImpl.class).doAdd(overoutInfo);
			}

			if(overoutGoodList!=null){
				for(LmsConsumableOveroutGoodInfo overoutGood : overoutGoodList){
					//副表新增
					overoutGood.setOverout_no(overoutNo);
					this.getBean(LmsConsumableOveroutGoodInfoServiceImpl.class).doAdd(overoutGood);

					//在耗材信息副表中修改对应批次的数量
					LmsConsumableLineInfo line = new LmsConsumableLineInfo();
					line.setObject_no(overoutGood.getObject_no());
					line.setBatch_no(overoutGood.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() - overoutGood.getNumbers());//将余量修改
						this.getBean(LmsConsumableLineInfoServiceImpl.class).doUpdate(ocl);
					}

					//耗材主表信息的更新
					String objectNo = overoutGood.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 经办人）
	 * @param sum
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<LmsConsumableOveroutInfo> sum){
		ResultModel rm = new ResultModel();
		LmsConsumableOveroutInfo overoutInfo = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		List<LmsConsumableOveroutInfo> overoutList = new ArrayList<LmsConsumableOveroutInfo>();
		PageModel<LmsConsumableOveroutInfo> pm=new PageModel<LmsConsumableOveroutInfo>(pageIndex, pageSize);
		int count = 0;
		try{
			overoutList = this.getMapper(LmsConsumableOveroutInfoMapper.class).doQueryList(overoutInfo, (pageIndex-1)*pageSize, pageIndex*pageSize);
			count = this.getMapper(LmsConsumableOveroutInfoMapper.class).countList(overoutInfo);
			pm.setRows(overoutList);
			pm.setTotal(count);
			rm.setData(pm);
			rm.setState("200");
		}catch(Exception e){
			e.printStackTrace();
			rm.setState("500");
		}
		return rm;
	}

	/**WEB端查询详情
	 * @param sum
	 * @return
	 */
	public ResultModel doQueryDetailWeb(CommonPage<LmsConsumableOveroutSum> sum){
		ResultModel rm = new ResultModel();
		String overoutNo = sum.getMemo1();
		LmsConsumableOveroutSum overoutSum = new LmsConsumableOveroutSum();
		try{
			//1 主表信息
			LmsConsumableOveroutInfo overoutInfo = this.getMapper(LmsConsumableOveroutInfoMapper.class).findById(overoutNo);

			//2 副表信息
			LmsConsumableOveroutGoodInfo good = new LmsConsumableOveroutGoodInfo();
			good.setOverout_no(overoutNo);
			List<LmsConsumableOveroutGoodInfo> overoutGoodList = this.getMapper(LmsConsumableOveroutGoodInfoMapper.class).find(good, 0, 0);

			overoutSum.setOveroutInfo(overoutInfo);
			overoutSum.setOveroutGoodList(overoutGoodList);
			
			rm.setData(overoutSum);
			rm.setState("200");
		}catch(Exception e){
			e.printStackTrace();
			rm.setState("500");
		}
		return rm;
	}
}
