/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-08-23
 * 描述            ：销售区域分点信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Slm.MarketMap.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.MarketMap.mapper.SlmBaseSaleareaChildMapper;
import market.Slm.MarketMap.mapper.SlmSaleareaPersonMapper;
import market.Slm.MarketMap.service.SlmBaseSaleareaChildService;
import market.Slm.MarketMap.vo.SlmBaseSaleareaChild;
import market.Slm.MarketMap.vo.SlmSaleareaPerson;

import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;
import com.ibm.icu.text.SimpleDateFormat;

@RemoteService(path="SlmBaseSaleareaChildService")
public class SlmBaseSaleareaChildServiceImpl extends BaseServiceImpl<SlmBaseSaleareaChild,Object> implements SlmBaseSaleareaChildService {
	/**
	 * 查询
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<SlmBaseSaleareaChild> oci){
		ResultModel rm=new ResultModel();
		try{
			SlmBaseSaleareaChild child=oci.getObject()==null?new SlmBaseSaleareaChild():oci.getObject();
			List<SlmBaseSaleareaChild> lc = this.getMapper(SlmBaseSaleareaChildMapper.class).findWithCondition(child, "", "p_sale_area_no,sale_area_name", 0, 0);
			rm.setState("200");
			rm.setData(lc);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败："+ex.getMessage());
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 新增
	 * @param oci
	 * @return
	 */
	public ResultModel doAddWeb(CommonPage<SlmBaseSaleareaChild> oci){
		ResultModel rm=new ResultModel();
		try{
			SlmBaseSaleareaChild sbc=new SlmBaseSaleareaChild();
			sbc.setSale_area_name(oci.getObject().getSale_area_name());
			sbc.setP_sale_area_no(oci.getObject().getP_sale_area_no());
			int icount=this.getMapper(SlmBaseSaleareaChildMapper.class).count(sbc);
			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);
			String id=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
			oci.getObject().setSale_area_no(id);
			oci.getObject().setCreate_man_no(userId);
			oci.getObject().setCreate_man_name(userName);
			oci.getObject().setCreate_time(new Date());
			oci.getObject().setValid_flag("1");
			this.getMapper(SlmBaseSaleareaChildMapper.class).insert(oci.getObject());
			
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("新增失败："+ex.getMessage());
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 修改
	 * @param oci
	 * @return
	 */
	@Transactional(value="tm" ,rollbackFor=Exception.class)
	public ResultModel doUpdateWeb(CommonPage<SlmBaseSaleareaChild> oci){
		ResultModel rm=new ResultModel();

		SlmBaseSaleareaChild sbc=this.getMapper(SlmBaseSaleareaChildMapper.class).findById(oci.getObject().getSale_area_no());
		if(sbc==null){
			rm.setState("500");
			rm.setData("数据已被删除");
			return rm;
		}
		if(!oci.getObject().getSale_area_name().equals(sbc.getSale_area_name())){
			SlmBaseSaleareaChild sb=new SlmBaseSaleareaChild();
			sb.setP_sale_area_no(oci.getObject().getP_sale_area_no());
			sb.setSale_area_name(oci.getObject().getSale_area_name());
			//int icount=this.getMapper(SlmBaseSaleareaChildMapper.class).count(sbc);
			int icount=this.getMapper(SlmBaseSaleareaChildMapper.class).doQueryCount(oci.getObject().getSale_area_name());
			if(icount>0){
				rm.setState("500");
				rm.setMsgInfo("存在相同名称的区域分项");
				return rm;
			} else {
				SlmSaleareaPerson ssp=new SlmSaleareaPerson();
				ssp.setSale_area_no(sb.getSale_area_no());
				List<SlmSaleareaPerson> lsp=this.getMapper(SlmSaleareaPersonMapper.class).find(ssp, 0, 0);
				if(lsp!=null&&lsp.size()>0){
					for(SlmSaleareaPerson sp:lsp){
						sp.setSale_area_name(oci.getObject().getSale_area_name());
						this.getMapper(SlmSaleareaPersonMapper.class).update(sp);
					}
				}
			}
		}
		
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		oci.getObject().setUpdate_man_no(userId);
		oci.getObject().setUpdate_man_name(userName);
		oci.getObject().setUpdate_time(new Date());
		this.getMapper(SlmBaseSaleareaChildMapper.class).update(oci.getObject());
		
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 删除
	 * @param oci
	 * @return
	 */
	public ResultModel doDeleteWeb(CommonPage<SlmBaseSaleareaChild> oci){
		ResultModel rm=new ResultModel();
		try{
			SlmBaseSaleareaChild sbc=this.getMapper(SlmBaseSaleareaChildMapper.class).findById(oci.getObject().getSale_area_no());
			if(sbc==null){
				rm.setState("200");
				return rm;
			}
			SlmSaleareaPerson ssp=new SlmSaleareaPerson();
			ssp.setSale_area_no(oci.getObject().getSale_area_no());
			int icount=this.getMapper(SlmSaleareaPersonMapper.class).count(ssp);
			if(icount>0){
				rm.setState("500");
				rm.setMsgInfo("该区域分项已与人员信息关联，请先删除关联信息");
				return rm;
			}
			rm.setState("200");
			this.getMapper(SlmBaseSaleareaChildMapper.class).delete(oci.getObject().getSale_area_no());
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("删除失败："+ex.getMessage());
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 修改状态
	 * memo1为 1启用，0停用
	 * @param oci
	 * @return
	 */
	@Transactional(value="tm" ,rollbackFor=Exception.class)
	public ResultModel doValidFlag(CommonPage<SlmBaseSaleareaChild> oci){
		ResultModel rm=new ResultModel();
		
		for(SlmBaseSaleareaChild ssc:oci.getObjectList()){
			SlmBaseSaleareaChild child=new SlmBaseSaleareaChild();
			child.setValid_flag(oci.getMemo1());
			child.setSale_area_no(ssc.getSale_area_no());
			this.getMapper(SlmBaseSaleareaChildMapper.class).update(child);
		}
		return rm;
	}
}
