package market.Lms.Consumable.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.pb.PbMethod;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Consumable.mapper.LmsConsumableAnnexMapper;
import market.Lms.Consumable.mapper.LmsConsumableLineInfoMapper;
import market.Lms.Consumable.service.LmsConsumableLineInfoService;
import market.Lms.Consumable.vo.LmsConsumableAnnex;
import market.Lms.Consumable.vo.LmsConsumableLineInfo;

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

@RemoteService(path="LmsConsumableLineInfo")
public class LmsConsumableLineInfoServiceImpl extends BaseServiceImpl<LmsConsumableLineInfo,Object> implements LmsConsumableLineInfoService{

	//新增
	public String doAdd(LmsConsumableLineInfo line){
		String id = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
		line.setLine_no(id);
		this.getMapper(LmsConsumableLineInfoMapper.class).insert(line);
		return id;
	}

	//删除
	public void doDelete(LmsConsumableLineInfo line){
		this.getMapper(LmsConsumableLineInfoMapper.class).delete(line.getLine_no());
	}

	//修改
	public void doUpdate(LmsConsumableLineInfo line){
		this.getMapper(LmsConsumableLineInfoMapper.class).update(line);
	}

	//查询
	public LmsConsumableLineInfo doQuery(String lineNo){
		LmsConsumableLineInfo line = new LmsConsumableLineInfo();
		line = this.getMapper(LmsConsumableLineInfoMapper.class).findById(lineNo);
		return line;
	}

	//=============================APP==================================================
	/**查询物品所有批次副表列表
	 * @param objectNo
	 * @return
	 */
	public List<LmsConsumableLineInfo> doQueryApp(LmsConsumableLineInfo line,int pageIndex,int pageSize){
		List<LmsConsumableLineInfo> lineList = new ArrayList<LmsConsumableLineInfo>();
		List<LmsConsumableAnnex> annexList = new ArrayList<LmsConsumableAnnex>();
		try{
			lineList = this.getMapper(LmsConsumableLineInfoMapper.class).findWithCondition(line,null,"line_no desc", pageIndex, pageSize);
			if(lineList!=null){
				for(LmsConsumableLineInfo ocal : lineList){
					//查询每个物品关联的附件
					LmsConsumableAnnex annex = new LmsConsumableAnnex();
					annex.setObject_no(ocal.getObject_no());
					annexList = this.getMapper(LmsConsumableAnnexMapper.class).find(annex, 0, 0);
					ocal.setAnnexList(annexList);
				}
			}
		}catch(Exception e){
			return new ArrayList<LmsConsumableLineInfo>();
		}
		return lineList;
	}

	//=============================APP==================================================
	/**查询物品所有批次副表列表
	 * @param objectNo
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<LmsConsumableLineInfo> sum){
		ResultModel rm = new ResultModel();
		LmsConsumableLineInfo line = sum.getObject();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		List<LmsConsumableLineInfo> lineList = new ArrayList<LmsConsumableLineInfo>();
		List<LmsConsumableAnnex> annexList = new ArrayList<LmsConsumableAnnex>();
		PageModel<LmsConsumableLineInfo> pm=new PageModel<LmsConsumableLineInfo>(pageIndex, pageSize);
		int count = 0;
		try{
			lineList = this.getMapper(LmsConsumableLineInfoMapper.class).findWithCondition(line,null," line_no desc", pageSize*(pageIndex-1), pageSize);
			if(lineList!=null){
				for(LmsConsumableLineInfo ocal : lineList){
					//查询每个物品关联的附件
					LmsConsumableAnnex annex = new LmsConsumableAnnex();
					annex.setObject_no(ocal.getObject_no());
					annexList = this.getMapper(LmsConsumableAnnexMapper.class).find(annex, 0, 0);
					ocal.setAnnexList(annexList);
				}
			}
			count = this.getMapper(LmsConsumableLineInfoMapper.class).count(line);
			pm.setRows(lineList);
			pm.setTotal(count);
			rm.setData(pm);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
		}
		return rm;
	}
}
