package com.gxwebsoft.hjm.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.hjm.service.HjmCoursesService;
import com.gxwebsoft.hjm.entity.HjmCourses;
import com.gxwebsoft.hjm.param.HjmCoursesParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 黄家明_课程控制器
 *
 * @author 科技小王子
 * @since 2025-06-02 12:59:49
 */
@Tag(name = "黄家明_课程管理")
@RestController
@RequestMapping("/api/hjm/hjm-courses")
public class HjmCoursesController extends BaseController {
    @Resource
    private HjmCoursesService hjmCoursesService;

    @Operation(summary = "分页查询黄家明_课程")
    @GetMapping("/page")
    public ApiResult<PageResult<HjmCourses>> page(HjmCoursesParam param) {
        // 使用关联查询
        return success(hjmCoursesService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('hjm:hjmCourses:list')")
    @Operation(summary = "查询全部黄家明_课程")
    @GetMapping()
    public ApiResult<List<HjmCourses>> list(HjmCoursesParam param) {
        // 使用关联查询
        return success(hjmCoursesService.listRel(param));
    }

    @Operation(summary = "根据id查询黄家明_课程")
    @GetMapping("/{id}")
    public ApiResult<HjmCourses> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(hjmCoursesService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('hjm:hjmCourses:save')")
    @OperationLog
    @Operation(summary = "添加黄家明_课程")
    @PostMapping()
    public ApiResult<?> save(@RequestBody HjmCourses hjmCourses) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         hjmCourses.setUserId(loginUser.getUserId());
        }
        if (hjmCoursesService.save(hjmCourses)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('hjm:hjmCourses:update')")
    @OperationLog
    @Operation(summary = "修改黄家明_课程")
    @PutMapping()
    public ApiResult<?> update(@RequestBody HjmCourses hjmCourses) {
        if (hjmCoursesService.updateById(hjmCourses)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('hjm:hjmCourses:remove')")
    @OperationLog
    @Operation(summary = "删除黄家明_课程")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (hjmCoursesService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('hjm:hjmCourses:save')")
    @OperationLog
    @Operation(summary = "批量添加黄家明_课程")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<HjmCourses> list) {
        if (hjmCoursesService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('hjm:hjmCourses:update')")
    @OperationLog
    @Operation(summary = "批量修改黄家明_课程")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<HjmCourses> batchParam) {
        if (batchParam.update(hjmCoursesService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('hjm:hjmCourses:remove')")
    @OperationLog
    @Operation(summary = "批量删除黄家明_课程")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (hjmCoursesService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
