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.LmsFixedPropertyMapper;
import market.Lms.FixedAssets.service.LmsFixedPropertyService;
import market.Lms.FixedAssets.vo.LmsFixedProperty;

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="LmsFixedPropertyService")
public class LmsFixedPropertyServiceImpl extends BaseServiceImpl<LmsFixedProperty,Object> implements LmsFixedPropertyService{
	
	/**
	 * 资产查询
	 * */
	public ResultModel doQueryWeb(CommonPage<LmsFixedProperty> sum){
		ResultModel rm=new ResultModel();
		LmsFixedProperty property=sum.getObject();
		int pageSize=sum.getPageSize();
		int pageIndex=sum.getPageIndex();
		PageModel<LmsFixedProperty> pm=new PageModel<LmsFixedProperty>(pageIndex, pageSize);
		try{
			String sWhere=" 1=1 ";
			if(property.getFixed_name()!=null&&!"".equals(property.getFixed_name())){
				sWhere+=" and fixed_name like '%"+property.getFixed_name()+"%'";
				property.setFixed_name(null);
			}
			if(property.getFixed_category_name()!=null&&!"".equals(property.getFixed_category_name())){
				sWhere+=" and fixed_category_name like '%"+property.getFixed_category_name()+"%'";
				property.setFixed_category_name(null);
			}
			if(property.getSpec_model()!=null&&!"".equals(property.getSpec_model())){
				sWhere+=" and spec_model like '%"+property.getSpec_model()+"%'";
				property.setSpec_model(null);
			}
			if(property.getRegion_name()!=null&&!"".equals(property.getRegion_name())){
				sWhere+=" and region_name like '%"+property.getRegion_name()+"%'";
				property.setRegion_name(null);
			}
			if(property.getPlace()!=null&&!"".equals(property.getPlace())){
				sWhere+=" and place like concat('%',concat('"+property.getPlace()+"','%'))";
				property.setPlace(null);
			}
			List<LmsFixedProperty> list=this.getMapper(LmsFixedPropertyMapper.class).findWithCondition(property, sWhere, " create_time desc", (pageIndex-1)*pageSize,pageSize);
			long count=this.getMapper(LmsFixedPropertyMapper.class).countWithCondition(property, 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(LmsFixedProperty property){
		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(property.getFixed_name()==null||"".equals(property.getFixed_name())){
				rm.setState("500");
				rm.setMsgInfo("请输入资产名称");
			}
			String id=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
			property.setFixed_no(id);
			property.setCreate_man_no(userId);
			property.setCreate_man_name(userName);
			property.setCreate_time(new Date());
			this.getMapper(LmsFixedPropertyMapper.class).insert(property);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 修改
	 * */
	public ResultModel doUpdateWeb(LmsFixedProperty property){
		ResultModel rm=new ResultModel();
		try{
			if(property.getFixed_name()==null||"".equals(property.getFixed_name())){
				rm.setState("500");
				rm.setMsgInfo("请输入备件名称");
			}
			this.getMapper(LmsFixedPropertyMapper.class).update(property);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 删除
	 * */
	public ResultModel doDeleteWeb(LmsFixedProperty property){
		ResultModel rm=new ResultModel();
		try{
			this.getMapper(LmsFixedPropertyMapper.class).delete(property.getFixed_no());
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 导入
	 * */
	public ResultModel doImportWeb(CommonPage<LmsFixedProperty> sum){
		ResultModel rm=new ResultModel();
		try { 
			//暂时用前端解析
			List<LmsFixedProperty> list=sum.getObjectList();
			for(LmsFixedProperty part:list){
				rm=doAddWeb(part);
				if("500".equals(rm.getState()))	return rm;
			}
			rm.setState("200");
		}catch(Exception e){
			System.out.println("导入失败");
			e.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}
}
