Browse Source

渠道相关接口及渠道管理接口

guanglong 3 years ago
parent
commit
a96faf7c8d

+ 114 - 0
mp-admin/src/main/java/com/qs/mp/web/controller/api/admin/ChannelTreeController.java

@@ -0,0 +1,114 @@
+package com.qs.mp.web.controller.api.admin;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.qs.mp.channel.domain.Channel;
+import com.qs.mp.channel.domain.vo.ChannelVO;
+import com.qs.mp.channel.service.IChannelService;
+import com.qs.mp.channel.service.IChannelUserRelService;
+import com.qs.mp.common.core.domain.AjaxResult;
+import com.qs.mp.common.core.page.TableDataInfo;
+import com.qs.mp.system.domain.SysUser;
+import com.qs.mp.system.service.ISysUserService;
+import com.qs.mp.utils.SecurityUtils;
+import com.qs.mp.web.controller.common.BaseApiController;
+import io.swagger.annotations.Api;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @auther zhongcp
+ * @create 2022-02-28 16:17:48
+ * @describe 渠道管理前端控制器
+ */
+@Api("渠道管理API")
+@RestController
+@RequestMapping("/api/v1/mp/admin/channel/*")
+@Component
+public class ChannelTreeController extends BaseApiController {
+
+	@Autowired
+	private IChannelService channelService;
+	
+	@Autowired
+	private IChannelUserRelService channelUserRelService;
+	
+	@Autowired
+	private ISysUserService sysUserService;
+
+	/**
+	 * 获取我的下级渠道列表信息,支持翻页
+	 *
+	 * @return
+	 */
+	@PostMapping("/tree")
+	public AjaxResult treeChannel(@RequestBody Channel channel) {
+		Long channelId = SecurityUtils.getLoginUser().getChannelId();
+		List<ChannelVO> list = new ArrayList<ChannelVO>();
+		if(null != channelId) {
+			QueryWrapper<Channel> queryWrapper = new QueryWrapper<>();
+			queryWrapper.eq("t1.parent_id", channelId);
+			queryWrapper.gt("t1.level", 0);
+			queryWrapper.orderByAsc("t1.channel_id");
+			list = channelService.selectChannelVoList(queryWrapper);
+			if(null != list && list.size() > 0) {
+				for(ChannelVO channelVO : list) {
+					if(null != channelVO && StringUtils.isNotBlank(channelVO.getChannelNo())) {
+//						int siteCnt = channelService.getChannelSiteCnt(channelVO.getChannelNo());
+//						int userCnt = channelUserRelService.getChannelUserCnt(channelVO.getChannelNo());
+//						channelVO.setSiteCnt(siteCnt);
+//						channelVO.setUserCnt(userCnt);
+						// 查询子渠道数量
+						int childCnt = channelService.count(
+								new LambdaQueryWrapper<Channel>().eq(Channel::getParentId, channelVO.getChannelId())
+								.gt(Channel::getLevel, 0));
+						channelVO.setChildCnt(childCnt);
+					}
+				}
+			}
+		}
+		return AjaxResult.success(list);
+	}
+	
+	/**
+	 * 获取我的下级渠道列表信息,支持翻页
+	 *
+	 * @return
+	 */
+	@PostMapping("/list")
+	public TableDataInfo listChannel(@RequestBody Channel channel) {
+		Long channelId = SecurityUtils.getLoginUser().getChannelId();
+		List<ChannelVO> list = new ArrayList<ChannelVO>();
+		if(null != channelId) {
+			channel.setParentId(channelId);
+			startPage();
+			QueryWrapper<Channel> queryWrapper = new QueryWrapper<>();
+			queryWrapper.eq("t1.parent_id", channelId);
+			queryWrapper.gt("t1.level", 0);
+			queryWrapper.orderByAsc("t1.channel_id");
+			list = channelService.selectChannelVoList(queryWrapper);
+			if(null != list && list.size() > 0) {
+				for(ChannelVO channelVO : list) {
+					if(null != channelVO && StringUtils.isNotBlank(channelVO.getChannelNo())) {
+						int siteCnt = channelService.getChannelSiteCnt(channelVO.getChannelNo());
+						int userCnt = channelUserRelService.getChannelUserCnt(channelVO.getChannelNo());
+						channelVO.setSiteCnt(siteCnt);
+						channelVO.setUserCnt(userCnt);
+						// 查询用户信息
+						SysUser sysUser = sysUserService.selectUserById(channelId);
+						channelVO.setSysUser(sysUser);
+
+					}
+				}
+			}
+		}
+		return getDataTable(list);
+	}
+}

+ 184 - 9
mp-admin/src/main/java/com/qs/mp/web/controller/api/channel/ChannelController.java

@@ -11,6 +11,8 @@ import com.qs.mp.channel.domain.vo.ChannelOperDataVO;
 import com.qs.mp.channel.domain.vo.ChannelVO;
 import com.qs.mp.channel.domain.vo.ChannelVO;
 import com.qs.mp.channel.service.IChannelCommissionService;
 import com.qs.mp.channel.service.IChannelCommissionService;
 import com.qs.mp.channel.service.IChannelService;
 import com.qs.mp.channel.service.IChannelService;
+import com.qs.mp.channel.service.IChannelUserRelService;
+import com.qs.mp.common.constant.UserConstants;
 import com.qs.mp.common.core.domain.AjaxResult;
 import com.qs.mp.common.core.domain.AjaxResult;
 import com.qs.mp.common.core.page.TableDataInfo;
 import com.qs.mp.common.core.page.TableDataInfo;
 import com.qs.mp.common.enums.ChannelCertifyStatusEnum;
 import com.qs.mp.common.enums.ChannelCertifyStatusEnum;
@@ -18,6 +20,7 @@ import com.qs.mp.common.enums.ChannelVerifyStatusEnum;
 import com.qs.mp.common.enums.ErrorCodeEnum;
 import com.qs.mp.common.enums.ErrorCodeEnum;
 import com.qs.mp.common.exception.ServiceException;
 import com.qs.mp.common.exception.ServiceException;
 import com.qs.mp.common.utils.DateUtils;
 import com.qs.mp.common.utils.DateUtils;
+import com.qs.mp.system.service.ISysUserService;
 import com.qs.mp.utils.SecurityUtils;
 import com.qs.mp.utils.SecurityUtils;
 import com.qs.mp.web.controller.common.BaseApiController;
 import com.qs.mp.web.controller.common.BaseApiController;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.Api;
@@ -57,6 +60,12 @@ public class ChannelController extends BaseApiController {
 	
 	
 	@Autowired
 	@Autowired
 	private IChannelCommissionService channelCommissionService;
 	private IChannelCommissionService channelCommissionService;
+	
+	@Autowired
+	private IChannelUserRelService channelUserRelService;
+	
+    @Autowired
+    private ISysUserService userService;
 
 
 	@Autowired
 	@Autowired
 	private MapperFacade mapperFacade;
 	private MapperFacade mapperFacade;
@@ -73,7 +82,21 @@ public class ChannelController extends BaseApiController {
 		if(null != channelId) {
 		if(null != channelId) {
 			channel.setParentId(channelId);
 			channel.setParentId(channelId);
 			startPage();
 			startPage();
-			list = channelService.selectChannelVoList(channel,"channel");
+			QueryWrapper<Channel> queryWrapper = new QueryWrapper<>();
+			queryWrapper.eq("t1.parent_id", channel.getParentId());
+			queryWrapper.gt("t1.level", 0);
+			queryWrapper.orderByAsc("t1.channel_id");
+			list = channelService.selectChannelVoList(queryWrapper);
+			if(null != list && list.size() > 0) {
+				for(ChannelVO channelVO : list) {
+					if(null != channelVO && StringUtils.isNotBlank(channelVO.getChannelNo())) {
+						int siteCnt = channelService.getChannelSiteCnt(channelVO.getChannelNo());
+						int userCnt = channelUserRelService.getChannelUserCnt(channelVO.getChannelNo());
+						channelVO.setSiteCnt(siteCnt);
+						channelVO.setUserCnt(userCnt);
+					}
+				}
+			}
 		}
 		}
 		return getDataTable(list);
 		return getDataTable(list);
 	}
 	}
@@ -119,8 +142,36 @@ public class ChannelController extends BaseApiController {
 		Long channelId = SecurityUtils.getLoginUser().getChannelId();
 		Long channelId = SecurityUtils.getLoginUser().getChannelId();
 		Channel channel = mapperFacade.map(channelParam, Channel.class);
 		Channel channel = mapperFacade.map(channelParam, Channel.class);
 		channel.setParentId(channelId);
 		channel.setParentId(channelId);
-		channelService.saveChannel(channel, "channel");
-		return AjaxResult.success("子渠道'" + channel.getName() + "'新增成功");
+		// 1、校验名称是否重复、手机号是否存在(渠道表)
+		LambdaQueryWrapper<Channel> queryWrapper = new LambdaQueryWrapper<>();
+		queryWrapper.eq(Channel::getName, channel.getName());
+		queryWrapper.gt(Channel::getLevel, 1);
+		int nameCount = channelService.count(queryWrapper);
+		if(nameCount > 0) {
+			return AjaxResult.error("渠道名称" + channel.getName() + "已存在!");
+		}
+		int mobileCount = channelService.count(
+		        new LambdaQueryWrapper<Channel>().eq(Channel::getMobile, channel.getMobile()));
+		if(mobileCount > 0) {
+			 return AjaxResult.error("手机号码" + channel.getMobile() + "已注册!");
+		}
+		// 2.校验佣金比例,不能高于其父渠道的佣金比例
+		Channel parentChannel = channelService.getById(channel.getParentId());
+		if(null != parentChannel && null != parentChannel.getCommRate()) {
+			 if(channel.getCommRate().compareTo(parentChannel.getCommRate()) > 0) {
+				 return AjaxResult.error("佣金比例不能高于父渠道的佣金比例");
+			 }
+		}
+		channel.setLevel(parentChannel.getLevel()+1);
+		// 3.插入数据
+		channel.setChannelNo(parentChannel.getChannelNo()+".");
+		try {
+			channelService.saveChannel(channel,"channel");
+		} catch (Exception e) {
+			return AjaxResult.error("渠道'" + channel.getName() + "'新增失败" + e.getMessage());
+		}
+		
+		return AjaxResult.success("渠道'" + channel.getName() + "'新增成功");
 	}
 	}
 
 
 	/**
 	/**
@@ -136,8 +187,61 @@ public class ChannelController extends BaseApiController {
 			return error(ErrorCodeEnum.ERROR_CODE_1001);
 			return error(ErrorCodeEnum.ERROR_CODE_1001);
 		}
 		}
 		Channel channel = mapperFacade.map(channelParam, Channel.class);
 		Channel channel = mapperFacade.map(channelParam, Channel.class);
-		channelService.updateChannel(channel, "channel");
-		return AjaxResult.success("子渠道'" + channel.getName() + "'编辑成功");
+		// 1、校验修改子渠道是否为当前用户的子渠道
+		Channel oldChannel = channelService.getById(channel.getChannelId());
+		if(null == oldChannel || null == oldChannel.getChannelId()) {
+			return AjaxResult.error("渠道'" + oldChannel.getName() + "'编辑失败,渠道ID异常");
+		}
+		Long channelId = SecurityUtils.getLoginUser().getChannelId();
+		if(!oldChannel.getParentId().equals(channelId)) {
+			return AjaxResult.error("渠道'" + oldChannel.getName() + "'编辑失败,非当前用户子渠道");
+		}
+		// 2.校验名称是否重复、手机号是否存在(渠道表);
+		if(!channel.getName().equals(oldChannel.getName())) {
+			LambdaQueryWrapper<Channel> queryWrapper = new LambdaQueryWrapper<>();
+			queryWrapper.eq(Channel::getName, channel.getName());
+			queryWrapper.gt(Channel::getLevel, 1);
+			int nameCount = channelService.count(queryWrapper);
+			if(nameCount > 0) {
+				return AjaxResult.error("渠道名称" + channel.getName() + "已存在!");
+			}
+		}
+		boolean mobileChange = false;  // 手机号码是否有变更
+		if(!channel.getMobile().equals(oldChannel.getMobile())) {
+			int mobileCount = channelService.count(
+			        new LambdaQueryWrapper<Channel>().eq(Channel::getMobile, channel.getMobile()));
+			if(mobileCount > 0) {
+				return AjaxResult.error("手机号码" + channel.getMobile() + "已注册!");
+			}
+			if(UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(channel.getMobile()))) {
+				return AjaxResult.error("手机号码" + channel.getMobile() + "已注册!");
+			}
+			mobileChange = true;
+		}
+		// 3.校验佣金比例,不能高于其父渠道的佣金比例,不能低于其子渠道的最大佣金比例
+		Channel parentChannel = channelService.getById(oldChannel.getParentId());
+		if(null != parentChannel && null != parentChannel.getCommRate()) {
+			 if(channel.getCommRate().compareTo(parentChannel.getCommRate()) > 0) {
+				 return AjaxResult.error("佣金比例不能高于父渠道的佣金比例");
+			 }
+		}
+		// 查询子渠道的最大佣金比例
+		QueryWrapper<Channel> queryWrapper = new QueryWrapper<Channel>();
+		queryWrapper.select("IFNULL(max(comm_rate),0) as commRate");
+		queryWrapper.lambda().eq(Channel::getParentId, channel.getChannelId());
+		Map<String, Object> map = channelService.getMap(queryWrapper);
+		if(null != map && map.containsKey("commRate")) {
+			BigDecimal commRate = new BigDecimal(map.get("commRate").toString());
+			if(!commRate.equals(BigDecimal.ZERO) && channel.getCommRate().compareTo(commRate) < 0) {
+				return AjaxResult.error("不能低于其子渠道的最大佣金比例");
+			}
+		}
+		try {
+			channelService.updateChannel(channel, mobileChange);
+		} catch (Exception e) {
+			return AjaxResult.error(e.getMessage());
+		}
+		return AjaxResult.success("渠道'" + channel.getName() + "'编辑成功");
 	}
 	}
 	
 	
 	
 	
@@ -220,7 +324,16 @@ public class ChannelController extends BaseApiController {
 		if(null != channelId) {
 		if(null != channelId) {
 			channel.setParentId(channelId);
 			channel.setParentId(channelId);
 			startPage();
 			startPage();
-			list = channelService.selectChannelVoList(channel,"channel");
+			QueryWrapper<Channel> queryWrapper = new QueryWrapper<>();
+			queryWrapper.eq("t1.parent_id", channel.getParentId());
+			queryWrapper.eq("t1.level", 0);
+			if(null != channel.getVerifyStatus()) {
+				queryWrapper.eq("t1.verify_status", channel.getVerifyStatus());
+			}
+			if(null != channel.getCertifyStatus()) {
+				queryWrapper.eq("t1.certify_status", channel.getCertifyStatus());
+			}
+			list = channelService.selectChannelVoList(queryWrapper);
 		}
 		}
 		return getDataTable(list);
 		return getDataTable(list);
 	}
 	}
@@ -267,10 +380,33 @@ public class ChannelController extends BaseApiController {
 
 
 		Channel channel = mapperFacade.map(channelParam, Channel.class);
 		Channel channel = mapperFacade.map(channelParam, Channel.class);
 		channel.setParentId(channelId);
 		channel.setParentId(channelId);
+		// 1、校验名称是否重复、手机号是否存在(渠道表)
+		LambdaQueryWrapper<Channel> queryWrapper = new LambdaQueryWrapper<>();
+		queryWrapper.eq(Channel::getName, channel.getName());
+		queryWrapper.eq(Channel::getLevel, 0);
+		int nameCount = channelService.count(queryWrapper);
+		if(nameCount > 0) {
+			return AjaxResult.error("经销商名称" + channel.getName() + "已存在!");
+		}
+		int mobileCount = channelService.count(
+		        new LambdaQueryWrapper<Channel>().eq(Channel::getMobile, channel.getMobile()));
+		if(mobileCount > 0) {
+			 return AjaxResult.error("手机号码" + channel.getMobile() + "已注册!");
+		}
+		// 2.校验佣金比例,不能高于其父渠道的佣金比例
+		Channel parentChannel = channelService.getById(channel.getParentId());
+		if(null != parentChannel && null != parentChannel.getCommRate()) {
+			 if(channel.getCommRate().compareTo(parentChannel.getCommRate()) > 0) {
+				 return AjaxResult.error("佣金比例不能高于父渠道的佣金比例");
+			 }
+		}
+		channel.setLevel(0);
+		// 3.插入数据
+		channel.setChannelNo(parentChannel.getChannelNo()+".");
 		try {
 		try {
 			channelService.saveChannel(channel,"site");
 			channelService.saveChannel(channel,"site");
 		} catch (Exception e) {
 		} catch (Exception e) {
-			return AjaxResult.error("经销商'" + channel.getName() + "'新增失败");
+			return AjaxResult.error("经销商'" + channel.getName() + "'新增失败"+e.getMessage());
 		}
 		}
 		return AjaxResult.success("经销商'" + channel.getName() + "'新增成功");
 		return AjaxResult.success("经销商'" + channel.getName() + "'新增成功");
 	}
 	}
@@ -288,8 +424,48 @@ public class ChannelController extends BaseApiController {
 			return error(ErrorCodeEnum.ERROR_CODE_1001);
 			return error(ErrorCodeEnum.ERROR_CODE_1001);
 		}
 		}
 		Channel channel = mapperFacade.map(channelParam, Channel.class);
 		Channel channel = mapperFacade.map(channelParam, Channel.class);
+		
+		// 1、校验修改子渠道是否为当前用户的子渠道
+		Channel oldChannel = channelService.getById(channel.getChannelId());
+		if(null == oldChannel || null == oldChannel.getChannelId()) {
+			return AjaxResult.error("经销商'" + oldChannel.getName() + "'编辑失败,渠道ID异常");
+		}
+		Long channelId = SecurityUtils.getLoginUser().getChannelId();
+		if(!oldChannel.getParentId().equals(channelId)) {
+			return AjaxResult.error("经销商'" + oldChannel.getName() + "'编辑失败,非当前用户子渠道");
+		}
+		// 2.校验名称是否重复、手机号是否存在(渠道表);
+		if(!channel.getName().equals(oldChannel.getName())) {
+			
+			LambdaQueryWrapper<Channel> queryWrapper = new LambdaQueryWrapper<>();
+			queryWrapper.eq(Channel::getName, channel.getName());
+			queryWrapper.eq(Channel::getLevel, 0);
+			int nameCount = channelService.count(queryWrapper);
+			if(nameCount > 0) {
+				return AjaxResult.error("经销商名称" + channel.getName() + "已存在!");
+			}
+		}
+		boolean mobileChange = false;  // 手机号码是否有变更
+		if(!channel.getMobile().equals(oldChannel.getMobile())) {
+			int mobileCount = channelService.count(
+			        new LambdaQueryWrapper<Channel>().eq(Channel::getMobile, channel.getMobile()));
+			if(mobileCount > 0) {
+				return AjaxResult.error("手机号码" + channel.getMobile() + "已注册!");
+			}
+			if(UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(channel.getMobile()))) {
+				return AjaxResult.error("手机号码" + channel.getMobile() + "已注册!");
+			}
+			mobileChange = true;
+		}
+		// 3.校验佣金比例,不能高于其父渠道的佣金比例,不能低于其子渠道的最大佣金比例
+		Channel parentChannel = channelService.getById(oldChannel.getParentId());
+		if(null != parentChannel && null != parentChannel.getCommRate()) {
+			 if(channel.getCommRate().compareTo(parentChannel.getCommRate()) > 0) {
+				 return AjaxResult.error("佣金比例不能高于父渠道的佣金比例");
+			 }
+		}
 		try {
 		try {
-			channelService.updateChannel(channel,"site");
+			channelService.updateChannel(channel, mobileChange);
 		} catch (Exception e) {
 		} catch (Exception e) {
 			return AjaxResult.error("经销商'" + channel.getName() + "'编辑失败");
 			return AjaxResult.error("经销商'" + channel.getName() + "'编辑失败");
 		}
 		}
@@ -404,7 +580,6 @@ public class ChannelController extends BaseApiController {
 		Map<String, Object> map = channelCommissionService.getMap(queryWrapper);
 		Map<String, Object> map = channelCommissionService.getMap(queryWrapper);
 		if(null != map && map.containsKey("commAmt")) {
 		if(null != map && map.containsKey("commAmt")) {
 			BigDecimal commAmt = new BigDecimal(map.get("commAmt").toString());
 			BigDecimal commAmt = new BigDecimal(map.get("commAmt").toString());
-			
 			ChannelOperDataVO channelOperDataVO = new ChannelOperDataVO();
 			ChannelOperDataVO channelOperDataVO = new ChannelOperDataVO();
 			channelOperDataVO.setCommAmt(commAmt.longValue());
 			channelOperDataVO.setCommAmt(commAmt.longValue());
 			channelVO.setOperData(channelOperDataVO);
 			channelVO.setOperData(channelOperDataVO);

+ 21 - 1
mp-admin/src/test/java/com/qs/mp/service/ChannelServiceTest.java

@@ -1,13 +1,16 @@
 package com.qs.mp.service;
 package com.qs.mp.service;
 
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.qs.mp.MpApplication;
 import com.qs.mp.MpApplication;
 import com.qs.mp.channel.domain.Channel;
 import com.qs.mp.channel.domain.Channel;
 import com.qs.mp.channel.domain.vo.ChannelVO;
 import com.qs.mp.channel.domain.vo.ChannelVO;
 import com.qs.mp.channel.service.IChannelService;
 import com.qs.mp.channel.service.IChannelService;
+import com.qs.mp.channel.service.IChannelUserRelService;
 
 
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.List;
 
 
+import org.apache.commons.lang3.StringUtils;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
@@ -23,13 +26,30 @@ public class ChannelServiceTest {
 
 
 	@Autowired
 	@Autowired
 	private IChannelService channelService;
 	private IChannelService channelService;
+	
+	@Autowired
+	private IChannelUserRelService channelUserRelService;
 
 
 	@Test
 	@Test
 	public void testListChannel() {
 	public void testListChannel() {
 		Channel channel = new Channel();
 		Channel channel = new Channel();
 		channel.setParentId(1L);
 		channel.setParentId(1L);
 		List<ChannelVO> list = new ArrayList<ChannelVO>();
 		List<ChannelVO> list = new ArrayList<ChannelVO>();
-		list = channelService.selectChannelVoList(channel,"channel");
+		QueryWrapper<Channel> queryWrapper = new QueryWrapper<>();
+		queryWrapper.eq("t1.parent_id", channel.getParentId());
+		queryWrapper.gt("t1.level", 0);
+		queryWrapper.orderByAsc("t1.channel_id");
+		list = channelService.selectChannelVoList(queryWrapper);
+		if(null != list && list.size() > 0) {
+			for(ChannelVO channelVO : list) {
+				if(null != channelVO && StringUtils.isNotBlank(channelVO.getChannelNo())) {
+					int siteCnt = channelService.getChannelSiteCnt(channelVO.getChannelNo());
+					int userCnt = channelUserRelService.getChannelUserCnt(channelVO.getChannelNo());
+					channelVO.setSiteCnt(siteCnt);
+					channelVO.setUserCnt(userCnt);
+				}
+			}
+		}
 		System.out.println("result:"+list.toString());
 		System.out.println("result:"+list.toString());
 	}
 	}
 	
 	

+ 7 - 4
mp-service/src/main/java/com/qs/mp/channel/domain/vo/ChannelVO.java

@@ -1,6 +1,8 @@
 package com.qs.mp.channel.domain.vo;
 package com.qs.mp.channel.domain.vo;
 
 
 import com.qs.mp.channel.domain.Channel;
 import com.qs.mp.channel.domain.Channel;
+import com.qs.mp.system.domain.SysUser;
+
 import lombok.Data;
 import lombok.Data;
 
 
 /**
 /**
@@ -9,10 +11,11 @@ import lombok.Data;
  */
  */
 @Data
 @Data
 public class ChannelVO extends Channel {
 public class ChannelVO extends Channel {
-  long siteCnt; // 经销网点数
-  long userCnt; // 经销用户数
-
+  long siteCnt;   // 经销网点数
+  long userCnt;   // 经销用户数
   String parentName; // 上级渠道名称
   String parentName; // 上级渠道名称
   
   
-  ChannelOperDataVO operData;
+  int childCnt;  // 子渠道数量
+  ChannelOperDataVO operData;  // 经营数据
+  SysUser sysUser;  // 关联的用户账号信息
 }
 }

+ 13 - 3
mp-service/src/main/java/com/qs/mp/channel/service/IChannelService.java

@@ -1,11 +1,15 @@
 package com.qs.mp.channel.service;
 package com.qs.mp.channel.service;
 
 
 import com.qs.mp.channel.domain.Channel;
 import com.qs.mp.channel.domain.Channel;
+import com.qs.mp.channel.domain.ChannelCart;
 import com.qs.mp.channel.domain.vo.ChannelOperDataVO;
 import com.qs.mp.channel.domain.vo.ChannelOperDataVO;
 import com.qs.mp.channel.domain.vo.ChannelVO;
 import com.qs.mp.channel.domain.vo.ChannelVO;
 
 
 import java.util.List;
 import java.util.List;
 
 
+import org.apache.ibatis.annotations.Param;
+
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.baomidou.mybatisplus.extension.service.IService;
 
 
 /**
 /**
@@ -31,10 +35,10 @@ public interface IChannelService extends IService<Channel> {
 	 * 编辑子渠道、经销商信息
 	 * 编辑子渠道、经销商信息
 	 * 
 	 * 
 	 * @param channel
 	 * @param channel
-	 * @param channelType channel:子渠道  site: 经销商
+	 * @param mobileChange 手机号码是否发生变更
 	 * @return
 	 * @return
 	 */
 	 */
-    public void updateChannel(Channel channel, String channelType);
+    public void updateChannel(Channel channel, boolean mobileChange);
     
     
     
     
 	/**
 	/**
@@ -43,7 +47,7 @@ public interface IChannelService extends IService<Channel> {
 	 * @param channel
 	 * @param channel
 	 * @return
 	 * @return
 	 */
 	 */
-	public List<ChannelVO> selectChannelVoList(Channel channel, String channelType);
+	public List<ChannelVO> selectChannelVoList(Wrapper<Channel> queryWrapper);
 	
 	
 	/**
 	/**
 	 * 根据用户ID查询渠道
 	 * 根据用户ID查询渠道
@@ -53,6 +57,12 @@ public interface IChannelService extends IService<Channel> {
 	 */
 	 */
 	public Channel getChannelByUserId(Long userId);
 	public Channel getChannelByUserId(Long userId);
 	
 	
+    /**
+     * 查询渠道及其子渠道经销商总数
+     * @param channelNo
+     * @return
+     */
+	public int getChannelSiteCnt(String channelNo);
 	
 	
 	/**
 	/**
 	 * 根据channelNo统计最近几天days的经营数据
 	 * 根据channelNo统计最近几天days的经营数据

+ 17 - 138
mp-service/src/main/java/com/qs/mp/channel/service/impl/ChannelServiceImpl.java

@@ -1,7 +1,7 @@
 package com.qs.mp.channel.service.impl;
 package com.qs.mp.channel.service.impl;
 
 
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.qs.mp.channel.domain.Channel;
 import com.qs.mp.channel.domain.Channel;
 import com.qs.mp.channel.domain.ChannelCommission;
 import com.qs.mp.channel.domain.ChannelCommission;
 import com.qs.mp.channel.domain.vo.ChannelOperDataVO;
 import com.qs.mp.channel.domain.vo.ChannelOperDataVO;
@@ -10,8 +10,6 @@ import com.qs.mp.channel.mapper.ChannelMapper;
 import com.qs.mp.channel.service.IChannelCommissionService;
 import com.qs.mp.channel.service.IChannelCommissionService;
 import com.qs.mp.channel.service.IChannelService;
 import com.qs.mp.channel.service.IChannelService;
 import com.qs.mp.channel.service.IChannelUserRelService;
 import com.qs.mp.channel.service.IChannelUserRelService;
-import com.qs.mp.common.constant.UserConstants;
-import com.qs.mp.common.core.domain.AjaxResult;
 import com.qs.mp.common.enums.RoleTypeEnum;
 import com.qs.mp.common.enums.RoleTypeEnum;
 import com.qs.mp.common.exception.ServiceException;
 import com.qs.mp.common.exception.ServiceException;
 import com.qs.mp.system.domain.SysRole;
 import com.qs.mp.system.domain.SysRole;
@@ -21,11 +19,7 @@ import com.qs.mp.system.service.ISysUserService;
 import com.qs.mp.utils.SecurityUtils;
 import com.qs.mp.utils.SecurityUtils;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 
 
-import java.math.BigDecimal;
 import java.util.List;
 import java.util.List;
-import java.util.Map;
-
-import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.annotation.Transactional;
@@ -41,7 +35,6 @@ import org.springframework.transaction.annotation.Transactional;
 @Service
 @Service
 public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, Channel> implements IChannelService {
 public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, Channel> implements IChannelService {
 
 
-	
 	@Autowired
 	@Autowired
 	private IChannelService channelService;
 	private IChannelService channelService;
 	
 	
@@ -60,47 +53,14 @@ public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, Channel> impl
 	@Override
 	@Override
 	@Transactional
 	@Transactional
 	public void saveChannel(Channel channel, String channelType){
 	public void saveChannel(Channel channel, String channelType){
-		
-		String title = channelType.equals("channel")?"渠道":"经销商";
-		// 1、校验名称是否重复、手机号是否存在(渠道表)
-		LambdaQueryWrapper<Channel> queryWrapper = new LambdaQueryWrapper<>();
-		queryWrapper.eq(Channel::getName, channel.getName());
-		if(channelType.equals("channel")) {
-			queryWrapper.gt(Channel::getLevel, 1);
-		}else {
-			queryWrapper.eq(Channel::getLevel, 0);
-		}
-		int nameCount = channelService.count(queryWrapper);
-		if(nameCount > 0) {
-			 throw new ServiceException(title +"名称" + channel.getName() + "已存在!");
-		}
-		int mobileCount = channelService.count(
-		        new LambdaQueryWrapper<Channel>().eq(Channel::getMobile, channel.getMobile()));
-		if(mobileCount > 0) {
-			 throw new ServiceException("手机号码" + channel.getMobile() + "已注册!");
-		}
-		// 2.校验佣金比例,不能高于其父渠道的佣金比例
-		Channel parentChannel = channelService.getById(channel.getParentId());
-		if(null != parentChannel && null != parentChannel.getCommRate()) {
-			 if(channel.getCommRate().compareTo(parentChannel.getCommRate()) > 0) {
-				 throw new ServiceException("佣金比例不能高于父渠道的佣金比例");
-			 }
-		}
-		if(channelType.equals("channel")) {
-			channel.setLevel(parentChannel.getLevel()+1);
-		}else {
-			channel.setLevel(0);
-		}
-		// 3.插入数据
-		channel.setChannelNo(parentChannel.getChannelNo()+".");
 		boolean res = channelService.save(channel);
 		boolean res = channelService.save(channel);
 		if(res && null != channel.getChannelId()) {
 		if(res && null != channel.getChannelId()) {
-			channel.setChannelNo(parentChannel.getChannelNo()+"."+channel.getChannelId());
+			channel.setChannelNo(channel.getChannelNo()+channel.getChannelId());
 		    // 获取默认的角色
 		    // 获取默认的角色
 		    SysRole sysRole = roleService.selectDefaultRoleByType(channelType.equals("channel")?RoleTypeEnum.CHANNEL_ROLE.getValue():RoleTypeEnum.SALESITE_ROLE.getValue());
 		    SysRole sysRole = roleService.selectDefaultRoleByType(channelType.equals("channel")?RoleTypeEnum.CHANNEL_ROLE.getValue():RoleTypeEnum.SALESITE_ROLE.getValue());
 		    if (null == sysRole || null == sysRole.getRoleId()) {
 		    if (null == sysRole || null == sysRole.getRoleId()) {
 		      throw new ServiceException(
 		      throw new ServiceException(
-		          "新增'" + title + channel.getName() + "'失败,未设置"+title+"默认角色,请联系管理员");
+		          "未设置默认角色,请联系管理员");
 		    }
 		    }
 			// 生成用户
 			// 生成用户
 			SysUser sysUser = new SysUser();
 			SysUser sysUser = new SysUser();
@@ -114,136 +74,55 @@ public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, Channel> impl
 		    sysUser.setRoleIds(roleIds);
 		    sysUser.setRoleIds(roleIds);
 			int userNum = userService.insertUser(sysUser);
 			int userNum = userService.insertUser(sysUser);
 		    if (userNum == 0 || null == sysUser.getUserId()) {
 		    if (userNum == 0 || null == sysUser.getUserId()) {
-		        throw new ServiceException("新增'" + title + channel.getName() + "'失败,请联系管理员");
+		        throw new ServiceException("账号未创建失败,请联系管理员");
 		    }
 		    }
 		    channel.setUserId(sysUser.getUserId());
 		    channel.setUserId(sysUser.getUserId());
 		    boolean ures = channelService.updateById(channel);
 		    boolean ures = channelService.updateById(channel);
 		    if(!ures) {
 		    if(!ures) {
-		    	throw new ServiceException("新增'" + title + channel.getName() + "'失败,请联系管理员");
+		    	throw new ServiceException("请联系管理员");
 		    }
 		    }
 		    // 4.发送账号创建成功短信
 		    // 4.发送账号创建成功短信
 		}else {
 		}else {
-			throw new ServiceException("新增'" + title + channel.getName() + "'失败,请联系管理员");
+			throw new ServiceException("请联系管理员");
 		}
 		}
 	}
 	}
 	
 	
 
 
 	@Override
 	@Override
 	@Transactional
 	@Transactional
-	public void updateChannel(Channel channel, String channelType) {
-		String title = channelType.equals("channel")?"渠道":"经销商";
-		// 1、校验修改子渠道是否为当前用户的子渠道
-		Channel oldChannel = channelService.getById(channel.getChannelId());
-		if(null == oldChannel || null == oldChannel.getChannelId()) {
-			throw new ServiceException(title + "'" + oldChannel.getName() + "'编辑失败,渠道ID异常");
-		}
-		Long channelId = SecurityUtils.getLoginUser().getChannelId();
-		if(!oldChannel.getParentId().equals(channelId)) {
-			throw new ServiceException(title + "'" + oldChannel.getName() + "'编辑失败,非当前用户子渠道");
-		}
-		// 2.校验名称是否重复、手机号是否存在(渠道表);
-		if(!channel.getName().equals(oldChannel.getName())) {
-			
-			LambdaQueryWrapper<Channel> queryWrapper = new LambdaQueryWrapper<>();
-			queryWrapper.eq(Channel::getName, channel.getName());
-			if(channelType.equals("channel")) {
-				queryWrapper.gt(Channel::getLevel, 1);
-			}else {
-				queryWrapper.eq(Channel::getLevel, 0);
-			}
-			int nameCount = channelService.count(queryWrapper);
-			if(nameCount > 0) {
-				 throw new ServiceException(title + "名称" + channel.getName() + "已存在!");
-			}
-		}
-		boolean mobileChange = false;  // 手机号码是否有变更
-		if(!channel.getMobile().equals(oldChannel.getMobile())) {
-			int mobileCount = channelService.count(
-			        new LambdaQueryWrapper<Channel>().eq(Channel::getMobile, channel.getMobile()));
-			if(mobileCount > 0) {
-				 throw new ServiceException("手机号码" + channel.getMobile() + "已注册!");
-			}
-			if(UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(channel.getMobile()))) {
-				throw new ServiceException("手机号码" + channel.getMobile() + "已注册!");
-			}
-			mobileChange = true;
-		}
-		// 3.校验佣金比例,不能高于其父渠道的佣金比例,不能低于其子渠道的最大佣金比例
-		Channel parentChannel = channelService.getById(oldChannel.getParentId());
-		if(null != parentChannel && null != parentChannel.getCommRate()) {
-			 if(channel.getCommRate().compareTo(parentChannel.getCommRate()) > 0) {
-				 throw new ServiceException("佣金比例不能高于父渠道的佣金比例");
-			 }
-		}
-		if(channelType.equals("channel")) {
-			// 查询子渠道的最大佣金比例
-			 QueryWrapper<Channel> queryWrapper = new QueryWrapper<Channel>();
-			 queryWrapper.select("IFNULL(max(comm_rate),0) as commRate");
-			 queryWrapper.lambda().eq(Channel::getParentId, channel.getChannelId());
-			 Map<String, Object> map = channelService.getMap(queryWrapper);
-			 if(null != map && map.containsKey("commRate")) {
-				 BigDecimal commRate = new BigDecimal(map.get("commRate").toString());
-				 if(!commRate.equals(BigDecimal.ZERO) && channel.getCommRate().compareTo(commRate) < 0) {
-					 throw new ServiceException("不能低于其子渠道的最大佣金比例");
-				 }
-			 }
-		}
+	public void updateChannel(Channel channel, boolean mobileChange) {
 	    // 4.更新入库
 	    // 4.更新入库
 		 boolean editRes = channelService.updateById(channel);
 		 boolean editRes = channelService.updateById(channel);
 		 if(editRes && mobileChange 
 		 if(editRes && mobileChange 
-				 && null != oldChannel.getUserId()) {
+				 && null != channel.getUserId()) {
 			 SysUser sysUser = new SysUser();
 			 SysUser sysUser = new SysUser();
-			 sysUser.setUserId(oldChannel.getUserId());
+			 sysUser.setUserId(channel.getUserId());
 			 sysUser.setUserName(channel.getMobile());
 			 sysUser.setUserName(channel.getMobile());
 			 sysUser.setPhonenumber(channel.getMobile());
 			 sysUser.setPhonenumber(channel.getMobile());
 			 sysUser.setNickName(channel.getName());
 			 sysUser.setNickName(channel.getName());
 			 sysUser.setDeptId(SecurityUtils.getDeptId());
 			 sysUser.setDeptId(SecurityUtils.getDeptId());
 			 int userNum = userService.updateUser(sysUser);
 			 int userNum = userService.updateUser(sysUser);
 			 if (userNum == 0 || null == sysUser.getUserId()) {
 			 if (userNum == 0 || null == sysUser.getUserId()) {
-		        throw new ServiceException(title + "'" + oldChannel.getName() + "'编辑失败,请联系管理员");
+		        throw new ServiceException("更新失败,请联系管理员");
 		      }
 		      }
 			 // 发送账号更新成功短信
 			 // 发送账号更新成功短信
 		 }
 		 }
 	}
 	}
 	
 	
-
 	@Override
 	@Override
-	public List<ChannelVO> selectChannelVoList(Channel channel, String channelType) {
-		QueryWrapper<Channel> queryWrapper = new QueryWrapper<>();
-		queryWrapper.eq("t1.parent_id", channel.getParentId());
-		
-		if(channelType.equals("channel")) {
-			queryWrapper.gt("t1.level", 0);
-			queryWrapper.orderByAsc("t1.channel_id");
-		}else {
-			queryWrapper.eq("t1.level", 0);
-			if(null != channel.getVerifyStatus()) {
-				queryWrapper.eq("t1.verify_status", channel.getVerifyStatus());
-			}
-			if(null != channel.getCertifyStatus()) {
-				queryWrapper.eq("t1.certify_status", channel.getCertifyStatus());
-			}
-			queryWrapper.orderByDesc("t1.certify_status").orderByAsc("t1.channel_id");
-		}
-		List<ChannelVO> list = getBaseMapper().selectChannelVoList(queryWrapper);
-		if(channelType.equals("channel") 
-				&& null != list && list.size() > 0) {
-			for(ChannelVO channelVO : list) {
-				if(null != channelVO && StringUtils.isNotBlank(channelVO.getChannelNo())) {
-					int siteCnt = getBaseMapper().getChannelSiteCnt(channelVO.getChannelNo());
-					int userCnt = channelUserRelService.getChannelUserCnt(channelVO.getChannelNo());
-					channelVO.setSiteCnt(siteCnt);
-					channelVO.setUserCnt(userCnt);
-				}
-			}
-		}
-		return list;
+	public List<ChannelVO> selectChannelVoList(Wrapper<Channel> queryWrapper) {
+		return getBaseMapper().selectChannelVoList(queryWrapper);
 	}
 	}
 	
 	
 	@Override
 	@Override
 	public Channel getChannelByUserId(Long userId) {
 	public Channel getChannelByUserId(Long userId) {
 		return getBaseMapper().selectOne(new LambdaQueryWrapper<Channel>().eq(Channel::getUserId, userId));
 		return getBaseMapper().selectOne(new LambdaQueryWrapper<Channel>().eq(Channel::getUserId, userId));
 	}
 	}
+	
+	@Override
+	public int getChannelSiteCnt(String channelNo) {
+		return getBaseMapper().getChannelSiteCnt(channelNo);
+	}
 
 
 	@Override
 	@Override
 	public ChannelOperDataVO getChannelOperData(String channelNo, int days) {
 	public ChannelOperDataVO getChannelOperData(String channelNo, int days) {