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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.school.EduSchoolManuscriptAuthor;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.news.service.EduSchoolManuscriptAuthorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
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;

/**
 * Controller
 *
 * @author zhanghaijun
 * @date 2021-07-19 17:21:58
 */
@Api(tags = "通知咨询-稿件作者")
@Slf4j
@RestController
@RequestMapping("/news/manuscript/author")
@RequiredArgsConstructor
public class EduSchoolManuscriptAuthorController {

    private final EduSchoolManuscriptAuthorService eduSchoolManuscriptAuthorService;

    @ApiOperation("查询作者")
    @GetMapping("/listAuthor")
    public ResultBean<List<EduSchoolManuscriptAuthor>> listAuthor(@ApiParam("学校id") Long schoolId) {
        if (schoolId == null) {
            schoolId = FebsUtil.getTopDeptId();
        }
        return ResultBean.buildSuccess(eduSchoolManuscriptAuthorService.list(Wrappers.lambdaQuery(new EduSchoolManuscriptAuthor())
                .eq(EduSchoolManuscriptAuthor::getSchoolId, schoolId)
                .orderByDesc(EduSchoolManuscriptAuthor::getCreateDate)));
    }

    @ApiOperation("保存、编辑作者")
    @PostMapping("/saveAuthor")
    public ResultBean saveAuthor(@RequestBody EduSchoolManuscriptAuthor author) {
        if (author.getSchoolId() == null) {
            author.setSchoolId(FebsUtil.getTopDeptId());
        }
        eduSchoolManuscriptAuthorService.saveAuthor(author);
        return ResultBean.buildSuccess();
    }

    @ApiModelProperty("删除作者")
    @PostMapping("/delAutorBatch")
    public ResultBean delAutorBatch(@RequestBody List<Long> ids) {
        eduSchoolManuscriptAuthorService.removeByIds(ids);
        return ResultBean.buildSuccess();
    }
}
