package com.xjrsoft.module.student.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.VoToColumnUtil;
import com.xjrsoft.module.student.dto.AddBaseStudentBehaviorCategoryDto;
import com.xjrsoft.module.student.dto.BaseStudentBehaviorCategoryListDto;
import com.xjrsoft.module.student.dto.BaseStudentBehaviorCategoryPageDto;
import com.xjrsoft.module.student.dto.UpdateBaseStudentBehaviorCategoryDto;
import com.xjrsoft.module.student.entity.BaseStudentBehaviorCategory;
import com.xjrsoft.module.student.service.IBaseStudentBehaviorCategoryService;
import com.xjrsoft.module.student.vo.BaseStudentBehaviorCategoryListVo;
import com.xjrsoft.module.student.vo.BaseStudentBehaviorCategoryPageVo;
import com.xjrsoft.module.student.vo.BaseStudentBehaviorCategoryVo;
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 fanxp
* @Date: 2023-11-15
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/basestudentbehaviorcategory")
@Api(value = "/student"  + "/basestudentbehaviorcategory",tags = "学生操行分考核类别代码")
@AllArgsConstructor
public class BaseStudentBehaviorCategoryController {


    private final IBaseStudentBehaviorCategoryService baseStudentBehaviorCategoryService;

    @GetMapping(value = "/list")
    @ApiOperation(value="学生操行分考核类别列表(不分页)")
    @SaCheckPermission("basestudentbehaviorcategory:detail")
    public RT<List<BaseStudentBehaviorCategoryListVo>> list(@Valid BaseStudentBehaviorCategoryListDto dto){

        LambdaQueryWrapper<BaseStudentBehaviorCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .orderByDesc(BaseStudentBehaviorCategory::getId)
                .select(BaseStudentBehaviorCategory.class,x -> VoToColumnUtil.fieldsToColumns(BaseStudentBehaviorCategoryListVo.class).contains(x.getProperty()));

        List<BaseStudentBehaviorCategory> list = baseStudentBehaviorCategoryService.list(queryWrapper);
        List<BaseStudentBehaviorCategoryListVo> listVos = BeanUtil.copyToList(list, BaseStudentBehaviorCategoryListVo.class);
        return RT.ok(listVos);
    }

    @GetMapping(value = "/page")
    @ApiOperation(value="学生操行分考核类别列表(分页)")
    @SaCheckPermission("basestudentbehaviorcategory:detail")
    public RT<PageOutput<BaseStudentBehaviorCategoryPageVo>> page(@Valid BaseStudentBehaviorCategoryPageDto dto){

        LambdaQueryWrapper<BaseStudentBehaviorCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                    .orderByDesc(BaseStudentBehaviorCategory::getId)
                .select(BaseStudentBehaviorCategory.class,x -> VoToColumnUtil.fieldsToColumns(BaseStudentBehaviorCategoryPageVo.class).contains(x.getProperty()));
        IPage<BaseStudentBehaviorCategory> page = baseStudentBehaviorCategoryService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<BaseStudentBehaviorCategoryPageVo> pageOutput = ConventPage.getPageOutput(page, BaseStudentBehaviorCategoryPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询学生操行分考核类别信息")
    @SaCheckPermission("basestudentbehaviorcategory:detail")
    public RT<BaseStudentBehaviorCategoryVo> info(@RequestParam Long id){
        BaseStudentBehaviorCategory baseStudentBehaviorCategory = baseStudentBehaviorCategoryService.getById(id);
        if (baseStudentBehaviorCategory == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(baseStudentBehaviorCategory, BaseStudentBehaviorCategoryVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增学生操行分考核类别")
    @SaCheckPermission("basestudentbehaviorcategory:add")
    public RT<Boolean> add(@Valid @RequestBody AddBaseStudentBehaviorCategoryDto dto){
        BaseStudentBehaviorCategory baseStudentBehaviorCategory = BeanUtil.toBean(dto, BaseStudentBehaviorCategory.class);
        boolean isSuccess = baseStudentBehaviorCategoryService.save(baseStudentBehaviorCategory);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改学生操行分考核类别")
    @SaCheckPermission("basestudentbehaviorcategory:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateBaseStudentBehaviorCategoryDto dto){

        BaseStudentBehaviorCategory baseStudentBehaviorCategory = BeanUtil.toBean(dto, BaseStudentBehaviorCategory.class);
        return RT.ok(baseStudentBehaviorCategoryService.updateById(baseStudentBehaviorCategory));

    }

    @DeleteMapping
    @ApiOperation(value = "删除学生操行分考核类别")
    @SaCheckPermission("basestudentbehaviorcategory:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(baseStudentBehaviorCategoryService.removeBatchByIds(ids));

    }

}