package org.nl.system.controller.notice;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.nl.common.base.TableDataInfo;
import org.nl.common.domain.query.PageQuery;
import org.nl.common.logging.annotation.Log;
import org.nl.system.service.notice.ISysNoticeService;
import org.nl.system.service.notice.dao.SysNotice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;
/**
* @author lyd
* @date 2023-05-09
**/
@Slf4j
@RestController
@Api(tags = "消息通知管理")
@RequestMapping("/api/notice")
public class SysNoticeController {

    @Autowired
    private ISysNoticeService noticeService;

    @GetMapping
    @Log("查询消息通知")
    @ApiOperation("查询消息通知")
    //@SaCheckPermission("@el.check('sysNotice:list')")
    public ResponseEntity<Object> query(@RequestParam Map whereJson, PageQuery page){
        return new ResponseEntity<>(TableDataInfo.build(noticeService.queryAll(whereJson,page)),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增消息通知")
    @ApiOperation("新增消息通知")
    //@SaCheckPermission("@el.check('sysNotice:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody SysNotice entity){
        noticeService.create(entity);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改消息通知")
    @ApiOperation("修改消息通知")
    //@SaCheckPermission("@el.check('sysNotice:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody SysNotice entity){
        noticeService.update(entity);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除消息通知")
    @ApiOperation("删除消息通知")
    //@SaCheckPermission("@el.check('sysNotice:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Set<String> ids) {
        noticeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("获取未读的接收消息条数")
    @GetMapping("/countByReceiveNotRead")
    public ResponseEntity<Object> countByReceiveNotRead(){
        return new ResponseEntity<>(noticeService.countByReceiveNotRead(), HttpStatus.OK);
    }

    @Log("接收消息分页")
    @GetMapping("/pageByReceive")
    public ResponseEntity<Object> pageByReceive(){
        return new ResponseEntity<>(noticeService.pageByReceive(), HttpStatus.OK);
    }

    @Log("标为已读")
    @PostMapping("/read")
    public ResponseEntity<Object> read(@RequestBody String id){
        noticeService.read(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("消息详情")
    @PostMapping("/findById")
    public ResponseEntity<Object> findById(@RequestBody String id){
        return new ResponseEntity<>(noticeService.getById(id), HttpStatus.OK);
    }

    @Log("修改已处理")
    @PostMapping("/deal")
    public ResponseEntity<Object> deal(@RequestBody String id){
        noticeService.deal(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("批量已读")
    @PostMapping("/changeRead")
    @ApiOperation("批量已读")
    public ResponseEntity<Object> changeRead(@RequestBody JSONObject jsonObject) {
        noticeService.changeRead(jsonObject);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
