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.StoreStaffFitnessExperience;
import shop.alien.store.service.StoreStaffFitnessExperienceService;

import java.util.List;

/**
 * 运动健身员工从业经历表 Controller
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"运动健身员工从业经历管理"})
@CrossOrigin
@RestController
@RequestMapping("/store/staff/fitness/experience")
@RequiredArgsConstructor
public class StoreStaffFitnessExperienceController {

    private final StoreStaffFitnessExperienceService storeStaffFitnessExperienceService;

    @ApiOperation("新增运动健身员工从业经历")
    @ApiOperationSupport(order = 1)
    @PostMapping
    public R<String> saveExperience(@RequestBody StoreStaffFitnessExperience experience) {
        log.info("StoreStaffFitnessExperienceController.saveExperience?experience={}", experience);
        // 参数校验
        if (experience.getStaffId() == null) {
            return R.fail("员工ID不能为空");
        }

        R<StoreStaffFitnessExperience> result = storeStaffFitnessExperienceService.addStoreStaffFitnessExperience(experience);
        if (result.getCode() == 200) {
            return R.success("新增成功");
        }
        return R.fail(result.getMsg());
    }

    @ApiOperation("修改运动健身员工从业经历")
    @ApiOperationSupport(order = 2)
    @PutMapping
    public R<String> updateExperience(@RequestBody StoreStaffFitnessExperience experience) {
        log.info("StoreStaffFitnessExperienceController.updateExperience?experience={}", experience);
        if (experience.getId() == null) {
            return R.fail("ID不能为空");
        }

        R<StoreStaffFitnessExperience> result = storeStaffFitnessExperienceService.editStoreStaffFitnessExperience(experience);
        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> deleteExperience(@PathVariable("id") Integer id) {
        log.info("StoreStaffFitnessExperienceController.deleteExperience?id={}", id);
        R<Boolean> result = storeStaffFitnessExperienceService.deleteStoreStaffFitnessExperience(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<StoreStaffFitnessExperience> getById(@PathVariable("id") Integer id) {
        log.info("StoreStaffFitnessExperienceController.getById?id={}", id);
        R<StoreStaffFitnessExperience> result = storeStaffFitnessExperienceService.getStoreStaffFitnessExperienceById(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 = "workUnit", value = "工作单位（模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "workPosition", value = "职位（模糊查询）", dataType = "String", paramType = "query")
    })
    @GetMapping("/page")
    public R<IPage<StoreStaffFitnessExperience>> getPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Integer staffId,
            @RequestParam(required = false) String workUnit,
            @RequestParam(required = false) String workPosition) {
        log.info("StoreStaffFitnessExperienceController.getPage?pageNum={}, pageSize={}, staffId={}, workUnit={}, workPosition={}",
                pageNum, pageSize, staffId, workUnit, workPosition);
        IPage<StoreStaffFitnessExperience> page = storeStaffFitnessExperienceService.getPage(pageNum, pageSize, staffId, workUnit, workPosition);
        return R.data(page);
    }

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

