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

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