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.AddBaseClassMajorSetDto;
import com.xjrsoft.module.student.dto.BaseClassMajorSetPageDto;
import com.xjrsoft.module.student.dto.UpdateBaseClassMajorSetDto;
import com.xjrsoft.module.student.entity.BaseClassMajorSet;
import com.xjrsoft.module.student.service.IBaseClassMajorSetService;
import com.xjrsoft.module.student.vo.BaseClassMajorSetPageVo;
import com.xjrsoft.module.student.vo.BaseClassMajorSetVo;
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 szs
* @Date: 2023-12-28
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/baseClassMajorSet")
@Api(value = "/student"  + "/baseClassMajorSet",tags = "班级专业方向表代码")
@AllArgsConstructor
public class BaseClassMajorSetController {


    private final IBaseClassMajorSetService baseClassMajorSetService;

    @GetMapping(value = "/page")
    @ApiOperation(value="班级专业方向表列表(分页)")
    @SaCheckPermission("baseclassmajorset:detail")
    public RT<PageOutput<BaseClassMajorSetPageVo>> page(@Valid BaseClassMajorSetPageDto dto){

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

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询班级专业方向表信息")
    @SaCheckPermission("baseclassmajorset:detail")
    public RT<BaseClassMajorSetVo> info(@RequestParam Long id){
        BaseClassMajorSet baseClassMajorSet = baseClassMajorSetService.getById(id);
        if (baseClassMajorSet == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(baseClassMajorSet, BaseClassMajorSetVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增班级专业方向表")
    @SaCheckPermission("baseclassmajorset:add")
    public RT<Boolean> add(@Valid @RequestBody AddBaseClassMajorSetDto dto){
        BaseClassMajorSet baseClassMajorSet = BeanUtil.toBean(dto, BaseClassMajorSet.class);
        boolean isSuccess = baseClassMajorSetService.save(baseClassMajorSet);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改班级专业方向表")
    @SaCheckPermission("baseclassmajorset:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateBaseClassMajorSetDto dto){

        BaseClassMajorSet baseClassMajorSet = BeanUtil.toBean(dto, BaseClassMajorSet.class);
        return RT.ok(baseClassMajorSetService.updateById(baseClassMajorSet));

    }

    @DeleteMapping
    @ApiOperation(value = "删除班级专业方向表")
    @SaCheckPermission("baseclassmajorset:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(baseClassMajorSetService.removeBatchByIds(ids));

    }

}