Pārlūkot izejas kodu

渠道和门店角色转换

Chris-zy 1 gadu atpakaļ
vecāks
revīzija
4914eebaa4

+ 84 - 0
mp-admin/src/main/java/com/qs/mp/web/controller/api/admin/ChannelMgrController.java

@@ -150,6 +150,20 @@ public class ChannelMgrController extends BaseApiController {
         queryWrapper.eq(null != channel && null != channel.getCityId(), "t1.city_id", channel.getCityId());
         queryWrapper.eq(null != channel && null != channel.getAreaId(), "t1.area_id", channel.getAreaId());
         list = channelService.selectChannelList(queryWrapper);
+        if (null != list && list.size() > 0) {
+            for (ChannelVO channelVO : list) {
+                //判断当前渠道下有无子渠道和门店
+                LambdaQueryWrapper<Channel> queryWrapper1 = new LambdaQueryWrapper<>();
+                queryWrapper1.eq(Channel::getParentId, channelVO.getChannelId());
+                queryWrapper1.gt(Channel::getLevel, -1);
+                int channelCount = channelService.count(queryWrapper1);
+                if (channelCount > 0) {
+                    channelVO.setIsTransform(false);
+                } else {
+                    channelVO.setIsTransform(true);
+                }
+            }
+        }
         return getDataTable(list);
     }
 
@@ -386,4 +400,74 @@ public class ChannelMgrController extends BaseApiController {
         return "";
     }
 
+    /**
+     * 渠道转门店
+     *
+     * @param
+     * @return
+     * 参数:{"channelId":"2439", parentId:"2438"}
+     * channelId当前渠道id
+     * parentId父渠道id
+     */
+    @Log(title = "渠道转门店", businessType = BusinessType.UPDATE)
+    @ApiOperation(value = "渠道转门店", notes = "渠道转门店")
+    @PostMapping("/transform")
+    @PreAuthorize("@ss.hasPermi('business:channel:edit')")
+    public AjaxResult channelTransformToShop( @RequestBody ChannelParam channelParam) {
+        if (null == channelParam || null == channelParam.getChannelId() || null == channelParam.getParentId()) {
+            return error(ErrorCodeEnum.ERROR_CODE_1001);
+        }
+        //修改当前渠道为门店,参考添加一级渠道
+        Channel channel = channelService.getById(channelParam.getChannelId());
+        if (null == channel || null == channel.getChannelId()) {
+            return AjaxResult.error("渠道'" + channel.getName() + "'转门店失败,渠道ID异常");
+        }
+        //判断父渠道Id是不是当前渠道Id
+        if (channelParam.getChannelId() == channelParam.getParentId()){
+            return AjaxResult.error("渠道'" + channel.getName() + "'转门店失败,不能选择自己作为上级渠道");
+        }
+        //判断当前渠道下有无子渠道
+        LambdaQueryWrapper<Channel> queryWrapper = new LambdaQueryWrapper<>();
+        if (null != channelParam.getChannelId()) {
+            queryWrapper.eq(Channel::getParentId, channelParam.getChannelId());
+        }
+        queryWrapper.gt(Channel::getLevel, -1);
+        int channelCount = channelService.count(queryWrapper);
+        if (channelCount > 0) {
+            return AjaxResult.error("当前渠道存在子渠道不能转为门店!");
+        }
+
+        //修改代理为门店
+        //查询上级代理信息
+        Channel parentChannel = channelService.getById(channelParam.getParentId());
+        boolean mobileChange = false;  // 手机号码是否有变更
+        channel.setParentId(channelParam.getParentId());
+        channel.setLevel(2);
+        channel.setChannelNo(parentChannel.getChannelNo()+"."+channel.getChannelId());
+        //佣金比例暂定为0
+        channel.setCommRate(BigDecimal.ZERO);
+        /**
+         //校验佣金比例,不能高于其父经销商的佣金比例
+         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("父经销商不存在");
+         }
+
+         }
+         */
+        channelService.updateChannel(channel, mobileChange);
+
+        //修改用户信息及岗位、角色关联
+        channelService.alterUserPostAndRole(channel, ChannelRoleEnum.SALESITE);
+
+        return AjaxResult.success("渠道'" + channel.getName() + "'变更为门店成功");
+    }
 }

+ 111 - 3
mp-admin/src/main/java/com/qs/mp/web/controller/api/admin/SaleSiteMgrController.java

@@ -28,9 +28,11 @@ import com.qs.mp.common.enums.BusinessType;
 import com.qs.mp.common.enums.ChannelRoleEnum;
 import com.qs.mp.common.enums.ErrorCodeEnum;
 import com.qs.mp.common.exception.ServiceException;
+import com.qs.mp.system.domain.SysUser;
 import com.qs.mp.system.service.ISysUserService;
 import com.qs.mp.user.service.IUserTicketOrderService;
 import com.qs.mp.utils.ExcelUtil;
+import com.qs.mp.utils.SecurityUtils;
 import com.qs.mp.web.controller.common.BaseApiController;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -38,6 +40,7 @@ import io.swagger.annotations.ApiResponse;
 import io.swagger.annotations.ApiResponses;
 import ma.glasnost.orika.MapperFacade;
 
+import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
@@ -438,19 +441,67 @@ public class SaleSiteMgrController extends BaseApiController {
         return AjaxResult.success("经销商'" + channel.getName() + "'新增成功");
     }
 
+    /**
+     *
+     * parentId必传
+     * @param saleSiteEditParam
+     * @return
+     */
     @PreAuthorize("@ss.hasPermi('business:salesite:edit')")
     @ApiOperation("修改门店信息")
     @PostMapping("/edit")
     public AjaxResult edit(@RequestBody SaleSiteEditParam saleSiteEditParam) {
+        boolean mobileChange = false;  // 手机号码是否有变更
         Channel channel = channelService.getById(saleSiteEditParam.getChannelId());
         if (channel == null) {
             return AjaxResult.error("门店信息不存在");
         }
+        //手机号是否重复校验
+        if (!saleSiteEditParam.getMobile().equals(channel.getMobile())) {
+            mobileChange = true;
+            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() + "已注册!");
+            }
+        }
+        //校验佣金比例,不能高于其父经销商的佣金比例
+        Channel parentChannel = channelService.getById(saleSiteEditParam.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("父经销商不存在");
+        }
 
         boolean rtn = channelService.update(new LambdaUpdateWrapper<Channel>()
-            .set(Channel::getName, saleSiteEditParam.getName())
-            .set(saleSiteEditParam.getCommFlag() != null, Channel::getCommFlag, saleSiteEditParam.getCommFlag())
-            .eq(Channel::getChannelId, saleSiteEditParam.getChannelId()));
+                .set(Channel::getName, saleSiteEditParam.getName())
+                .set(Channel::getChannelNo, parentChannel.getChannelNo()+"."+channel.getChannelId())
+                .set(Channel::getParentId, saleSiteEditParam.getParentId())
+                .set(Channel::getMobile, saleSiteEditParam.getMobile())
+                .set(saleSiteEditParam.getCommFlag() != null, Channel::getCommFlag, saleSiteEditParam.getCommFlag())
+                .eq(Channel::getChannelId, saleSiteEditParam.getChannelId()));
+
+        if (mobileChange){
+            //修改用户信息
+            SysUser sysUser = new SysUser();
+            sysUser.setUserId(channel.getUserId());
+            sysUser.setUserName(channel.getMobile());
+            sysUser.setPhonenumber(channel.getMobile());
+            sysUser.setNickName(channel.getName());
+            sysUser.setDeptId(SecurityUtils.getDeptId());
+            int userNum = userService.updateUser(sysUser);
+            if (userNum == 0 || null == sysUser.getUserId()) {
+                throw new ServiceException("更新失败,请联系管理员");
+            }
+        }
 
         if (!rtn) {
             return AjaxResult.error("更新门店信息失败");
@@ -610,4 +661,61 @@ public class SaleSiteMgrController extends BaseApiController {
         return AjaxResult.success(channelVO);
     }
 
+    /**
+     * 门店转渠道
+     *
+     * @param
+     * @return
+     * 参数:{"channelId":"2439", parentId:"2438"}
+     * channelId当前门店id
+     * parentId父渠道id
+     */
+    @Log(title = "门店转渠道", businessType = BusinessType.UPDATE)
+    @ApiOperation(value = "门店转渠道", notes = "门店转渠道")
+    @PostMapping("/transform")
+    @PreAuthorize("@ss.hasPermi('business:salesite:edit')")
+    public AjaxResult channelTransformToShop(@RequestBody ChannelParam channelParam) {
+        if (null == channelParam || null == channelParam.getChannelId()) {
+            return error(ErrorCodeEnum.ERROR_CODE_1001);
+        }
+        //修改当前门店为渠道
+        Channel channel = channelService.getById(channelParam.getChannelId());
+        if (null == channel || null == channel.getChannelId()) {
+            return AjaxResult.error("门店'" + channel.getName() + "'转渠道失败,门店ID异常");
+        }
+
+        //修改门店为渠道
+        //查询上级代理信息
+        Channel parentChannel = channelService.getById(channelParam.getParentId());
+        boolean mobileChange = false;  // 手机号码是否有变更
+        channel.setParentId(0L);
+        channel.setLevel(1);
+        channel.setChannelNo(channel.getChannelId().toString());
+        //佣金比例暂定为0
+        channel.setCommRate(BigDecimal.ZERO);
+        /**
+         //校验佣金比例,不能高于其父经销商的佣金比例
+         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("父经销商不存在");
+         }
+
+         }
+         */
+        channelService.updateChannel(channel, mobileChange);
+
+        //修改用户信息及岗位、角色关联
+        channelService.alterUserPostAndRole(channel, ChannelRoleEnum.CHANNEL);
+
+        return AjaxResult.success("门店'" + channel.getName() + "'变更为渠道成功");
+    }
+
 }

+ 7 - 0
mp-service/src/main/java/com/qs/mp/channel/domain/Channel.java

@@ -233,6 +233,13 @@ public class Channel implements Serializable {
   @ApiModelProperty("分佣标识:0默认,1不分佣")
   private Integer commFlag;
 
+  /**
+   * 门店二维码
+   */
+  @TableField("qr_code_pic")
+  @ApiModelProperty("门店二维码")
+  private String qrCodePic;
+
   /**
    * 创建时间
    */

+ 6 - 0
mp-service/src/main/java/com/qs/mp/channel/domain/param/SaleSiteEditParam.java

@@ -24,7 +24,13 @@ public class SaleSiteEditParam {
     @ApiModelProperty("门店名称")
     private String name;
 
+    @ApiModelProperty("手机号")
+    private String mobile;
+
     @ApiModelProperty("分佣标识:0默认,1不分佣")
     private Integer commFlag;
 
+    @ApiModelProperty("父渠道ID")
+    private Integer parentId;
+
 }

+ 2 - 1
mp-service/src/main/java/com/qs/mp/channel/domain/vo/ChannelVO.java

@@ -39,5 +39,6 @@ public class ChannelVO extends Channel {
     @ApiModelProperty("线上销售(显示盲票销售张数)")
     long onLineSaleCnt; // 线上销售(显示盲票销售张数)
 
-
+    @ApiModelProperty("是否可转为门店标识")
+    Boolean isTransform = true;
 }

+ 9 - 0
mp-service/src/main/java/com/qs/mp/channel/service/IChannelService.java

@@ -160,4 +160,13 @@ public interface IChannelService extends IService<Channel> {
      * @param siteList
      */
     void importSite(List<ChannelImportExcel> siteList);
+
+    /**
+     * 修改用户信息及岗位、角色关联
+     *
+     * @param channel
+     * @param channelRole
+     * @return
+     */
+    public void alterUserPostAndRole(Channel channel, ChannelRoleEnum channelRole);
 }

+ 54 - 0
mp-service/src/main/java/com/qs/mp/channel/service/impl/ChannelServiceImpl.java

@@ -29,6 +29,7 @@ import com.qs.mp.common.exception.ServiceException;
 import com.qs.mp.common.jsms.JSMSUtils;
 import com.qs.mp.system.domain.SysRole;
 import com.qs.mp.system.domain.SysUser;
+import com.qs.mp.system.domain.SysUserRole;
 import com.qs.mp.system.service.ISysRoleService;
 import com.qs.mp.system.service.ISysUserService;
 import com.qs.mp.user.domain.UserAddr;
@@ -412,4 +413,57 @@ public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, Channel> impl
 
         return true;
     }
+
+    //修改用户信息及岗位、角色关联
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void alterUserPostAndRole(Channel channel, ChannelRoleEnum channelRole) {
+        if (null != channel.getChannelId()) {
+            SysUser sysUser = userService.selectUserByUserName(channel.getMobile());
+            // 获取默认的角色
+            SysRole sysRole = roleService.selectDefaultRoleByType(
+                    channelRole == ChannelRoleEnum.CHANNEL ? RoleTypeEnum.CHANNEL_ROLE.getValue()
+                            : RoleTypeEnum.SALESITE_ROLE.getValue());
+            if (null == sysRole || null == sysRole.getRoleId()) {
+                throw new ServiceException(
+                        "未设置默认角色,请联系管理员");
+            }
+            Long[] roleIds = new Long[1];
+            roleIds[0] = sysRole.getRoleId();
+
+            if (null == sysUser) {
+                // 生成用户
+                sysUser = new SysUser();
+                sysUser.setUserName(channel.getMobile());
+                sysUser.setPhonenumber(channel.getMobile());
+                sysUser.setNickName(channel.getMobile());
+
+                sysUser.setRoleIds(roleIds);
+                int userNum = userService.insertUser(sysUser);
+                if (userNum == 0 || null == sysUser.getUserId()) {
+                    throw new ServiceException("创建角色失败,请联系管理员");
+                }
+            }else {
+                //更新用户、用户角色、用户岗位
+                //查询用户岗位信息 sys_user_post,暂时不管没有用
+
+                //查询用户角色信息
+                SysUserRole sysUserRole = userService.selectUserRoleById(channel.getUserId());
+                if (null != sysUserRole){
+                    sysUserRole.setRoleId(sysRole.getRoleId());
+                    int result = userService.updateUserRole(sysUserRole);
+
+                }else {
+                    sysUser.setRoleIds(roleIds);
+                    //新增用户角色关联
+                    userService.insertUserRole(sysUser);
+
+                }
+
+            }
+
+        } else {
+            throw new ServiceException("渠道门店互转无渠道Id");
+        }
+    }
 }

+ 12 - 0
mp-service/src/main/java/com/qs/mp/system/mapper/SysUserRoleMapper.java

@@ -59,4 +59,16 @@ public interface SysUserRoleMapper
      * @return 结果
      */
     public int deleteUserRoleInfos(@Param("roleId") Long roleId, @Param("userIds") Long[] userIds);
+
+    /**
+     * 根据角色id查询角色关联表
+     */
+    public SysUserRole selectUserRoleById(Long userId);
+
+    /**
+     * 更新用户角色关系
+     * @param sysUserRole
+     * @return
+     */
+    int updateUserRole(SysUserRole sysUserRole);
 }

+ 21 - 0
mp-service/src/main/java/com/qs/mp/system/service/ISysUserService.java

@@ -3,6 +3,8 @@ package com.qs.mp.system.service;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.qs.mp.channel.domain.ChannelBankCard;
 import com.qs.mp.system.domain.SysUser;
+import com.qs.mp.system.domain.SysUserRole;
+
 import java.util.List;
 
 /**
@@ -241,4 +243,23 @@ public interface ISysUserService  extends IService<SysUser>
      * 根据手机号查询用户昵称
      */
     SysUser selectNickNameByPhoneNumber(String phone);
+
+    /**
+     * 通过用户ID查询角色关联
+     *
+     * @param userId 用户Id
+     * @return 用户对象信息
+     */
+    public SysUserRole selectUserRoleById(Long userId);
+
+    /**
+     * 更新用户角色关系
+     * @param sysUserRole
+     * @return
+     */
+    int updateUserRole(SysUserRole sysUserRole);
+
+    public void insertUserPost(SysUser user);
+
+    public void insertUserRole(SysUser user);
 }

+ 22 - 0
mp-service/src/main/java/com/qs/mp/system/service/impl/SysUserServiceImpl.java

@@ -585,4 +585,26 @@ public class SysUserServiceImpl  extends ServiceImpl<SysUserMapper, SysUser> imp
     public SysUser selectNickNameByPhoneNumber(String phone) {
         return userMapper.selectNickNameByPhoneNumber(phone);
     }
+
+    /**
+     * 通过用户名查询用户
+     *
+     * @param userId 用户id
+     * @return 用户角色信息
+     */
+    @Override
+    public SysUserRole selectUserRoleById(Long userId)
+    {
+        return userRoleMapper.selectUserRoleById(userId);
+    }
+
+    /**
+     * 更新用户角色关系
+     * @param sysUserRole
+     * @return
+     */
+    @Override
+    public int updateUserRole(SysUserRole sysUserRole) {
+        return userRoleMapper.updateUserRole(sysUserRole);
+    }
 }

+ 8 - 0
mp-service/src/main/resources/mapper/system/SysUserRoleMapper.xml

@@ -41,4 +41,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  	        #{userId}
             </foreach> 
 	</delete>
+	<select id="selectUserRoleById" parameterType="java.lang.Long" resultMap="SysUserRoleResult">
+		select user_id, role_id from sys_user_role
+		where user_id = #{userId}
+	</select>
+
+	<update id="updateUserRole" parameterType="com.qs.mp.system.domain.SysUserRole">
+		update sys_user_role set role_id = #{roleId} where user_id=#{userId}
+	</update>
 </mapper>