package com.xjrsoft.module.textbook.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
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.base.entity.BaseCourseSubject;
import com.xjrsoft.module.textbook.dto.AddSubjectGroupCourseDto;
import com.xjrsoft.module.textbook.dto.SubjectGroupCoursePageDto;
import com.xjrsoft.module.textbook.dto.UpdateSubjectGroupCourseDto;
import com.xjrsoft.module.textbook.entity.SubjectGroup;
import com.xjrsoft.module.textbook.entity.SubjectGroupCourse;
import com.xjrsoft.module.textbook.service.ISubjectGroupCourseService;
import com.xjrsoft.module.textbook.vo.SubjectGroupCoursePageVo;
import com.xjrsoft.module.textbook.vo.SubjectGroupCourseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* @title: 学科组课程管理
* @Author szs
* @Date: 2023-12-25
* @Version 1.0
*/
@RestController
@RequestMapping("/textbook" + "/subjectGroupCourse")
@Api(value = "/textbook"  + "/subjectGroupCourse",tags = "学科组课程管理")
@AllArgsConstructor
public class SubjectGroupCourseController {


    private final ISubjectGroupCourseService subjectGroupCourseService;

    @GetMapping(value = "/page")
    @ApiOperation(value="学科组课程管理(分页)")
    @SaCheckPermission("subjectgroupcourse:detail")
    public RT<PageOutput<SubjectGroupCoursePageVo>> page(@Valid SubjectGroupCoursePageDto dto){
        MPJLambdaWrapper<SubjectGroupCourse> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .selectAs(BaseCourseSubject::getName, SubjectGroupCoursePageVo::getCourseSubjectIdCN)
                .selectAs(SubjectGroup::getGroupName, SubjectGroupCoursePageVo::getSubjectGroupIdCN)
                .select(SubjectGroupCourse.class,x -> VoToColumnUtil.fieldsToColumns(SubjectGroupCoursePageVo.class).contains(x.getProperty()))
                .leftJoin(BaseCourseSubject.class,BaseCourseSubject::getId,SubjectGroupCourse::getCourseSubjectId)
                .leftJoin(SubjectGroup.class,SubjectGroup::getId,SubjectGroupCourse::getSubjectGroupId)
                .eq(ObjectUtil.isNotNull(dto.getSubjectGroupId()) && dto.getSubjectGroupId() > 0, SubjectGroupCourse::getSubjectGroupId,dto.getSubjectGroupId())
                .orderByDesc(SubjectGroupCourse::getId);
        IPage<SubjectGroupCoursePageVo> page = subjectGroupCourseService.selectJoinListPage(ConventPage.getPage(dto),SubjectGroupCoursePageVo.class, queryWrapper);
        PageOutput<SubjectGroupCoursePageVo> pageOutput = ConventPage.getPageOutput(page, SubjectGroupCoursePageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询学科组课程管理信息")
    @SaCheckPermission("subjectgroupcourse:detail")
    public RT<SubjectGroupCourseVo> info(@RequestParam Long id){
        SubjectGroupCourse subjectGroupCourse = subjectGroupCourseService.getById(id);
        if (subjectGroupCourse == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(subjectGroupCourse, SubjectGroupCourseVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增学科组课程管理")
    @SaCheckPermission("subjectgroupcourse:add")
    public RT<Boolean> add(@Valid @RequestBody AddSubjectGroupCourseDto dto){
        SubjectGroupCourse subjectGroupCourse = BeanUtil.toBean(dto, SubjectGroupCourse.class);
        boolean isSuccess = subjectGroupCourseService.save(subjectGroupCourse);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改学科组课程管理")
    @SaCheckPermission("subjectgroupcourse:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateSubjectGroupCourseDto dto){

        SubjectGroupCourse subjectGroupCourse = BeanUtil.toBean(dto, SubjectGroupCourse.class);
        return RT.ok(subjectGroupCourseService.updateById(subjectGroupCourse));

    }

    @DeleteMapping
    @ApiOperation(value = "删除学科组课程管理")
    @SaCheckPermission("subjectgroupcourse:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(subjectGroupCourseService.removeBatchByIds(ids));

    }

}