package com.qmth.sop.server.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.bean.result.SysNoticeResult;
import com.qmth.sop.business.entity.SysNotice;
import com.qmth.sop.business.service.SysNoticeService;
import com.qmth.sop.common.annotation.OperationLog;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.LogTypeEnum;
import com.qmth.sop.common.enums.NoticeStatusEnum;
import com.qmth.sop.common.enums.NoticeTypeEnum;
import com.qmth.sop.common.util.Result;
import com.qmth.sop.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
 * 公告表 控制器
 *
 * @author: shudonghui
 * @date: 2023-08-07 13:53:29
 * @version: 1.0
 * @email: shudonghui@qmth.com.cn
 * @Company: www.qmth.com.cn
 */
@Api(tags = "公告表 Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_NOTICE)
public class SysNoticeController {


    @Resource
    SysNoticeService sysNoticeService;


    @ApiOperation(value = "公告表查询接口")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "公告表查询结果", response = SysNoticeResult.class)})
    public Result query(
            @ApiParam(value = "模糊查询条件", required = false) @RequestParam(required = false) String query,
            @ApiParam(value = "发布状态", required = false) @RequestParam(required = false) NoticeStatusEnum status,
            @ApiParam(value = "发布类型", required = false) @RequestParam(required = false) NoticeTypeEnum type,
            @ApiParam(value = "发布时间开始", required = false) @RequestParam(required = false) Long startTime,
            @ApiParam(value = "发布时间结束", required = false) @RequestParam(required = false) Long endTime,
            @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
            @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        IPage<SysNoticeResult> resultIPage = sysNoticeService.query(new Page<>(pageNumber, pageSize), query, status, type, startTime, endTime);

        return ResultUtil.ok(resultIPage);
    }


    @ApiOperation(value = "公告表新增修改接口")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.EDIT)
    public Result save(@Valid @ApiParam(value = "公告表信息", required = true) @RequestBody(required = true) SysNotice sysNotice, BindingResult bindingResult) throws InterruptedException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(sysNoticeService.saveSysNotice(sysNotice));
    }


    @ApiOperation(value = "公告表删除接口")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.DELETE)
    public Result delete(@ApiParam(value = "公告表id", required = true) @RequestParam(required = true) long id) {
        return ResultUtil.ok(sysNoticeService.delete(id));
    }
    /*
        批量禁用
     */

    @ApiOperation(value = "公告表批量禁用接口")
    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.DELETE)
    public Result disable(@ApiParam(value = "公告表id", required = true) @RequestParam(required = true) long[] ids) {
        return ResultUtil.ok(sysNoticeService.disable(ids));
    }

    /*
        发布或者撤销发布
     */

    @ApiOperation(value = "公告表发布或者撤销发布接口")
    @RequestMapping(value = "/publishOrUnpublish", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result publishOrUnpublish(@ApiParam(value = "公告表id", required = true) @RequestParam(required = true) long id) {
        return ResultUtil.ok(sysNoticeService.publishOrUnpublish(id));
    }


    @ApiOperation(value = "公告表获取单个接口")
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "公告表信息", response = SysNotice.class)})
    public Result get(@ApiParam(value = "公告表id", required = true) @RequestParam long id) {
        return ResultUtil.ok(sysNoticeService.getById(id));
    }


}