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.LmsConsumableOffGoodInfoMapper;
import market.Lms.Consumable.mapper.LmsConsumableWriteOffInfoMapper;
import market.Lms.Consumable.service.LmsConsumableWriteSumService;
import market.Lms.Consumable.vo.LmsConsumableLineInfo;
import market.Lms.Consumable.vo.LmsConsumableOffGoodInfo;
import market.Lms.Consumable.vo.LmsConsumableWriteOffInfo;
import market.Lms.Consumable.vo.LmsConsumableWriteSum;

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="LmsConsumableWriteSum")
public class LmsConsumableWriteSumServiceImpl extends BaseServiceImpl<LmsConsumableWriteSum,Object> implements LmsConsumableWriteSumService{

	//=======================================APP===================================
	/**APP端冲销新增
	 * @return
	 */
	public ResultModel doSubmitApp(LmsConsumableWriteSum sum){
		ResultModel rm = new ResultModel();
		String offNo = "";
		try{
			//数量判断
			List<LmsConsumableOffGoodInfo> offGoodList = sum.getOffGoodList();
			if(offGoodList!=null){
				for(LmsConsumableOffGoodInfo offGood : offGoodList){
					LmsConsumableLineInfo line = new LmsConsumableLineInfo();
					line.setObject_no(offGood.getObject_no());
					line.setBatch_no(offGood.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() - offGood.getNumbers() <0){
							rm.setState("500");
							rm.setMsgInfo("冲销数量大于入库数量");
							return rm;
						}
					}
				}
			}

			//主表新增
			if(sum.getWriteOff() != null){
				offNo = this.getBean(LmsConsumableWriteOffInfoServiceImpl.class).doAdd(sum.getWriteOff());
			}

			if(offGoodList!=null){
				for(LmsConsumableOffGoodInfo offGood : offGoodList){
					//副表新增
					offGood.setOff_no(offNo);
					this.getBean(LmsConsumableOffGoodInfoServiceImpl.class).doAdd(offGood);

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

					//耗材主表信息的更新
					String objectNo = offGood.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<LmsConsumableWriteOffInfo> doQueryApp(CommonPage<LmsConsumableWriteOffInfo> sum){
		LmsConsumableWriteOffInfo writeOff = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		String beginTime = sum.getStartTime();
		String endTime = sum.getEndTime();
		List<LmsConsumableWriteOffInfo> writeList = new ArrayList<LmsConsumableWriteOffInfo>();
		try{
			writeList = this.getMapper(LmsConsumableWriteOffInfoMapper.class).doQueryList(writeOff,beginTime,endTime,pageIndex, pageSize);
		}catch(Exception e){
			e.printStackTrace();
			return new ArrayList<LmsConsumableWriteOffInfo>();
		}
		return writeList;
	}

	/**app端查询详情
	 * @param sum
	 * @return
	 */
	public LmsConsumableWriteSum doQueryDetailApp(String offNo){
		LmsConsumableWriteSum writeSum = new LmsConsumableWriteSum();
		try{
			//1 主表信息
			LmsConsumableWriteOffInfo writeOff = this.getMapper(LmsConsumableWriteOffInfoMapper.class).findById(offNo);

			//2 副表信息
			LmsConsumableOffGoodInfo good = new LmsConsumableOffGoodInfo();
			good.setOff_no(offNo);
			List<LmsConsumableOffGoodInfo> goodInfoList = this.getMapper(LmsConsumableOffGoodInfoMapper.class).find(good, 0, 0);

			writeSum.setOffGoodList(goodInfoList);
			writeSum.setWriteOff(writeOff);
		}catch(Exception e){
			e.printStackTrace();
			return new LmsConsumableWriteSum();
		}
		return writeSum;
	}

	//=======================================WEB===================================
	/**WEB端冲销新增
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSubmitWeb(LmsConsumableWriteSum sum) throws Exception{
		ResultModel rm = new ResultModel();
		String offNo = "";
		try{
			//数量判断
			List<LmsConsumableOffGoodInfo> offGoodList = sum.getOffGoodList();
			if(offGoodList!=null){
				for(LmsConsumableOffGoodInfo offGood : offGoodList){
					LmsConsumableLineInfo line = new LmsConsumableLineInfo();
					line.setObject_no(offGood.getObject_no());
					line.setBatch_no(offGood.getBatch_no());
					List<LmsConsumableLineInfo> lineList = this.getMapper(LmsConsumableLineInfoMapper.class).find(line, 0, 0);
					if(lineList.size() >0){
						LmsConsumableLineInfo ocl = lineList.get(0);
						if(ocl.getNumber1() - offGood.getNumbers() <0){
							rm.setState("500");
							rm.setMsgInfo("冲销数量大于入库数量");
							return rm;
						}
					}
				}
			}

			//主表新增
			if(sum.getWriteOff() != null){
				offNo = this.getBean(LmsConsumableWriteOffInfoServiceImpl.class).doAdd(sum.getWriteOff());
			}

			if(offGoodList!=null){
				for(LmsConsumableOffGoodInfo offGood : offGoodList){
					//副表新增
					offGood.setOff_no(offNo);
					this.getBean(LmsConsumableOffGoodInfoServiceImpl.class).doAdd(offGood);

					//在耗材信息副表中修改对应批次的数量
					LmsConsumableLineInfo line = new LmsConsumableLineInfo();
					line.setObject_no(offGood.getObject_no());
					line.setBatch_no(offGood.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() - offGood.getNumbers()<0?0:ocl.getNumber2() - offGood.getNumbers());//将余量修改
						ocl.setNumber1(ocl.getNumber1() - offGood.getNumbers());//冲销改入库数量
						this.getBean(LmsConsumableLineInfoServiceImpl.class).doUpdate(ocl);
					}

					//耗材主表信息的更新
					String objectNo = offGood.getObject_no();//得到物品编号
					this.getBean(LmsConsumableHeatInfoServiceImpl.class).doUpdateByLine(objectNo);

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

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

	/**app端查询详情
	 * @param sum
	 * @return
	 */
	public ResultModel doQueryDetailWeb(CommonPage<LmsConsumableWriteSum> sum){
		ResultModel rm = new ResultModel();
		String offNo = sum.getMemo1();
		LmsConsumableWriteSum writeSum = new LmsConsumableWriteSum();
		try{
			//1 主表信息
			LmsConsumableWriteOffInfo writeOff = this.getMapper(LmsConsumableWriteOffInfoMapper.class).findById(offNo);

			//2 副表信息
			LmsConsumableOffGoodInfo good = new LmsConsumableOffGoodInfo();
			good.setOff_no(offNo);
			List<LmsConsumableOffGoodInfo> goodInfoList = this.getMapper(LmsConsumableOffGoodInfoMapper.class).find(good, 0, 0);

			writeSum.setOffGoodList(goodInfoList);
			writeSum.setWriteOff(writeOff);

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

}
