package com.xjrsoft.module.classtime.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.activity.dto.ChangeStatusDto;
import com.xjrsoft.module.classtime.dto.AddClassTimeDeleteDto;
import com.xjrsoft.module.classtime.dto.ClassTimeDeletePageDto;
import com.xjrsoft.module.classtime.dto.UpdateClassTimeDeleteDto;
import com.xjrsoft.module.classtime.entity.ClassTimeDelete;
import com.xjrsoft.module.classtime.service.IClassTimeDeleteService;
import com.xjrsoft.module.classtime.vo.ClassTimeDeletePageVo;
import com.xjrsoft.module.classtime.vo.ClassTimeDeleteVo;
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-09-26
* @Version 1.0
*/
@RestController
@RequestMapping("/classtime" + "/classTimeDelete")
@Api(value = "/classtime"  + "/classTimeDelete",tags = "课时删除代码")
@AllArgsConstructor
public class ClassTimeDeleteController {


    private final IClassTimeDeleteService classTimeDeleteService;

    @GetMapping(value = "/page")
    @ApiOperation(value="课时删除列表(分页)")
    @SaCheckPermission("classtimedelete:detail")
    public RT<PageOutput<ClassTimeDeletePageVo>> page(@Valid ClassTimeDeletePageDto dto){

        Page<ClassTimeDeletePageVo> pages = classTimeDeleteService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<ClassTimeDeletePageVo> pageOutput = ConventPage.getPageOutput(pages, ClassTimeDeletePageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询课时删除信息")
    @SaCheckPermission("classtimedelete:detail")
    public RT<ClassTimeDeleteVo> info(@RequestParam Long id){
        ClassTimeDelete classTimeDelete = classTimeDeleteService.getById(id);
        if (classTimeDelete == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(classTimeDelete, ClassTimeDeleteVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增课时删除")
    @SaCheckPermission("classtimedelete:add")
    public RT<Boolean> add(@Valid @RequestBody AddClassTimeDeleteDto dto){
        ClassTimeDelete classTimeDelete = BeanUtil.toBean(dto, ClassTimeDelete.class);
        classTimeDelete.setCreateDate(new Date());
        boolean isSuccess = classTimeDeleteService.save(classTimeDelete);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改课时删除")
    @SaCheckPermission("classtimedelete:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateClassTimeDeleteDto dto){

        ClassTimeDelete classTimeDelete = BeanUtil.toBean(dto, ClassTimeDelete.class);
        classTimeDelete.setModifyDate(new Date());
        return RT.ok(classTimeDeleteService.updateById(classTimeDelete));

    }

    @DeleteMapping
    @ApiOperation(value = "删除课时删除")
    @SaCheckPermission("classtimedelete:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(classTimeDeleteService.removeBatchByIds(ids));

    }

    @PostMapping(value = "/change-status")
    @ApiOperation(value="修改状态")
    @SaCheckPermission("classtimestatistics:detail")
    public RT<Boolean> changeStatus(@Valid @RequestBody ChangeStatusDto dto) throws Exception {
        ClassTimeDelete classTimeDelete = classTimeDeleteService.getById(dto.getId());
        if(classTimeDelete == null){
            throw new MyException("未能查到数据");
        }
        classTimeDelete.setStatus(dto.getStatus());
        classTimeDeleteService.updateById(classTimeDelete);
        return RT.ok(true);
    }

}