package org.dromara.ygpassport.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.toolkit.MPJWrappers;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.ygpassport.domain.ScoreList;
import org.dromara.ygpassport.domain.StudentList;
import org.dromara.ygpassport.domain.bo.ScoreListBo;
import org.dromara.ygpassport.domain.dto.StuScoreListDto;
import org.dromara.ygpassport.domain.vo.ScoreListVo;
import org.dromara.ygpassport.domain.vo.StudentImportVo;
import org.dromara.ygpassport.listener.ScoreListImportListener;
import org.dromara.ygpassport.service.IScoreListService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 学生成绩信息列
 *
 * @author Lion Li
 * @date 2023-10-30
 */

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ygpassport/scoreList")
public class ScoreListController extends BaseController {

    private final IScoreListService scoreListService;

    /**
     * 查询学生成绩信息列列表 old
     */
    @SaCheckPermission("ygpassport:scoreList:list")
    @GetMapping("/list")
    public TableDataInfo<ScoreListVo> list(ScoreListBo bo, PageQuery pageQuery) {
        return scoreListService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询学生成绩信息列列表 new
     */
    @SaCheckPermission("ygpassport:scoreList:listNew")
    @GetMapping("/listNew")
    public IPage<StuScoreListDto> listNew(PageQuery pageQuery, StuScoreListDto stuScoreListDto) {
        return scoreListService.selectJoinListPage(new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize()),StuScoreListDto.class,
            MPJWrappers.<ScoreList>lambdaJoin()
                .selectAll(ScoreList.class)
                .select(StudentList ::getStuName)
                .select(StudentList ::getStuSex)
                .select(StudentList ::getFaculties)
                .select(StudentList ::getStuClass)
                .leftJoin(StudentList.class, StudentList :: getStuNo, ScoreList :: getStuNo));
    }

    /**
     * 导出学生成绩信息列列表
     */
    @SaCheckPermission("ygpassport:scoreList:export")
    @Log(title = "学生成绩信息列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ScoreListBo bo, HttpServletResponse response) {
        List<ScoreListVo> list = scoreListService.queryList(bo);
        ExcelUtil.exportExcel(list, "学生成绩信息列", ScoreListVo.class, response);
    }

    /**
     * 导入成绩数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "成绩信息", businessType = BusinessType.IMPORT)
    @SaCheckPermission("ygpassport:scoreList:import")
    @PostMapping(value = "/importScoreListData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ScoreListImportListener listener = new ScoreListImportListener(updateSupport);

        ExcelResult<ScoreListVo> result = ExcelUtil.importExcel(file.getInputStream(), ScoreListVo.class, listener);
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "第一课堂成绩信息", ScoreListVo.class, response);
    }

    /**
     * 获取学生成绩信息列详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("ygpassport:scoreList:query")
    @GetMapping("/{id}")
    public R<ScoreListVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(scoreListService.queryById(id));
    }

    /**
     * 新增学生成绩信息列
     */
    @SaCheckPermission("ygpassport:scoreList:add")
    @Log(title = "学生成绩信息列", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ScoreListBo bo) {
        return toAjax(scoreListService.insertByBo(bo));
    }

    /**
     * 修改学生成绩信息列
     */
    @SaCheckPermission("ygpassport:scoreList:edit")
    @Log(title = "学生成绩信息列", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ScoreListBo bo) {
        return toAjax(scoreListService.updateByBo(bo));
    }

    /**
     * 删除学生成绩信息列
     *
     * @param ids 主键串
     */
    @SaCheckPermission("ygpassport:scoreList:remove")
    @Log(title = "学生成绩信息列", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(scoreListService.deleteWithValidByIds(List.of(ids), true));
    }
}
