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.RecordManageService;
import com.lutao.entity.RecordManage;
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 RecordManageStrategy implements CommandStrategy<RecordManage> {

    @Resource
    private RecordManageService recordManageService;

    private static RecordManageStrategy recordManageStrategy;

    @PostConstruct
    public void init() {
        recordManageStrategy = this;
        recordManageStrategy.recordManageService = this.recordManageService;
    }

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

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

            log.info("RecordManageStrategy 进行add操作 ......");
            recordManageStrategy.recordManageService.saveOrUpdate(recordManage,
                    Wrappers.lambdaQuery(RecordManage.class)
                            .eq(RecordManage::getAppId,recordManage.getAppId())
                            .eq(RecordManage::getOriginId,recordManage.getOriginId()));

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

            log.info("RecordManageStrategy 进行del操作 .......");
            recordManageStrategy.recordManageService.remove(Wrappers.lambdaQuery(RecordManage.class)
                    .eq(RecordManage::getAppId, recordManage.getAppId())
                    .eq(RecordManage::getOriginId, recordManage.getOriginId()));

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

            log.info("RecordManageStrategy 进行upd操作 .......");
            recordManageStrategy.recordManageService.update(recordManage, Wrappers.lambdaQuery(RecordManage.class)
                    .eq(RecordManage::getAppId, recordManage.getAppId())
                    .eq(RecordManage::getOriginId, recordManage.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("RecordManageStrategy 进行find操作 .......");
            return MessageInfo.ok(recordManageStrategy.recordManageService.list(
                    Wrappers.lambdaQuery(RecordManage.class).in(RecordManage::getOriginId, idList)
                            .eq(RecordManage::getAppId,dataAddVO.getAppId())
            ));
        }

        return MessageInfo.ok();
    }

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