hjl
2024-07-16 ec6d43aa07ee0e8faf34498057ebcfbb446aa015
ruoyi-service/ruoyi-admin/src/main/java/com/ruoyi/admin/controller/FranchiseeController.java
@@ -5,11 +5,16 @@
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.admin.entity.Franchisee;
import com.ruoyi.admin.entity.SysUser;
import com.ruoyi.admin.entity.UserRole;
import com.ruoyi.admin.service.FranchiseeService;
import com.ruoyi.admin.service.SysUserService;
import com.ruoyi.admin.service.UserRoleService;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
@@ -38,6 +43,10 @@
    @Resource
    private FranchiseeService franchiseeService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private UserRoleService userRoleService;
    /**
     * 加盟商信息分页列表
@@ -45,6 +54,7 @@
     * @param pageNum  页码
     * @param pageSize 每页显示条数
     */
    @RequiresPermissions("franchisee")
    @ApiOperation(value = "加盟商信息分页查询列表", tags = {"后台-加盟商管理"})
    @GetMapping(value = "/page")
    @ApiImplicitParams({
@@ -72,13 +82,29 @@
     *
     * @param id 加盟商信息id
     */
    @RequiresPermissions("franchisee")
    @ApiOperation(value = "加盟商信息详情", tags = {"后台-加盟商管理"})
    @GetMapping(value = "/detail")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "加盟商信息id", name = "id", dataType = "Integer", required = true)
    })
    public R<Franchisee> detail(@RequestParam Integer id) {
        return R.ok(franchiseeService.getById(id));
    public R<Franchisee> detail(@RequestParam("id") Integer id) {
        return R.ok(franchiseeService.lambdaQuery()
                .eq(Franchisee::getId, id).eq(Franchisee::getIsDelete, 0).one());
    }
    /**
     * 加盟商管辖城市详情
     * -- 远程调用
     *
     * @param id 加盟商信息id
     */
    @ApiOperation(value = "加盟商管辖城市详情", tags = {"后台-加盟商管理"})
    @GetMapping(value = "/cityDetail")
    public R<List<String>> cityDetail(@RequestParam("id") Integer id) {
        Franchisee franchisee = franchiseeService.lambdaQuery()
                .eq(Franchisee::getId, id).eq(Franchisee::getIsDelete, 0).one();
        return R.ok(Arrays.stream(franchisee.getCity().split(",")).collect(Collectors.toList()));
    }
    /**
@@ -86,11 +112,31 @@
     *
     * @param franchisee 加盟商信息信息
     */
    @RequiresPermissions("franchisee")
    @ApiOperation(value = "新增加盟商信息", tags = {"后台-加盟商管理"})
    @PostMapping(value = "/save")
    public R<String> save(@RequestBody @Validated Franchisee franchisee) {
        checkFranchisee(franchisee);
        return franchiseeService.save(franchisee) ? R.ok() : R.fail();
        if (null == franchisee.getAdminPassword() || StringUtils.isBlank(franchisee.getAdminPassword())) {
            throw new GlobalException("请输入管理员初始密码!");
        }
        String md5Password = checkFranchisee(franchisee);
        boolean save = franchiseeService.save(franchisee);
        // 生成sysUser账号
        SysUser sysUser = new SysUser();
        sysUser.setFranchiseeId(franchisee.getId());
        sysUser.setNickName(franchisee.getName());
        sysUser.setAccount(franchisee.getAdminAccount());
        sysUser.setPassword(md5Password);
        sysUser.setIsEnable(Constants.ONE);
        sysUser.setIsDelete(Constants.ZERO);
        save = save && sysUserService.save(sysUser);
        // 添加账号与角色关联
        UserRole userRole = new UserRole();
        userRole.setUserId(sysUser.getUserId());
        userRole.setRoleId(franchisee.getRoleId().longValue());
        save = save && userRoleService.save(userRole);
        //franchisee.setCityStr(String.valueOf(franchisee.getCityArr()));
        return save ? R.ok() : R.fail();
    }
    /**
@@ -98,11 +144,55 @@
     *
     * @param franchisee 加盟商信息信息
     */
    @RequiresPermissions("franchisee")
    @ApiOperation(value = "修改加盟商信息", tags = {"后台-加盟商管理"})
    @PostMapping(value = "/update")
    public R<String> update(@RequestBody @Validated Franchisee franchisee) {
        checkFranchisee(franchisee);
        String md5Password = checkFranchisee(franchisee);
        SysUser sysUser = sysUserService.lambdaQuery()
                .eq(SysUser::getFranchiseeId, franchisee.getId())
                .eq(SysUser::getIsDelete, Constants.ZERO).one();
        if (null == sysUser) {
            sysUser = new SysUser();
            sysUser.setNickName(franchisee.getName());
            sysUser.setAccount(franchisee.getAdminAccount());
            sysUser.setPassword(md5Password);
            sysUser.setIsEnable(franchisee.getIsEnable());
            sysUser.setIsDelete(Constants.ZERO);
            sysUserService.save(sysUser);
        } else {
            // 生成sysUser账号
            sysUser.setNickName(franchisee.getName());
            sysUser.setAccount(franchisee.getAdminAccount());
            sysUser.setPassword(md5Password);
            sysUser.setIsEnable(franchisee.getIsEnable());
            sysUserService.updateById(sysUser);
        }
        return franchiseeService.updateById(franchisee) ? R.ok() : R.fail();
    }
    /**
     * 启用/关闭加盟商
     *
     * @param id     加盟商id
     * @param enable 启用/关闭
     */
    @RequiresPermissions("franchisee")
    @ApiOperation(value = "启用/关闭加盟商", tags = {"后台-加盟商管理"})
    @GetMapping(value = "/enable")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "加盟商id", name = "id", dataType = "Integer", required = true),
            @ApiImplicitParam(value = "0:关闭;1:启用", name = "enable", dataType = "Integer", required = true)
    })
    public R<String> enable(@RequestParam Integer id, @RequestParam Integer enable) {
        boolean update = franchiseeService.lambdaUpdate().set(Franchisee::getIsEnable, enable)
                .eq(Franchisee::getId, id).update();
        // 启动/关闭后台账号
        sysUserService.lambdaUpdate()
                .eq(SysUser::getFranchiseeId, id)
                .set(SysUser::getIsEnable, enable).update();
        return update ? R.ok() : R.fail();
    }
    /**
@@ -110,20 +200,39 @@
     *
     * @param franchisee 加盟商信息
     */
    private void checkFranchisee(Franchisee franchisee) {
        String city = franchisee.getCity();
        List<String> cityList = Arrays.stream(city.split(",")).collect(Collectors.toList());
        for (String c : cityList) {
            Franchisee one = franchiseeService.lambdaQuery().like(Franchisee::getCity, c)
                    .eq(Franchisee::getIsDelete, 0).one();
            if (null != one) {
                throw new GlobalException("当前所选城市中 " + c + " 已有加盟商所管辖!");
            }
    private String checkFranchisee(Franchisee franchisee) {
        LambdaQueryChainWrapper<Franchisee> wrapper = franchiseeService.lambdaQuery()
                .eq(Franchisee::getAdminAccount, franchisee.getAdminAccount())
                .eq(Franchisee::getIsDelete, 0);
        // 校验账号是否唯一
        Franchisee only;
        if (null == franchisee.getId()) {
            only = wrapper.one();
        } else {
            only = wrapper.ne(Franchisee::getId, franchisee.getId()).one();
        }
        // 校验后台账号是否存在当前账号
        SysUser user = sysUserService.lambdaQuery()
                .eq(SysUser::getAccount, franchisee.getAdminAccount())
                .eq(SysUser::getIsDelete, Constants.ZERO)
                .eq(SysUser::getFranchiseeId, null).one();
        if (null != only || null != user) {
            throw new GlobalException("该账号已存在!");
        }
//        String city = franchisee.getCity();
//        List<String> cityList = Arrays.stream(city.split(",")).collect(Collectors.toList());
//        for (String c : cityList) {
//            Franchisee one = franchiseeService.lambdaQuery().like(Franchisee::getCity, c)
//                    .eq(Franchisee::getIsDelete, 0).one();
//            if (null != one) {
//                throw new GlobalException("当前所选城市中 " + c + " 已有加盟商所管辖!");
//            }
//        }
        // MD5加密登录密码(新)
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String md5Password = passwordEncoder.encode(Constants.DEFAULT_PASSWORD);
        String md5Password = passwordEncoder.encode(franchisee.getAdminPassword());
        franchisee.setAdminPassword(md5Password);
        return md5Password;
    }
    /**
@@ -131,6 +240,7 @@
     *
     * @param ids 加盟商信息多条id拼接
     */
    @RequiresPermissions("franchisee")
    @ApiOperation(value = "批量删除加盟商信息", tags = {"后台-加盟商管理"})
    @GetMapping(value = "/batchDelete")
    @ApiImplicitParams({
@@ -140,6 +250,10 @@
        List<String> idList = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        List<Franchisee> list = franchiseeService.lambdaQuery().in(Franchisee::getId, idList).list();
        list.forEach(data -> data.setIsDelete(1));
        // 删除对应sysUser账号
        sysUserService.lambdaUpdate()
                .in(SysUser::getFranchiseeId, idList)
                .set(SysUser::getIsDelete, Constants.ONE).update();
        return franchiseeService.updateBatchById(list) ? R.ok() : R.fail();
    }