package com.gxwebsoft.hjm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.hjm.entity.HjmChoices;
import com.gxwebsoft.hjm.service.HjmChoicesService;
import com.gxwebsoft.hjm.service.HjmQuestionsService;
import com.gxwebsoft.hjm.entity.HjmQuestions;
import com.gxwebsoft.hjm.param.HjmQuestionsParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
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.util.CollectionUtils;
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-questions")
public class HjmQuestionsController extends BaseController {
  @Resource
  private HjmQuestionsService hjmQuestionsService;
  @Resource
  private HjmChoicesService hjmChoicesService;

  @Operation(summary = "分页查询黄家明_题目")
  @GetMapping("/page")
  public ApiResult<PageResult<HjmQuestions>> page(HjmQuestionsParam param) {
    // 使用关联查询
    return success(hjmQuestionsService.pageRel(param));
  }

  @PreAuthorize("hasAuthority('hjm:hjmQuestions:list')")
  @Operation(summary = "查询全部黄家明_题目")
  @GetMapping()
  public ApiResult<List<HjmQuestions>> list(HjmQuestionsParam param) {
    // 使用关联查询
    return success(hjmQuestionsService.listRel(param));
  }

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

  @PreAuthorize("hasAuthority('hjm:hjmQuestions:save')")
  @OperationLog
  @Operation(summary = "添加黄家明_题目")
  @PostMapping()
  public ApiResult<?> save(@RequestBody HjmQuestions hjmQuestions) {
    // 记录当前登录用户id
    User loginUser = getLoginUser();
    if (loginUser != null) {
      hjmQuestions.setUserId(loginUser.getUserId());
    }
    if (hjmQuestionsService.save(hjmQuestions)) {
      final List<HjmChoices> choicesList = hjmQuestions.getChoicesList();
      if (!CollectionUtils.isEmpty(choicesList)) {
        choicesList.forEach(choice -> {
          choice.setQuestionId(hjmQuestions.getId());
        });
        hjmChoicesService.saveBatch(choicesList);
      }
      return success("添加成功");
    }
    return fail("添加失败");
  }

  @PreAuthorize("hasAuthority('hjm:hjmQuestions:update')")
  @OperationLog
  @Operation(summary = "修改黄家明_题目")
  @PutMapping()
  public ApiResult<?> update(@RequestBody HjmQuestions hjmQuestions) {
    if (hjmQuestionsService.updateById(hjmQuestions)) {
      if (!CollectionUtils.isEmpty(hjmQuestions.getChoicesList())) {
        hjmChoicesService.updateBatchById(hjmQuestions.getChoicesList());
      }
      return success("修改成功");
    }
    return fail("修改失败");
  }

  @PreAuthorize("hasAuthority('hjm:hjmQuestions:remove')")
  @OperationLog
  @Operation(summary = "删除黄家明_题目")
  @DeleteMapping("/{id}")
  public ApiResult<?> remove(@PathVariable("id") Integer id) {
    if (hjmQuestionsService.removeById(id)) {
      hjmChoicesService.remove(new LambdaQueryWrapper<HjmChoices>().eq(HjmChoices::getQuestionId, id));
      return success("删除成功");
    }
    return fail("删除失败");
  }

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

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

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

}
