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.StoreStaffFitnessCertification;
import shop.alien.store.service.StoreStaffFitnessCertificationService;

import java.util.List;

/**
 * 运动健身员工认证/荣誉表 Controller
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"运动健身员工认证/荣誉管理"})
@CrossOrigin
@RestController
@RequestMapping("/store/staff/fitness/certification")
@RequiredArgsConstructor
public class StoreStaffFitnessCertificationController {

    private final StoreStaffFitnessCertificationService storeStaffFitnessCertificationService;

    @ApiOperation("新增运动健身员工认证/荣誉")
    @ApiOperationSupport(order = 1)
    @PostMapping
    public R<String> saveCertification(@RequestBody StoreStaffFitnessCertification certification) {
        log.info("StoreStaffFitnessCertificationController.saveCertification?certification={}", certification);
        // 参数校验
        if (certification.getStaffId() == null) {
            return R.fail("员工ID不能为空");
        }
        if (certification.getType() == null) {
            return R.fail("类型不能为空（1-认证 2-荣誉）");
        }
        if (certification.getType() != 1 && certification.getType() != 2) {
            return R.fail("类型值无效，只能为1（认证）或2（荣誉）");
        }

        R<StoreStaffFitnessCertification> result = storeStaffFitnessCertificationService.addStoreStaffFitnessCertification(certification);
        if (result.getCode() == 200) {
            return R.success("新增成功");
        }
        return R.fail(result.getMsg());
    }

    @ApiOperation("修改运动健身员工认证/荣誉")
    @ApiOperationSupport(order = 2)
    @PutMapping
    public R<String> updateCertification(@RequestBody StoreStaffFitnessCertification certification) {
        log.info("StoreStaffFitnessCertificationController.updateCertification?certification={}", certification);
        if (certification.getId() == null) {
            return R.fail("ID不能为空");
        }

        R<StoreStaffFitnessCertification> result = storeStaffFitnessCertificationService.editStoreStaffFitnessCertification(certification);
        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> deleteCertification(@PathVariable("id") Integer id) {
        log.info("StoreStaffFitnessCertificationController.deleteCertification?id={}", id);
        R<Boolean> result = storeStaffFitnessCertificationService.deleteStoreStaffFitnessCertification(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<StoreStaffFitnessCertification> getById(@PathVariable("id") Integer id) {
        log.info("StoreStaffFitnessCertificationController.getById?id={}", id);
        R<StoreStaffFitnessCertification> result = storeStaffFitnessCertificationService.getStoreStaffFitnessCertificationById(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 = "type", value = "类型（1-认证 2-荣誉）", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称（模糊查询）", dataType = "String", paramType = "query")
    })
    @GetMapping("/page")
    public R<IPage<StoreStaffFitnessCertification>> getPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Integer staffId,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) String name) {
        log.info("StoreStaffFitnessCertificationController.getPage?pageNum={}, pageSize={}, staffId={}, type={}, name={}",
                pageNum, pageSize, staffId, type, name);
        IPage<StoreStaffFitnessCertification> page = storeStaffFitnessCertificationService.getPage(pageNum, pageSize, staffId, type, name);
        return R.data(page);
    }

    @ApiOperation("批量删除运动健身员工认证/荣誉")
    @ApiOperationSupport(order = 6)
    @PostMapping("/batchDelete")
    public R<String> deleteBatch(@RequestBody List<Integer> ids) {
        log.info("StoreStaffFitnessCertificationController.deleteBatch?ids={}", ids);
        if (ids == null || ids.isEmpty()) {
            return R.fail("ID列表不能为空");
        }
        boolean result = storeStaffFitnessCertificationService.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<StoreStaffFitnessCertification>> getListByStaffId(@RequestParam("staffId") Integer staffId) {
        log.info("StoreStaffFitnessCertificationController.getListByStaffId?staffId={}", staffId);
        List<StoreStaffFitnessCertification> list = storeStaffFitnessCertificationService.getListByStaffId(staffId);
        return R.data(list);
    }

    @ApiOperation("根据员工ID和类型查询认证/荣誉列表")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "staffId", value = "员工ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "类型（1-认证 2-荣誉）", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/listByStaffIdAndType")
    public R<List<StoreStaffFitnessCertification>> getListByStaffIdAndType(
            @RequestParam("staffId") Integer staffId,
            @RequestParam(required = false) Integer type) {
        log.info("StoreStaffFitnessCertificationController.getListByStaffIdAndType?staffId={}, type={}", staffId, type);
        List<StoreStaffFitnessCertification> list = storeStaffFitnessCertificationService.getListByStaffIdAndType(staffId, type);
        return R.data(list);
    }
}
