package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreImg;
import shop.alien.entity.store.StorePlatformMenu;
import shop.alien.entity.store.StoreUser;
import shop.alien.entity.store.UserLoginInfo;
import shop.alien.entity.store.dto.StoreUserPasswordDto;
import shop.alien.entity.store.vo.StoreInfoVo;
import shop.alien.entity.store.vo.StoreUserVo;
import shop.alien.entity.store.vo.StoreUserWithStoreVo;
import shop.alien.entity.store.vo.SubAccountStoreListVo;
import shop.alien.mapper.StoreImgMapper;
import shop.alien.mapper.StoreUserMapper;
import shop.alien.store.config.BaseRedisService;
import shop.alien.store.service.RoleMenuService;
import shop.alien.store.service.StoreInfoService;
import shop.alien.store.service.StoreUserPasswordService;
import shop.alien.store.service.StoreUserService;
import shop.alien.store.service.SubAccountStoreService;
import shop.alien.util.common.TokenInfo;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 门店用户 前端控制器
 *
 * @author ssk
 * @since 2024-12-11
 */
@Slf4j
@Api(tags = {"二期-门店用户"})
@ApiSort(8)
@CrossOrigin
@RestController
@RequestMapping("/store/user")
@RequiredArgsConstructor
public class StoreUserController {

    private final StoreUserService storeUserService;

    private final StoreUserPasswordService storeUserPasswordService;

    private final StoreUserMapper storeUserMapper;

    private final StoreInfoService storeInfoService;

    private final StoreImgMapper storeImgMapper;

    private final BaseRedisService baseRedisService;

    private final SubAccountStoreService subAccountStoreService;
    
    private final RoleMenuService roleMenuService;

    /**
     * 校验商户端账号是否禁用
     *
     * @param accountId
     * @return
     */
    @ApiOperation("校验账号是否被禁用")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({@ApiImplicitParam(name = "accountId", value = "账号id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/checkAccount")
    private R<StoreUserVo> checkAccount(Integer accountId) {
        return storeUserService.getById(accountId).getStatus() != 1 ? R.success("账号正常") : R.fail("账号禁用");
    }

    @ApiOperation("修改密码/忘记密码/更换绑定手机号")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "newPhone", value = "新手机号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "oldPassword", value = "旧密码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "confirmNewPassword", value = "新密码确认", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "verificationCode", value = "验证码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "类型:0:忘记密码,1:修改密码,2:更换绑定手机号", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/updatePassword")
    public R<String> updatePassword(String phone, String newPhone, String oldPassword, String newPassword, String confirmNewPassword, String verificationCode, Integer type) {
        log.info("StoreUserController.updatePassword?phone={}&newPhone={}&oldPassword={}&newPassword={}&confirmNewPassword={}&verificationCode={}&type={}", phone, newPhone, oldPassword, newPassword, confirmNewPassword, verificationCode, type);
        // 2025-11-04 验证码-商家端修改密码/忘记密码/更换绑定手机号
        String businessType = "";
        switch (type) {
            case 0:
                businessType = "forget_password";
                break;
            case 1:
                businessType = "modify_password";
                break;
            case 2:
                businessType = "modify_phone";
                break;
        }
        String cacheCode = baseRedisService.getString("verification_store_" + businessType + "_" + phone);
        if (StringUtils.isBlank(oldPassword) && null == cacheCode) {
            return R.fail("当验证码过期或未发送");
        }
        if (StringUtils.isBlank(oldPassword) && !cacheCode.trim().equals(verificationCode.trim())) {
            return R.fail("验证码错误，请重新输入");
        }
        return storeUserService.forgetOrModifyPassword(phone, newPhone, oldPassword, newPassword, confirmNewPassword, verificationCode, type);
    }

    @ApiOperation("设置当前商户账号登录密码（需登录）")
    @ApiOperationSupport(order = 3)
    @PostMapping("/password/set")
    public R<String> setPassword(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo,
                                 @RequestBody StoreUserPasswordDto dto) {
        log.info("StoreUserController.setPassword, userId={}",
                userLoginInfo == null ? null : userLoginInfo.getUserId());
        return storeUserPasswordService.setPassword(userLoginInfo, dto);
    }

    @ApiOperation("更换绑定手机号效验原密码或验证码")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true), @ApiImplicitParam(name = "oldPassword", value = "旧密码", dataType = "String", paramType = "query"), @ApiImplicitParam(name = "verificationCode", value = "验证码", dataType = "String", paramType = "query")})
    @GetMapping("/changePhoneVerification")
    public R<Map<String, String>> changePhoneVerification(String phone, String oldPassword, String verificationCode) {
        log.info("StoreUserController.changePhoneVerification?phone={}&oldPassword={}&verificationCode={}", phone, oldPassword, verificationCode);
        // 2025-11-04 验证码-商家端更换绑定手机号效验原密码或验证码
        String cacheCode = baseRedisService.getString("verification_store_modify_phone_" + phone);
        if (null == cacheCode) {
            return R.fail("当验证码过期或未发送");
        }
        if (!cacheCode.trim().equals(verificationCode.trim())) {
            return R.fail("验证码错误，请重新输入");
        }
        try {
            return R.data(storeUserService.changePhoneVerification(phone, oldPassword, verificationCode));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("修改用户信息")
    @ApiOperationSupport(order = 3)
    @PostMapping("/setUserInfo")
    public R<Boolean> setUserInfo(@RequestBody StoreUser storeUserVo) {
        log.info("StoreUserController.setUserInfo?storeUserVo={}", storeUserVo);
        if (storeUserService.setUserInfo(storeUserVo)) {
            return R.success("修改成功");
        }
        return R.fail("修改失败");
    }

    @ApiOperation("根据手机号获取用户")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/getUserByPhone")
    public R<StoreUser> getUserByPhone(String phone) {
        log.info("StoreUserController.getUserInfo?phone={}", phone);
        StoreUser storeUser = storeUserService.getUserByPhone(phone);
        if (null != storeUser) {
            return R.data(storeUser);
        }
        return R.fail("手机号不存在");
    }

    @ApiOperation("查询店铺用户及绑定店铺信息")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "查询类型：1-店铺用户ID，2-店铺ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "店铺用户ID或店铺ID", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/getStoreUserWithStore")
    public R<StoreUserWithStoreVo> getStoreUserWithStore(@RequestParam("type") Integer type,
                                                         @RequestParam("id") Integer id) {
        log.info("StoreUserController.getStoreUserWithStore?type={}&id={}", type, id);
        return storeUserService.getStoreUserWithStore(type, id);
    }

    @ApiOperation("修改支付密码")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键", dataType = "String", paramType = "query", required = true), @ApiImplicitParam(name = "payPassword", value = "支付密码", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/setPayPassword")
    public R<Boolean> setPayPassword(Integer id, String payPassword) {
        if (storeUserService.setPayPassword(id, payPassword)) {
            return R.success("修改成功");
        }
        return R.fail("修改失败");
    }

    @ApiOperation("修改用户状态")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true), @ApiImplicitParam(name = "type", value = "修改类型 (pass:密码状态改为1, face:人脸状态改为2)", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/updateUserStatus")
    public R<StoreUser> updateUserStatus(String phone, String type) {
        log.info("StoreUserController.updateUserStatus?phone={}&type={}", phone, type);
        if (storeUserService.updateUserStatus(phone, type)) {
            return R.success("修改成功");
        }
        return R.fail("修改失败");
    }

    @ApiOperation("修改商户信息")
    @ApiOperationSupport(order = 7)
    @PostMapping("/setStoreUserInfo")
    public R<Boolean> setStoreUserInfo(@RequestBody StoreInfoVo storeInfo) {
        log.info("StoreUserController.setStoreUserInfo?storeInfo={}", storeInfo);

        // 查询店铺图片信息
        LambdaQueryWrapper<StoreImg> queryWrapper = new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getImgType, 10).eq(StoreImg::getStoreId, storeInfo.getId());
        StoreImg storeImg = storeImgMapper.selectOne(queryWrapper);

        // 如果查询到店铺图片信息，则更新图片 URL
        if (storeImg != null) {
            storeImg.setImgUrl(storeInfo.getImgUrl());
            int imgUpdateResult = storeImgMapper.updateById(storeImg);
            if (imgUpdateResult == 0) {
                log.error("更新店铺图片信息失败，storeInfo: {}", storeInfo);
                return R.fail("修改失败，更新店铺图片信息失败");
            }
        } else {
            StoreImg storeImg1 = new StoreImg();
            storeImg1.setStoreId(storeInfo.getId());
            storeImg1.setImgType(10);
            storeImg1.setImgUrl(storeInfo.getImgUrl());
            storeImg1.setImgSort(0);
            int insert = storeImgMapper.insert(storeImg1);
            if (insert == 0) {
                log.error("添加店铺图片信息失败，storeInfo: {}", storeInfo);
                return R.fail("添加失败，添加店铺图片信息失败");
            }
        }

        // 更新店铺信息
        boolean infoUpdateResult = storeInfoService.updateById(storeInfo);
        if (!infoUpdateResult) {
            log.error("更新店铺信息失败，storeInfo: {}", storeInfo);
            return R.fail("修改失败，更新店铺信息失败");
        }

        // 所有更新操作成功，返回成功信息
        return R.success("修改成功");

    }

    /**
     * web端查询用户列表（支持主账号和子账号查询）
     */
    @ApiOperation("web端查询用户列表（支持主账号和子账号查询）")
    @ApiOperationSupport(order = 7)
    @GetMapping("/getStoreUserList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize", value = "页容", dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "id", value = "账号ID（主账号为自身id，子账号为子账号id）", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "phone", value = "联系电话（主账号/子账号自身电话）", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "accountType", value = "账号类型：1-主账号，2-子账号", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "mainAccountId", value = "主账号ID（子账号列表时支持模糊查询）", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "mainAccountPhone", value = "主账号联系电话（子账号列表时支持模糊查询）", dataType = "String", paramType = "query", required = false)
    })
    public R<IPage<StoreUserVo>> getStoreUserList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, @RequestParam(required = false) String id, @RequestParam(required = false) String phone, @RequestParam(required = false) Integer status, @RequestParam(required = true) Integer accountType, @RequestParam(required = false) String mainAccountId, @RequestParam(required = false) String mainAccountPhone) {
        log.info("StoreUserController.getStoreUserList?pageNum={},pageSize={},id={},phone={},status={},accountType={},mainAccountId={},mainAccountPhone={}", pageNum, pageSize, id, phone, status, accountType, mainAccountId, mainAccountPhone);
        return storeUserService.getStoreUserList(pageNum, pageSize, id, phone, status, accountType, mainAccountId, mainAccountPhone);
    }

    /**
     * web端新增商家端用户
     */
    @ApiOperation("web端新增商家端用户")
    @ApiOperationSupport(order = 7)
    @PostMapping("/addStoreUser")
    public R<StoreUserVo> addStoreUser(@RequestBody StoreUserVo storeUserVo) {
        log.info("StoreUserController.addStoreUser?storeUserVo={}", storeUserVo);
        try {
            R<StoreUserVo> storeUserVoR = storeUserService.addStoreUser(storeUserVo);
            return storeUserVoR;
        } catch (RuntimeException e) {
            log.error("新增商家端用户失败，手机号：{}", storeUserVo.getPhone(), e);
            if (e.getMessage() != null && e.getMessage().contains("手机号已存在")) {
                return R.fail("手机号已存在，请更换手机号");
            }
            return R.fail("新增商家端用户失败：" + e.getMessage());
        }
    }

    /**
     * web端编辑商家端用户
     */
    @ApiOperation("web端编辑商家端用户")
    @ApiOperationSupport(order = 7)
    @PostMapping("/editStoreUser")
    public R<StoreUserVo> editStoreUser(@RequestBody StoreUser storeUser) {
        log.info("StoreUserController.editStoreUser?storeUser={}", storeUser);
        R<StoreUserVo> storeUserVoR = storeUserService.editStoreUser(storeUser);
        return storeUserVoR;
    }

    /**
     * web端重置商家端用户密码
     */
    @ApiOperation("web端编辑商家端用户")
    @ApiOperationSupport(order = 7)
    @PutMapping("/resetStoreUserPassword")
    public R<StoreUserVo> resetStoreUserPassword(@RequestBody StoreUser storeUser) {
        log.info("StoreUserController.resetStoreUserPassword?storeUser={}", storeUser);
        storeUserService.resetStoreUserPassword(storeUser);
        return R.success("初始化成功");
    }



    /**
     * web端切换商家端用户状态  （主账号有子账号时禁止禁用；子账号无限制；返回更新后状态供前端及时展示）
     */
    @ApiOperation("web端切换商家端用户状态（主账号有子账号时禁止禁用；返回更新后状态供前端及时展示）")
    @ApiOperationSupport(order = 7)
    @PostMapping("/switchingStates")
    public R<StoreUserVo> switchingStates(@RequestBody StoreUser storeUser) {
        log.info("StoreUserController.switchingStates?storeUser={}", storeUser);
        try {
            return storeUserService.switchingStates(storeUser);
        } catch (Exception e) {
            log.warn("StoreUserController.switchingStates 校验或切换失败: {}", e.getMessage());
            return R.fail(e.getMessage());
        }
    }


    @ApiOperation(value = "web端导出商家端账号相关信息")
    @ApiOperationSupport(order = 6)
    @GetMapping("/exportExcel")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户id", dataType = "String", paramType = "query"), @ApiImplicitParam(name = "phone", value = "联系电话", dataType = "String", paramType = "query"), @ApiImplicitParam(name = "status", value = "状态", dataType = "String", paramType = "query"),@ApiImplicitParam(name = "accountType", value = "账号类型：1-主账号，2-子账号", dataType = "int", paramType = "query")})
    @ResponseBody
    public R exportExcel(@RequestParam(value = "id", required = false) String id, @RequestParam(value = "phone", required = false) String phone, @RequestParam(value = "status", required = false) String status, @RequestParam(required = false) Integer accountType) throws IOException {
        log.info("StoreInfoController.exportExcel");
        String excelPath = storeUserService.exportExcel(id, phone, status,accountType);
        return R.data(excelPath);
    }


    /**
     * register
     *
     * @param phone
     * @param password
     * @return
     */
    @ApiOperation("门店用户注册")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true), @ApiImplicitParam(name = "password", value = "密码", dataType = "String", paramType = "query", required = true), @ApiImplicitParam(name = "code", value = "验证码", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/register")
    public R<Boolean> register(@RequestParam("phone") String phone, @RequestParam("password") String password, @RequestParam("code") String code) {
        log.info("StoreUserController.register?phone={}&password={}&code={}", phone, password, code);
        // 2025-11-04 验证码-商家端注册
        String cacheCode = baseRedisService.getString("verification_store_register_" + phone);
        if (null == cacheCode) {
            return R.fail("当验证码过期或未发送");
        }
        if (!cacheCode.trim().equals(code.trim())) {
            return R.fail("验证码错误，请重新输入");
        }
        log.info("StoreUserController.register?phone={}&password={}", phone, password);
        return storeUserService.register(phone, password);
    }


    /**
     * register
     *
     * @param phone
     * @return
     */
    @ApiOperation("门店用户注册校验")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/checkRegister")
    public R<Boolean> checkRegister(String phone) {
        log.info("StoreUserController.register?phone={}", phone);
        Boolean res = storeUserService.checkRegister(phone);
        return R.data(res);
    }


    /**
     * register
     *
     * @param phone
     * @return
     */
    @ApiOperation("门店用户登录校验")
    @ApiOperationSupport(order = 9)
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/checkLogin")
    public R<StoreUser> checkLogin(String phone) {
        log.info("StoreUserController.checkLogin?phone={}", phone);
        return storeUserService.checkLogin(phone);
    }

    /**
     * 商家端注销用户效验
     */
    @ApiOperation("商家端注销用户效验")
    @PostMapping("/storeCancelAccountVerification")
    public R<Map<String, String>> storeCancelAccountVerification(@RequestBody StoreUserVo storeUserVo) {
        log.info("StoreUserController.storeCancelAccountVerification?storeUserVo={}", storeUserVo);
        return R.data(storeUserService.storeCancelAccountVerification(storeUserVo));
    }

    /**
     * 商家端注销用户
     */
    @ApiOperation("商家端注销用户")
    @PostMapping("/storeCancelAccount")
    public R<StoreUserVo> storeCancelAccount(@RequestBody StoreUserVo storeUserVo) {
        log.info("StoreUserController.storeCancelAccount?storeUserVo={}", storeUserVo);
        // 2025-11-04 验证码-商家端注册
        String cacheCode = baseRedisService.getString("verification_store_cancel_account_" + storeUserVo.getPhone());
        if (null == cacheCode) {
            return R.fail("当验证码过期或未发送");
        }
        if (!cacheCode.trim().equals(storeUserVo.getVerificationCode().trim())) {
            return R.fail("验证码错误，请重新输入");
        }
        try {
            storeUserService.storeCancelAccount(storeUserVo);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.success("注销成功");
    }

    /**
     * 商家端撤销注销用户
     */
    @ApiOperation("商家端撤销注销用户")
    @PostMapping("/storeCancelAccountUn")
    public R<StoreUserVo> storeCancelAccountUn(@RequestBody StoreUserVo storeUserVo) {
        log.info("StoreUserController.storeCancelAccountUn?storeUserVo={}", storeUserVo);
        storeUserService.storeCancelAccountUn(storeUserVo);
        return R.success("撤回注销成功");
    }

    /**
     * 手动删除商家账号及店铺。
     * 主账号：有关联店铺或有关联子账号禁止删除。
     * 子账号：按 userId(subAccountId)+storeId 逻辑删除角色；若主账号下仅 1 个子账号且非主账号则同时逻辑删除 store_user。
     */
    @ApiOperation("手动删除商家账号及店铺")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "subAccountId", value = "子账号用户ID(store_user.id)", dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "id", value = "主账号时为主账号ID；子账号时可作主账号id(parentAccountId)时传 parentAccountId", dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "parentAccountId", value = "主账号ID(子账号删除时必传，与分页列表一致)", dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "accountType", value = "账号类型：1-主账号，2-子账号", dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "phone", value = "子账号联系电话(可选，与 subAccountId 二选一)", dataType = "String", paramType = "body")
    })
    @PostMapping("/deleteStoreAccountInfo")
    public R<String> deleteStoreAccountInfo(@RequestBody StoreUser storeUser) {
        log.info("StoreUserController.deleteStoreAccountInfo? id={}", storeUser.getId());
        String errMsg = storeUserService.deleteStoreAccountInfo(storeUser);
        if ("删除成功".equals(errMsg)) {
            return R.success("删除成功");
        }
        return R.fail(errMsg);
    }

    /**
     * 根据id判断是否有支付密码
     */
    @ApiOperation("是否有支付密码")
    @GetMapping("/havePayPassword")
    public R<Boolean> havePayPassword(@RequestParam String storeUserId, @RequestParam(required = false) String password) {
        log.info("StoreUserController.havePayPassword?storeUserId={},password={}", storeUserId, password);
        return storeUserService.havePayPassword(storeUserId, password);
    }

    /**
     * 支付设置增加支付宝账户
     */
    @ApiOperation("支付设置增加支付宝账户")
    @PostMapping("/addAlipayAccount")
    public R<Integer> addAlipayAccount(@RequestBody StoreUserVo storeUserVo) {
        log.info("StoreUserController.addAlipayAccount?storeUserVo={}", storeUserVo);
        LambdaUpdateWrapper<StoreUser> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(StoreUser::getAlipayAccount, storeUserVo.getAlipayAccount());
        lambdaUpdateWrapper.eq(StoreUser::getId, storeUserVo.getId());
        return R.data(storeUserMapper.update(null, lambdaUpdateWrapper));
    }

    /**
     * 子账号关联门店列表
     */
    @ApiOperation(value = "子账号关联门店列表", notes = "根据用户ID查询子账号关联的门店列表")
    @ApiOperationSupport(order = 10)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID（store_user.id）", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getSubAccountStoreList")
    public R<List<SubAccountStoreListVo>> getSubAccountStoreList(@RequestParam("userId") Integer userId) {
        log.info("StoreUserController.getSubAccountStoreList?userId={}", userId);
        List<SubAccountStoreListVo> storeList = subAccountStoreService.getSubAccountStoreList(userId);
        return R.data(storeList);
    }

    /**
     * 通过角色ID查询权限菜单
     */
    @ApiOperation(value = "通过角色ID查询权限菜单", notes = "根据角色ID查询该角色拥有的所有权限菜单")
    @ApiOperationSupport(order = 11)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", dataType = "Long", paramType = "query", required = true)
    })
    @GetMapping("/getMenusByRoleId")
    public R<List<StorePlatformMenu>> getMenusByRoleId(@RequestParam("roleId") Long roleId) {
        log.info("StoreUserController.getMenusByRoleId?roleId={}", roleId);
        
        if (roleId == null) {
            log.warn("角色ID为空");
            return R.fail("角色ID不能为空");
        }
        
        try {
            List<StorePlatformMenu> menus = roleMenuService.getMenusByRoleId(roleId);
            return R.data(menus);
        } catch (Exception e) {
            log.error("查询角色权限菜单失败 - roleId: {}", roleId, e);
            return R.fail("查询菜单失败：" + e.getMessage());
        }
    }
}
