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.teacher.entity.XjrUser;
import com.xjrsoft.module.textbook.dto.AddCourseToSubjectGroupDto;
import com.xjrsoft.module.textbook.dto.AddSubjectGroupDto;
import com.xjrsoft.module.textbook.dto.SubjectGroupPageDto;
import com.xjrsoft.module.textbook.dto.UpdateSubjectGroupDto;
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.service.ISubjectGroupService;
import com.xjrsoft.module.textbook.vo.SubjectGroupCoursePageVo;
import com.xjrsoft.module.textbook.vo.SubjectGroupPageVo;
import com.xjrsoft.module.textbook.vo.SubjectGroupVo;
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" + "/subjectGroup")
@Api(value = "/textbook"  + "/subjectGroup",tags = "学科组管理代码")
@AllArgsConstructor
public class SubjectGroupController {


    private final ISubjectGroupService subjectGroupService;

    private final ISubjectGroupCourseService subjectGroupCourseService;

    @GetMapping(value = "/page")
    @ApiOperation(value="学科组管理列表(分页)")
    @SaCheckPermission("subjectgroup:detail")
    public RT<PageOutput<SubjectGroupPageVo>> page(@Valid SubjectGroupPageDto dto){

        MPJLambdaWrapper<SubjectGroup> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .select(SubjectGroup::getId)
                .selectAs(XjrUser::getName,SubjectGroupPageVo::getLeaderUserIdCN)
                .select(SubjectGroup.class,x -> VoToColumnUtil.fieldsToColumns(SubjectGroupPageVo.class).contains(x.getProperty()))
                .leftJoin(XjrUser.class,XjrUser::getId,SubjectGroup::getLeaderUserId)
                .like(ObjectUtil.isNotNull(dto.getGroupName()) && !dto.getGroupName().equals(""),SubjectGroup::getGroupName,dto.getGroupName())
                .eq(ObjectUtil.isNotNull(dto.getLeaderUserId()) && dto.getLeaderUserId() > 0, SubjectGroup::getLeaderUserId, dto.getLeaderUserId())
                .orderByDesc(SubjectGroup::getId);
        IPage<SubjectGroupPageVo> page = subjectGroupService.selectJoinListPage(ConventPage.getPage(dto),SubjectGroupPageVo.class, queryWrapper);
        for (SubjectGroupPageVo subjectGroupPageVo : page.getRecords()) {
            MPJLambdaWrapper<SubjectGroupCourse> queryWrapper1 = new MPJLambdaWrapper<>();
            queryWrapper1
                    .select(SubjectGroupCourse::getId)
                    .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(SubjectGroupCourse::getSubjectGroupId,subjectGroupPageVo.getId())
                    .orderByDesc(SubjectGroupCourse::getId);

            List<SubjectGroupCoursePageVo> subjectGroupCourseList = subjectGroupCourseService.selectJoinList(SubjectGroupCoursePageVo.class,queryWrapper1);
            subjectGroupPageVo.setSubjectGroupCourseList(subjectGroupCourseList);
        }
        PageOutput<SubjectGroupPageVo> pageOutput = ConventPage.getPageOutput(page, SubjectGroupPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询学科组管理信息")
    @SaCheckPermission("subjectgroup:detail")
    public RT<SubjectGroupVo> info(@RequestParam Long id){
        SubjectGroup subjectGroup = subjectGroupService.getById(id);
        if (subjectGroup == null) {
           return RT.error("找不到此数据！");
        }

        MPJLambdaWrapper<SubjectGroupCourse> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .select(SubjectGroupCourse::getId)
                .selectAs(BaseCourseSubject::getName, SubjectGroupCoursePageVo::getCourseSubjectIdCN)
                .select(SubjectGroupCourse.class,x -> VoToColumnUtil.fieldsToColumns(SubjectGroupCoursePageVo.class).contains(x.getProperty()))
                .leftJoin(BaseCourseSubject.class,BaseCourseSubject::getId,SubjectGroupCourse::getCourseSubjectId)
                .eq(SubjectGroupCourse::getSubjectGroupId,subjectGroup.getId())
                .orderByDesc(SubjectGroupCourse::getId);

        List<SubjectGroupCoursePageVo> subjectGroupCourseList = subjectGroupCourseService.selectJoinList(SubjectGroupCoursePageVo.class,queryWrapper);
        SubjectGroupVo subjectGroupVo = BeanUtil.toBean(subjectGroup, SubjectGroupVo.class);
        subjectGroupVo.setSubjectGroupCourseList(subjectGroupCourseList);
        return RT.ok(subjectGroupVo);
    }


    @PostMapping
    @ApiOperation(value = "新增学科组管理")
    @SaCheckPermission("subjectgroup:add")
    public RT<Boolean> add(@Valid @RequestBody AddSubjectGroupDto dto){
        Boolean isSuccess = subjectGroupService.add(dto);
        return RT.ok(isSuccess);
    }

    @PostMapping("/addCourse")
    @ApiOperation(value = "新增学科组下面的课程")
    @SaCheckPermission("subjectgroup:add")
    public RT<Boolean> addCourse(@Valid @RequestBody AddCourseToSubjectGroupDto dto){
        Boolean isSuccess = subjectGroupService.addCourse(dto);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改学科组管理")
    @SaCheckPermission("subjectgroup:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateSubjectGroupDto dto){
        return RT.ok(subjectGroupService.updateCourse(dto));
    }

    @PutMapping("/updateCourse")
    @ApiOperation(value = "修改学科组管理")
    @SaCheckPermission("subjectgroup:edit")
    public RT<Boolean> updateCourse(@Valid @RequestBody UpdateSubjectGroupDto dto){
        return RT.ok(subjectGroupService.updateCourse(dto));
    }

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

    @DeleteMapping("/deleteCourse")
    @ApiOperation(value = "删除学科组下面的课程")
    @SaCheckPermission("subjectgroup:delete")
    public RT<Boolean> deleteCourse(@Valid @RequestBody List<Long> ids){
        return RT.ok(subjectGroupCourseService.removeBatchByIds(ids));
    }

}