package com.muyu.enterpise.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.domain.MessageValue;
import com.muyu.domain.req.MessageValueReq;
import com.muyu.domain.resp.MessageValueListResp;
import com.muyu.enterprise.cache.MessageValueCacheService;
import com.muyu.enterpise.mapper.MessageValueMapper;
import com.muyu.enterpise.service.MessageValueService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 消息值服务实现类
 *  * @ClassName MessageValueServiceImpl
 *  * @Description TODO
 *  * @Author Li HD
 *  * @Date 2024/9/19
 */
@Service
public class MessageValueServiceImpl
        extends ServiceImpl<MessageValueMapper, MessageValue>
        implements MessageValueService {

    @Resource
    private MessageValueMapper messageValueMapper;
    @Resource
    private MessageValueCacheService messageValueCacheService;



    /**
     * 查询所有消息值
     * @param messageValueReq
     * @return
     */
    @Override
    public List<MessageValueListResp> findAll(MessageValueReq messageValueReq) {
        LambdaQueryWrapper<MessageValue> queryWrapper = new LambdaQueryWrapper<>();
        if (null != messageValueReq.getMessageTemplateId()) {
            queryWrapper.eq(
                    MessageValue::getTemplateId, messageValueReq.getMessageTemplateId()
            );
        }

        if (StringUtils.isNotBlank(messageValueReq.getMessageType())){
            queryWrapper.eq(
                    MessageValue::getMessageType, messageValueReq.getMessageType()
            );
        }

        List<MessageValue> list = this.list(queryWrapper);
        messageValueCacheService.put(String.valueOf(messageValueReq.getMessageTemplateId()),list);
        List<MessageValue> messageValues = messageValueCacheService.get(String.valueOf(messageValueReq.getMessageTemplateId()));
        System.out.println("asoldonasond:"+messageValues);
        return list.stream()
                .map(messageValue -> MessageValueListResp.valueBuild(
                                messageValue
                        )
                )
                .toList();
    }

    /**
     * 测试
     * @param str
     */
    @Override
    public void test(String str) {
        JSONObject entries = new JSONObject();
        String[] split = str.split(" ");
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : split) {

        }
    }

    @Override
    public Long findByTemplateId(String stringVin) {
        return messageValueMapper.findByTemplateId(stringVin);
    }


}
