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.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.logic.notice.NoticeWebsitLogic;
import com.gree.mall.manager.plus.entity.NoticeWebsit;
import com.gree.mall.manager.plus.entity.NoticeWebsitRecord;
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 = "网点通知API", tags = {"网点通知API"})
@RequestMapping(value = "/noticeWebsit", produces = "application/json; charset=utf-8")
public class NoticeWebsitController {

    @Autowired
    private NoticeWebsitLogic noticeWebsitLogic;


    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "通知列表")
    public ResponseHelper<IPage<NoticeWebsitVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<NoticeWebsitVO> stockBeanIPage = noticeWebsitLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(stockBeanIPage, new TypeReference<NoticeWebsitVO>() {});
    }

    @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<NoticeWebsitVO> stockBeanIPage = noticeWebsitLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }

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

    @PostMapping("/add")
    @ApiOperation(value = "通知-新增")
    public ResponseHelper add(@RequestBody NoticeWebsitBean noticeBean) {
        noticeWebsitLogic.add(noticeBean);
        return ResponseHelper.success();
    }


    @PostMapping("/update")
    @ApiOperation(value = "通知-修改")
    public ResponseHelper update(@RequestBody NoticeWebsitBean noticeBean) {
        noticeWebsitLogic.update(noticeBean);
        return ResponseHelper.success();
    }


    @PostMapping("/ofOrSend")
    @ApiOperation(value = "通知-关闭修改")
    public ResponseHelper ofOrSend(
            @ApiParam(value = "id", required = true) @RequestParam(required = true) String id,
            @ApiParam(value = "SEND 发布 OFF 关闭", required = true) @RequestParam(required = true) String status

    ) {
        noticeWebsitLogic.ofOrSend(id,status);
        return ResponseHelper.success();
    }


    @ZfireList
    @PostMapping("/listWebsit")
    @ApiOperation(value = "网点通知信息列表")
    public ResponseHelper<IPage<NoticeWebsitRecordVO>> listWebsit(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<NoticeWebsitRecordVO> stockBeanIPage = noticeWebsitLogic.listWebsit(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(stockBeanIPage, new TypeReference<NoticeWebsitRecordVO>() {});
    }

    @PostMapping("/listWebsit/export")
    @ApiOperation(value = "网点通知信息列表导出")
    public void listWebsitExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<NoticeWebsitRecordVO> stockBeanIPage = noticeWebsitLogic.listWebsit(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @PostMapping("/list/count")
    @ApiOperation(value = "未读已读消息数量")
    public ResponseHelper<Integer> unreadCount(
                                                 @ApiParam(value = "YES 已读  NO 未读", required = false) @RequestParam String readFlag,
                                                 HttpServletRequest request) {
        return ResponseHelper.success(noticeWebsitLogic.unreadCount(request,readFlag));
    }


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

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



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


}
