package com.gxwebsoft.bszx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.bszx.entity.BszxBranch;
import com.gxwebsoft.bszx.entity.BszxEra;
import com.gxwebsoft.bszx.entity.BszxGrade;
import com.gxwebsoft.bszx.param.BszxGradeParam;
import com.gxwebsoft.bszx.service.BszxBranchService;
import com.gxwebsoft.bszx.service.BszxEraService;
import com.gxwebsoft.bszx.service.BszxGradeService;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.bszx.service.BszxClassService;
import com.gxwebsoft.bszx.entity.BszxClass;
import com.gxwebsoft.bszx.param.BszxClassParam;
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 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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 百色中学-班级控制器
 *
 * @author 科技小王子
 * @since 2025-03-06 22:50:25
 */
@Tag(name = "百色中学-班级管理")
@RestController
@RequestMapping("/api/bszx/bszx-class")
public class BszxClassController extends BaseController {
  @Resource
  private BszxClassService bszxClassService;
  @Resource
  private BszxGradeService bszxGradeService;
  @Resource
  private BszxBranchService bszxBranchService;

  @Operation(summary = "分页查询百色中学-班级")
  @GetMapping("/page")
  public ApiResult<PageResult<BszxClass>> page(BszxClassParam param) {
    // 使用关联查询
    return success(bszxClassService.pageRel(param));
  }

  @Operation(summary = "查询全部百色中学-班级")
  @GetMapping()
  public ApiResult<List<BszxClass>> list(BszxClassParam param) {
    // 使用关联查询
    return success(bszxClassService.listRel(param));
  }

  @Operation(summary = "根据id查询百色中学-班级")
  @GetMapping("/{id}")
  public ApiResult<BszxClass> get(@PathVariable("id") Integer id) {
    // 使用关联查询
    return success(bszxClassService.getByIdRel(id));
  }

  @Operation(summary = "百色中学-年级班级数据")
  @GetMapping("/tree")
  public ApiResult<List<BszxBranch>> tree() {
    final List<BszxBranch> list = bszxBranchService.list();
    final BszxGradeParam bszxGradeParam = new BszxGradeParam();
    final List<BszxGrade> gradeList = bszxGradeService.listRel(bszxGradeParam);
    final BszxClassParam bszxClassParam = new BszxClassParam();
    final List<BszxClass> bszxClasseList = bszxClassService.listRel(bszxClassParam);
    final Map<Integer, List<BszxClass>> collectClass = bszxClasseList.stream().collect(Collectors.groupingBy(BszxClass::getGradeId));
    gradeList.forEach(d -> {
      d.setChildren(collectClass.get(d.getId()));
    });
    final Map<Integer, List<BszxGrade>> collectGrade = gradeList.stream().collect(Collectors.groupingBy(BszxGrade::getBranch));

    list.forEach(d -> {
      d.setChildren(collectGrade.get(d.getId()));
    });

    return success(list);
  }

  @PreAuthorize("hasAuthority('bszx:bszxClass:save')")
  @OperationLog
  @Operation(summary = "添加百色中学-班级")
  @PostMapping()
  public ApiResult<?> save(@RequestBody BszxClass bszxClass) {
    if (bszxClassService.save(bszxClass)) {
      return success("添加成功");
    }
    return fail("添加失败");
  }

  @PreAuthorize("hasAuthority('bszx:bszxClass:update')")
  @OperationLog
  @Operation(summary = "修改百色中学-班级")
  @PutMapping()
  public ApiResult<?> update(@RequestBody BszxClass bszxClass) {
    if (bszxClassService.updateById(bszxClass)) {
      return success("修改成功");
    }
    return fail("修改失败");
  }

  @PreAuthorize("hasAuthority('bszx:bszxClass:remove')")
  @OperationLog
  @Operation(summary = "删除百色中学-班级")
  @DeleteMapping("/{id}")
  public ApiResult<?> remove(@PathVariable("id") Integer id) {
    if (bszxClassService.removeById(id)) {
      return success("删除成功");
    }
    return fail("删除失败");
  }

  @PreAuthorize("hasAuthority('bszx:bszxClass:save')")
  @OperationLog
  @Operation(summary = "批量添加百色中学-班级")
  @PostMapping("/batch")
  public ApiResult<?> saveBatch(@RequestBody List<BszxClass> list) {
    if (bszxClassService.saveBatch(list)) {
      return success("添加成功");
    }
    return fail("添加失败");
  }

  @PreAuthorize("hasAuthority('bszx:bszxClass:update')")
  @OperationLog
  @Operation(summary = "批量修改百色中学-班级")
  @PutMapping("/batch")
  public ApiResult<?> removeBatch(@RequestBody BatchParam<BszxClass> batchParam) {
    if (batchParam.update(bszxClassService, "id")) {
      return success("修改成功");
    }
    return fail("修改失败");
  }

  @PreAuthorize("hasAuthority('bszx:bszxClass:remove')")
  @OperationLog
  @Operation(summary = "批量删除百色中学-班级")
  @DeleteMapping("/batch")
  public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
    if (bszxClassService.removeByIds(ids)) {
      return success("删除成功");
    }
    return fail("删除失败");
  }

}
