package com.xjrsoft.module.base.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.common.enums.EnabledMark;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.base.dto.AddBaseClassPageDto;
import com.xjrsoft.module.base.dto.BaseClassPageDto;
import com.xjrsoft.module.base.dto.UpdateBaseClassPageDto;
import com.xjrsoft.module.base.entity.BaseClass;
import com.xjrsoft.module.base.service.IBaseClassService;
import com.xjrsoft.module.base.vo.BaseClassPageVo;
import com.xjrsoft.module.base.vo.BaseClassVo;
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.Date;
import java.util.List;

/**
* @title: 班级管理
* @Author dzx
* @Date: 2024年4月28日
* @Version 1.0
*/
@RestController
@RequestMapping("/baseClass")
@Api(value = "/baseClass",tags = "班级管理")
@AllArgsConstructor
public class BaseClassController {
    private final IBaseClassService baseClassService;

    @GetMapping(value = "/page")
    @ApiOperation(value="班级列表(分页)")
    @SaCheckPermission("baseclass:detail")
    public RT<PageOutput<BaseClassPageVo>> page(@Valid BaseClassPageDto dto){

        Page<BaseClassPageVo> page = baseClassService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<BaseClassPageVo> pageOutput = ConventPage.getPageOutput(page, BaseClassPageVo.class);
        return RT.ok(pageOutput);
    }
    @DeleteMapping
    @ApiOperation(value = "删除")
    @SaCheckPermission("baseclass:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(baseClassService.removeByIds(ids));
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询班级信息")
    @SaCheckPermission("baseclass:detail")
    public RT<BaseClassVo> info(@RequestParam Long id){
        BaseClass baseClass = baseClassService.getByIdDeep(id);
        if (baseClass == null) {
            return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(baseClass, BaseClassVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增班级")
    @SaCheckPermission("baseclass:add")
    public RT<Boolean> add(@Valid @RequestBody AddBaseClassPageDto dto){
        BaseClass baseClass = BeanUtil.toBean(dto, BaseClass.class);
        baseClass.setDeleteMark(DeleteMark.NODELETE.getCode());
        baseClass.setCreateDate(new Date());
        baseClass.setEnabledMark(EnabledMark.ENABLED.getCode());
        baseClass.setCreateUserId(StpUtil.getLoginIdAsLong());
        boolean isSuccess = baseClassService.save(baseClass);
        return RT.ok(isSuccess);
    }


    @PutMapping
    @ApiOperation(value = "修改班级")
    @SaCheckPermission("baseclass:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateBaseClassPageDto dto){
        BaseClass baseClass = BeanUtil.toBean(dto, BaseClass.class);
        baseClass.setModifyDate(new Date());
        baseClass.setModifyUserId(StpUtil.getLoginIdAsLong());
        return RT.ok(baseClassService.updateById(baseClass));
    }
}