/***文档注释***********************************************
 * 作者               :                   易鑫
 * 创建日期      :                   2017.10.24
 * 描述               :        			客户组织机构信息
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Slm.CustomerInformation.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
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.CustomerInformation.mapper.SlmCustomerOrgMapper;
import market.Slm.CustomerInformation.service.SlmCustomerOrgService;
import market.Slm.CustomerInformation.vo.SlmCustomerOrg;

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;

@RemoteService(path="SlmCustomerOrgService")
public class SlmCustomerOrgServiceImpl extends BaseServiceImpl<SlmCustomerOrg,Object> implements SlmCustomerOrgService {
	/*-------------------------Web端逻辑接口-----------------------------*/
	//客户组织机构下的根节点
	public ResultModel doQueryWeb(CommonPage<SlmCustomerOrg> sum){
		ResultModel rm=new ResultModel();
		try{
			SlmCustomerOrg oco=sum.getObject()==null?new SlmCustomerOrg():sum.getObject();
			rm.setData(this.getMapper(SlmCustomerOrgMapper.class).find(oco, 0, 0));
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("服务端异常，未找到数据!");
		}
		return rm;
	}
	
	/**
	 * 查看详情
	 * */
	public ResultModel doQueryDetailWeb(CommonPage<SlmCustomerOrg> sum){
		ResultModel rm=new ResultModel();
		try{
			rm.setState("200");
			rm.setData(this.getMapper(SlmCustomerOrgMapper.class).findById(sum.getObject().getOrg_no()));
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	//组织机构展示
	public ResultModel doQueryChildrenWeb(CommonPage<SlmCustomerOrg> sum){
		ResultModel rm=new ResultModel();
		try{
			List<SlmCustomerOrg> octPreList=new ArrayList<SlmCustomerOrg>();
			List<SlmCustomerOrg> octNextList=new ArrayList<SlmCustomerOrg>();
			if(sum.getObject()!=null){
				SlmCustomerOrg oct=sum.getObject();
				octPreList=this.getMapper(SlmCustomerOrgMapper.class).findWithCondition(null," customer_no='"+oct.getCustomer_no()+"' and (p_org_no='' or p_org_no is null) and valid_flag='1'", null, 0, 0);
				for(SlmCustomerOrg ozt:octPreList){
					octNextList.add(getChildData(ozt));
				}
			}
			rm.setData(octNextList);
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("服务端异常，未找到数据!");
		}
		return rm;
	}
	
	private SlmCustomerOrg getChildData(SlmCustomerOrg ozt){
		if(ozt!=null){
			List<SlmCustomerOrg> childrenlist=this.getMapper(SlmCustomerOrgMapper.class).findWithCondition(null, " customer_no='"+ozt.getCustomer_no()+"' and p_org_no='"+ozt.getOrg_no()+"' and valid_flag='1'", null, 0, 0);
			if(childrenlist!=null && childrenlist.size()>0){
				ozt.setChildren(childrenlist);
				for(SlmCustomerOrg o:childrenlist){
					getChildData(o);
				}
			}
		}
		return ozt;
	}
	
	/**
	 * memo1 为floorOrganizationNo 下级机构编号
	 * memo2 为floorOrganizationName 下级机构名称
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doAddWeb(CommonPage<SlmCustomerOrg> sum) throws Exception{
		ResultModel rm=new ResultModel();
		
		try{
			SlmCustomerOrg cya=sum.getObject();
			if(cya.getP_org_name()!=null &&sum.getMemo2()!=null && cya.getP_org_name().equals(sum.getMemo2())){
				rm.setState("上、下级机构不能相同!");
				return rm;
			}
			
			SlmCustomerOrg organ=new SlmCustomerOrg();
			organ.setCustomer_name(cya.getCustomer_name());
			organ.setOrg_name(cya.getOrg_name());
			List<SlmCustomerOrg> organList=this.getMapper(SlmCustomerOrgMapper.class).find(organ, 0, 0);
			if(organList!=null && organList.size()>0){
				rm.setState("存在同名的组织机构!");
				return rm;
			}
			
			String no=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
			List<SlmCustomerOrg> node=this.getMapper(SlmCustomerOrgMapper.class).findWithCondition(null, " customer_no='"+cya.getCustomer_no()+"' and p_org_no='"+cya.getP_org_no()+"' and valid_flag='1'", null, 0, 0);
			if("".equals(sum.getMemo1()) && "".equals(cya.getP_org_no())){
				if(node.size()>0){
					rm.setState("根节点机构已存在!");
					return rm;
				}else{
					cya.setOrg_no(no);
					InsertOrganization(cya);
				}	
			}else{
				if(!"".equals(sum.getMemo1()) && !"".equals(cya.getOrg_no())){
					rm.setState("不能同时选择上下级机构!");
					return rm;
				}
				if(!"".equals(sum.getMemo1()) && "".equals(cya.getOrg_no())){
						cya.setOrg_no(no);
						InsertOrganization(cya);
						SlmCustomerOrg coz=new SlmCustomerOrg();
						coz.setOrg_no(sum.getMemo1());
						coz.setP_org_no(no);
						coz.setP_org_name(cya.getOrg_name());
						this.getMapper(SlmCustomerOrgMapper.class).update(coz);	
				}
				if("".equals(sum.getMemo1()) && !"".equals(cya.getP_org_no())){
					cya.setOrg_no(no);
					InsertOrganization(cya);
				}
			}
			
			rm.setState("200");
		}
		catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
		return rm;
	}
	
	private void InsertOrganization(SlmCustomerOrg cya){
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		cya.setCreate_man_no(userId);
		cya.setCreate_man_name(userName);
		cya.setCreate_time(new Date());
		cya.setValid_flag("1");
		this.getMapper(SlmCustomerOrgMapper.class).insert(cya);
		//绘制时间
		SlmCustomerOrg odraw=new SlmCustomerOrg();
		odraw.setCustomer_no(cya.getCustomer_no());
		odraw.setDraw_time(new Date());
		this.getMapper(SlmCustomerOrgMapper.class).doUpdate(odraw);
	}
	
	/**
	 * memo1 oldOrganizationName 原有组织机构名称
	 * memo2 floorOrganizationNo 下级组织机构编号
	 * memo3 floorOrganizationName 下级组织机构名称
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doUpdateWeb(CommonPage<SlmCustomerOrg> sum) throws Exception{	
		ResultModel rm=new ResultModel();
		SlmCustomerOrg ckm=sum.getObject();
		if(!ckm.getOrg_name().equals(sum.getMemo1())){
			SlmCustomerOrg organ=new SlmCustomerOrg();
			organ.setCustomer_no(ckm.getCustomer_no());
			organ.setOrg_name(ckm.getOrg_name());
			List<SlmCustomerOrg> organList=this.getMapper(SlmCustomerOrgMapper.class).find(organ, 0, 0);
			if(organList!=null && organList.size()>0){
				rm.setState("存在同名的组织机构!");
				return rm;
			}
		}else{
			if(ckm.getOrg_name().equals(ckm.getP_org_name()) || ckm.getOrg_name().equals(sum.getMemo3())){
				rm.setState("上、下级机构不能为本身!");
				return rm;
			}
		}

		try{
			SlmCustomerOrg coz=this.getMapper(SlmCustomerOrgMapper.class).findById(ckm.getOrg_no());
			if("".equals(sum.getMemo2()) && "".equals(ckm.getP_org_no())){
				if("".equals(coz.getP_org_no())){
					UpdateOrganization(ckm,sum.getMemo1());
				}else{
					rm.setState("非根节点需上或下级机构!");
					return rm;
				}
			}else{
				if(!"".equals(sum.getMemo2()) && !"".equals(ckm.getP_org_no())){
					rm.setState("不能同时选择上下级机构!");
					return rm;
				}
				if(!"".equals(sum.getMemo2()) && "".equals(ckm.getP_org_no())){
					ckm.setP_org_no("");
					ckm.setP_org_name("");
					UpdateOrganization(ckm,sum.getMemo1());
					SlmCustomerOrg zat=new SlmCustomerOrg();
					zat.setOrg_no(sum.getMemo2());
					zat.setP_org_no(ckm.getOrg_no());
					zat.setP_org_name(ckm.getOrg_name());
					this.getMapper(SlmCustomerOrgMapper.class).update(zat);
				}
				if("".equals(sum.getMemo2()) && !"".equals(ckm.getP_org_no())){
					UpdateOrganization(ckm,sum.getMemo1());
				}
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
			throw new Exception(e.getMessage());
		}
		
		return rm;
	}
	
	private void UpdateOrganization(SlmCustomerOrg ckm,String oldOrganizationName){
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
		ckm.setUpdate_man_name(userName);
		ckm.setUpdate_man_no(userId);
		ckm.setUpdate_time(new Date());
		this.getMapper(SlmCustomerOrgMapper.class).update(ckm);
		
		if(!ckm.getOrg_name().equals(oldOrganizationName)){
			SlmCustomerOrg cyz=new SlmCustomerOrg();
			cyz.setP_org_no(ckm.getOrg_no());
			cyz.setCustomer_name(ckm.getCustomer_no());
			List<SlmCustomerOrg> cyzlist=this.getMapper(SlmCustomerOrgMapper.class).find(cyz, 0, 0);
			if(cyzlist!=null && cyzlist.size()>0){
				cyz.setP_org_no(ckm.getP_org_name());
				this.getMapper(SlmCustomerOrgMapper.class).update(cyz);
			}
		}
		
		SlmCustomerOrg odraw=new SlmCustomerOrg();
		odraw.setCustomer_no(ckm.getCustomer_no());
		odraw.setDraw_time(new Date());
		this.getMapper(SlmCustomerOrgMapper.class).doUpdate(odraw);
	}
	
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doDeleteWeb(CommonPage<SlmCustomerOrg> sum) throws Exception{
		ResultModel rm=new ResultModel();
		SlmCustomerOrg cya=sum.getObject();
		try{
			SlmCustomerOrg coz=new SlmCustomerOrg();
			coz.setCustomer_no(cya.getCustomer_no());
			coz.setP_org_no(cya.getOrg_no());
			List<SlmCustomerOrg> cozList=this.getMapper(SlmCustomerOrgMapper.class).find(coz, 0,0);
			if(cozList!=null && cozList.size()>0){
				rm.setState("500");
				rm.setMsgInfo("存在下属部门,不能删除!");
				return rm;
			}
			
			//做物理删除
			this.getMapper(SlmCustomerOrgMapper.class).doDelete(cya);
			
			SlmCustomerOrg odraw=new SlmCustomerOrg();
			odraw.setCustomer_no(cya.getCustomer_no());
			odraw.setDraw_time(new Date());
			this.getMapper(SlmCustomerOrgMapper.class).doUpdate(odraw);
			rm.setState("200");
		}
		catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
			throw new Exception(e.getMessage());
		}
	    return rm;
	}
	/*-------------------------Web端逻辑接口-----------------------------*/
}
