Browse Source

渠道门店转换

Chris-zy 1 year ago
parent
commit
b386245292

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

@@ -17,6 +17,8 @@ import com.qs.mp.common.core.page.TableDataInfo;
 import com.qs.mp.common.enums.BusinessType;
 import com.qs.mp.common.enums.BusinessType;
 import com.qs.mp.common.enums.ChannelRoleEnum;
 import com.qs.mp.common.enums.ChannelRoleEnum;
 import com.qs.mp.common.enums.ErrorCodeEnum;
 import com.qs.mp.common.enums.ErrorCodeEnum;
+import com.qs.mp.system.domain.SysUser;
+import com.qs.mp.system.domain.SysUserRole;
 import com.qs.mp.system.service.ISysUserService;
 import com.qs.mp.system.service.ISysUserService;
 import com.qs.mp.user.service.IUserTicketOrderService;
 import com.qs.mp.user.service.IUserTicketOrderService;
 import com.qs.mp.web.controller.common.BaseApiController;
 import com.qs.mp.web.controller.common.BaseApiController;
@@ -386,4 +388,71 @@ public class ChannelMgrController extends BaseApiController {
         return "";
         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(@Validated @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异常");
+        }
+        //判断当前渠道下有无子渠道
+        LambdaQueryWrapper<Channel> queryWrapper = new LambdaQueryWrapper<>();
+        if (null != channelParam.getChannelId()) {
+            queryWrapper.eq(Channel::getParentId, channelParam.getChannelId());
+        }
+        queryWrapper.gt(Channel::getLevel, 0);
+        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() + "'变更为门店成功");
+    }
 }
 }

+ 61 - 0
mp-admin/src/main/java/com/qs/mp/web/controller/api/admin/SaleSiteMgrController.java

@@ -38,6 +38,7 @@ import io.swagger.annotations.ApiResponse;
 import io.swagger.annotations.ApiResponses;
 import io.swagger.annotations.ApiResponses;
 import ma.glasnost.orika.MapperFacade;
 import ma.glasnost.orika.MapperFacade;
 
 
+import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.List;
 import java.util.Objects;
 import java.util.Objects;
@@ -623,4 +624,64 @@ public class SaleSiteMgrController extends BaseApiController {
         return AjaxResult.success(channelVO);
         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(@Validated @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(channelParam.getParentId());
+        channel.setLevel(0);
+        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() + "'变更为渠道成功");
+    }
+
+
+
 }
 }

+ 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
      * @param siteList
      */
      */
     void importSite(List<ChannelImportExcel> siteList);
     void importSite(List<ChannelImportExcel> siteList);
+
+    /**
+     * 修改用户信息及岗位、角色关联
+     *
+     * @param channel
+     * @param channelRole
+     * @return
+     */
+    public void alterUserPostAndRole(Channel channel, ChannelRoleEnum channelRole);
 }
 }

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

@@ -29,6 +29,8 @@ import com.qs.mp.common.exception.ServiceException;
 import com.qs.mp.common.jsms.JSMSUtils;
 import com.qs.mp.common.jsms.JSMSUtils;
 import com.qs.mp.system.domain.SysRole;
 import com.qs.mp.system.domain.SysRole;
 import com.qs.mp.system.domain.SysUser;
 import com.qs.mp.system.domain.SysUser;
+import com.qs.mp.system.domain.SysUserRole;
+import com.qs.mp.system.mapper.SysUserRoleMapper;
 import com.qs.mp.system.service.ISysRoleService;
 import com.qs.mp.system.service.ISysRoleService;
 import com.qs.mp.system.service.ISysUserService;
 import com.qs.mp.system.service.ISysUserService;
 import com.qs.mp.user.domain.UserAddr;
 import com.qs.mp.user.domain.UserAddr;
@@ -412,4 +414,57 @@ public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, Channel> impl
 
 
         return true;
         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");
+        }
+    }
 }
 }

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

@@ -1,5 +1,6 @@
 package com.qs.mp.system.mapper;
 package com.qs.mp.system.mapper;
 
 
+import com.qs.mp.system.domain.SysUser;
 import com.qs.mp.system.domain.SysUserRole;
 import com.qs.mp.system.domain.SysUserRole;
 import java.util.List;
 import java.util.List;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.annotations.Param;
@@ -59,4 +60,16 @@ public interface SysUserRoleMapper
      * @return 结果
      * @return 结果
      */
      */
     public int deleteUserRoleInfos(@Param("roleId") Long roleId, @Param("userIds") Long[] userIds);
     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.baomidou.mybatisplus.extension.service.IService;
 import com.qs.mp.channel.domain.ChannelBankCard;
 import com.qs.mp.channel.domain.ChannelBankCard;
 import com.qs.mp.system.domain.SysUser;
 import com.qs.mp.system.domain.SysUser;
+import com.qs.mp.system.domain.SysUserRole;
+
 import java.util.List;
 import java.util.List;
 
 
 /**
 /**
@@ -241,4 +243,23 @@ public interface ISysUserService  extends IService<SysUser>
      * 根据手机号查询用户昵称
      * 根据手机号查询用户昵称
      */
      */
     SysUser selectNickNameByPhoneNumber(String phone);
     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) {
     public SysUser selectNickNameByPhoneNumber(String phone) {
         return userMapper.selectNickNameByPhoneNumber(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);
+    }
 }
 }

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

@@ -41,4 +41,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  	        #{userId}
  	        #{userId}
             </foreach> 
             </foreach> 
 	</delete>
 	</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> 
 </mapper>