package com.gree.mall.manager.controller.notice;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.gree.mall.manager.annotation.ApiNotAuth;
import com.gree.mall.manager.annotation.ZfireList;

import com.gree.mall.manager.bean.notice.*;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.notice.NoticeLogic;
import com.gree.mall.manager.plus.entity.NoticeRecord;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
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 javax.servlet.http.HttpServletResponse;
import java.util.List;

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

    @Autowired
    private NoticeLogic noticeLogic;


    @ZfireList
    @PostMapping("/list")
     @ApiOperation(value = "获取消息列表")
    public ResponseHelper<IPage<NoticeRecordReadBean>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<NoticeRecordReadBean> stockBeanIPage = noticeLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(stockBeanIPage, new TypeReference<NoticeRecordReadBean>() {});
    }

    @PostMapping("/list/export")
     @ApiOperation(value = "获取消息列表导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<NoticeRecordReadBean> stockBeanIPage = noticeLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @GetMapping("/detail")
     @ApiOperation(value = "消息详情")
    public ResponseHelper<NoticeRecordBean> detail(
             @ApiParam(value = "id", required = true) @RequestParam(required = true) String id
    ){
        NoticeRecordBean detail = noticeLogic.detail(id);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/add")
     @ApiOperation(value = "系统公告-新增")
    public ResponseHelper<Integer> add(@RequestBody NoticeBean noticeBean) {
        noticeLogic.add(noticeBean);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "系统公告-修改")
    public ResponseHelper<Integer> update(@RequestBody NoticeBean noticeBean) {
        noticeLogic.update(noticeBean);
        return ResponseHelper.success();
    }

    @GetMapping("/list/out")
    @ApiOperation(value = "轮询获得几秒得数据弹窗")
    public ResponseHelper<List<NoticeRecord>> out(
            @ApiParam(value = "获得几秒得数据", required = true) @RequestParam Integer num,
            HttpServletRequest request) {
        List<NoticeRecord> noticeRecords = noticeLogic.out(num);
        return ResponseHelper.success(noticeRecords);
    }

    @PostMapping("/updateIsNotice")
    @ApiOperation(value = "变更开启轮询通知")
    public ResponseHelper updateIsNotic(            @ApiParam(value = "YES开启 NO关闭", required = true) @RequestParam String isNotice
                                                    ) {
        noticeLogic.updateIsNotic(isNotice);
        return ResponseHelper.success();
    }


    @GetMapping("/list/count")
     @ApiOperation(value = "未读消息数量")
    public ResponseHelper<Integer> unreadCount(  @ApiParam(value = "NOTICE 平台公告   WBI 维保消息费用申请  WBK 维保费用申请审批  CRE_ORDER 创建工单  SEND_MALL  商城订单发货  RP_MALL 商城订单维权", required = false)
                                                     @RequestParam List<String> noticeType,
     @ApiParam(value = "YES 已读  NO 未读", required = false) @RequestParam String readFlag,
                                                HttpServletRequest request) {
        return ResponseHelper.success(noticeLogic.unreadCount(request,noticeType,readFlag));
    }


    @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();
    }

    @ZfireList
    @PostMapping("/read/listRecord")
    @ApiOperation(value = "公告已读消息列表")
    public ResponseHelper<IPage<NoticeRecordReadVOBean>> listRecord(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<NoticeRecordReadVOBean> stockBeanIPage = noticeLogic.listRecord(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(stockBeanIPage, new TypeReference<NoticeRecordReadBean>() {});
    }

    @PostMapping("/read/listRecord/export")
    @ApiOperation(value = "公告已读消息列表导出")
    public void listRecordExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<NoticeRecordReadVOBean> stockBeanIPage = noticeLogic.listRecord(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }



    @ZfireList
    @PostMapping("/read/list")
     @ApiOperation(value = "已读消息列表")
    public ResponseHelper<IPage<NoticeRecordReadBean>> readList(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<NoticeRecordReadBean> stockBeanIPage = noticeLogic.readList(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(stockBeanIPage, new TypeReference<NoticeRecordReadBean>() {});
    }

    @PostMapping("/read/list/export")
     @ApiOperation(value = "已读消息列表导出")
    public void readListExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<NoticeRecordReadBean> stockBeanIPage = noticeLogic.readList(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @ApiNotAuth
    @ZfireList
    @PostMapping("/listNotice")
     @ApiOperation(value = "系统公告-列表")
    public ResponseHelper<IPage<NoticeVO>> listNotice(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<NoticeVO> stockBeanIPage = noticeLogic.listNotice(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(stockBeanIPage, new TypeReference<NoticeVO>() {});
    }

    @PostMapping("/listNotice/export")
     @ApiOperation(value = "系统公告-列表导出")
    public void listNoticeExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<NoticeVO> stockBeanIPage = noticeLogic.listNotice(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @GetMapping("/detailNotice")
     @ApiOperation(value = "系统公告详情")
    public ResponseHelper<NoticeDetail> detailNotice(
             @ApiParam(value = "id", required = true) @RequestParam(required = true) String noticeId
    ){
        NoticeDetail detail = noticeLogic.detailNotice(noticeId);
        return ResponseHelper.success(detail);
    }

}
