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.StoreStaffFitnessCourse;
import shop.alien.store.service.StoreStaffFitnessCourseService;

import java.util.List;

/**
 * 运动健身员工课程信息表 Controller
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"运动健身员工课程管理"})
@CrossOrigin
@RestController
@RequestMapping("/store/staff/fitness/course")
@RequiredArgsConstructor
public class StoreStaffFitnessCourseController {

    private final StoreStaffFitnessCourseService storeStaffFitnessCourseService;

    @ApiOperation("新增运动健身员工课程")
    @ApiOperationSupport(order = 1)
    @PostMapping
    public R<String> saveCourse(@RequestBody StoreStaffFitnessCourse course) {
        log.info("StoreStaffFitnessCourseController.saveCourse?course={}", course);
        // 参数校验
        if (course.getStaffId() == null) {
            return R.fail("员工ID不能为空");
        }
        if (course.getCourseName() == null || course.getCourseName().trim().isEmpty()) {
            return R.fail("项目名称不能为空");
        }

        R<StoreStaffFitnessCourse> result = storeStaffFitnessCourseService.addStoreStaffFitnessCourse(course);
        if (result.getCode() == 200) {
            return R.success("新增成功");
        }
        return R.fail(result.getMsg());
    }

    @ApiOperation("修改运动健身员工课程")
    @ApiOperationSupport(order = 2)
    @PutMapping
    public R<String> updateCourse(@RequestBody StoreStaffFitnessCourse course) {
        log.info("StoreStaffFitnessCourseController.updateCourse?course={}", course);
        if (course.getId() == null) {
            return R.fail("ID不能为空");
        }

        R<StoreStaffFitnessCourse> result = storeStaffFitnessCourseService.editStoreStaffFitnessCourse(course);
        if (result.getCode() == 200) {
            return R.success("修改成功");
        }
        return R.fail(result.getMsg());
    }

    @ApiOperation("删除运动健身员工课程")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Integer", paramType = "path", required = true)
    })
    @DeleteMapping("/{id}")
    public R<String> deleteCourse(@PathVariable("id") Integer id) {
        log.info("StoreStaffFitnessCourseController.deleteCourse?id={}", id);
        R<Boolean> result = storeStaffFitnessCourseService.deleteStoreStaffFitnessCourse(id);
        if (result.getCode() == 200) {
            return R.success("删除成功");
        }
        return R.fail(result.getMsg());
    }

    @ApiOperation("根据ID查询运动健身员工课程详情")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Integer", paramType = "path", required = true)
    })
    @GetMapping("/{id}")
    public R<StoreStaffFitnessCourse> getById(@PathVariable("id") Integer id) {
        log.info("StoreStaffFitnessCourseController.getById?id={}", id);
        R<StoreStaffFitnessCourse> result = storeStaffFitnessCourseService.getStoreStaffFitnessCourseById(id);
        if (result.getCode() == 200 && result.getData() != null) {
            return result;
        }
        return R.fail("未找到该课程信息");
    }

    @ApiOperation("分页查询运动健身员工课程列表")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页容", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "staffId", value = "员工ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "courseType", value = "课程类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "courseName", value = "项目名称（模糊查询）", dataType = "String", paramType = "query")
    })
    @GetMapping("/page")
    public R<IPage<StoreStaffFitnessCourse>> getPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Integer staffId,
            @RequestParam(required = false) String courseType,
            @RequestParam(required = false) String courseName) {
        log.info("StoreStaffFitnessCourseController.getPage?pageNum={}, pageSize={}, staffId={}, courseType={}, courseName={}",
                pageNum, pageSize, staffId, courseType, courseName);
        IPage<StoreStaffFitnessCourse> page = storeStaffFitnessCourseService.getPage(pageNum, pageSize, staffId, courseType, courseName);
        return R.data(page);
    }

    @ApiOperation("批量删除运动健身员工课程")
    @ApiOperationSupport(order = 6)
    @PostMapping("/batchDelete")
    public R<String> deleteBatch(@RequestBody List<Integer> ids) {
        log.info("StoreStaffFitnessCourseController.deleteBatch?ids={}", ids);
        if (ids == null || ids.isEmpty()) {
            return R.fail("ID列表不能为空");
        }
        boolean result = storeStaffFitnessCourseService.deleteBatch(ids);
        if (result) {
            return R.success("批量删除成功");
        }
        return R.fail("批量删除失败");
    }

    @ApiOperation("根据员工ID查询课程列表")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "staffId", value = "员工ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/listByStaffId")
    public R<List<StoreStaffFitnessCourse>> getListByStaffId(@RequestParam("staffId") Integer staffId) {
        log.info("StoreStaffFitnessCourseController.getListByStaffId?staffId={}", staffId);
        List<StoreStaffFitnessCourse> list = storeStaffFitnessCourseService.getListByStaffId(staffId);
        return R.data(list);
    }
}

