package com.xjrsoft.module.student.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.student.dto.AddClassHonorsDto;
import com.xjrsoft.module.student.dto.ClassHonorsPageDto;
import com.xjrsoft.module.student.dto.UpdateClassHonorsDto;
import com.xjrsoft.module.student.entity.ClassHonors;
import com.xjrsoft.module.student.service.IClassHonorsService;
import com.xjrsoft.module.student.vo.ClassHonorsPageVo;
import com.xjrsoft.module.student.vo.ClassHonorsSemesterClassTreeVo;
import com.xjrsoft.module.student.vo.ClassHonorsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
* @title: 班级荣誉
* @Author szs
* @Date: 2023-12-06
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/classHonors")
@Api(value = "/student"  + "/classHonors",tags = "班级荣誉代码")
@AllArgsConstructor
public class ClassHonorsController {


    private final IClassHonorsService classHonorsService;

    @GetMapping(value = "/semesterClassTree")
    @ApiOperation(value="班级荣誉学期班级树")
    @SaCheckPermission("classhonors:detail")
    public RT<List<ClassHonorsSemesterClassTreeVo>> semesterClassTree(){
        List<ClassHonorsSemesterClassTreeVo> list = classHonorsService.getSemesterClassTree();
        return RT.ok(list);
    }

    @GetMapping(value = "/page")
    @ApiOperation(value="班级荣誉列表(分页)")
    @SaCheckPermission("classhonors:detail")
    public RT<PageOutput<ClassHonorsPageVo>> page(@Valid ClassHonorsPageDto dto){
        IPage<ClassHonorsPageVo> page = classHonorsService.getPagePC(dto);
        PageOutput<ClassHonorsPageVo> pageOutput = ConventPage.getPageOutput(page, ClassHonorsPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询班级荣誉信息")
    @SaCheckPermission("classhonors:detail")
    public RT<ClassHonorsVo> info(@RequestParam Long id){
        ClassHonorsVo classHonorsVo = classHonorsService.getInfoById(id);
        if (classHonorsVo == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(classHonorsVo);
    }


    @PostMapping
    @ApiOperation(value = "新增班级荣誉")
    @SaCheckPermission("classhonors:add")
    public RT<Boolean> add(@Valid @RequestBody AddClassHonorsDto dto){
        ClassHonors classHonors = BeanUtil.toBean(dto, ClassHonors.class);
        boolean isSuccess = classHonorsService.save(classHonors);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改班级荣誉")
    @SaCheckPermission("classhonors:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateClassHonorsDto dto){

        ClassHonors classHonors = BeanUtil.toBean(dto, ClassHonors.class);
        return RT.ok(classHonorsService.updateById(classHonors));

    }

    @DeleteMapping
    @ApiOperation(value = "删除班级荣誉")
    @SaCheckPermission("classhonors:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(classHonorsService.removeBatchByIds(ids));

    }
    @PostMapping("/import")
    @ApiOperation(value = "导入")
    public RT<Boolean> importData(@RequestParam MultipartFile file) throws IOException {
        List<ClassHonorsPageVo> savedDataList = EasyExcel.read(file.getInputStream()).head(ClassHonorsPageVo.class).sheet().doReadSync();
        Boolean result = classHonorsService.saveBatch(BeanUtil.copyToList(savedDataList, ClassHonors.class));
        return RT.ok(result);
    }

}