package shop.alien.store.controller;

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.StoreStaffConfig;
import shop.alien.entity.store.UserLoginInfo;
import shop.alien.entity.store.dto.StoreStaffConfigListQueryDto;
import shop.alien.entity.store.vo.*;
import shop.alien.mapper.StoreDictionaryMapper;
import shop.alien.store.service.StoreStaffConfigService;
import shop.alien.store.util.CommonConstant;
import shop.alien.util.common.TokenInfo;
import springfox.documentation.annotations.ApiIgnore;

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

/**
 * @Author: fcw
 * @CreateTime: 2025-04-11
 * @Description: 员工管理
 */
@Api(tags = {"员工管理"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/storeStaffConfig")
@RequiredArgsConstructor
public class StoreStaffConfigController {

    private final StoreStaffConfigService storeStaffConfigService;

    private final StoreDictionaryMapper storeDictionaryMapper;

    @ApiOperation("员工列表（商家端）")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "店铺ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "page", value = "分页页数", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "status", value = "员工状态(0-待审核 1-审核通过 2-审核拒绝)", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "onlineStatus", value = "上线状态（0-上线 1-下线）", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "staffPosition", value = "职位", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "staffName", value = "员工姓名", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "startCreatedTime", value = "创建时间开始（yyyy-MM-dd HH:mm:ss）", dataType = "Date", paramType = "query", required = false),
            @ApiImplicitParam(name = "endCreatedTime", value = "创建时间结束（yyyy-MM-dd HH:mm:ss）", dataType = "Date", paramType = "query", required = false),
    })
    @GetMapping("/getStaffConfigList")
    public R<IPage<StoreStaffConfig>> getStaffConfigList(StoreStaffConfigListQueryDto query) {
        Integer storeId = query == null ? null : query.getStoreId();
        log.info("StoreStaffConfigController.getStaffConfigList?storeId={},query={}", storeId, query);
//        if (storeId == null) {
//            return R.fail("storeId未传入,请检查参数");
//        }
        return R.data(storeStaffConfigService.getStaffConfigList(query));
    }

    @ApiOperation("新增修改员工（商家端）")
    @ApiOperationSupport(order = 2)
    @PostMapping("/addOrUpdateStaffConfig")
    public R<String> addOrUpdateStaffConfig(@RequestBody StoreStaffConfig storeStaffConfig) {
        log.info("StoreStaffConfigController.addOrUpdateStaffConfig?multipartRequest={}, storeStaffConfig={}", storeStaffConfig);
        int num = storeStaffConfigService.addOrUpdateStaffConfig(storeStaffConfig);
        if (0 == num) {
            return R.fail("新增修改员工失败");
        } else {
            return R.data("新增修改员工成功");
        }
    }

    @ApiOperation("员工审核")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "comment", value = "审批意见", dataType = "String", paramType = "query")})
    @GetMapping("/audit")
    public R<Integer> audit(Integer id, Integer status, String rejectionReason) {
        log.info("StoreStaffConfigController.audit?id={},status={},rejectionReason={}", id, status, rejectionReason);
        return R.data(storeStaffConfigService.audit(id, status, rejectionReason));
    }

    @ApiOperation("员工详情（商家端）")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getStaffConfigDeatail")
    public R<StoreStaffConfig> getStaffConfigDeatail(Integer id) {
        log.info("StoreStaffConfigController.getStaffConfigDeatail?id={}", id);
        return R.data(storeStaffConfigService.getStaffConfigDeatail(id));
    }

    @ApiOperation("员工导出")
    @ApiOperationSupport(order = 11)
    @GetMapping("/staffConfigExport")
    public R<String> staffConfigExport(@RequestParam(required = false) String status) throws IOException {
        log.info("PlatformStoreCouponController.staffConfigExport");
        String s = storeStaffConfigService.staffConfigExport(status);
        return R.data(s);
    }

    @ApiOperation("删除员工（商家端）")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/deleteStaffConfig")
    public R<Integer> deleteStaffConfig(Integer id) {
        log.info("StoreStaffConfigController.deleteStaffConfig?id={}", id);
        return R.data(storeStaffConfigService.deleteStaffConfig(id));
    }

    @ApiOperation("置顶员工（商家端）")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "topStatus", value = "置顶状态 0-取消置顶, 1-置顶", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/setTopStatus")
    public R<Integer> setTopStatus(Integer id, Integer topStatus) {
        log.info("StoreStaffConfigController.setTopStatus?id={},topStatus={}", id, topStatus);
        return R.data(storeStaffConfigService.setTopStatus(id, topStatus));
    }

    @ApiOperation("设置员工上线/下线状态（商家端）")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "onlineStatus", value = "上线状态 0-上线, 1-下线", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/setOnlineStatus")
    public R<Integer> setOnlineStatus(Integer id, Integer onlineStatus) {
        log.info("StoreStaffConfigController.setOnlineStatus?id={},onlineStatus={}", id, onlineStatus);
        return R.data(storeStaffConfigService.setOnlineStatus(id, onlineStatus));
    }


    /**
     * 查询员工职位列表（商家端）
     *
     * @param storeId 店铺ID，必填
     * @return 员工职位名称列表
     */
    @ApiOperation("查询员工职位列表（商家端）")
    @ApiOperationSupport(order = 11)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "店铺ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getStaffPositionList")
    public R<List<String>> getStaffPositionList(
            @RequestParam(value = "storeId") Integer storeId) {
        log.info("查询员工职位列表，参数：storeId={}", storeId);

        try {
            // 参数校验
            if (storeId == null || storeId <= 0) {
                log.warn("查询员工职位列表失败，店铺ID无效：storeId={}", storeId);
                return R.fail("店铺ID不能为空且必须大于0");
            }

            List<String> result = storeStaffConfigService.getStaffPositionList(storeId);
            log.info("查询员工职位列表成功，storeId={}，职位数量：{}", storeId, result.size());
            return R.data(result);
        } catch (IllegalArgumentException e) {
            log.warn("查询员工职位列表失败，参数错误：{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("查询员工职位列表异常，storeId={}，异常信息：{}", storeId, e.getMessage(), e);
            return R.fail("查询失败：" + e.getMessage());
        }
    }


    /**
     * 员工列表查询接口（用户端）
     * <p>
     * 根据店铺ID查询员工列表，支持按状态和职位筛选，返回结果包含员工基本信息和今日是否有演出标识
     * </p>
     *
     * @param page          分页页数，默认1，必须大于0
     * @param size          分页条数，默认10，必须大于0且不超过100
     * @param storeId       店铺ID，必填，必须大于0
     * @param status        员工状态，可选（0-待审核 1-审核通过 2-审核拒绝）
     * @param staffPosition 员工职位，可选，支持精确匹配
     * @return 员工列表分页结果，包含员工基本信息和今日是否有演出标识
     */
    @ApiOperation("员工列表查询（用户端）")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页页数", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "storeId", value = "店铺ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "员工状态(0-待审核 1-审核通过 2-审核拒绝)", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "staffPosition", value = "员工职位", dataType = "String", paramType = "query", required = false)
    })
    @GetMapping("/queryStaffList")
    public R<IPage<StoreStaffConfig>> queryStaffList(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "storeId") Integer storeId,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "staffPosition", required = false) String staffPosition) {
        log.info("查询员工列表，参数：page={}, size={}, storeId={}, status={}, staffPosition={}",
                page, size, storeId, status, staffPosition);

        try {
            // 参数校验
            if (storeId == null || storeId <= 0) {
                log.warn("查询员工列表参数校验失败，店铺ID无效：storeId={}", storeId);
                return R.fail("店铺ID不能为空且必须大于0");
            }
            if (page != null && page < 1) {
                log.warn("查询员工列表参数校验失败，分页页数无效：page={}", page);
                return R.fail("分页页数必须大于0");
            }
            if (size != null && size < 1) {
                log.warn("查询员工列表参数校验失败，分页条数无效：size={}", size);
                return R.fail("分页条数必须大于0");
            }
            if (size != null && size > CommonConstant.MAX_PAGE_SIZE) {
                log.warn("查询员工列表参数校验失败，分页条数超过最大值：size={}", size);
                return R.fail("分页条数不能超过" + CommonConstant.MAX_PAGE_SIZE);
            }

            // 规范化分页参数
            Integer normalizedPage = (page == null || page < 1) ? CommonConstant.DEFAULT_PAGE_NUM : page;
            Integer normalizedSize;
            if (size == null || size < 1) {
                normalizedSize = CommonConstant.DEFAULT_PAGE_SIZE;
            } else if (size > CommonConstant.MAX_PAGE_SIZE) {
                normalizedSize = CommonConstant.MAX_PAGE_SIZE;
            } else {
                normalizedSize = size;
            }

            // 调用服务层查询
            IPage<StoreStaffConfig> result = storeStaffConfigService.queryStaffList(
                    normalizedPage, normalizedSize, storeId, status, staffPosition);

            log.info("查询员工列表成功，店铺ID={}，共{}条记录，当前页{}条",
                    storeId, result.getTotal(), result.getRecords() != null ? result.getRecords().size() : 0);
            return R.data(result);
        } catch (IllegalArgumentException e) {
            log.warn("查询员工列表参数错误，storeId={}，错误信息：{}", storeId, e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("查询员工列表异常，storeId={}，异常信息：{}", storeId, e.getMessage(), e);
            return R.fail("查询员工列表失败，请稍后重试");
        }
    }

    /**
     * 员工详情查询接口（用户端）
     * <p>
     * 查询员工基本信息以及关联的演出安排列表
     * 演出安排列表包含未来30天内的所有演出时间，格式：12月12日 周五 20:30-次日00:00
     * </p>
     *
     * @param id 员工主键ID，必填，必须大于0
     * @return 员工详情（包含演出列表）
     */
    @ApiOperation("员工详情查询（用户端，包含演出列表）")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "员工主键ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/queryStaffDetail")
    public R<StoreStaffDetailWithPerformanceVo> queryStaffDetail(
            @RequestParam(value = "id", required = true) Integer id,
            @ApiIgnore @TokenInfo UserLoginInfo userLoginInfo) {
        log.info("查询员工详情（包含演出列表），id={}，userId={}", 
                id, userLoginInfo != null ? userLoginInfo.getUserId() : null);

        try {
            // 参数校验
            if (id == null || id <= 0) {
                log.warn("查询员工详情参数校验失败，员工ID无效：id={}", id);
                return R.fail("员工ID不能为空且必须大于0");
            }

            // 获取用户ID（可能为null，如果用户未登录）
            String userId = "";
            if (userLoginInfo != null && StringUtils.isNoneBlank(userLoginInfo.getType()) && StringUtils.isNoneBlank(userLoginInfo.getUserPhone())) {
                userId = userLoginInfo.getType()+ "_" + userLoginInfo.getUserPhone();
            }

            // 调用服务层查询
            StoreStaffDetailWithPerformanceVo result =
                    storeStaffConfigService.queryStaffDetailWithPerformance(id, userId);

            if (result == null) {
                log.warn("查询员工详情失败，员工不存在：id={}", id);
                return R.fail("员工不存在");
            }

            log.info("查询员工详情成功，id={}，演出安排数量：{}，是否喜欢：{}",
                    id, result.getPerformanceScheduleList() != null ? result.getPerformanceScheduleList().size() : 0,
                    result.getIsLiked());
            return R.data(result);
        } catch (IllegalArgumentException e) {
            log.warn("查询员工详情参数错误，id={}，错误信息：{}", id, e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("查询员工详情异常，id={}，异常信息：{}", id, e.getMessage(), e);
            return R.fail("查询员工详情失败，请稍后重试");
        }
    }

    /**
     * 员工列表查询（按标题分组）（用户端）
     * <p>
     * 根据店铺ID查询store_staff_title表中的记录，并通过staff_ids关联出store_staff_config
     * 返回按标题分组的员工列表，每个标题下包含对应的员工信息
     * 保留原有逻辑：今日是否有演出、点赞数、好评数等
     * </p>
     *
     * @param storeId 店铺ID，必填，必须大于0
     * @return 按标题分组的员工列表
     */
    @ApiOperation("员工列表查询（按标题分组）（用户端）")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "店铺ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/queryStaffListByTitle")
    public R<List<StaffTitleGroupVo>> queryStaffListByTitle(
            @RequestParam(value = "storeId") Integer storeId) {
        log.info("查询员工列表（按标题分组），参数：storeId={}", storeId);

        try {
            // 参数校验
            if (storeId == null || storeId <= 0) {
                log.warn("查询员工列表（按标题分组）参数校验失败，店铺ID无效：storeId={}", storeId);
                return R.fail("店铺ID不能为空且必须大于0");
            }

            // 调用服务层查询
            List<StaffTitleGroupVo> result = storeStaffConfigService.queryStaffListByTitle(storeId);

            log.info("查询员工列表（按标题分组）成功，店铺ID={}，标题数量：{}", storeId, result != null ? result.size() : 0);
            return R.data(result);
        } catch (IllegalArgumentException e) {
            log.warn("查询员工列表（按标题分组）参数错误，storeId={}，错误信息：{}", storeId, e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("查询员工列表（按标题分组）异常，storeId={}，异常信息：{}", storeId, e.getMessage(), e);
            return R.fail("查询员工列表（按标题分组）失败，请稍后重试");
        }
    }

    /**
     * 根据标题ID查询员工列表（用户端）
     * <p>
     * 根据店铺ID和标题ID查询store_staff_title表中的记录，并通过staff_ids关联出store_staff_config
     * 返回该标题下的员工列表
     * 保留原有逻辑：今日是否有演出、点赞数、好评数等
     * </p>
     *
     * @param storeId 店铺ID，必填，必须大于0
     * @param titleId 标题ID，必填，必须大于0
     * @return 员工列表
     */
    @ApiOperation("根据标题ID查询员工列表（用户端）")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "店铺ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "titleId", value = "标题ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/queryStaffListByTitleId")
    public R<List<StoreStaffConfig>> queryStaffListByTitleId(
            @RequestParam(value = "storeId") Integer storeId,
            @RequestParam(value = "titleId") Integer titleId) {
        log.info("根据标题ID查询员工列表，参数：storeId={}, titleId={}", storeId, titleId);

        try {
            // 参数校验
            if (storeId == null || storeId <= 0) {
                log.warn("根据标题ID查询员工列表参数校验失败，店铺ID无效：storeId={}", storeId);
                return R.fail("店铺ID不能为空且必须大于0");
            }
            if (titleId == null || titleId <= 0) {
                log.warn("根据标题ID查询员工列表参数校验失败，标题ID无效：titleId={}", titleId);
                return R.fail("标题ID不能为空且必须大于0");
            }

            // 调用服务层查询
            List<StoreStaffConfig> result = storeStaffConfigService.queryStaffListByTitleId(storeId, titleId);

            log.info("根据标题ID查询员工列表成功，店铺ID={}，标题ID={}，员工数量：{}",
                    storeId, titleId, result != null ? result.size() : 0);
            return R.data(result);
        } catch (IllegalArgumentException e) {
            log.warn("根据标题ID查询员工列表参数错误，storeId={}, titleId={}，错误信息：{}",
                    storeId, titleId, e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("根据标题ID查询员工列表异常，storeId={}, titleId={}，异常信息：{}",
                    storeId, titleId, e.getMessage(), e);
            return R.fail("根据标题ID查询员工列表失败，请稍后重试");
        }
    }

    @ApiOperation("获取美食员工列表")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页页数", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "storeId", value = "店铺ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "staffPosition", value = "员工职位", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "status", value = "员工状态", dataType = "String", paramType = "query", required = false)
    })
    @GetMapping("/getFoodStaffConfigList")
    public R<IPage<StoreStaffConfig>> getFoodStaffConfigList(@RequestParam(value = "page", defaultValue = "1") int page,
                                                         @RequestParam(value = "size", defaultValue = "10") int size,
                                                         @RequestParam(value = "storeId", required = true) Integer storeId,
                                                         @RequestParam(value = "staffPosition", required = false) String staffPosition,
                                                         @RequestParam(value = "status", required = false) String status) {
        log.info("StoreStaffConfigController.getFoodStaffConfigList?page={}&size={}&storeId={}&staffPosition={}&status={}", page, size, storeId, staffPosition, status);
        return R.data(storeStaffConfigService.getFoodStaffConfigList(page, size, storeId, staffPosition, status));
    }

    /**
     * 根据ID查询员工详情（包含员工信息和课程列表）
     *
     * @param id 员工主键ID
     * @return 员工详情（包含员工信息和课程列表）
     */
    @ApiOperation("根据ID查询员工详情（包含员工信息和课程列表）用户端")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "员工主键ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getStaffDetailWithCourse")
    public R<StoreStaffDetailVo> getStaffDetailWithCourse(
            @RequestParam(value = "id") Integer id) {
        log.info("查询员工详情（包含课程信息），id={}", id);

        try {
            // 参数校验
            if (id == null || id <= 0) {
                log.warn("查询员工详情失败，员工ID无效：{}", id);
                return R.fail("员工ID不能为空且必须大于0");
            }

            StoreStaffDetailVo result = storeStaffConfigService.getStaffDetailWithCourse(id);

            if (result == null) {
                log.warn("查询员工详情失败，员工不存在：id={}", id);
                return R.fail("员工不存在");
            }

            log.info("查询员工详情成功，id={}，课程数量：{}", id,
                    result.getCourseList() != null ? result.getCourseList().size() : 0);
            return R.data(result);
        } catch (Exception e) {
            log.error("查询员工详情异常，id={}，异常信息：{}", id, e.getMessage(), e);
            return R.fail("查询失败：" + e.getMessage());
        }
    }

    /**
     * 查询健身教练详情（包含员工信息、基本信息、认证/荣誉列表和从业经历）
     *
     * @param id 员工主键ID
     * @return 健身教练详情（包含员工信息、基本信息、认证/荣誉列表和从业经历）
     */
    @ApiOperation("查询健身教练详情（包含员工信息、基本信息、认证/荣誉列表和从业经历）用户端")
    @ApiOperationSupport(order = 9)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "员工主键ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getFitnessCoachDetail")
    public R<StoreStaffFitnessDetailVo> getFitnessCoachDetail(
            @RequestParam(value = "id") Integer id) {
        log.info("查询健身教练详情，id={}", id);

        try {
            // 参数校验
            if (id == null || id <= 0) {
                log.warn("查询健身教练详情失败，员工ID无效：{}", id);
                return R.fail("员工ID不能为空且必须大于0");
            }
            
            StoreStaffFitnessDetailVo result = storeStaffConfigService.getFitnessCoachDetail(id);
            
            if (result == null) {
                log.warn("查询健身教练详情失败，员工不存在：id={}", id);
                return R.fail("员工不存在");
            }

            return R.data(result);
        } catch (Exception e) {
            log.error("查询健身教练详情异常，id={}，异常信息：{}", id, e.getMessage(), e);
            return R.fail("查询失败：" + e.getMessage());
        }
    }

    /**
     * 查询指定店铺的员工职位统计信息
     *
     * @param storeId 店铺ID，必填
     * @return 员工职位统计列表，每个元素包含职位名称和对应员工数量
     */
    @ApiOperation("查询员工职位统计（用户端）")
    @ApiOperationSupport(order = 10)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "店铺ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getStaffPositionCount")
    public R<List<StoreStaffPositionCountVo>> getStaffPositionCount(
            @RequestParam(value = "storeId") Integer storeId) {
        log.info("查询员工职位统计，参数：storeId={}", storeId);

        try {
            // 参数校验
            if (storeId == null || storeId <= 0) {
                log.warn("查询员工职位统计失败，店铺ID无效：storeId={}", storeId);
                return R.fail("店铺ID不能为空且必须大于0");
            }

            List<StoreStaffPositionCountVo> result = storeStaffConfigService.getStaffPositionCount(storeId);
            log.info("查询员工职位统计成功，storeId={}，职位数量：{}", storeId, result.size());
            return R.data(result);
        } catch (IllegalArgumentException e) {
            log.warn("查询员工职位统计失败，参数错误：{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("查询员工职位统计异常，storeId={}，异常信息：{}", storeId, e.getMessage(), e);
            return R.fail("查询失败：" + e.getMessage());
        }
    }

}
