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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.school.EduSchoolMatter;
import com.yida.data.common.core.entity.school.EduSchoolMatterGroup;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.news.service.EduSchoolMatterGroupService;
import com.yida.data.school.news.service.EduSchoolMatterService;
import com.yida.data.school.vo.index.MatterPageInfoVO;
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.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * Controller
 *
 * @author zhanghaijun
 * @date 2021-07-19 17:21:58
 */
@Slf4j
@Api(tags = "通知咨询-素材")
@RestController
@RequestMapping("/news/matter")
@RequiredArgsConstructor
public class EduSchoolMatterController {

    private final EduSchoolMatterService eduSchoolMatterService;
    private final EduSchoolMatterGroupService eduSchoolMatterGroupService;

    @ApiOperation("分页查询素材列表")
    @GetMapping("/listMatterPage")
    public ResultBean<IPage<MatterPageInfoVO>> listMatterPage(@ApiParam("学校id") Long schoolId,
                                                              @ApiParam("类型0-图片，1-视频，2-音频") Integer type,
                                                              @ApiParam("关键字") String keyword,
                                                              @ApiParam("分组id") Long groupId,
                                                              @ApiParam("当前页码,默认1") @RequestParam(defaultValue = "1") Integer pageNum,
                                                              @ApiParam("分页大小，默认10") @RequestParam(defaultValue = "10") Integer pageSize) {
        if (schoolId == null) {
            schoolId = FebsUtil.getTopDeptId();
        }
        return ResultBean.buildSuccess(eduSchoolMatterService.listMatterPage(new Page(pageNum, pageSize),
                schoolId, type, keyword, groupId));
    }

    @ApiOperation("新增、编辑素材")
    @PostMapping("/saveMatter")
    public ResultBean saveMatter(@RequestBody EduSchoolMatter matter) {
        if (matter.getSchoolId() == null) {
            matter.setSchoolId(FebsUtil.getTopDeptId());
        }
        eduSchoolMatterService.saveMatter(matter);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("批量删除素材")
    @PostMapping("/delMatterBatch")
    public ResultBean delMatterBatch(@RequestBody List<Long> ids) {
        eduSchoolMatterService.delMatterBatch(ids, FebsUtil.getTopDeptId());
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询分组列表")
    @GetMapping("/listMatterGroup")
    public ResultBean listMatterGroup(@ApiParam("学校id") Long schoolId,
                                      @ApiParam("分组类型，0-图片，1-视频，2-音频") Integer type) {
        if (schoolId == null) {
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            if (Objects.nonNull(currentUser.getMainDeptId())) {
                schoolId = currentUser.getMainDeptId();
            } else {
                schoolId = FebsUtil.getTopDeptId();
            }
        }
        return ResultBean.buildSuccess(eduSchoolMatterGroupService.listMatterGroup(schoolId, type));
    }

    @ApiOperation("删除分组")
    @PostMapping("/delGroupBatch")
    public ResultBean delGroupBatch(@RequestBody List<Long> ids) {
        eduSchoolMatterGroupService.delGroupBatch(ids);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("新增、编辑分组")
    @PostMapping("/saveGroup")
    public ResultBean saveGroup(@RequestBody EduSchoolMatterGroup group) {
        if (group.getSchoolId() == null) {
            group.setSchoolId(FebsUtil.getTopDeptId());
        }
        eduSchoolMatterGroupService.saveOrUpdate(group);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("批量分组")
    @GetMapping("/batchCluster")
    public ResultBean batchCluster(@ApiParam("分组id") Long groupId,
                                   @ApiParam("素材id") Long[] matterIds) {
        eduSchoolMatterService.batchCluster(groupId, matterIds);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("批量移除分组")
    @GetMapping("/batchUnCluster")
    public ResultBean batchUnCluster(@ApiParam("分组id") Long groupId,
                                     @ApiParam("素材id") Long[] matterIds) {
        eduSchoolMatterService.batchUnCluster(groupId, matterIds);
        return ResultBean.buildSuccess();
    }
}
