package market.Lms.Consumable.impl;


import java.util.List;

import market.Lms.Consumable.mapper.LmsConsumableHeatInfoMapper;
import market.Lms.Consumable.mapper.LmsConsumableLineInfoMapper;
import market.Lms.Consumable.service.LmsConsumableHeatInfoService;
import market.Lms.Consumable.vo.LmsConsumableHeatInfo;
import market.Lms.Consumable.vo.LmsConsumableLineInfo;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;

@RemoteService(path="LmsConsumableHeatInfo")
public class LmsConsumableHeatInfoServiceImpl extends BaseServiceImpl<LmsConsumableHeatInfo,Object> implements LmsConsumableHeatInfoService{

	//新增
	public String doAdd(LmsConsumableHeatInfo heat){
		String id = heat.getObject_no();
		this.getMapper(LmsConsumableHeatInfoMapper.class).insert(heat);
		return id;
	}

	//删除
	public void doDelete(LmsConsumableHeatInfo heat){
		this.getMapper(LmsConsumableHeatInfoMapper.class).delete(heat.getObject_no());
	}

	//修改
	public void doUpdate(LmsConsumableHeatInfo heat){
		this.getMapper(LmsConsumableHeatInfoMapper.class).update(heat);
	}

	//查询
	public LmsConsumableHeatInfo doQuery(String heatNo){
		LmsConsumableHeatInfo heat = new LmsConsumableHeatInfo();
		heat = this.getMapper(LmsConsumableHeatInfoMapper.class).findById(heatNo);
		return heat;
	}

	//=========================公共方法=======================================
	/**更新物品信息
	 * @param objectNo
	 */
	public void doUpdateByLine(String objectNo){
		LmsConsumableLineInfo line = new LmsConsumableLineInfo();
		line.setObject_no(objectNo);
		List<LmsConsumableLineInfo> lineList = this.getMapper(LmsConsumableLineInfoMapper.class).find(line, 0, 0);
		int number = 0;
		double totalAmount = 0;
		double weightPrice = 0;
		if(lineList!=null){
			for(LmsConsumableLineInfo ocl : lineList){
				number += ocl.getNumber2();//余量相加得到总量
				totalAmount += ocl.getNumber2() * ocl.getPrice();//得到每个的总金额
			}
		}
		weightPrice =(number!=0)?totalAmount/number:0;//加权价格

		//耗材主表信息的更新
		LmsConsumableHeatInfo heat = new LmsConsumableHeatInfo();
		heat = this.getBean(LmsConsumableHeatInfoServiceImpl.class).doQuery(objectNo);
		if(heat != null){
			heat.setNumbers(number);
			heat.setWeight_price(weightPrice);
			this.getBean(LmsConsumableHeatInfoServiceImpl.class).doUpdate(heat);
		}else{
			//该物品第一次新增
			LmsConsumableHeatInfo och = new LmsConsumableHeatInfo();
			och.setObject_no(objectNo);
			och.setObject_name(lineList.get(0).getObject_name());
			och.setMeasure_unit_no(lineList.get(0).getMeasure_unit_no());
			och.setMeasure_unit_name(lineList.get(0).getMeasure_unit_name());
			och.setNumbers(number);
			och.setWeight_price(weightPrice);
			this.getBean(LmsConsumableHeatInfoServiceImpl.class).doAdd(och);
		}
	}

}
