package com.yinlihupo.enlish.service.controller;


import com.yinlihupo.enlish.service.domain.dataobject.GradeDO;
import com.yinlihupo.enlish.service.model.vo.grade.AddGradeReqVO;
import com.yinlihupo.enlish.service.model.vo.grade.DeleteGradeReqVO;
import com.yinlihupo.enlish.service.model.vo.grade.FindGradeListReqVO;
import com.yinlihupo.enlish.service.model.vo.grade.FindGradeListRspVO;
import com.yinlihupo.enlish.service.service.GradeService;
import com.yinlihupo.framework.biz.operationlog.aspect.ApiOperationLog;
import com.yinlihupo.framework.common.response.PageResponse;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/grade")
public class GradeController {

    @Resource
    private GradeService gradeService;

    @PostMapping("/list")
    @ApiOperationLog(description = "查询年级列表")
    public PageResponse<FindGradeListRspVO> findGradeList(@RequestBody FindGradeListReqVO findGradeListReqVO) {
        Integer page = findGradeListReqVO.getPage();
        Integer pageSize = findGradeListReqVO.getPageSize();
        List<GradeDO> gradeList = gradeService.findGradeList(page, pageSize);

        List<FindGradeListRspVO> list = gradeList.stream().map(gradeDO -> FindGradeListRspVO.builder()
                .id(gradeDO.getId())
                .title(gradeDO.getTitle())
                .build()
        ).toList();
        Integer gradeCount = gradeService.findGradeCount();

        return PageResponse.success(list, page, gradeCount, pageSize);
    }

    @PostMapping("/add")
    @ApiOperationLog(description = "添加年级")
    public void addGrade(@RequestBody AddGradeReqVO addGradeReqVO) {
        gradeService.insert(addGradeReqVO.getTitle());
    }

    @PostMapping("/delete")
    @ApiOperationLog(description = "删除年级")
    public void deleteGrade(@RequestBody DeleteGradeReqVO deleteGradeReqVO) {
        gradeService.delete(deleteGradeReqVO.getId());
    }
}
