package com.xjrsoft.module.classtime.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.classtime.dto.AddClassTimeCalendarDto;
import com.xjrsoft.module.classtime.dto.ClassTimeCalendarPageDto;
import com.xjrsoft.module.classtime.dto.RefreshStatisticsDto;
import com.xjrsoft.module.classtime.dto.UpdateClassTimeCalendarDto;
import com.xjrsoft.module.classtime.entity.ClassTimeCalendar;
import com.xjrsoft.module.classtime.service.IClassTimeCalendarService;
import com.xjrsoft.module.classtime.vo.ClassTimeCalendarPageVo;
import com.xjrsoft.module.classtime.vo.ClassTimeCalendarVo;
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-09-26
* @Version 1.0
*/
@RestController
@RequestMapping("/classtime" + "/classTimeCalendar")
@Api(value = "/classtime"  + "/classTimeCalendar",tags = "节假日调课设置代码")
@AllArgsConstructor
public class ClassTimeCalendarController {


    private final IClassTimeCalendarService classTimeCalendarService;

    @GetMapping(value = "/page")
    @ApiOperation(value="节假日调课设置列表(分页)")
    @SaCheckPermission("classtimecalendar:detail")
    public RT<PageOutput<ClassTimeCalendarPageVo>> page(@Valid ClassTimeCalendarPageDto dto){

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

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询节假日调课设置信息")
    @SaCheckPermission("classtimecalendar:detail")
    public RT<ClassTimeCalendarVo> info(@RequestParam Long id){
        ClassTimeCalendar classTimeCalendar = classTimeCalendarService.getById(id);
        if (classTimeCalendar == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(classTimeCalendar, ClassTimeCalendarVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增节假日调课设置")
    @SaCheckPermission("classtimecalendar:add")
    public RT<Boolean> add(@Valid @RequestBody AddClassTimeCalendarDto dto){
        boolean isSuccess = classTimeCalendarService.add(dto);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改节假日调课设置")
    @SaCheckPermission("classtimecalendar:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateClassTimeCalendarDto dto){

        ClassTimeCalendar classTimeCalendar = BeanUtil.toBean(dto, ClassTimeCalendar.class);
        return RT.ok(classTimeCalendarService.updateById(classTimeCalendar));

    }

    @DeleteMapping
    @ApiOperation(value = "删除节假日调课设置")
    @SaCheckPermission("classtimecalendar:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(classTimeCalendarService.removeBatchByIds(ids));

    }

    @PostMapping(value = "/cancel")
    @ApiOperation(value="作废")
    @SaCheckPermission("classtimecalendar:detail")
    public RT<Boolean> cancel(@Valid @RequestBody RefreshStatisticsDto dto) throws Exception {
        Boolean cancel = classTimeCalendarService.cancel(dto.getId());
        return RT.ok(cancel);
    }

}