package com.lutao.cloud.consume.factory;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import com.lutao.cloud.common.core.kafka.MessageCommandEnum;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.collect.vo.DataAddVO;
import com.lutao.cloud.consume.service.PlatformInfoService;
import com.lutao.entity.PlatformInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

/**
 * 平台信息策略
 *
 * @author yx
 * @date 2023/07/21
 */
@Component
@Slf4j
public class PlatformInfoStrategy implements CommandStrategy<PlatformInfo> {

    @Resource
    private PlatformInfoService platformInfoService;

    private static PlatformInfoStrategy platformInfoStrategy;

    @PostConstruct
    public void init() {
        platformInfoStrategy = this;
        platformInfoStrategy.platformInfoService = this.platformInfoService;
    }

    @Override
    public MessageInfo execute(String command, JsonNode data, DataAddVO dataAddVO) {

        if (MessageCommandEnum.MESSAGE_ADD.getCommand().equals(command)) {
            PlatformInfo platformInfo = JSONUtil.toBean(data.toString(), PlatformInfo.class);
            platformInfo.setAppId(dataAddVO.getAppId());

            log.info("PlatformInfoStrategy 进行add操作 ......");
            platformInfoStrategy.platformInfoService.saveOrUpdate(platformInfo,
                    Wrappers.lambdaQuery(PlatformInfo.class)
                            .eq(PlatformInfo::getAppId, platformInfo.getAppId())
                            .eq(PlatformInfo::getOriginId, platformInfo.getOriginId()));

        } else if (MessageCommandEnum.MESSAGE_DEL.getCommand().equals(command)) {
            PlatformInfo platformInfo = JSONUtil.toBean(data.toString(), PlatformInfo.class);
            platformInfo.setAppId(dataAddVO.getAppId());

            log.info("PlatformInfoStrategy 进行del操作 .......");
            platformInfoStrategy.platformInfoService.remove(Wrappers.lambdaQuery(PlatformInfo.class)
                    .eq(PlatformInfo::getAppId, platformInfo.getAppId())
                    .eq(PlatformInfo::getOriginId, platformInfo.getOriginId()));

        } else if (MessageCommandEnum.MESSAGE_upd.getCommand().equals(command)) {
            PlatformInfo platformInfo = JSONUtil.toBean(data.toString(), PlatformInfo.class);
            platformInfo.setAppId(dataAddVO.getAppId());

            log.info("PlatformInfoStrategy 进行upd操作 .......");
            platformInfoStrategy.platformInfoService.update(platformInfo, Wrappers.lambdaQuery(PlatformInfo.class)
                    .eq(PlatformInfo::getAppId, platformInfo.getAppId())
                    .eq(PlatformInfo::getOriginId, platformInfo.getOriginId()));
        } 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("PlatformInfoStrategy 进行find操作 .......");
            return MessageInfo.ok(platformInfoStrategy.platformInfoService.list(
                    Wrappers.lambdaQuery(PlatformInfo.class).in(PlatformInfo::getOriginId, idList)
            ));
        }

        return MessageInfo.ok();
    }

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