package com.eitc.patient.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eitc.common.annotation.Log;
import com.eitc.common.core.controller.BaseController;
import com.eitc.common.core.domain.AjaxResult;
import com.eitc.common.core.page.TableDataInfo;
import com.eitc.common.enums.BusinessType;
import com.eitc.common.utils.StringUtils;
import com.eitc.patient.domain.NoticeManage;
import com.eitc.patient.enums.NoticeManagePublishStatusEnum;
import com.eitc.patient.service.INoticeManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * NoticeManageController
 *
 * @author mtx
 * @date: 2024/05/17 15:51
 */
@Api(tags = "公告管理")
@RestController
@RequestMapping("/notice-manage")
public class NoticeManageController extends BaseController {

    @Resource
    private INoticeManageService noticeManageService;

    /**
     * 查询公告管理
     */
    @GetMapping("/list")
    @ApiOperation("查询公告管理")
    public TableDataInfo list(NoticeManage noticeManage) {
        startPage();
        LambdaQueryWrapper<NoticeManage> wrapper = Wrappers.<NoticeManage>lambdaQuery().orderByDesc(NoticeManage::getCreateDate)
                .like(StringUtils.isNotBlank(noticeManage.getNoticeTitle()), NoticeManage::getNoticeTitle, noticeManage.getNoticeTitle())
                .like(Objects.nonNull(noticeManage.getPublishStatus()), NoticeManage::getPublishStatus, noticeManage.getPublishStatus())
                .like(Objects.nonNull(noticeManage.getDisplayStatus()), NoticeManage::getDisplayStatus, noticeManage.getDisplayStatus());
        List<NoticeManage> list = noticeManageService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 获取公告管理详细信息
     */
    @GetMapping(value = "/detail")
    @ApiOperation("获取公告管理详细信息")
    public AjaxResult detail(String id) {
        return success(noticeManageService.getById(id));
    }

    /**
     * 新增公告管理
     */
    @Log(title = "新增公告管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增公告管理")
    public AjaxResult add(@Valid @RequestBody NoticeManage noticeManage) {
        noticeManage.setDisplayStatus(noticeManageService.checkDisplayStatus(noticeManage));
        return toAjax(noticeManageService.save(noticeManage));
    }

    /**
     * 修改公告管理
     */
    @Log(title = "修改公告管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改公告管理")
    public AjaxResult edit(@Valid @RequestBody NoticeManage entry) {
        NoticeManage noticeManage = noticeManageService.getById(entry.getId());
        if (Objects.isNull(noticeManage)) {
            return AjaxResult.error("公告不存在");
        }
        if (Objects.equals(noticeManage.getPublishStatus(), NoticeManagePublishStatusEnum.PUBLISH.getCode())) {
            return AjaxResult.error("请先撤回");
        }
        entry.setDisplayStatus(noticeManageService.checkDisplayStatus(entry));
        return toAjax(noticeManageService.updateById(entry));
    }

    /**
     * 撤回公告管理
     */
    @Log(title = "撤回公告管理", businessType = BusinessType.UPDATE)
    @PostMapping("/revoke")
    @ApiOperation("撤回公告管理")
    public AjaxResult revoke(@RequestBody NoticeManage entry) {
        NoticeManage noticeManage = noticeManageService.getById(entry.getId());
        if (Objects.isNull(noticeManage)) {
            return AjaxResult.error("公告不存在");
        }
        NoticeManage notice = new NoticeManage();
        notice.setPublishStatus(NoticeManagePublishStatusEnum.NO_PUBLISH.getCode());
        notice.setId(entry.getId());
        return toAjax(noticeManageService.updateById(notice));
    }

    /**
     * 删除公告管理
     */
    @Log(title = "删除公告管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    @ApiOperation("删除公告管理")
    public AjaxResult remove(String id) {
        NoticeManage noticeManage = noticeManageService.getById(id);
        if (Objects.isNull(noticeManage)) {
            return AjaxResult.error("公告不存在");
        }
        if (Objects.equals(noticeManage.getPublishStatus(), NoticeManagePublishStatusEnum.PUBLISH.getCode())) {
            return AjaxResult.error("请先撤回");
        }
        return toAjax(noticeManageService.removeById(id));
    }

}
