package com.xjrsoft.module.teacher.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.utils.TreeUtil;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.teacher.dto.AddTeacherAwardItemDto;
import com.xjrsoft.module.teacher.dto.TeacherAwardItemPageDto;
import com.xjrsoft.module.teacher.dto.UpdateTeacherAwardItemDto;
import com.xjrsoft.module.teacher.entity.TeacherAwardItem;
import com.xjrsoft.module.teacher.service.ITeacherAwardItemService;
import com.xjrsoft.module.teacher.vo.TeacherAwardItemPageVo;
import com.xjrsoft.module.teacher.vo.TeacherAwardItemVo;
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.List;

/**
* @title: 教师奖项
* @Author dzx
* @Date: 2024-06-04
* @Version 1.0
*/
@RestController
@RequestMapping("/teacher" + "/teacherAwardItem")
@Api(value = "/teacher"  + "/teacherAwardItem",tags = "教师奖项代码")
@AllArgsConstructor
public class TeacherAwardItemController {


    private final ITeacherAwardItemService teacherAwardItemService;

    @GetMapping(value = "/tree-list")
    @ApiOperation(value="教师奖项列表(树)")
    @SaCheckPermission("teacherawarditem:detail")
    public RT<List<TeacherAwardItemVo>> page(@Valid TeacherAwardItemPageDto dto){

        LambdaQueryWrapper<TeacherAwardItem> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper
                .orderByDesc(TeacherAwardItem::getId)
                .eq(dto.getIsThesis() != null, TeacherAwardItem::getIsThesis, dto.getIsThesis())
                .select(TeacherAwardItem.class,x -> VoToColumnUtil.fieldsToColumns(TeacherAwardItemPageVo.class).contains(x.getProperty()))
                ;
        List<TeacherAwardItem> list = teacherAwardItemService.list(queryWrapper);
        List<TeacherAwardItemVo> itemVos = BeanUtil.copyToList(list, TeacherAwardItemVo.class);
        List<TeacherAwardItemVo> build = TreeUtil.build(itemVos);
        return RT.ok(build);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询教师奖项信息")
    @SaCheckPermission("teacherawarditem:detail")
    public RT<TeacherAwardItemVo> info(@RequestParam Long id){
        TeacherAwardItem teacherAwardItem = teacherAwardItemService.getById(id);
        if (teacherAwardItem == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(teacherAwardItem, TeacherAwardItemVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增教师奖项")
    @SaCheckPermission("teacherawarditem:add")
    public RT<Boolean> add(@Valid @RequestBody AddTeacherAwardItemDto dto){
        TeacherAwardItem teacherAwardItem = BeanUtil.toBean(dto, TeacherAwardItem.class);
        boolean isSuccess = teacherAwardItemService.save(teacherAwardItem);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改教师奖项")
    @SaCheckPermission("teacherawarditem:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateTeacherAwardItemDto dto){

        TeacherAwardItem teacherAwardItem = BeanUtil.toBean(dto, TeacherAwardItem.class);
        return RT.ok(teacherAwardItemService.updateById(teacherAwardItem));

    }

    @DeleteMapping
    @ApiOperation(value = "删除教师奖项")
    @SaCheckPermission("teacherawarditem:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(teacherAwardItemService.removeBatchByIds(ids));
    }

}