package com.lutao.cloud.consume.factory;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lutao.cloud.collect.vo.DataAddVO;
import com.lutao.cloud.common.core.kafka.MessageCommandEnum;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.consume.service.AppInfoService;
import com.lutao.entity.AppInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * 应用程序信息策略
 *
 * @author yx
 * @date 2023/07/20
 */
@Slf4j
@Component
public class AppInfoStrategy implements CommandStrategy<AppInfo> {

    @Autowired
    private AppInfoService appInfoService;

    private static AppInfoStrategy appInfoStrategy;

    @PostConstruct
    public void init() {
        appInfoStrategy = this;
        appInfoStrategy.appInfoService = this.appInfoService;
    }

    @Override
    public MessageInfo execute(String command, JsonNode data, DataAddVO dataAddVO) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            if (MessageCommandEnum.MESSAGE_ADD.getCommand().equals(command)) {
                AppInfo appInfo = objectMapper.treeToValue(data, AppInfo.class);

                log.info("AppInfoStrategy 进行add操作 ......");
                appInfoStrategy.appInfoService.save(appInfo);

            } else if (MessageCommandEnum.MESSAGE_DEL.getCommand().equals(command)) {
                AppInfo appInfo = objectMapper.treeToValue(data, AppInfo.class);

                log.info("AppInfoStrategy 进行del操作 .......");
                appInfoStrategy.appInfoService.remove(Wrappers.lambdaQuery(AppInfo.class)
                        .eq(AppInfo::getId, appInfo.getId()));

            } else if (MessageCommandEnum.MESSAGE_upd.getCommand().equals(command)) {
                AppInfo appInfo = objectMapper.treeToValue(data, AppInfo.class);

                log.info("AppInfoStrategy 进行upd操作 .......");
                appInfoStrategy.appInfoService.update(appInfo, Wrappers.lambdaQuery(AppInfo.class)
                        .eq(AppInfo::getId, appInfo.getId()));

            } else if (MessageCommandEnum.MESSAGE_FIND.getCommand().equals(command)) {

                List<String> idList = new ArrayList<>();
                for (JsonNode node : data) {
                    String id = node.get("id").asText();
                    idList.add(id);
                }

                log.info("AppInfoStrategy 进行find操作 .......");
                List<AppInfo> appInfoList = appInfoStrategy.appInfoService.list(Wrappers.lambdaQuery(AppInfo.class)
                        .in(AppInfo::getId, idList));
                return MessageInfo.ok(appInfoList);
            }

        } catch (JsonProcessingException e) {
            throw new RuntimeException("AppInfoStrategy::JsonNode-To-Entity::filed");
        }
        return MessageInfo.ok();
    }

    @Override
    public MessageInfo executeBatch(List<DataAddVO> dataAddVOS) {
        return null;
    }
}
