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

import java.text.SimpleDateFormat;
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.Slm.BaseInformation.mapper.SlmBaseShipPortMapper;
import market.Slm.BaseInformation.service.SlmBaseShipPortService;
import market.Slm.BaseInformation.vo.SlmBaseShipPort;

import org.springframework.transaction.annotation.Transactional;

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="SlmBaseShipPortService")
public class SlmBaseShipPortServiceImpl extends BaseServiceImpl<SlmBaseShipPort,Object> implements SlmBaseShipPortService {
	/**
	 * 查询数据
	 * memo1有值时不查父当前节点
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<SlmBaseShipPort> sum) {
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		try{
			PageModel<SlmBaseShipPort> pm = new PageModel<SlmBaseShipPort>(pageIndex,pageSize);
			SlmBaseShipPort sb=sum.getObject()==null?(new SlmBaseShipPort()):sum.getObject();
			String sWhere=" 1=1 ";//sort_code='1001' ";
			
			if(sb!=null){
				if(sb.getShip_port_name()!=null&&!"".equals(sb.getShip_port_name())){
					sWhere+=" and (ship_port_name LIKE '%"+sb.getShip_port_name()+"%' or py like '%"+sb.getShip_port_name()+"%') ";
					sb.setShip_port_name(null);
				}
			}
	
			pm.setRows(this.getMapper(SlmBaseShipPortMapper.class).findWithCondition(sb, sWhere, " ship_port_name ", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(SlmBaseShipPortMapper.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<SlmBaseShipPort> sum){
		ResultModel rm=new ResultModel();
		try{
			rm.setState("200");
			rm.setData(this.getMapper(SlmBaseShipPortMapper.class).findById(sum.getObject().getShip_port_no()));
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	
	/**
	 * 添加数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doAddWeb(CommonPage<SlmBaseShipPort> sum) {
		ResultModel rm=new ResultModel();
		try {
			SlmBaseShipPort bd=sum.getObject();
			SlmBaseShipPort cbi=new SlmBaseShipPort();
			
			cbi.setShip_port_name(bd.getShip_port_name());
			int icount=this.getMapper(SlmBaseShipPortMapper.class).count(cbi);
			if(icount>0){
				rm.setState("500");
				rm.setMsgInfo("已存在相同名称的数据!");
				return rm;
			}
			
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			bd.setShip_port_no(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3));
			bd.setCreate_man_no(userId);
			bd.setCreate_man_name(userName);
			bd.setValid_flag("1");
			bd.setCreate_time(new Date());
			bd.setPy(PbMethod.ChineseSpell(bd.getShip_port_name()));
			
			this.getMapper(SlmBaseShipPortMapper.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<SlmBaseShipPort> sum) {
		ResultModel rm=new ResultModel();
		SlmBaseShipPort bd = sum.getObject();
		try {
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			
			//查询当前名称
			SlmBaseShipPort cbi=this.getMapper(SlmBaseShipPortMapper.class).findById(bd.getShip_port_no());
			if(cbi!=null&&cbi.getShip_port_name()!=null){
				if(!cbi.getShip_port_name().equals(bd.getShip_port_name())){
					SlmBaseShipPort sba=new SlmBaseShipPort();
					sba.setShip_port_name(bd.getShip_port_name());
					int icount=this.getMapper(SlmBaseShipPortMapper.class).count(sba);
					if(icount>0){
						rm.setState("500");
						rm.setMsgInfo("已存在相同名称的数据!");
						return rm;
					}
				}
				
				bd.setUpdate_man_no(userId);
				bd.setUpdate_man_name(userName);
				bd.setUpdate_time(new Date());
				bd.setPy(PbMethod.ChineseSpell(bd.getShip_port_name()));
				this.getMapper(SlmBaseShipPortMapper.class).update(bd);
				rm.setState("200");
			}else{
				rm.setState("500");
				rm.setMsgInfo("当前数据已被删除!");
				return rm;
			}
			
			
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("修改失败!");
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 启用、停用数据
	 * @param bd
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doUpdateFlagWeb(CommonPage<SlmBaseShipPort> sum) {
		ResultModel rm=new ResultModel();
		List<SlmBaseShipPort> obd = sum.getObjectList();
		for(SlmBaseShipPort cbi:obd){
			this.getMapper(SlmBaseShipPortMapper.class).update(cbi);
		}
		rm.setState("200");
		return rm;
	}
}
