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.LmsConsumableOverGoodInfoMapper;
import market.Lms.Consumable.mapper.LmsConsumableOverInInfoMapper;
import market.Lms.Consumable.service.LmsConsumableOverSumService;
import market.Lms.Consumable.vo.LmsConsumableLineInfo;
import market.Lms.Consumable.vo.LmsConsumableOverGoodInfo;
import market.Lms.Consumable.vo.LmsConsumableOverInInfo;
import market.Lms.Consumable.vo.LmsConsumableOverSum;

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="LmsConsumableOverSum")
public class LmsConsumableOverSumServiceImpl extends BaseServiceImpl<LmsConsumableOverSum,Object> implements LmsConsumableOverSumService{

	//=====================================APP=============================
	/**APP端耗材盘盈入库提交
	 * @param sum
	 * @return
	 */
	public ResultModel doSubmitApp(LmsConsumableOverSum sum){
		ResultModel rm = new ResultModel();
		LmsConsumableOverInInfo overInInfo = new LmsConsumableOverInInfo();
		String overInNo = "";
		try{
			//主表新增
			overInInfo = sum.getOverInInfo();
			if(overInInfo != null){
				overInNo = this.getBean(LmsConsumableOverInInfoServiceImpl.class).doAdd(overInInfo);
			}

			List<LmsConsumableOverGoodInfo> overGoodList = sum.getOverGoodList();
			if(overGoodList!=null){
				for(LmsConsumableOverGoodInfo overGood : overGoodList){
					//副表新增
					overGood.setOver_in_no(overInNo);
					this.getBean(LmsConsumableOverGoodInfoServiceImpl.class).doAdd(overGood);

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

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

	/**app端查询详情
	 * @param sum
	 * @return
	 */
	public LmsConsumableOverSum doQueryDetailApp(String overInNo){
		LmsConsumableOverSum overInSum = new LmsConsumableOverSum();
		try{
			//1 主表信息
			LmsConsumableOverInInfo overInInfo = this.getMapper(LmsConsumableOverInInfoMapper.class).findById(overInNo);

			//2 副表信息
			LmsConsumableOverGoodInfo good = new LmsConsumableOverGoodInfo();
			good.setOver_in_no(overInNo);
			List<LmsConsumableOverGoodInfo> overGoodList = this.getMapper(LmsConsumableOverGoodInfoMapper.class).find(good, 0, 0);

			overInSum.setOverGoodList(overGoodList);
			overInSum.setOverInInfo(overInInfo);
		}catch(Exception e){
			e.printStackTrace();
			return new LmsConsumableOverSum();
		}
		return overInSum;
	}

	//=====================================WEB=============================
	/**WEB端耗材盘盈入库提交
	 * @param sum
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSubmitWeb(LmsConsumableOverSum sum) throws Exception{
		ResultModel rm = new ResultModel();
		LmsConsumableOverInInfo overInInfo = new LmsConsumableOverInInfo();
		String overInNo = "";
		try{
			//主表新增
			overInInfo = sum.getOverInInfo();
			if(overInInfo != null){
				overInNo = this.getBean(LmsConsumableOverInInfoServiceImpl.class).doAdd(overInInfo);
			}

			List<LmsConsumableOverGoodInfo> overGoodList = sum.getOverGoodList();
			if(overGoodList!=null){
				for(LmsConsumableOverGoodInfo overGood : overGoodList){
					//副表新增
					overGood.setOver_in_no(overInNo);
					this.getBean(LmsConsumableOverGoodInfoServiceImpl.class).doAdd(overGood);

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

					//耗材主表信息的更新
					String objectNo = overGood.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<LmsConsumableOverInInfo> sum){
		ResultModel rm = new ResultModel();
		LmsConsumableOverInInfo overInInfo = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		String beginTime = sum.getStartTime();
		String endTime = sum.getEndTime();
		List<LmsConsumableOverInInfo> overInList = new ArrayList<LmsConsumableOverInInfo>();
		PageModel<LmsConsumableOverInInfo> pm=new PageModel<LmsConsumableOverInInfo>(pageIndex, pageSize);
		int count = 0;
		try{
			overInList = this.getMapper(LmsConsumableOverInInfoMapper.class).doQueryList(overInInfo,beginTime,endTime,(pageIndex-1)*pageSize, pageIndex*pageSize);
			count = this.getMapper(LmsConsumableOverInInfoMapper.class).countList(overInInfo,beginTime,endTime);
			pm.setRows(overInList);
			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<LmsConsumableOverSum> sum){
		ResultModel rm = new ResultModel();
		String overInNo = sum.getMemo1();
		LmsConsumableOverSum overInSum = new LmsConsumableOverSum();
		try{
			//1 主表信息
			LmsConsumableOverInInfo overInInfo = this.getMapper(LmsConsumableOverInInfoMapper.class).findById(overInNo);

			//2 副表信息
			LmsConsumableOverGoodInfo good = new LmsConsumableOverGoodInfo();
			good.setOver_in_no(overInNo);
			List<LmsConsumableOverGoodInfo> overGoodList = this.getMapper(LmsConsumableOverGoodInfoMapper.class).find(good, 0, 0);

			overInSum.setOverGoodList(overGoodList);
			overInSum.setOverInInfo(overInInfo);
			
			rm.setData(overInSum);
			rm.setState("200");
		}catch(Exception e){
			e.printStackTrace();
			rm.setState("500");
		}
		return rm;
	}
}
