|
@@ -0,0 +1,248 @@
|
|
|
+package com.qs.mp.web.controller.api.admin;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+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.param.ChannelParam;
|
|
|
+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.constant.UserConstants;
|
|
|
+import com.qs.mp.common.core.domain.AjaxResult;
|
|
|
+import com.qs.mp.common.core.page.TableDataInfo;
|
|
|
+import com.qs.mp.common.enums.ErrorCodeEnum;
|
|
|
+import com.qs.mp.system.domain.SysUser;
|
|
|
+import com.qs.mp.system.service.ISysUserService;
|
|
|
+import com.qs.mp.web.controller.common.BaseApiController;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import ma.glasnost.orika.MapperFacade;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+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 liugl
|
|
|
+ * @create 2022-03-09 23:45:48
|
|
|
+ * @describe 代金券管理前端控制器
|
|
|
+ */
|
|
|
+@Api("渠道管理API")
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/v1/mp/admin/coupon/*")
|
|
|
+@Component
|
|
|
+public class CouponMgrController extends BaseApiController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IChannelService channelService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IChannelUserRelService channelUserRelService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysUserService userService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MapperFacade mapperFacade;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取我的下级渠道列表信息,支持翻页
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @PostMapping("/list")
|
|
|
+ public TableDataInfo listChannel(@RequestBody Channel channel) {
|
|
|
+ List<ChannelVO> list = new ArrayList<ChannelVO>();
|
|
|
+ startPage();
|
|
|
+ QueryWrapper<Channel> queryWrapper = new QueryWrapper<>();
|
|
|
+ if(null!=channel && null != channel.getParentId()) {
|
|
|
+ queryWrapper.eq("t1.parent_id", channel.getParentId());
|
|
|
+ }
|
|
|
+ queryWrapper.gt("t1.level", 0);
|
|
|
+ queryWrapper.likeRight(null != channel && StringUtils.isNotBlank(channel.getMobile()), "t1.mobile", channel.getMobile());
|
|
|
+ queryWrapper.likeRight(null != channel && StringUtils.isNotBlank(channel.getName()), "t1.name", channel.getName());
|
|
|
+ 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.getChannelTotalUserCnt(channelVO.getChannelNo());
|
|
|
+ channelVO.setSiteCnt(siteCnt);
|
|
|
+ channelVO.setUserCnt(userCnt);
|
|
|
+ // 查询用户信息
|
|
|
+ // SysUser sysUser = userService.selectUserById(channelVO.getUserId());
|
|
|
+ // channelVO.setSysUser(sysUser);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return getDataTable(list);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增子渠道信息
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ApiOperation(value = "新增子渠道信息", notes = "渠道端新增子渠道")
|
|
|
+ @PostMapping("/create")
|
|
|
+ public AjaxResult channelCreate(@Validated @RequestBody ChannelParam channelParam) {
|
|
|
+ if (channelParam.getChannelId() != null && channelParam.getChannelId() != 0) {
|
|
|
+ return AjaxResult.error("该渠道已存在");
|
|
|
+ }
|
|
|
+ Channel channel = mapperFacade.map(channelParam, Channel.class);
|
|
|
+ // 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.校验佣金比例,不能高于其父渠道的佣金比例
|
|
|
+ if(null != channel.getParentId() && channel.getParentId() != 0) {
|
|
|
+ Channel parentChannel = channelService.getById(channel.getParentId());
|
|
|
+ if(null != parentChannel) {
|
|
|
+ if(null != parentChannel.getCommRate()
|
|
|
+ && channel.getCommRate().compareTo(parentChannel.getCommRate()) > 0) {
|
|
|
+ return AjaxResult.error("佣金比例不能高于父渠道的佣金比例");
|
|
|
+ }
|
|
|
+ channel.setLevel(parentChannel.getLevel()+1);
|
|
|
+ channel.setChannelNo(parentChannel.getChannelNo()+".");
|
|
|
+ }else {
|
|
|
+ return AjaxResult.error("父渠道不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ }else {
|
|
|
+ channel.setLevel(1);
|
|
|
+ channel.setChannelNo("");
|
|
|
+ }
|
|
|
+ // 3.插入数据
|
|
|
+ try {
|
|
|
+ channelService.saveChannel(channel,"channel");
|
|
|
+ } catch (Exception e) {
|
|
|
+ return AjaxResult.error("渠道'" + channel.getName() + "'新增失败" + e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return AjaxResult.success("渠道'" + channel.getName() + "'新增成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编辑子渠道信息
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ApiOperation(value = "编辑子渠道信息", notes = "渠道端编辑子渠道")
|
|
|
+ @PostMapping("/update")
|
|
|
+ public AjaxResult channelUpdate(@Validated @RequestBody ChannelParam channelParam) {
|
|
|
+ if (null == channelParam || null == channelParam.getChannelId()) {
|
|
|
+ return error(ErrorCodeEnum.ERROR_CODE_1001);
|
|
|
+ }
|
|
|
+ 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异常");
|
|
|
+ }
|
|
|
+ // 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) {
|
|
|
+ if(null != parentChannel.getCommRate()
|
|
|
+ && channel.getCommRate().compareTo(parentChannel.getCommRate()) > 0) {
|
|
|
+ return AjaxResult.error("佣金比例不能高于父渠道的佣金比例");
|
|
|
+ }
|
|
|
+ channel.setLevel(parentChannel.getLevel()+1);
|
|
|
+ channel.setChannelNo(parentChannel.getChannelNo()+".");
|
|
|
+ }else {
|
|
|
+ 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() + "'编辑成功");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 停用、启用渠道
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ApiOperation(value = "停用、启用渠道信息", notes = "渠道管理编辑子渠道")
|
|
|
+ @PostMapping("/status")
|
|
|
+ public AjaxResult channelStatus(@RequestBody JSONObject jsonObject) {
|
|
|
+ String channelId = jsonObject.containsKey("channelId")?jsonObject.get("channelId").toString():"";
|
|
|
+ String status = jsonObject.containsKey("status")?jsonObject.get("status").toString():"";
|
|
|
+ if (null == jsonObject || StringUtils.isBlank(channelId)
|
|
|
+ || StringUtils.isBlank(status)) {
|
|
|
+ return error(ErrorCodeEnum.ERROR_CODE_1001);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ channelService.lambdaUpdate().set(Channel::getStatus, status).eq(Channel::getChannelId, channelId).update();
|
|
|
+ // 查询渠道信息
|
|
|
+// Channel channel = channelService.getById(channelId);
|
|
|
+// if(null != channel && null != channel.getUserId()) {
|
|
|
+// SysUser sysUser = new SysUser();
|
|
|
+// sysUser.setUserId(channel.getUserId());
|
|
|
+// sysUser.setStatus(status);
|
|
|
+// userService.updateUserStatus(sysUser);
|
|
|
+// }
|
|
|
+ } catch (Exception e) {
|
|
|
+ return AjaxResult.error("操作失败");
|
|
|
+ }
|
|
|
+ return AjaxResult.success("操作成功");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|