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

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.SlmBaseSaleareaInfoMapper;
import market.Slm.BaseInformation.service.SlmBaseSaleareaInfoService;
import market.Slm.BaseInformation.vo.SlmBaseSaleareaInfo;

import org.apache.commons.lang.StringUtils;
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="SlmBaseSaleareaInfoService")
public class SlmBaseSaleareaInfoServiceImpl extends BaseServiceImpl<SlmBaseSaleareaInfo,Object> implements SlmBaseSaleareaInfoService {
	/**
	 * 查询
	 * memo1有值时不查父当前节点
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<SlmBaseSaleareaInfo> sum) {
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		try{
			PageModel<SlmBaseSaleareaInfo> pm = new PageModel<SlmBaseSaleareaInfo>(pageIndex,pageSize);
			SlmBaseSaleareaInfo sb=sum.getObject();
			String sWhere=" 1=1 ";
			if(sb!=null){
				if(sb.getP_sale_area_no()!=null&&!"".equals(sb.getP_sale_area_no())){
					if(sum.getMemo1()!=null&&!"".equals(sum.getMemo1())){//memo1有值时不查父当前节点
						sWhere+=" and p_sale_area_no='"+sb.getP_sale_area_no()+"' ";;
						sb.setP_sale_area_no(null);
					}else{
						sWhere+=" and (sale_area_no='"+sb.getP_sale_area_no()+"' or p_sale_area_no='"+sb.getP_sale_area_no()+"') ";
						sb.setP_sale_area_no(null);
					}
				}
				if (StringUtils.isNotEmpty(sum.getMemo1())) {
					sWhere += " and valid_flag = '"+sum.getMemo1()+"'";
				}
				
				if(sb.getSale_area_name()!=null&&!"".equals(sb.getSale_area_name())){
					sWhere+=" and sale_area_name like '%"+sb.getSale_area_name()+"%' ";
					sb.setSale_area_name(null);
				}
			}
	
			pm.setRows(this.getMapper(SlmBaseSaleareaInfoMapper.class).findWithCondition(sb, sWhere, " sale_area_no ", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(SlmBaseSaleareaInfoMapper.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<SlmBaseSaleareaInfo> sum){
		ResultModel rm=new ResultModel();
		try{
			rm.setState("200");
			rm.setData(this.getMapper(SlmBaseSaleareaInfoMapper.class).findById(sum.getObject().getSale_area_no()));
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 添加数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doAddWeb(CommonPage<SlmBaseSaleareaInfo> sum) {
		ResultModel rm=new ResultModel();
		try {
			SlmBaseSaleareaInfo bd=sum.getObject();
			SlmBaseSaleareaInfo area=new SlmBaseSaleareaInfo();
			area.setP_sale_area_no(bd.getP_sale_area_no());
			area.setSale_area_name(bd.getSale_area_name());
			int number=this.getMapper(SlmBaseSaleareaInfoMapper.class).count(area);
			if(number>0){
				rm.setState("500");
				rm.setMsgInfo("已存在相同的区域名称");
				return rm;
			}
			String maxCode=this.getMapper(SlmBaseSaleareaInfoMapper.class).doMaxCode(bd);
			int count=0;
			if (bd.getP_sale_area_no()!= null&&!"".equals(bd.getP_sale_area_no())){ //判断上级是否为空
				if(maxCode.length()>1){
					count=Integer.parseInt(maxCode.substring(maxCode.length()-2,maxCode.length()))+1;
				}else{
					count=Integer.parseInt(maxCode)+1;
				}
				bd.setSale_area_no(bd.getP_sale_area_no() +(String.format("%02d", count)));
			} else {
				count=Integer.parseInt(maxCode)+1;
				bd.setSale_area_no(String.format("%04d", count));
			}
			
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			bd.setPy(PbMethod.getPYIndexStr(bd.getSale_area_name(), true));
			bd.setCreate_man_no(userId);
			bd.setCreate_man_name(userName);
			bd.setValid_flag("1");
			bd.setCreate_time(new Date());
			this.getMapper(SlmBaseSaleareaInfoMapper.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<SlmBaseSaleareaInfo> sum) {
		ResultModel rm=new ResultModel();
		SlmBaseSaleareaInfo obd = sum.getObject();
		try {
			SlmBaseSaleareaInfo area=new SlmBaseSaleareaInfo();
			area.setP_sale_area_no(obd.getP_sale_area_no());
			area.setSale_area_name(obd.getSale_area_name());
			List<SlmBaseSaleareaInfo> areaList=this.getMapper(SlmBaseSaleareaInfoMapper.class).find(area,0,0);
			if(areaList.size()>0&&!areaList.get(0).getSale_area_no().equals(obd.getSale_area_no())){
				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);
			obd.setPy(PbMethod.getPYIndexStr(obd.getSale_area_name(), true));
			obd.setUpdate_man_no(userId);
			obd.setUpdate_man_name(userName);
			obd.setUpdate_time(new Date());
			this.getMapper(SlmBaseSaleareaInfoMapper.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<SlmBaseSaleareaInfo> sum) {
		ResultModel rm=new ResultModel();
		List<SlmBaseSaleareaInfo> obd = sum.getObjectList();
		for(SlmBaseSaleareaInfo sbsi:obd){
			this.getMapper(SlmBaseSaleareaInfoMapper.class).doUpdateFlag(sbsi);
		}
		rm.setState("200");
		return rm;
	}
}
