package com.xjrsoft.module.student.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
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.common.utils.TreeUtil;
import com.xjrsoft.module.student.dto.AddStudentHonorsDto;
import com.xjrsoft.module.student.dto.StudentHonorsPageDto;
import com.xjrsoft.module.student.dto.UpdateStudentHonorsDto;
import com.xjrsoft.module.student.entity.StudentHonors;
import com.xjrsoft.module.student.service.IStudentHonorsService;
import com.xjrsoft.module.student.vo.StudentHonorsPageVo;
import com.xjrsoft.module.student.vo.StudentHonorsTreeVo;
import com.xjrsoft.module.student.vo.StudentHonorsVo;
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 javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
* @title: 学生荣誉
* @Author dzx
* @Date: 2023-12-05
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/studentHonors")
@Api(value = "/student"  + "/studentHonors",tags = "学生荣誉代码")
@AllArgsConstructor
public class StudentHonorsController {


    private final IStudentHonorsService studentHonorsService;

    @GetMapping(value = "/page")
    @ApiOperation(value="学生荣誉列表(分页)")
    @SaCheckPermission("studenthonors:detail")
    public RT<PageOutput<StudentHonorsPageVo>> page(@Valid StudentHonorsPageDto dto){
        IPage<StudentHonorsPageVo> page = studentHonorsService.getPage(dto);
        PageOutput<StudentHonorsPageVo> pageOutput = ConventPage.getPageOutput(page, StudentHonorsPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/tree")
    @ApiOperation(value = "学生荣誉列表(树)")
    @SaCheckPermission("studenthonors:detail")
    public RT<List<StudentHonorsTreeVo>> tree() {
        List<StudentHonorsTreeVo> voList = new ArrayList<>();
        studentHonorsService.getSemesterInfo().forEach((node) -> {
            voList.add(new StudentHonorsTreeVo(){{
                setId(node.getId());
                setName(node.getName());
            }});
        });
        studentHonorsService.getClassInfo().forEach((node) -> {
            voList.add(new StudentHonorsTreeVo(){{
                setId(node.getId());
                setName(node.getName());
                setParentId(node.getParentId());
            }});
        });
        List<StudentHonorsTreeVo> treeVoList = TreeUtil.build(voList);

        return RT.ok(treeVoList);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询学生荣誉信息")
    @SaCheckPermission("studenthonors:detail")
    public RT<StudentHonorsVo> info(@RequestParam Long id){
        StudentHonorsVo studentHonors = studentHonorsService.getInfo(id);
        if (studentHonors == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(studentHonors);
    }


    @PostMapping
    @ApiOperation(value = "新增学生荣誉")
    @SaCheckPermission("studenthonors:add")
    public RT<Boolean> add(@Valid @RequestBody AddStudentHonorsDto dto){
        StudentHonors studentHonors = BeanUtil.toBean(dto, StudentHonors.class);
        Integer sortCode = studentHonorsService.getSortCode();
        studentHonors.setSortCode(sortCode);
        studentHonors.setDataEntryMethod(2);
        studentHonors.setStatus(1);
        boolean isSuccess = studentHonorsService.save(studentHonors);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改学生荣誉")
    @SaCheckPermission("studenthonors:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateStudentHonorsDto dto){

        StudentHonors studentHonors = BeanUtil.toBean(dto, StudentHonors.class);
        return RT.ok(studentHonorsService.updateById(studentHonors));

    }

    @DeleteMapping
    @ApiOperation(value = "删除学生荣誉")
    @SaCheckPermission("studenthonors:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(studentHonorsService.removeBatchByIds(ids));

    }

}