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


import cn.hutool.core.collection.CollUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tmerclub.cloud.biz.constant.NotifyType;
import com.tmerclub.cloud.biz.dto.NotifyLogDTO;
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.exception.LuckException;
import com.tmerclub.cloud.common.response.ResponseEnum;
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.Parameters;
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.*;

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

/**
 * @author lhd
 * @date 2020-05-12 08:21:24
 */
@RestController("adminNotifyLogController")
@RequestMapping("/mp/notifyLog")
@Tag(name = "商家端和供应商端消息通知")
public class NotifyLogController {

    @Autowired
    private NotifyLogService notifyLogService;


    /**
     * 查询用户未读消息数量
     */
    @GetMapping("/unreadCount")
    @Operation(summary = "查询用户未读消息数量", description = "查询用户未读消息数量")
    public ServerResponseEntity<Integer> getNotifyCount() {
        Long tenantId = AuthUserContext.get().getTenantId();
        return ServerResponseEntity.success(notifyLogService.countUnreadBySendTypeAndRemindType(tenantId, Constant.MSG_TYPE));
    }


    /**
     * 查询未读消息列表
     */
    @GetMapping("/getUnReadCountList")
    @Operation(summary = "查询消息列表", description = "查询消息列表")
    @Parameter(name = "status", description = "状态 0未读 1已读 不传查询全部按照已读未读排序")
    public ServerResponseEntity<PageVO<NotifyLogVO>> getUnReadCountList(@Valid PageDTO page, NotifyLogDTO notifyLogDTO) {
        Long tenantId = AuthUserContext.get().getTenantId();
        notifyLogDTO.setShopId(tenantId);
        notifyLogDTO.setSysType(AuthUserContext.get().getSysType());
        notifyLogDTO.setRemindType(NotifyType.APP.value());
        PageVO<NotifyLogVO> notifyLogPageVO = notifyLogService.pageShopNotifyLogByParam(page, notifyLogDTO);
        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.setStatus(StatusEnum.ENABLE.value());
            notifyLogVO.setLogId(record.getLogId());
            updateList.add(notifyLogVO);
        }
        if (CollectionUtils.isNotEmpty(updateList)) {
            notifyLogService.updateBatchById(updateList);
        }
        return ServerResponseEntity.success(notifyLogPageVO);
    }

    @GetMapping("/info")
    @Operation(summary = "获取消息详情", description = "根据id获取消息详情")
    public ServerResponseEntity<NotifyLogVO> getById(@RequestParam Long logId) {
        notifyLogService.updateStatus(logId);
        NotifyLogVO notifyLogVO = notifyLogService.getByLogId(logId);
        if (Objects.isNull(notifyLogVO)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        }
        return ServerResponseEntity.success(notifyLogVO);
    }

    @PutMapping("/isRead")
    @Operation(summary = "批量设置消息为已读状态 ", description = "批量设置消息为已读状态")
    @Parameters({
            @Parameter(name = "logIds", description = "消息id", required = true),
            @Parameter(name = "type", description = "数据类型 1全部 0已选")
    })
    public ServerResponseEntity<Void> isRead(@RequestParam(value = "logIds", required = false) List<Long> logIds, Integer type) {
        Long tenantId = AuthUserContext.get().getTenantId();
        if (Objects.equals(type, 1)) {
            notifyLogService.isReadByShopId(tenantId);
        } else if (Objects.equals(type, 0) && CollUtil.isNotEmpty(logIds)) {
            notifyLogService.isReadByIds(logIds);
        }
        return ServerResponseEntity.success();
    }


}

