package cn.com.ty.lift.push.message.controller;

import cn.com.ty.lift.common.constants.ApiConstants;
import cn.com.ty.lift.common.model.TimeMessage;
import cn.com.ty.lift.push.message.object.MessageBO;
import cn.com.ty.lift.push.message.object.MessageVo;
import cn.com.ty.lift.push.message.service.TimeMessageService;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

@RestController
@AllArgsConstructor
@Slf4j
@RequestMapping("message")
public class TimeMessageController {
    private final TimeMessageService timeMessageService;

    /**
     * 消息列表
     *
     * @param messageVo 分页数据的请求对象
     * @return 带已读，未读条数的分页列表
     */
    @PostMapping("list")
    public RestResponse<MessageBO> list(@RequestBody MessageVo messageVo) {
        IPage<TimeMessage> page = new Page<>(messageVo.getPageNum(), messageVo.getPageSize());
        IPage<TimeMessage> result = timeMessageService.page(page, Wrappers.<TimeMessage>query()
                .eq(messageVo.getUserId() != null, "user_id", messageVo.getUserId())
                .eq(messageVo.getType() != null, "type", messageVo.getType())
                .notIn("device_model", ApiConstants.DeviceModel.PC)
                .orderByAsc("view_flag")
                .orderByDesc("create_time"));
        MessageBO messageBO = MessageBO.pageCastMessageBo(result);
        messageBO.setReadCount((long) timeMessageService.count(Wrappers.<TimeMessage>query()
                .eq(messageVo.getUserId() != null, "user_id", messageVo.getUserId())
                .eq("view_flag", 1)
                .eq("type", messageVo.getType())
                .notIn("device_model", ApiConstants.DeviceModel.PC)));
        messageBO.setUnreadCount(messageBO.getTotal() - messageBO.getReadCount());
        return RestResponse.success(messageBO);
    }

    /**
     * @param messageVo 分页功能的请求对象
     * @return 一条消息详情
     */
    @PostMapping
    public RestResponse<TimeMessage> message(@RequestBody MessageVo messageVo) {
        TimeMessage byId = timeMessageService.getById(messageVo.getId());
        byId.setViewFlag(1);
        timeMessageService.updateById(byId);
        return RestResponse.success(byId);
    }

    /**
     * 设置多条消息已读
     *
     * @param messageVo id列表
     * @return 操作结果
     */
    @PostMapping("read")
    public RestResponse<Boolean> messages(@RequestBody MessageVo messageVo) {
        Collection<TimeMessage> timeMessages = timeMessageService.listByIds(messageVo.getIds());
        if (timeMessages.isEmpty()) {
            return RestResponse.success(true);
        }
        timeMessages.forEach(timeMessage -> timeMessage.setViewFlag(1));
        return RestResponse.success(timeMessageService.updateBatchById(timeMessages));
    }
}
