package cn.pluss.platform.controller;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.entity.NoticeReadRecord;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.notice.NoticeReadRecordService;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author djh
 * 通知已读相关接口
 */
@RestController
@RequestMapping("/noticeReadRecord")
public class NoticeReadRecordController {

    @Setter(onMethod_ = {@Autowired})
    private NoticeReadRecordService noticeReadRecordService;

    @GetMapping("/notice")
    public Result<Object> getNoReadNoticeCount() {
        Map<String, Object> resultMap = noticeReadRecordService.getAppNoReadNoticeCount();
        return ResultGenerator.genSuccessResult(resultMap);
    }

    @GetMapping("/article")
    public Result<Object> getNoReadArticleCount() {
        Map<String, Object> resultMap = noticeReadRecordService.getAppNoReadArticleCount();
        return ResultGenerator.genSuccessResult(resultMap);
    }

    @PostMapping("/article")
    public Result<Object> save(@RequestBody NoticeReadRecord noticeReadRecord) {
        try {
            noticeReadRecordService.save(noticeReadRecord);
        } catch (Exception ignored) {
        }

        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/clearReadFlag")
    public Result<Object> clearReadFlag(@RequestBody Map<String, Object> params) {
        if (params.get("typeFirst") == null) {
            throw new MsgException("缺少类别一");
        }

        if (params.get("typeSecond") == null) {
            throw new MsgException("缺少类别二");
        }

        noticeReadRecordService.clearReadFlag(params.get("typeFirst").toString(), params.get("typeSecond").toString());

        return ResultGenerator.genSuccessResult();
    }
}
