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.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.FitnessEquipmentInfo;
import shop.alien.store.service.FitnessEquipmentInfoService;

import java.util.List;

/**
 * 健身设备信息控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"健身设备信息管理"})
@ApiSort(1)
@CrossOrigin
@RestController
@RequestMapping("/fitness/equipment/info")
@RequiredArgsConstructor
public class FitnessEquipmentInfoController {

    /**
     * 操作成功消息
     */
    private static final String OPERATION_SUCCESS_MSG = "操作成功";

    /**
     * 操作失败消息
     */
    private static final String OPERATION_FAIL_MSG = "操作失败";

    /**
     * 参数为空消息
     */
    private static final String PARAM_NULL_MSG = "参数不能为空";

    /**
     * 查询失败消息
     */
    private static final String QUERY_FAIL_MSG = "查询失败";

    /**
     * 新增成功消息
     */
    private static final String ADD_SUCCESS_MSG = "新增成功";

    /**
     * 修改成功消息
     */
    private static final String MODIFY_SUCCESS_MSG = "修改成功";

    /**
     * 删除成功消息
     */
    private static final String DELETE_SUCCESS_MSG = "删除成功";

    /**
     * 状态：启用
     */
    private static final int STATUS_ENABLED = 1;

    /**
     * 状态：禁用
     */
    private static final int STATUS_DISABLED = 0;

    /**
     * 健身设备信息服务
     */
    private final FitnessEquipmentInfoService fitnessEquipmentInfoService;

    /**
     * 分页查询健身设备信息列表
     *
     * @param pageNum       页码，默认1
     * @param pageSize      页大小，默认10
     * @param equipmentType 设备类型(1:心肺训练, 2:核心训练, 3:臀腿训练, 4:上肢训练, 5:全身训练, 6:其他)，可选
     * @param equipmentName 设备名称（模糊查询），可选
     * @param status        状态(0:禁用, 1:启用)，可选
     * @return 健身设备信息分页列表
     */
    @ApiOperation("分页查询健身设备信息列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize", value = "页大小", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "equipmentType", value = "设备类型(1:心肺训练, 2:核心训练, 3:臀腿训练, 4:上肢训练, 5:全身训练, 6:其他)", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "equipmentName", value = "设备名称（模糊查询）", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "status", value = "状态(0:禁用, 1:启用)", dataType = "Integer", paramType = "query", required = false)
    })
    @GetMapping("/page")
    public R<IPage<FitnessEquipmentInfo>> getPageList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer equipmentType,
            @RequestParam(required = false) String equipmentName,
            @RequestParam(required = false) Integer status) {
        log.info("分页查询健身设备信息列表，pageNum={}, pageSize={}, equipmentType={}, equipmentName={}, status={}", 
                pageNum, pageSize, equipmentType, equipmentName, status);

        try {
            IPage<FitnessEquipmentInfo> result = fitnessEquipmentInfoService.getPageList(
                    pageNum, pageSize, equipmentType, equipmentName, status);
            log.info("分页查询健身设备信息列表成功，查询到{}条记录", result.getTotal());
            return R.data(result);
        } catch (Exception e) {
            log.error("分页查询健身设备信息列表异常，异常信息：{}", e.getMessage(), e);
            return R.fail(QUERY_FAIL_MSG + "：" + e.getMessage());
        }
    }

    /**
     * 查询健身设备信息列表
     *
     * @param equipmentType 设备类型(1:心肺训练, 2:核心训练, 3:臀腿训练, 4:上肢训练, 5:全身训练, 6:其他)，可选
     * @param status        状态(0:禁用, 1:启用)，可选
     * @return 健身设备信息列表
     */
    @ApiOperation("查询健身设备信息列表")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "equipmentType", value = "设备类型(1:心肺训练, 2:核心训练, 3:臀腿训练, 4:上肢训练, 5:全身训练, 6:其他)", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "status", value = "状态(0:禁用, 1:启用)", dataType = "Integer", paramType = "query", required = false)
    })
    @GetMapping("/list")
    public R<List<FitnessEquipmentInfo>> getList(
            @RequestParam(required = false) Integer equipmentType,
            @RequestParam(required = false) Integer status) {
        log.info("查询健身设备信息列表，equipmentType={}, status={}", equipmentType, status);

        try {
            List<FitnessEquipmentInfo> result = fitnessEquipmentInfoService.getList(equipmentType, status);
            log.info("查询健身设备信息列表成功，查询到{}条记录", result.size());
            return R.data(result);
        } catch (Exception e) {
            log.error("查询健身设备信息列表异常，异常信息：{}", e.getMessage(), e);
            return R.fail(QUERY_FAIL_MSG + "：" + e.getMessage());
        }
    }

    /**
     * 根据设备类型查询设备列表（用于前端分类展示）
     *
     * @param equipmentType 设备类型(1:心肺训练, 2:核心训练, 3:臀腿训练, 4:上肢训练, 5:全身训练, 6:其他)，必传
     * @return 健身设备信息列表
     */
    @ApiOperation("根据设备类型查询设备列表（用于前端分类展示）")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "equipmentType", value = "设备类型(1:心肺训练, 2:核心训练, 3:臀腿训练, 4:上肢训练, 5:全身训练, 6:其他)", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/listByType")
    public R<List<FitnessEquipmentInfo>> getListByType(@RequestParam("equipmentType") Integer equipmentType) {
        log.info("根据设备类型查询设备列表，equipmentType={}", equipmentType);

        try {
            // 参数验证
            if (equipmentType == null || equipmentType <= 0) {
                log.warn("根据设备类型查询设备列表失败，设备类型无效：{}", equipmentType);
                return R.fail("设备类型不能为空且必须大于0");
            }

            List<FitnessEquipmentInfo> result = fitnessEquipmentInfoService.getListByType(equipmentType);
            log.info("根据设备类型查询设备列表成功，equipmentType={}，查询到{}条记录", equipmentType, result.size());
            return R.data(result);
        } catch (Exception e) {
            log.error("根据设备类型查询设备列表异常，equipmentType={}，异常信息：{}", equipmentType, e.getMessage(), e);
            return R.fail(QUERY_FAIL_MSG + "：" + e.getMessage());
        }
    }

    /**
     * 根据ID查询健身设备信息详情
     *
     * @param id 主键ID，不能为空
     * @return 健身设备信息
     */
    @ApiOperation("根据ID查询健身设备信息详情")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/detail")
    public R<FitnessEquipmentInfo> getDetailById(@RequestParam("id") Integer id) {
        log.info("根据ID查询健身设备信息详情，id={}", id);

        try {
            // 参数验证
            if (id == null || id <= 0) {
                log.warn("根据ID查询健身设备信息详情失败，ID无效：{}", id);
                return R.fail("ID不能为空且必须大于0");
            }

            FitnessEquipmentInfo result = fitnessEquipmentInfoService.getDetailById(id);
            if (result == null) {
                log.warn("根据ID查询健身设备信息详情失败，设备不存在，id={}", id);
                return R.fail("设备不存在");
            }

            log.info("根据ID查询健身设备信息详情成功，id={}", id);
            return R.data(result);
        } catch (Exception e) {
            log.error("根据ID查询健身设备信息详情异常，id={}，异常信息：{}", id, e.getMessage(), e);
            return R.fail(QUERY_FAIL_MSG + "：" + e.getMessage());
        }
    }

    /**
     * 新增健身设备信息
     *
     * @param fitnessEquipmentInfo 健身设备信息对象，不能为空
     * @return 操作结果
     */
    @ApiOperation("新增健身设备信息")
    @ApiOperationSupport(order = 5)
    @PostMapping("/save")
    public R<Boolean> saveEquipment(@RequestBody FitnessEquipmentInfo fitnessEquipmentInfo) {
        log.info("新增健身设备信息，请求参数：{}", fitnessEquipmentInfo);

        try {
            // 参数验证
            if (fitnessEquipmentInfo == null) {
                log.warn("新增健身设备信息失败，设备信息对象为空");
                return R.fail(PARAM_NULL_MSG);
            }

            boolean result = fitnessEquipmentInfoService.saveEquipment(fitnessEquipmentInfo);
            if (result) {
                log.info("新增健身设备信息成功");
                return R.success(ADD_SUCCESS_MSG);
            } else {
                log.warn("新增健身设备信息失败");
                return R.fail(OPERATION_FAIL_MSG);
            }
        } catch (Exception e) {
            log.error("新增健身设备信息异常，异常信息：{}", e.getMessage(), e);
            return R.fail(OPERATION_FAIL_MSG + "：" + e.getMessage());
        }
    }

    /**
     * 修改健身设备信息
     *
     * @param fitnessEquipmentInfo 健身设备信息对象，不能为空
     * @return 操作结果
     */
    @ApiOperation("修改健身设备信息")
    @ApiOperationSupport(order = 6)
    @PostMapping("/update")
    public R<Boolean> updateEquipment(@RequestBody FitnessEquipmentInfo fitnessEquipmentInfo) {
        log.info("修改健身设备信息，请求参数：{}", fitnessEquipmentInfo);

        try {
            // 参数验证
            if (fitnessEquipmentInfo == null || fitnessEquipmentInfo.getId() == null) {
                log.warn("修改健身设备信息失败，设备信息对象为空或ID为空");
                return R.fail(PARAM_NULL_MSG);
            }
            if (fitnessEquipmentInfo.getId() <= 0) {
                log.warn("修改健身设备信息失败，ID无效：{}", fitnessEquipmentInfo.getId());
                return R.fail("ID不能为空且必须大于0");
            }

            boolean result = fitnessEquipmentInfoService.updateEquipment(fitnessEquipmentInfo);
            if (result) {
                log.info("修改健身设备信息成功，id={}", fitnessEquipmentInfo.getId());
                return R.success(MODIFY_SUCCESS_MSG);
            } else {
                log.warn("修改健身设备信息失败，id={}", fitnessEquipmentInfo.getId());
                return R.fail(OPERATION_FAIL_MSG);
            }
        } catch (Exception e) {
            log.error("修改健身设备信息异常，异常信息：{}", e.getMessage(), e);
            return R.fail(OPERATION_FAIL_MSG + "：" + e.getMessage());
        }
    }

    /**
     * 删除健身设备信息（逻辑删除）
     *
     * @param id 主键ID，不能为空
     * @return 操作结果
     */
    @ApiOperation("删除健身设备信息")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Integer", paramType = "query", required = true)
    })
    @DeleteMapping("/delete")
    public R<Boolean> deleteEquipment(@RequestParam("id") Integer id) {
        log.info("删除健身设备信息，id={}", id);

        try {
            // 参数验证
            if (id == null || id <= 0) {
                log.warn("删除健身设备信息失败，ID无效：{}", id);
                return R.fail("ID不能为空且必须大于0");
            }

            boolean result = fitnessEquipmentInfoService.deleteEquipment(id);
            if (result) {
                log.info("删除健身设备信息成功，id={}", id);
                return R.success(DELETE_SUCCESS_MSG);
            } else {
                log.warn("删除健身设备信息失败，id={}", id);
                return R.fail(OPERATION_FAIL_MSG);
            }
        } catch (Exception e) {
            log.error("删除健身设备信息异常，id={}，异常信息：{}", id, e.getMessage(), e);
            return R.fail(OPERATION_FAIL_MSG + "：" + e.getMessage());
        }
    }

    /**
     * 启用/禁用健身设备信息
     *
     * @param id     主键ID，不能为空
     * @param status 状态(0:禁用, 1:启用)，不能为空
     * @return 操作结果
     */
    @ApiOperation("启用/禁用健身设备信息")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "状态(0:禁用, 1:启用)", dataType = "Integer", paramType = "query", required = true)
    })
    @PostMapping("/updateStatus")
    public R<Boolean> updateStatus(@RequestParam("id") Integer id, @RequestParam("status") Integer status) {
        log.info("启用/禁用健身设备信息，id={}, status={}", id, status);

        try {
            // 参数验证
            if (id == null || id <= 0) {
                log.warn("启用/禁用健身设备信息失败，ID无效：{}", id);
                return R.fail("ID不能为空且必须大于0");
            }
            if (status == null || (status != STATUS_ENABLED && status != STATUS_DISABLED)) {
                log.warn("启用/禁用健身设备信息失败，状态值无效：{}", status);
                return R.fail("状态值必须为0（禁用）或1（启用）");
            }

            boolean result = fitnessEquipmentInfoService.updateStatus(id, status);
            if (result) {
                log.info("启用/禁用健身设备信息成功，id={}, status={}", id, status);
                return R.success(OPERATION_SUCCESS_MSG);
            } else {
                log.warn("启用/禁用健身设备信息失败，id={}, status={}", id, status);
                return R.fail(OPERATION_FAIL_MSG);
            }
        } catch (Exception e) {
            log.error("启用/禁用健身设备信息异常，id={}, status={}，异常信息：{}", id, status, e.getMessage(), e);
            return R.fail(OPERATION_FAIL_MSG + "：" + e.getMessage());
        }
    }
}

