/***文档注释***********************************************
 * 作者               :                   龙文航
 * 创建日期      :                   2017.11.21
 * 描述               :                   仓库管理-成品库管理-调拨单管理
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/

package market.Lms.Warehouse.impl;

import java.util.Date;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.mapper.YdmBaseButtressconfigMapper;
import market.Lms.Warehouse.service.YdmBaseButtressconfigService;
import market.Lms.Warehouse.vo.YdmBaseButtressconfig;

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="YdmBaseButtressconfigService")
public class YdmBaseButtressconfigServiceImpl extends BaseServiceImpl<YdmBaseButtressconfig,Object> implements YdmBaseButtressconfigService {
	
	/**
	 * 获取仓库编码，有则返回最大+1，否则1001
	 * @throws Exception 
	 * */
	public String GetButtressno() throws Exception
	{
		try
		{
			String buttressno=this.getMapper(YdmBaseButtressconfigMapper.class).GetmaxButtressno();
			if(buttressno!=null&&buttressno!="")
				return String.valueOf(Integer.valueOf(buttressno).intValue()+1);
			else
				return "1001";
		}
		catch(Exception ex)
		{
			throw(ex);
		}
	}
	
	/**
	 * 获取区域编码，有则返回最大+1，否则仓库编码+001
	 * @throws Exception 
	 * */
	public String GetAreano(String sortcode) throws Exception
	{
		try
		{
			String areano=this.getMapper(YdmBaseButtressconfigMapper.class).GetmaxAreano(sortcode);
			if(areano!=null&&areano!="")
				return String.valueOf(Integer.valueOf(areano).intValue()+1);
			else
				return sortcode+"001";
		}
		catch(Exception ex)
		{
			throw(ex);
		}
	}
	
	/**
	 * 获取垛位编码，有则返回最大+1，否则区域编码+001
	 * @throws Exception 
	 * */
	public String Getbuttressno(String sortcode) throws Exception
	{
		try
		{
			String buttressno=this.getMapper(YdmBaseButtressconfigMapper.class).Getmaxbuttressno(sortcode);
			if(buttressno!=null&&buttressno!="")
				return String.valueOf(Integer.valueOf(buttressno).intValue()+1);
			else
				return sortcode+"001";
		}
		catch(Exception ex)
		{
			throw(ex);
		}
	}
	
	
	/**
	 * 成品库管理 垛位基础配置管理 新增仓库
	 * 传入managementno
	 * */
	public ResultModel doAddStorageattr(CommonPage<YdmBaseButtressconfig> cp)
	{
		ResultModel rm=new ResultModel();
		try 
		{
			String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			YdmBaseButtressconfig ybb=cp.getObject();
			ybb.setButtressno(this.GetButtressno());
			ybb.setCreatedate(new Date());
			ybb.setCreater(username);
			ybb.setStoragesort("A");
			ybb.setValidflag("1");
			ybb.setSortcode(null);
			this.getMapper(YdmBaseButtressconfigMapper.class).insert(ybb);
			rm.setState("200");
			rm.setData(ybb.getButtressno());
		} 
		catch (Exception ex) 
		{
			rm.setState("500");
			rm.setMsgInfo("新增失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 成品库管理 垛位基础配置管理 新增区域
	 * 传入sortcode storageattr managementno
	 * */
	public ResultModel doAddArea(CommonPage<YdmBaseButtressconfig> cp)
	{
		ResultModel rm=new ResultModel();
		try 
		{
			String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			YdmBaseButtressconfig ybb=cp.getObject();
			ybb.setButtressno(this.GetAreano(ybb.getSortcode()));
			ybb.setCreatedate(new Date());
			ybb.setCreater(username);
			ybb.setStoragesort("B");
			ybb.setValidflag("1");
			this.getMapper(YdmBaseButtressconfigMapper.class).insert(ybb);
			rm.setData(ybb.getButtressno());
			rm.setState("200");
		} 
		catch (Exception ex) 
		{
			rm.setState("500");
			rm.setMsgInfo("新增失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 成品库管理 垛位基础配置管理 新增垛位
	 * 传入sortcode storageattr managementno
	 * */
	public ResultModel doAddButtress(CommonPage<YdmBaseButtressconfig> cp)
	{
		ResultModel rm=new ResultModel();
		try 
		{
			String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			YdmBaseButtressconfig ybb=cp.getObject();
			ybb.setButtressno(this.Getbuttressno(ybb.getSortcode()));
			ybb.setCreatedate(new Date());
			ybb.setCreater(username);
			ybb.setStoragesort("C");
			ybb.setValidflag("1");
			this.getMapper(YdmBaseButtressconfigMapper.class).insert(ybb);
			rm.setState("200");
			rm.setData(ybb.getButtressno());
		} 
		catch (Exception ex) 
		{
			rm.setState("500");
			rm.setMsgInfo("新增失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 成品库管理 垛位基础配置管理 垛位查询
	 * */
	public ResultModel doQueryWebBySQLButtress(CommonPage<YdmBaseButtressconfig> cp)
	{
		ResultModel rm=new ResultModel();
		int pageSize=cp.getPageSize();
		int pageIndex=cp.getPageIndex();
		PageModel<YdmBaseButtressconfig> pm = new PageModel<YdmBaseButtressconfig>(pageIndex,pageSize);
		try 
		{
			YdmBaseButtressconfig ybb=cp.getObject();
			if(ybb.getSortcode()!=null&&ybb.getSortcode()!="")
			{
				pm.setRows(this.getMapper(YdmBaseButtressconfigMapper.class).doQueryWebBySQLButtress
						((pageIndex-1)*pageSize, (pageIndex-1)*pageSize+pageSize,ybb.getSortcode()));
				pm.setTotal(this.getMapper(YdmBaseButtressconfigMapper.class).doQueryWebBySQLButtressCount
						(ybb.getSortcode()));
				rm.setData(pm);
				rm.setState("200");
			}
			else 
			{
				rm.setState("500");
				rm.setMsgInfo("传入的区域编码为空!");
			}
		} 
		catch (Exception ex) 
		{
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 获取详细信息
	 * */
	public ResultModel doQueryDetail(CommonPage<YdmBaseButtressconfig> cp){
		ResultModel rm=new ResultModel();
		try{
			YdmBaseButtressconfig ydm=cp.getObject();
			rm.setData(this.getMapper(YdmBaseButtressconfigMapper.class).findById(ydm.getButtressno()));
			rm.setState("200");
		}
		catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("获取失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 成品库管理 仓库修改
	 * */
	public ResultModel doUpdateStorage(CommonPage<YdmBaseButtressconfig> cp){
		ResultModel rm=new ResultModel();
		try{
			YdmBaseButtressconfig ydm=cp.getObject();
			String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			if(ydm.getButtressno()!=null&&ydm.getButtressno()!=""){
				YdmBaseButtressconfig ybb=this.getMapper(YdmBaseButtressconfigMapper.class).findById(ydm.getButtressno());
				if(ybb!=null){
					if (ydm.getButtressname()!=null) {
						ybb.setButtressname(ydm.getButtressname());
					}
					ybb.setUpdatedate(new Date());
					ybb.setUpdater(username);
					if (ydm.getStoragetype()!=null) {
						ybb.setStoragetype(ydm.getStoragetype());
					}
					if (ydm.getValidflag()!=null) {
						ybb.setValidflag(ydm.getValidflag());
					}
					if (ydm.getStorageattr()!=null) {
						ybb.setStorageattr(ydm.getStorageattr());
					}
					this.getMapper(YdmBaseButtressconfigMapper.class).update(ybb);
					rm.setState("200");
				}
			}
		}
		catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("修改失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 成品库管理 区域修改
	 * */
	public ResultModel doUpdateArea(CommonPage<YdmBaseButtressconfig> cp){
		ResultModel rm=new ResultModel();
		try{
			YdmBaseButtressconfig ydm=cp.getObject();
			String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			if(ydm.getButtressno()!=null&&ydm.getButtressno()!=""){
				YdmBaseButtressconfig ybb=this.getMapper(YdmBaseButtressconfigMapper.class).findById(ydm.getButtressno());
				if(ybb!=null){
					if (ydm.getButtressname()!=null) {
						ybb.setButtressname(ydm.getButtressname());
					}
					ybb.setUpdatedate(new Date());
					ybb.setUpdater(username);
					if (ydm.getAreaattr()!=null) {
						ybb.setAreaattr(ydm.getAreaattr());
					}
					if (ydm.getValidflag()!=null) {
						ybb.setValidflag(ydm.getValidflag());
					}
					this.getMapper(YdmBaseButtressconfigMapper.class).update(ybb);
					rm.setState("200");
				}
			}
		}
		catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("修改失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 成品库管理 货位修改
	 * */
	public ResultModel doUpdateButtress(CommonPage<YdmBaseButtressconfig> cp){
		ResultModel rm=new ResultModel();
		try{
			YdmBaseButtressconfig ydm=cp.getObject();
			String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			if(ydm.getButtressno()!=null&&ydm.getButtressno()!=""){
				YdmBaseButtressconfig ybb=this.getMapper(YdmBaseButtressconfigMapper.class).findById(ydm.getButtressno());
				if(ybb!=null){
					if (ydm.getButtressname()!=null) {
						ybb.setButtressname(ydm.getButtressname());
					}
					ybb.setUpdatedate(new Date());
					ybb.setUpdater(username);
					if (ydm.getLayer()!=null) {
						ybb.setLayer(ydm.getLayer());
					}
					if (ydm.getColwidth()!=null) {
						ybb.setColwidth(ydm.getColwidth());
					}
					if (ydm.getValidflag()!=null) {
						ybb.setValidflag(ydm.getValidflag());
					}
					this.getMapper(YdmBaseButtressconfigMapper.class).update(ybb);
					rm.setState("200");
				}
			}
		}
		catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("修改失败!");
			ex.printStackTrace();
		}
		return rm;
	}
}
