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.LmsConsumableAnnexMapper;
import market.Lms.Consumable.mapper.LmsConsumableGoodInfoMapper;
import market.Lms.Consumable.mapper.LmsConsumableInInfoMapper;
import market.Lms.Consumable.service.LmsConsumableInSumService;
import market.Lms.Consumable.vo.LmsConsumableAnnex;
import market.Lms.Consumable.vo.LmsConsumableGoodInfo;
import market.Lms.Consumable.vo.LmsConsumableInInfo;
import market.Lms.Consumable.vo.LmsConsumableInSum;
import market.Lms.Consumable.vo.LmsConsumableLineInfo;

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="LmsConsumableInSum")
public class LmsConsumableInSumServiceImpl extends BaseServiceImpl<LmsConsumableInSum,Object> implements LmsConsumableInSumService{

	//=====================================APP=============================
	/**APP端耗材入库提交
	 * @param sum
	 * @return
	 */
	public ResultModel doSubmitApp(LmsConsumableInSum sum){
		ResultModel rm = new ResultModel();
		LmsConsumableInInfo inInfo = new LmsConsumableInInfo();
		String inNo = "";
		try{
			//主表新增
			inInfo = sum.getInInfo();
			if(sum.getInInfo() != null){
				inNo = this.getBean(LmsConsumableInInfoServiceImpl.class).doAdd(inInfo);
			}

			List<LmsConsumableGoodInfo> goodInfoList = sum.getGoodInfoList();
			if(goodInfoList!=null){
				for(LmsConsumableGoodInfo good : goodInfoList){
					//副表新增
					good.setIn_no(inNo);
					good.setAmount(good.getPrice()*good.getNumbers());
					String lineId=this.getBean(LmsConsumableGoodInfoServiceImpl.class).doAdd(good);

					//附件新增
					if(good.getAnnexList()!=null){
						for(LmsConsumableAnnex annex : good.getAnnexList()){
							annex.setObject_no(lineId);//将附件与入库单副表关联起来
							this.getBean(LmsConsumableAnnexServiceImpl.class).doAdd(annex);
						}
					}

					//耗材副表新增
					addLine(inInfo,good);

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

	/**app端查询详情
	 * @param sum
	 * @return
	 */
	public LmsConsumableInSum doQueryDetailApp(String inNo){
		LmsConsumableInSum inSum = new LmsConsumableInSum();
		try{
			//1 主表信息
			LmsConsumableInInfo inInfo = this.getMapper(LmsConsumableInInfoMapper.class).findById(inNo);

			//2 副表信息
			LmsConsumableGoodInfo good = new LmsConsumableGoodInfo();
			good.setIn_no(inNo);
			List<LmsConsumableGoodInfo> goodInfoList = this.getMapper(LmsConsumableGoodInfoMapper.class).find(good, 0, 0);
			if(goodInfoList!=null){
				//查询附件
				for(LmsConsumableGoodInfo ocg : goodInfoList){
					LmsConsumableAnnex annex = new LmsConsumableAnnex();
					annex.setObject_no(ocg.getGood_no());
					List<LmsConsumableAnnex> annexList = this.getMapper(LmsConsumableAnnexMapper.class).find(annex, 0, 0);
					ocg.setAnnexList(annexList);
				}
			}
			inSum.setInInfo(inInfo);
			inSum.setGoodInfoList(goodInfoList);
		}catch(Exception e){
			e.printStackTrace();
			return new LmsConsumableInSum();
		}
		return inSum;
	}

	//=====================================WEB=============================
	/**WEB端耗材入库提交
	 * @param sum
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSubmitWeb(LmsConsumableInSum sum) throws Exception{
		ResultModel rm = new ResultModel();
		LmsConsumableInInfo inInfo = new LmsConsumableInInfo();
		String inNo = "";
		try{
			List<LmsConsumableGoodInfo> goodAllList =this.getMapper(LmsConsumableGoodInfoMapper.class).find(null, 0, 0);
			List<LmsConsumableGoodInfo> goodInfoList = sum.getGoodInfoList();
			if(goodInfoList!=null){
				for(int a=0;a<goodInfoList.size();a++){
					for(int b=a+1;b<goodInfoList.size();b++){
						if(goodInfoList.get(a).getBatch_no().equals(goodInfoList.get(b).getBatch_no())){
							rm.setState("500");
							rm.setMsgInfo("存在相同批次号，请修改明细");
							return rm;
						}
					}
					for(LmsConsumableGoodInfo goods:goodAllList){
						if(goodInfoList.get(a).getBatch_no().equals(goods.getBatch_no())){
							rm.setState("500");
							rm.setMsgInfo("库中已存在该批次号，请修改明细");
							return rm;
						}
					}
				}
			}
			//主表新增
			inInfo = sum.getInInfo();
			if(sum.getInInfo() != null){
				inNo = this.getBean(LmsConsumableInInfoServiceImpl.class).doAdd(inInfo);
			}

				for(LmsConsumableGoodInfo good : goodInfoList){
					//副表新增
					good.setIn_no(inNo);
					good.setAmount(good.getPrice()*good.getNumbers());
					String lineId=this.getBean(LmsConsumableGoodInfoServiceImpl.class).doAdd(good);

					//附件新增
					if(good.getAnnexList()!=null){
						for(LmsConsumableAnnex annex : good.getAnnexList()){
							annex.setObject_no(lineId);//将附件与入库单副表关联起来
							this.getBean(LmsConsumableAnnexServiceImpl.class).doAdd(annex);
						}
					}

					//耗材副表新增
					addLine(inInfo,good);

					//耗材主表信息的更新
					String objectNo = good.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<LmsConsumableInInfo> sum){
		ResultModel rm = new ResultModel();
		LmsConsumableInInfo in = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		String beginTime = sum.getStartTime();
		String endTime = sum.getEndTime();
		List<LmsConsumableInInfo> inList = new ArrayList<LmsConsumableInInfo>();
		PageModel<LmsConsumableInInfo> pm=new PageModel<LmsConsumableInInfo>(pageIndex, pageSize);
		int count = 0;
		try{
			inList = this.getMapper(LmsConsumableInInfoMapper.class).doQueryList(in,beginTime,endTime,(pageIndex-1)*pageSize, pageIndex*pageSize);
			count = this.getMapper(LmsConsumableInInfoMapper.class).countList(in,beginTime,endTime);
			pm.setRows(inList);
			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<LmsConsumableInSum> sum){
		ResultModel rm = new ResultModel();
		LmsConsumableInSum inSum = new LmsConsumableInSum();
		String inNo = sum.getMemo1();
		try{
			//1 主表信息
			LmsConsumableInInfo inInfo = this.getMapper(LmsConsumableInInfoMapper.class).findById(inNo);

			//2 副表信息
			LmsConsumableGoodInfo good = new LmsConsumableGoodInfo();
			good.setIn_no(inNo);
			List<LmsConsumableGoodInfo> goodInfoList = this.getMapper(LmsConsumableGoodInfoMapper.class).find(good, 0, 0);
			if(goodInfoList!=null){
				//查询附件
				for(LmsConsumableGoodInfo ocg : goodInfoList){
					LmsConsumableAnnex annex = new LmsConsumableAnnex();
					annex.setObject_no(ocg.getGood_no());
					List<LmsConsumableAnnex> annexList = this.getMapper(LmsConsumableAnnexMapper.class).find(annex, 0, 0);
					ocg.setAnnexList(annexList);
				}
			}
			inSum.setInInfo(inInfo);
			inSum.setGoodInfoList(goodInfoList);
			rm.setData(inSum);
			rm.setState("200");
		}catch(Exception e){
			e.printStackTrace();
			rm.setState("500");
		}
		return rm;
	}

	//私有方法新增耗材信息副表
	private LmsConsumableLineInfo addLine(LmsConsumableInInfo inInfo,LmsConsumableGoodInfo good){
		LmsConsumableLineInfo line = new LmsConsumableLineInfo();
		try{
			//在新增入库的时候需要将每个入库副表信息新增到耗材副表中
			line.setObject_no(good.getObject_no());
			line.setObject_name(good.getObject_name());
			line.setBatch_no(good.getBatch_no());
			line.setSn_code(good.getSn_code());
			line.setSpec(good.getSpec());
			line.setNumber1(good.getNumbers());
			line.setNumber2(good.getNumbers());//初始余量设置为入库数量
			line.setPrice(good.getPrice());
			line.setMeasure_unit_no(good.getMeasure_unit_no());
			line.setMeasure_unit_name(good.getMeasure_unit_name());
			line.setWarehouse_no(inInfo.getWarehouse_no());
			line.setWarehouse_name(inInfo.getWarehouse_name());
			line.setSupplier_no(inInfo.getSupplier_no());
			line.setSupplier_name(inInfo.getSupplier_name());
			line.setIn_date(inInfo.getIn_date());

			this.getBean(LmsConsumableLineInfoServiceImpl.class).doAdd(line);
		}catch(Exception e){
			e.printStackTrace();
			return new LmsConsumableLineInfo();
		}
		return line;
	}

}
