package com.zfire.mall.manager.controller.admin;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.annotation.ZfireList;
import com.zfire.mall.manager.bean.admin.NoticeRecordReadBean;
import com.zfire.mall.manager.bean.listV2.NoticeRecordReadVO;
import com.zfire.mall.manager.bean.notice.NoticeBean;
import com.zfire.mall.manager.bean.notice.NoticeRecordBean;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.admin.NoticeLogic;
import com.zfire.mall.manager.plus.entity.NoticeRecord;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@Api(value = "消息通知接口", tags = {"消息通知接口"})
@RequestMapping(value = "/notice/", produces = "application/json; charset=utf-8")
public class NoticeController {

    @Autowired
    private NoticeLogic noticeLogic;


    @ZfireList
    @PostMapping("/list/v2")
    @ApiOperation(value = "消息通知列表V2")
    public ResponseHelper<NoticeRecordReadVO> listV2(
            @RequestBody ZfireParamBean zfireParamBean
    ){
        IPage<NoticeRecordReadVO> list = noticeLogic.listV2(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<NoticeRecordReadVO>() {});
    }


    @GetMapping("/list")
    @ApiOperation(value = "获取消息列表")
    public ResponseHelper<IPage<NoticeRecordReadBean>> list(
            HttpServletRequest request,
            @ApiParam(value = "是否已通知 true=已通知 false=未通知", required = false) @RequestParam(required = false) Boolean isNotice,
            @ApiParam(value = "通知类型(1=系统通知 2=文件下发 3=返利确认)", required = false) @RequestParam(required = false) String type,
            @ApiParam(value = "文件编码", required = false) @RequestParam(required = false) String code,
            @ApiParam(value = "文件标题", required = false) @RequestParam(required = false) String title,
            @ApiParam(value = "发布开始日期", required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "发布结束日期", required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "已读标记true:已读,false:未读,不传获取全部消息", required = false) @RequestParam(required = false) Boolean readFlag,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) {
        return ResponseHelper.success(noticeLogic.page(request,isNotice,type,code,title,startTime,endTime,readFlag, pageNum, pageSize));
    }

    @GetMapping("/detail")
    public ResponseHelper<NoticeRecordBean> detail(
            @ApiParam(value = "id", required = true) @RequestParam(required = true) String id,
            @ApiParam(value = "是否在登录消息框已读 true=是 false=否") @RequestParam(required = false) Boolean isTopRead
    ){
        NoticeRecordBean detail = noticeLogic.detail(id, isTopRead);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增通知信息")
    public ResponseHelper<Integer> add(@RequestBody NoticeBean noticeBean) {
        noticeLogic.add(noticeBean);
        return ResponseHelper.success();
    }


    @GetMapping("/list/count")
    @ApiOperation(value = "未读消息数量")
    public ResponseHelper<Integer> unreadCount(HttpServletRequest request) {
        return ResponseHelper.success(noticeLogic.unreadCount(request));
    }


    @PostMapping("/mark/read")
    @ApiOperation(value = "标记已读")
    public ResponseHelper markRead(
            @ApiParam(value = "通知消息id数组", required = true) @RequestParam List<String> noticeIds
    ) throws Exception {
        noticeLogic.markRead(noticeIds);
        return ResponseHelper.success();
    }


    @PostMapping("/mark/read/all")
    @ApiOperation(value = "全部标记已读")
    public ResponseHelper markReadAll(HttpServletRequest request) {
        noticeLogic.markReadAll(request);
        return ResponseHelper.success();
    }

    @PostMapping("/mark/del")
    @ApiOperation(value = "批量删除")
    public ResponseHelper markDel(
            @ApiParam(value = "通知消息id数组", required = true) @RequestParam List<String> noticeIds
    ){
        noticeLogic.markDel(noticeIds);
        return ResponseHelper.success();
    }

    @GetMapping("/read/list")
    @ApiOperation(value = "已读消息列表")
    public ResponseHelper<IPage<NoticeRecord>> readList(
            @ApiParam(value = "消息ID", required = true) @RequestParam String noticeId,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) {
        return ResponseHelper.success(noticeLogic.readListPage(noticeId, pageNum, pageSize));
    }

}
