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

import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.BaseInformation.mapper.SlmBaseRegionMapper;
import market.Slm.BaseInformation.service.SlmBaseRegionService;
import market.Slm.BaseInformation.vo.SlmBaseRegion;

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="SlmBaseRegionService")
public class SlmBaseRegionServiceImpl extends BaseServiceImpl<SlmBaseRegion,Object> implements SlmBaseRegionService {
	/**
	 * 寻找上级行政区域编号
	 * memo1有值时不查父当前节点
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<SlmBaseRegion> sum) {
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();

		PageModel<SlmBaseRegion> pm = new PageModel<SlmBaseRegion>(pageIndex,pageSize);
		
		try {
			SlmBaseRegion sb=sum.getObject();
			String sWhere=" 1= 1";
//			sWhere+=" valid_flag='1' ";
			if(sb!=null){
				if(!"".equals(sb.getP_region_no())&&sb.getP_region_no()!=null){
					if(sum.getMemo1()!=null&&!"".equals(sum.getMemo1())){//memo1有值时不查父当前节点
						sWhere+=" and p_region_no='"+sb.getP_region_no()+"' ";
					}else{
						sWhere+=" and (region_no='"+sb.getP_region_no()+"' or p_region_no='"+sb.getP_region_no()+"') ";
					}
					sb.setP_region_no(null);
				}
				if(!"".equals(sb.getRegion_name())&&sb.getRegion_name()!=null){
					sWhere+=" and (region_name like '%"+sb.getRegion_name()+"%') ";
					sb.setRegion_name(null);
				}
			}
			

			pm.setRows(this.getMapper(SlmBaseRegionMapper.class).findWithCondition(sb, sWhere, " region_no ", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(SlmBaseRegionMapper.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<SlmBaseRegion> sum){
		ResultModel rm=new ResultModel();
		try{
			rm.setState("200");
			rm.setData(this.getMapper(SlmBaseRegionMapper.class).findById(sum.getObject().getRegion_no()));
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 添加数据
	 * 
	 */
	public ResultModel doAddWeb(CommonPage<SlmBaseRegion> sum) {
		ResultModel rm=new ResultModel();
		String strBaseDataNo = "";
		try {
			SlmBaseRegion bd=sum.getObject();
			
			String maxCode=this.getMapper(SlmBaseRegionMapper.class).doMaxCode(bd);
			int count=0;
			
			if (bd.getP_region_no()!= null&&!"".equals(bd.getP_region_no())){ //判断上级是否为空
				if(bd.getP_region_no().length()==1){//上级为洲则下级为3位长度
					if(maxCode.length()>1){
						count=Integer.parseInt(maxCode.substring(maxCode.length()-3,maxCode.length()))+1;
					}else{
						count=Integer.parseInt(maxCode)+1;
					}
					strBaseDataNo = bd.getP_region_no() + String.format("%03d", count);
				}else{
					if(maxCode.length()>1){
						count=Integer.parseInt(maxCode.substring(maxCode.length()-2,maxCode.length()))+1;
					}else{
						count=Integer.parseInt(maxCode)+1;
					}
					strBaseDataNo = bd.getP_region_no() + String.format("%02d", count);
				}
			} else {//洲为1位长度
				count=Integer.parseInt(maxCode)+1;
				bd.setP_region_no("");
				strBaseDataNo = count+"";//String.format("%04d", count);
			}

			bd.setRegion_no(strBaseDataNo);
			SlmBaseRegion obd = new SlmBaseRegion();
			obd.setRegion_no(bd.getRegion_no());
			List<SlmBaseRegion> list2 = this.getMapper(SlmBaseRegionMapper.class).find(obd, 0, 0);
			if (list2 != null && list2.size() > 0) {
				rm.setState("500");
				rm.setMsgInfo("编号已存在");
			}else{
				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(SlmBaseRegionMapper.class).insert(bd);
				rm.setState("200");
			}
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，新增失败!");
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 修改数据
	 * @param br
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<SlmBaseRegion> sum) {
		ResultModel rm=new ResultModel();
		SlmBaseRegion 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(SlmBaseRegionMapper.class).update(obd);
			rm.setState("200");
		} 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<SlmBaseRegion> sum) {
		ResultModel rm=new ResultModel();
		List<SlmBaseRegion> obd = sum.getObjectList();
		for(SlmBaseRegion sbr:obd){
			this.getMapper(SlmBaseRegionMapper.class).doUpdateFlag(sbr);
		}
		rm.setState("200");
		return rm;
	}
}
