package com.muyu.enterprise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.domain.MessageValue;
import com.muyu.domain.req.MessageValueReq;
import com.muyu.domain.resp.MessageValueListResp;
import com.muyu.enterprise.cache.AllMessageValueCacheService;
import com.muyu.enterprise.mapper.MessageValueMapper;
import com.muyu.enterprise.service.MessageValueService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: LiDongJia
 * @Package: com.muyu.car.service.impl
 * @Project: 2112-car-cloud-server
 * @name: MessageValueServiceImpl
 * @Date: 2024/9/19  15:48
 * @Description: 报文数据业务层
 */
@Log4j2
@Service
public class MessageValueServiceImpl
        extends ServiceImpl<MessageValueMapper, MessageValue>
        implements MessageValueService {

    @Autowired
    private MessageValueMapper messageValueMapper;

    @Autowired
    private AllMessageValueCacheService allMessageValueCacheService;

    public MessageValueServiceImpl(MessageValueMapper messageValueMapper) {
        this.messageValueMapper = messageValueMapper;
    }

    @Override
    public List<MessageValueListResp> findAll(MessageValueReq messageValueReq) {
        LambdaQueryWrapper<MessageValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(
                MessageValue::getTemplateId, messageValueReq.getMessageTemplateId()
        );
        queryWrapper.eq(
                MessageValue::getMessageType, messageValueReq.getMessageType()
        );
        List<MessageValue> list = this.list(queryWrapper);
        return list.stream()
                .map(messageValue -> MessageValueListResp.valueBuild(
                                messageValue
                        )
                ).collect(Collectors.toList());
//        ).list;
    }

    /**
     * 根据报文模版id获取报文模板列表
     * @param templateId 报文模版id
     * @return 报文模板列表
     */
    @Override
    public List<MessageValueListResp> findByTemplateId(Long templateId) {
        LambdaQueryWrapper<MessageValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MessageValue::getTemplateId, templateId);
        List<MessageValue> list = this.list(queryWrapper);
        List<MessageValueListResp> collect = list.stream()
                .map(messageValue -> MessageValueListResp.valueBuild(
                                messageValue
                        )
                ).collect(Collectors.toList());
//        allMessageValueCacheService.put(String.valueOf(templateId), collect);
        return collect;
    }
}
