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.StoreStaffFitnessBase;
import shop.alien.store.service.StoreStaffFitnessBaseService;

import java.util.List;

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

    private final StoreStaffFitnessBaseService storeStaffFitnessBaseService;

    @ApiOperation("新增运动健身员工基本信息")
    @ApiOperationSupport(order = 1)
    @PostMapping
    public R<String> saveBase(@RequestBody StoreStaffFitnessBase base) {
        log.info("StoreStaffFitnessBaseController.saveBase?base={}", base);
        // 参数校验
        if (base.getStaffId() == null) {
            return R.fail("员工ID不能为空");
        }

        R<StoreStaffFitnessBase> result = storeStaffFitnessBaseService.addStoreStaffFitnessBase(base);
        if (result.getCode() == 200) {
            return R.success("新增成功");
        }
        return R.fail(result.getMsg());
    }

    @ApiOperation("修改运动健身员工基本信息")
    @ApiOperationSupport(order = 2)
    @PutMapping
    public R<String> updateBase(@RequestBody StoreStaffFitnessBase base) {
        log.info("StoreStaffFitnessBaseController.updateBase?base={}", base);
        if (base.getId() == null) {
            return R.fail("ID不能为空");
        }

        R<StoreStaffFitnessBase> result = storeStaffFitnessBaseService.editStoreStaffFitnessBase(base);
        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> deleteBase(@PathVariable("id") Integer id) {
        log.info("StoreStaffFitnessBaseController.deleteBase?id={}", id);
        R<Boolean> result = storeStaffFitnessBaseService.deleteStoreStaffFitnessBase(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<StoreStaffFitnessBase> getById(@PathVariable("id") Integer id) {
        log.info("StoreStaffFitnessBaseController.getById?id={}", id);
        R<StoreStaffFitnessBase> result = storeStaffFitnessBaseService.getStoreStaffFitnessBaseById(id);
        if (result.getCode() == 200 && result.getData() != null) {
            return result;
        }
        return R.fail("未找到该基本信息");
    }

    @ApiOperation("根据员工ID查询基本信息")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "staffId", value = "员工ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getByStaffId")
    public R<StoreStaffFitnessBase> getByStaffId(@RequestParam("staffId") Integer staffId) {
        log.info("StoreStaffFitnessBaseController.getByStaffId?staffId={}", staffId);
        R<StoreStaffFitnessBase> result = storeStaffFitnessBaseService.getByStaffId(staffId);
        if (result.getCode() == 200 && result.getData() != null) {
            return result;
        }
        return R.fail("未找到该员工的基本信息");
    }

    @ApiOperation("分页查询运动健身员工基本信息列表")
    @ApiOperationSupport(order = 6)
    @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 = "education", value = "学历", dataType = "String", paramType = "query")
    })
    @GetMapping("/page")
    public R<IPage<StoreStaffFitnessBase>> getPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Integer staffId,
            @RequestParam(required = false) String education) {
        log.info("StoreStaffFitnessBaseController.getPage?pageNum={}, pageSize={}, staffId={}, education={}",
                pageNum, pageSize, staffId, education);
        IPage<StoreStaffFitnessBase> page = storeStaffFitnessBaseService.getPage(pageNum, pageSize, staffId, education);
        return R.data(page);
    }

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

