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

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tmerclub.cloud.api.im.feign.ImFeignClient;
import com.tmerclub.cloud.biz.service.NotifyLogService;
import com.tmerclub.cloud.biz.vo.NotifyLogVO;
import com.tmerclub.cloud.common.constant.Constant;
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.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
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.apache.dubbo.config.annotation.DubboReference;
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.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * @author lhd
 * @date 2020-05-12 08:21:24
 */
@RestController("appNotifyLogController")
@RequestMapping("/notifyLog")
@Tag(name = "消息通知")
public class NotifyLogController {

    @Autowired
    private NotifyLogService notifyLogService;
    @DubboReference
    private ImFeignClient imFeignClient;

    /**
     * 查询用户未读消息数量
     */
    @GetMapping("/unreadCount")
    @Operation(summary = "查询用户未读消息数量", description = "查询用户未读消息数量")
    public ServerResponseEntity<Integer> getNotifyCount() {
        Long userId = AuthUserContext.get().getUserId();
        int count = notifyLogService.countUnreadBySendTypeAndRemindType(userId, Constant.MSG_TYPE);
        // 添加未读客服消息数量
        Integer size = imFeignClient.getImUserUnreadNum(userId).getData();
        return ServerResponseEntity.success(count + size);
    }


    /**
     * 查询未读消息列表
     */
    @GetMapping("/getUnReadCountList")
    @Operation(summary = "查询消息列表", description = "查询消息列表")
    @Parameter(name = "status", description = "状态 0未读 1已读 不传查询全部按照已读未读排序")
    public ServerResponseEntity<PageVO<NotifyLogVO>> getUnReadCountList(@Valid PageDTO page, Integer status) {
        Long userId = AuthUserContext.get().getUserId();
        PageVO<NotifyLogVO> notifyLogPageVO = notifyLogService.pageBySendTypeAndRemindType(page, userId, Constant.MSG_TYPE, status);
        List<NotifyLogVO> records = notifyLogPageVO.getList();
        List<NotifyLogVO> updateList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (NotifyLogVO record : records) {
            if (!Objects.equals(StatusEnum.DISABLE.value(), record.getStatus())) {
                continue;
            }
            NotifyLogVO notifyLogVO = new NotifyLogVO();
            notifyLogVO.setLogId(record.getLogId());
            notifyLogVO.setStatus(StatusEnum.ENABLE.value());
            updateList.add(notifyLogVO);
        }
        if (CollectionUtils.isNotEmpty(updateList)) {
            notifyLogService.updateBatchById(updateList);
        }
        return ServerResponseEntity.success(notifyLogPageVO);
    }


}
