package market.Lms.FixedAssets.impl;

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.FixedAssets.mapper.LmsFixedSparePartMapper;
import market.Lms.FixedAssets.service.LmsFixedSparePartService;
import market.Lms.FixedAssets.vo.LmsFixedSparePart;

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;
import com.ibm.icu.text.SimpleDateFormat;

@RemoteService(path="LmsFixedSparePartService")
public class LmsFixedSparePartServiceImpl extends BaseServiceImpl<LmsFixedSparePart,Object> implements LmsFixedSparePartService{
	
	/**
	 * 备品备件查询
	 * */
	public ResultModel doQueryWeb(CommonPage<LmsFixedSparePart> sum){
		ResultModel rm=new ResultModel();
		LmsFixedSparePart part=sum.getObject();
		int pageSize=sum.getPageSize();
		int pageIndex=sum.getPageIndex();
		PageModel<LmsFixedSparePart> pm=new PageModel<LmsFixedSparePart>(pageIndex, pageSize);
		try{
			String sWhere=" 1=1 ";
			if(part.getGoods_name()!=null&&!"".equals(part.getGoods_name())){
				sWhere+=" and goods_name like '%"+part.getGoods_name()+"%'";
				part.setGoods_name(null);
			}
			if(part.getManufactor()!=null&&!"".equals(part.getManufactor())){
				sWhere+=" and manufactor like '%"+part.getManufactor()+"%'";
				part.setManufactor(null);
			}if(part.getBar_code()!=null&&!"".equals(part.getBar_code())){
				sWhere+=" and bar_code like '%"+part.getBar_code()+"%'";
				part.setBar_code(null);
			}if(part.getSpec_model()!=null&&!"".equals(part.getSpec_model())){
				sWhere+=" and spec_model like '%"+part.getSpec_model()+"%'";
				part.setSpec_model(null);
			}
			List<LmsFixedSparePart> list=this.getMapper(LmsFixedSparePartMapper.class).findWithCondition(part, sWhere, " create_time desc", (pageIndex-1)*pageSize,pageSize);
			long count=this.getMapper(LmsFixedSparePartMapper.class).countWithCondition(part, sWhere);
			pm.setRows(list);
			pm.setTotal(count);
			rm.setData(pm);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 新增
	 * */
	public ResultModel doAddWeb(LmsFixedSparePart part){
		ResultModel rm=new ResultModel();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			if(part.getGoods_name()==null||"".equals(part.getGoods_name())){
				rm.setState("500");
				rm.setMsgInfo("请输入备件名称");
			}
			String id=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
			part.setGoods_no(id);
			part.setCreate_man_no(userId);
			part.setCreate_man_name(userName);
			part.setCreate_time(new Date());
			this.getMapper(LmsFixedSparePartMapper.class).insert(part);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 修改
	 * */
	public ResultModel doUpdateWeb(LmsFixedSparePart part){
		ResultModel rm=new ResultModel();
		try{
			if(part.getGoods_name()==null||"".equals(part.getGoods_name())){
				rm.setState("500");
				rm.setMsgInfo("请输入备件名称");
			}
			this.getMapper(LmsFixedSparePartMapper.class).update(part);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 删除
	 * */
	public ResultModel doDeleteWeb(LmsFixedSparePart part){
		ResultModel rm=new ResultModel();
		try{
			this.getMapper(LmsFixedSparePartMapper.class).delete(part.getGoods_no());
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 导入
	 * */
	public ResultModel doImportWeb(CommonPage<LmsFixedSparePart> sum){
		ResultModel rm=new ResultModel();
		try { 
			//暂时用前端解析
			List<LmsFixedSparePart> list=sum.getObjectList();
			for(LmsFixedSparePart part:list){
				rm=doAddWeb(part);
				if("500".equals(rm.getState()))	return rm;
			}
//		File file = new File(path);
//		Sheet sheet;
//		Workbook book;
//		Cell cell1,cell2,cell3,cell4,cell5,cell6,cell7,cell8,cell9,cell10,cell11;
//			//t.xls为要读取的excel文件名
//			book= Workbook.getWorkbook(new File(file.getAbsolutePath())); 
//			//获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)
//			sheet=book.getSheet(1); 
//			//获取左上角的单元格
//			int i=1;
//			while(i<sheet.getRows()){
//				LmsFixedSparePart part=new LmsFixedSparePart();
//				cell1=sheet.getCell(0,i);		//城市ID
//				cell2=sheet.getCell(1,i);		//城市名
//				cell3=sheet.getCell(2,i);		//城市名（英文）
//				cell4=sheet.getCell(3,i);		//国家
//				cell5=sheet.getCell(4,i);		//国家code
//				cell6=sheet.getCell(5,i);		//行政归属1
//				cell7=sheet.getCell(6,i);		//行政归属 英文1
//				cell8=sheet.getCell(7,i);		//行政归属2
//				cell9=sheet.getCell(8,i);		//行政归属 英文2
//				cell10=sheet.getCell(9,i);		//时区
//				cell11=sheet.getCell(10,i);		//城市级别
//
//				this.getMapper(LmsFixedSparePartMapper.class).insert(part);
//				i++;
//			}
//			book.close(); 
			rm.setState("200");
		}catch(Exception e){
			System.out.println("导入失败");
			e.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}
}
