package com.yida.data.school.classSchedule.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.classschedule.EduClassScheduleTimeModel;
import com.yida.data.common.core.entity.classschedule.EduClassScheduleTimeModelDetail;
import com.yida.data.school.classSchedule.service.EduClassScheduleTimeModelDetailService;
import com.yida.data.school.classSchedule.service.EduClassScheduleTimeModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
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;

/**
 * 课程表时间安排模板 Controller
 *
 * @author wjm
 * @date 2021-08-13 16:53:42
 */
@Api(tags = "课程表-时间安排模板")
@Slf4j
@Validated
@RestController
@RequestMapping("/classSchedule/timeModel")
@RequiredArgsConstructor
public class EduClassScheduleTimeModelController {


    private final EduClassScheduleTimeModelService eduClassScheduleTimeModelService;
    private final EduClassScheduleTimeModelDetailService eduClassScheduleTimeModelDetailService;


    @ApiOperation("查询全部模板")
    @GetMapping("/listTimeModelPage")
    public ResultBean<IPage<EduClassScheduleTimeModel>> findList(@ApiParam("模板名称") @RequestParam(required = false) String name,
        @RequestParam(defaultValue = "1", required = false) Integer pageNum,
        @RequestParam(defaultValue = "10", required = false) Integer pageSize) {
        LambdaQueryWrapper<EduClassScheduleTimeModel> condition = Wrappers.lambdaQuery(new EduClassScheduleTimeModel());
        if (StrUtil.isNotBlank(name)) {
            condition.like(EduClassScheduleTimeModel::getName, name);
        }
        return ResultBean.buildSuccess(eduClassScheduleTimeModelService.page(Page.of(pageNum, pageSize), condition));
    }


    @ApiOperation("查询模板详情")
    @GetMapping("/getById")
    public ResultBean<EduClassScheduleTimeModel> getById(@ApiParam(value = "主键ID", required = true) Long id) {
        EduClassScheduleTimeModel eduClassScheduleTimeModel = eduClassScheduleTimeModelService.getById(id);
        eduClassScheduleTimeModel.setDetailList(eduClassScheduleTimeModelDetailService.list(
            Wrappers.<EduClassScheduleTimeModelDetail>query().lambda()
                .eq(EduClassScheduleTimeModelDetail::getModelId, eduClassScheduleTimeModel.getId())));
        return ResultBean.buildSuccess(eduClassScheduleTimeModel);
    }


    @ApiOperation("删除课时模板")
    @GetMapping("/deleteModel")
    public ResultBean deleteById(@ApiParam(value = "主键ID", required = true) Long id) {
        eduClassScheduleTimeModelService.removeById(id);
        eduClassScheduleTimeModelDetailService.remove(
            Wrappers.<EduClassScheduleTimeModelDetail>query().lambda().eq(EduClassScheduleTimeModelDetail::getModelId, id));
        return ResultBean.buildSuccess();
    }

    @ApiOperation("保存课时模板")
    @PostMapping("/saveModel")
    public ResultBean saveModel(@RequestBody EduClassScheduleTimeModel eduClassScheduleTimeModel) {
        eduClassScheduleTimeModelService.saveModel(eduClassScheduleTimeModel);
        return ResultBean.buildSuccess();
    }

}
