/***文档注释***********************************************
 * 作者               :                   易鑫
 * 创建日期      :                   2017.10.27
 * 描述               :        			长度基础信息
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Slm.BaseInformation.impl;

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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.BaseInformation.mapper.SlmBaseLengthMapper;
import market.Slm.BaseInformation.service.SlmBaseLengthService;
import market.Slm.BaseInformation.vo.SlmBaseLength;

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="SlmBaseLengthService")
public class SlmBaseLengthServiceImpl extends BaseServiceImpl<SlmBaseLength,Object> implements SlmBaseLengthService {
	/**
	 * 查询
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<SlmBaseLength> sum) {
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		try{
			PageModel<SlmBaseLength> pm = new PageModel<SlmBaseLength>(pageIndex,pageSize);
			SlmBaseLength sb=sum.getObject();
			String sWhere="";
			
			pm.setRows(this.getMapper(SlmBaseLengthMapper.class).findWithCondition(sb, sWhere, " LENGTH_NO ", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(SlmBaseLengthMapper.class).countWithCondition(sb,sWhere));
	
			rm.setState("200");
			rm.setData(pm);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;

	}
	
	/**
	 * 查看详情
	 * */
	public ResultModel doQueryDetailWeb(CommonPage<SlmBaseLength> sum){
		ResultModel rm=new ResultModel();
		try{
			rm.setState("200");
			rm.setData(this.getMapper(SlmBaseLengthMapper.class).findById(sum.getObject().getLength_no()));
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 添加数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doAddWeb(CommonPage<SlmBaseLength> sum) {
		ResultModel rm=new ResultModel();
		try {
			String No=new SimpleDateFormat("yyyyMMdd").format(new Date());
			int icount=this.getMapper(SlmBaseLengthMapper.class).countWithCondition(null," length_no like '"+No+"%' ")+1;
			SlmBaseLength bd=sum.getObject();
			bd.setLength_no(No+String.format("%04d", icount));

			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			bd.setCreate_man_no(userId);
			bd.setCreate_man_name(userName);
			bd.setValid_flag("1");
			bd.setCreate_time(new Date());
			this.getMapper(SlmBaseLengthMapper.class).insert(bd);
			rm.setState("200");
			
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，新增失败!");
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<SlmBaseLength> sum) {
		ResultModel rm=new ResultModel();
		SlmBaseLength obd = sum.getObject();
		try {
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			obd.setUpdate_man_no(userId);
			obd.setUpdate_man_name(userName);
			obd.setUpdate_time(new Date());
			this.getMapper(SlmBaseLengthMapper.class).update(obd);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("修改失败!");
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 启用、停用
	 * @param bd
	 * @return
	 */
	public ResultModel doUpdateFlagWeb(CommonPage<SlmBaseLength> sum) {
		ResultModel rm=new ResultModel();
		
		List<SlmBaseLength> obd = sum.getObjectList();
		try {
			for(SlmBaseLength bd:obd){
				this.getMapper(SlmBaseLengthMapper.class).update(bd);
			}
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			e.printStackTrace();
		}
		return rm;
	}
	
	
}
