package com.gxwebsoft.hjm.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.hjm.service.HjmExamLogService;
import com.gxwebsoft.hjm.entity.HjmExamLog;
import com.gxwebsoft.hjm.param.HjmExamLogParam;
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.web.bind.annotation.*;

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

/**
 * 黄家明_学习记录控制器
 *
 * @author 科技小王子
 * @since 2025-06-05 14:32:03
 */
@Tag(name = "黄家明_学习记录管理")
@RestController
@RequestMapping("/api/hjm/hjm-exam-log")
public class HjmExamLogController extends BaseController {
  @Resource
  private HjmExamLogService hjmExamLogService;

  @Operation(summary = "分页查询黄家明_学习记录")
  @GetMapping("/page")
  public ApiResult<PageResult<HjmExamLog>> page(HjmExamLogParam param) {
    // 使用关联查询
    return success(hjmExamLogService.pageRel(param));
  }

  @Operation(summary = "查询全部黄家明_学习记录")
  @GetMapping()
  public ApiResult<List<HjmExamLog>> list(HjmExamLogParam param) {
    // 使用关联查询
    return success(hjmExamLogService.listRel(param));
  }

  @Operation(summary = "根据id查询黄家明_学习记录")
  @GetMapping("/{id}")
  public ApiResult<HjmExamLog> get(@PathVariable("id") Integer id) {
    // 使用关联查询
    return success(hjmExamLogService.getByIdRel(id));
  }

  @Operation(summary = "添加黄家明_学习记录")
  @PostMapping()
  public ApiResult<?> save(@RequestBody HjmExamLog hjmExamLog) {
    // 记录当前登录用户id
    User loginUser = getLoginUser();
    if (loginUser != null) {
      hjmExamLog.setUserId(loginUser.getUserId());
    }
    if (hjmExamLogService.save(hjmExamLog)) {
      return success("添加成功");
    }
    return fail("添加失败");
  }

  @PreAuthorize("hasAuthority('hjm:hjmExamLog:update')")
  @OperationLog
  @Operation(summary = "修改黄家明_学习记录")
  @PutMapping()
  public ApiResult<?> update(@RequestBody HjmExamLog hjmExamLog) {
    if (hjmExamLogService.updateById(hjmExamLog)) {
      return success("修改成功");
    }
    return fail("修改失败");
  }

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

  @PreAuthorize("hasAuthority('hjm:hjmExamLog:save')")
  @OperationLog
  @Operation(summary = "批量添加黄家明_学习记录")
  @PostMapping("/batch")
  public ApiResult<?> saveBatch(@RequestBody List<HjmExamLog> list) {
    if (hjmExamLogService.saveBatch(list)) {
      return success("添加成功");
    }
    return fail("添加失败");
  }

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

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

  @Operation(summary = "查询本月是否已完成学习任务")
  @GetMapping("/getMyMonthExamLog")
  public ApiResult<List<HjmExamLog>> getMyMonthExamLog() {
    User loginUser = getLoginUser();
    if (loginUser == null) {
      return fail("请先登录",null);
    }

    // 查询当前月份的记录
    List<HjmExamLog> list = hjmExamLogService.list(new LambdaQueryWrapper<HjmExamLog>()
      .eq(HjmExamLog::getStatus, 1)
      .eq(HjmExamLog::getUserId, loginUser.getUserId())
      .ge(HjmExamLog::getCreateTime, DateUtil.beginOfMonth(DateUtil.date()))
      .le(HjmExamLog::getCreateTime, DateUtil.endOfMonth(DateUtil.date())));

    return success("查询成功", list);
  }

  @Operation(summary = "检查本月是否已完成学习任务")
  @GetMapping("/checkMonthTaskCompleted")
  public ApiResult<Boolean> checkMonthTaskCompleted() {
    User loginUser = getLoginUser();
    if (loginUser == null) {
      return fail("请先登录",null);
    }

    // 查询本月是否有完成的学习记录
    long count = hjmExamLogService.count(new LambdaQueryWrapper<HjmExamLog>()
      .eq(HjmExamLog::getStatus, 1)
      .eq(HjmExamLog::getUserId, loginUser.getUserId())
      .ge(HjmExamLog::getCreateTime, DateUtil.beginOfMonth(DateUtil.date()))
      .le(HjmExamLog::getCreateTime, DateUtil.endOfMonth(DateUtil.date())));

    boolean isCompleted = count > 0;
    return success(isCompleted ? "本月已完成学习任务" : "本月尚未完成学习任务", isCompleted);
  }

}
