package com.xjrsoft.module.courseTable.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.courseTable.dto.AddClassTimeDto;
import com.xjrsoft.module.courseTable.dto.ClassTimePageDto;
import com.xjrsoft.module.courseTable.dto.UpdateClassTimeDto;
import com.xjrsoft.module.courseTable.entity.ClassTime;
import com.xjrsoft.module.courseTable.service.IClassTimeService;
import com.xjrsoft.module.courseTable.vo.ClassTimePageVo;
import com.xjrsoft.module.courseTable.vo.ClassTimeVo;
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.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 上课时间 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-09-02 02:19:56
 */
@RequestMapping("/classtime")
@Api(value = "/classtime", tags = "上课时间")
@AllArgsConstructor
@RestController
public class ClassTimeController {
    private final IClassTimeService classTimeService;
    @GetMapping(value = "/page")
    @ApiOperation(value="上课时间列表(分页)")
    @SaCheckPermission("basenewstudent:detail")
    public RT<PageOutput<ClassTimePageVo>> page(@Valid ClassTimePageDto dto){
        LambdaQueryWrapper<ClassTime> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .orderByDesc(ClassTime::getId)
                .select(ClassTime.class,x -> VoToColumnUtil.fieldsToColumns(ClassTime.class).contains(x.getProperty()));
        IPage<ClassTime> page = classTimeService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<ClassTimePageVo> pageOutput = ConventPage.getPageOutput(page, ClassTimePageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询上课时间")
    @SaCheckPermission("basemajorcategor:detail")
    public RT<ClassTimeVo> info(@RequestParam Long id){
        ClassTime ClassTime = classTimeService.getById(id);
        if (ClassTime == null) {
            return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(ClassTime, ClassTimeVo.class));
    }

    @PostMapping
    @ApiOperation(value = "新增上课时间")
    @SaCheckPermission("basemajorcategor:add")
    public RT<Boolean> add(@Valid @RequestBody AddClassTimeDto dto){
        ClassTime classtime = BeanUtil.toBean(dto, ClassTime.class);
        classtime.setCreateDate(LocalDateTime.now());
        boolean isSuccess = classTimeService.save(classtime);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改上课时间")
    @SaCheckPermission("basemajorcategor:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateClassTimeDto dto){

        ClassTime classtime = BeanUtil.toBean(dto, ClassTime.class);
        classtime.setModifyDate(LocalDateTime.now());
        return RT.ok(classTimeService.updateById(classtime));
    }

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