/***文档注释***********************************************
 * 作者               :                   易鑫
 * 创建日期      :                   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.SlmBaseRailStationMapper;
import market.Slm.BaseInformation.service.SlmBaseRailStationService;
import market.Slm.BaseInformation.vo.SlmBaseRailStation;

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="SlmBaseRailStationService")
public class SlmBaseRailStationServiceImpl extends BaseServiceImpl<SlmBaseRailStation,Object> implements SlmBaseRailStationService {
	/**
	 * 查询数据
	 * memo1有值时不查父当前节点
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<SlmBaseRailStation> sum) {
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		try{
			PageModel<SlmBaseRailStation> pm = new PageModel<SlmBaseRailStation>(pageIndex,pageSize);
			SlmBaseRailStation sb=sum.getObject()==null?(new SlmBaseRailStation()):sum.getObject();
			String sWhere=" 1=1 ";//sort_code='1001' ";
			
			if(sb!=null){
				if(sb.getStation_name()!=null&&!"".equals(sb.getStation_name())){
					sWhere+=" and (station_name LIKE '%"+sb.getStation_name()+"%' or py like '%"+sb.getStation_name()+"%') ";
					sb.setStation_name(null);
				}
			}
	
			pm.setRows(this.getMapper(SlmBaseRailStationMapper.class).findWithCondition(sb, sWhere, " station_name ", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(SlmBaseRailStationMapper.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<SlmBaseRailStation> sum){
		ResultModel rm=new ResultModel();
		try{
			rm.setState("200");
			rm.setData(this.getMapper(SlmBaseRailStationMapper.class).findById(sum.getObject().getStation_no()));
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	
	/**
	 * 添加数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doAddWeb(CommonPage<SlmBaseRailStation> sum) {
		ResultModel rm=new ResultModel();
		try {
			SlmBaseRailStation bd=sum.getObject();
			SlmBaseRailStation cbi=new SlmBaseRailStation();
			
			cbi.setStation_name(bd.getStation_name());
			int icount=this.getMapper(SlmBaseRailStationMapper.class).count(cbi);
			if(icount>0){
				rm.setState("500");
				rm.setMsgInfo("已存在相同名称的数据!");
				return rm;
			}
			bd.setStation_no(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3));
			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());
			bd.setPy(PbMethod.ChineseSpell(bd.getStation_name()));
			
			this.getMapper(SlmBaseRailStationMapper.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<SlmBaseRailStation> sum) {
		ResultModel rm=new ResultModel();
		SlmBaseRailStation bd = sum.getObject();
		try {
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			
			//查询当前名称
			SlmBaseRailStation cbi=this.getMapper(SlmBaseRailStationMapper.class).findById(bd.getStation_no());
			if(cbi!=null&&cbi.getStation_no()!=null){
				if(!cbi.getStation_name().equals(bd.getStation_name())){
					SlmBaseRailStation sba=new SlmBaseRailStation();
					sba.setStation_name(bd.getStation_name());
					int icount=this.getMapper(SlmBaseRailStationMapper.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.getStation_name()));
				this.getMapper(SlmBaseRailStationMapper.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<SlmBaseRailStation> sum) {
		ResultModel rm=new ResultModel();
		List<SlmBaseRailStation> obd = sum.getObjectList();
		for(SlmBaseRailStation cbi:obd){
			this.getMapper(SlmBaseRailStationMapper.class).update(cbi);
		}
		rm.setState("200");
		return rm;
	}
}
