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.LmsConsumableGoodInfoMapper;
import market.Lms.Consumable.service.LmsConsumableGoodInfoService;
import market.Lms.Consumable.vo.LmsConsumableAnnex;
import market.Lms.Consumable.vo.LmsConsumableGoodInfo;

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

@RemoteService(path="LmsConsumableGoodInfo")
public class LmsConsumableGoodInfoServiceImpl extends BaseServiceImpl<LmsConsumableGoodInfo,Object> implements LmsConsumableGoodInfoService{

	//新增
	public String doAdd(LmsConsumableGoodInfo good){
		String id = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		good.setValid_flag("1");
		good.setGood_no(id);
		good.setCreate_man_no(userId);
		good.setCreate_man_name(userName);
		good.setCreate_time(new Date());
		this.getMapper(LmsConsumableGoodInfoMapper.class).insert(good);
		return id;
	}

	//删除
	public void doDelete(LmsConsumableGoodInfo good){
		this.getMapper(LmsConsumableGoodInfoMapper.class).delete(good.getGood_no());
	}

	//修改
	public void doUpdate(LmsConsumableGoodInfo good){
		this.getMapper(LmsConsumableGoodInfoMapper.class).update(good);
	}

	//查询
	public LmsConsumableGoodInfo doQuery(String goodNo){
		LmsConsumableGoodInfo good = new LmsConsumableGoodInfo();
		good = this.getMapper(LmsConsumableGoodInfoMapper.class).findById(goodNo);
		return good;
	}

	//=======================================APP=======================================
	//APP端入库副表列表
	public List<LmsConsumableGoodInfo> doQueryApp(String inNo,String warehouseNo,String supplierNo,String objectNo,int pageIndex,int pageSize){
		List<LmsConsumableGoodInfo> goodList = new ArrayList<LmsConsumableGoodInfo>();
		try{
			goodList = this.getMapper(LmsConsumableGoodInfoMapper.class).doQueryListApp(inNo,warehouseNo, supplierNo, objectNo, pageIndex, pageSize);
			if(goodList!=null){
				//查询附件
				for(LmsConsumableGoodInfo ocg : goodList){
					LmsConsumableAnnex annex = new LmsConsumableAnnex();
					annex.setObject_no(ocg.getGood_no());
					List<LmsConsumableAnnex> annexList = this.getMapper(LmsConsumableAnnexMapper.class).find(annex, 0, 0);
					if(annexList != null){
						ocg.setAnnexList(annexList);
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			return new ArrayList<LmsConsumableGoodInfo>();
		}
		return goodList;
	}

	//====================================WEB===========================================
	//WEB端入库副表列表
	public ResultModel doQueryWeb(CommonPage<LmsConsumableGoodInfo> sum){
		ResultModel rm = new ResultModel();
		List<LmsConsumableGoodInfo> goodList = new ArrayList<LmsConsumableGoodInfo>();
		String inNo = sum.getMemo1();
		String warehouseNo = sum.getMemo2();
		String supplierNo = sum.getMemo3();
		String objectNo = sum.getMemo4();
		int pageIndex = sum.getPageIndex();
		int pageSize = sum.getPageSize();
		int count = 0;
		PageModel<LmsConsumableGoodInfo> pm=new PageModel<LmsConsumableGoodInfo>(pageIndex, pageSize);
		try{
			goodList = this.getMapper(LmsConsumableGoodInfoMapper.class).doQueryListApp(inNo,warehouseNo, supplierNo, objectNo, (pageIndex-1)*pageSize, pageSize);
			if(goodList!=null){
				//查询附件
				for(LmsConsumableGoodInfo ocg : goodList){
					LmsConsumableAnnex annex = new LmsConsumableAnnex();
					annex.setObject_no(ocg.getGood_no());
					List<LmsConsumableAnnex> annexList = this.getMapper(LmsConsumableAnnexMapper.class).find(annex, 0, 0);
					if(annexList != null){
						ocg.setAnnexList(annexList);
					}
				}
			}
			count = this.getMapper(LmsConsumableGoodInfoMapper.class).countListApp(inNo, warehouseNo, supplierNo, objectNo);
			pm.setRows(goodList);
			pm.setTotal(count);
			rm.setData(pm);
			rm.setState("200");
		}catch(Exception e){
			e.printStackTrace();
			rm.setState("500");
		}
		return rm;
	}

}
