
package com.tmerclub.cloud.multishop.controller.app;

import com.tmerclub.cloud.api.auth.bo.UidInfoBO;
import com.tmerclub.cloud.common.constant.StatusEnum;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.multishop.constant.NoticeType;
import com.tmerclub.cloud.multishop.dto.NoticeDTO;
import com.tmerclub.cloud.multishop.service.NoticeService;
import com.tmerclub.cloud.multishop.vo.NoticeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 公告
 *
 * @author YXF
 * @date 2020-11-25 17:57:56
 */
@RestController("appNoticeController")
@RequestMapping("/mua/app/notice")
//@RequestMapping("/ma/notice")
@Tag(name = "app-公告")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/list")
    @Operation(summary = "获取公告列表", description = "获取公告列表")
    @Parameter(name = "shopId", description = "店铺id")
    public ServerResponseEntity<List<NoticeVO>> list(@RequestParam("shopId") Long shopId) {
        List<NoticeVO> notices;
        List<NoticeVO> result;
        Long accountId;
        UidInfoBO uidInfoBO = AuthUserContext.get();
        notices = noticeService.getlistByShopId(shopId, NoticeType.TO_USER.value());
        //筛选出立即发送和到时间定时发送的公告
        List<NoticeVO> list = notices.stream().filter(noticeVO -> Objects.equals(noticeVO.getImmediatelySend(), 1) || (Objects.equals(noticeVO.getImmediatelySend(), 0) && noticeVO.getSendTime().before(new Date()))).collect(Collectors.toList());
        //登录状态显示 指定全部用户和指定用户可见的公告
        if (Objects.nonNull(uidInfoBO)) {
            accountId = AuthUserContext.get().getUserId();
            String userId = String.valueOf(accountId);
            result = list.stream().filter(notice -> Objects.isNull(notice.getUserIds()) || (Objects.nonNull(notice.getUserIds()) && notice.getUserIds().contains(userId))).collect(Collectors.toList());
        } else {
            //未登录状态只显示 全部用户可见的公告
            result = list.stream().filter(notice -> Objects.isNull(notice.getUserIds())).collect(Collectors.toList());
        }

        return ServerResponseEntity.success(result);
    }

    @GetMapping
    @Operation(summary = "获取公告分页列表", description = "获取公告分页列表")
    @Parameter(name = "shopId", description = "店铺id")
    public ServerResponseEntity<PageVO<NoticeVO>> page(@Valid PageDTO pageDTO, @RequestParam("shopId") Long shopId) {
        NoticeDTO noticeDTO = new NoticeDTO();
        noticeDTO.setShopId(shopId);
        noticeDTO.setType(NoticeType.TO_USER.value());
        noticeDTO.setStatus(StatusEnum.ENABLE.value());
        noticeDTO.setSendTime(new Date());
        noticeDTO.setAccountId(Objects.nonNull(AuthUserContext.get()) ? AuthUserContext.get().getUserId() : null);
        PageVO<NoticeVO> noticePage = noticeService.pagePlatformNotice(pageDTO, noticeDTO);
        return ServerResponseEntity.success(noticePage);
    }

    @GetMapping("/info")
    @Operation(summary = "获取公告", description = "根据id获取公告并更新为已读状态")
    public ServerResponseEntity<NoticeVO> getById(@RequestParam Long id) {
        noticeService.updateIsLearning(id);
        NoticeVO noticeVO = noticeService.getById(id);
        if (Objects.isNull(noticeVO)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        }
        List<Integer> typeList = Arrays.stream(noticeVO.getTypes().split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        for (Integer type : typeList) {
            noticeService.removeCacheByShopIdAndType(noticeVO.getShopId(), type);
        }
        return ServerResponseEntity.success(noticeVO);
    }

}
