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.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.ValveStatusMonitorService;
import com.lutao.entity.ValveStatusMonitor;
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;

@Component
@Slf4j
public class ValueStausMonitorStrategy implements CommandStrategy<ValveStatusMonitor>{

    @Resource
    private ValveStatusMonitorService valveStatusMonitorService;
    

    private static ValueStausMonitorStrategy valueStausMonitorStrategy;

    private List<ValveStatusMonitor> valveStatusMonitors = new ArrayList<>();


    @PostConstruct
    public void init() {
        valueStausMonitorStrategy = this;
        valueStausMonitorStrategy.valveStatusMonitorService = this.valveStatusMonitorService;
        valueStausMonitorStrategy.valveStatusMonitors = this.valveStatusMonitors;
    }

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

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

            log.info("valueStausMonitorStrategy 进行add操作 ......");
            valueStausMonitorStrategy.valveStatusMonitorService.saveOrUpdate(valveStatusMonitor,
                    Wrappers.lambdaQuery(ValveStatusMonitor.class)
                            .eq(ValveStatusMonitor::getAppId, valveStatusMonitor.getAppId())
                            .eq(ValveStatusMonitor::getOriginId, valveStatusMonitor.getOriginId()));

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

            log.info("valueStausMonitorStrategy 进行del操作 .......");
            valueStausMonitorStrategy.valveStatusMonitorService.remove( Wrappers.lambdaQuery(ValveStatusMonitor.class)
                    .eq(ValveStatusMonitor::getAppId, valveStatusMonitor.getAppId())
                    .eq(ValveStatusMonitor::getOriginId, valveStatusMonitor.getOriginId()));

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

            log.info("valueStausMonitorStrategy 进行upd操作 .......");
            valueStausMonitorStrategy.valveStatusMonitorService.update(valveStatusMonitor, Wrappers.lambdaQuery(ValveStatusMonitor.class)
                    .eq(ValveStatusMonitor::getAppId, valveStatusMonitor.getAppId())
                    .eq(ValveStatusMonitor::getOriginId, valveStatusMonitor.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("valueStausMonitorStrategy 进行find操作 .......");
            List<ValveStatusMonitor> list = valueStausMonitorStrategy.valveStatusMonitorService.list(
                    Wrappers.lambdaQuery(ValveStatusMonitor.class).in(ValveStatusMonitor::getOriginId, idList)
                            .eq(ValveStatusMonitor::getAppId, dataAddVO.getAppId())
            );
            return MessageInfo.ok(list);
        }

        return null;
    }

    @Override
    public MessageInfo executeBatch(List<DataAddVO> dataAddVOS) {
        String command = dataAddVOS.get(0).getCommand();

        List<ValveStatusMonitor> valveStatusMonitors = new ArrayList<>();

        for (DataAddVO dataAddVO : dataAddVOS) {
            ValveStatusMonitor ValveStatusMonitor = JSONUtil.toBean(dataAddVO.getJsonDate().toString(), ValveStatusMonitor.class);
            ValveStatusMonitor.setAppId(dataAddVO.getAppId());
            valveStatusMonitors.add(ValveStatusMonitor);
        }

        if (MessageCommandEnum.MESSAGE_ADD.getCommand().equals(command)) {
            log.info("valueStausMonitorStrategy 进行add-batch操作 ......添加数量为：" + valveStatusMonitors.size());
            for (ValveStatusMonitor valveStatusMonitor : valveStatusMonitors) {
                valueStausMonitorStrategy.valveStatusMonitorService.saveOrUpdate(valveStatusMonitor,
                        Wrappers.lambdaQuery(ValveStatusMonitor.class)
                                .eq(ValveStatusMonitor::getAppId, valveStatusMonitor.getAppId())
                                .eq(ValveStatusMonitor::getOriginId, valveStatusMonitor.getOriginId()));
            }
        } else if (MessageCommandEnum.MESSAGE_DEL.getCommand().equals(command)) {
            log.info("valueStausMonitorStrategy 进行del-batch操作 .......删除数量为：" + valveStatusMonitors.size());
            for (ValveStatusMonitor valveStatusMonitor : valveStatusMonitors) {

                valueStausMonitorStrategy.valveStatusMonitorService.remove(Wrappers.lambdaQuery(ValveStatusMonitor.class)
                        .eq(ValveStatusMonitor::getAppId, valveStatusMonitor.getAppId())
                        .eq(ValveStatusMonitor::getOriginId, valveStatusMonitor.getOriginId()));
            }
        } else if (MessageCommandEnum.MESSAGE_upd.getCommand().equals(command)) {
            log.info("valueStausMonitorStrategy 进行upd-batch操作 .......更新数量为：" + valveStatusMonitors.size());
            for (ValveStatusMonitor valveStatusMonitor : valveStatusMonitors) {

                valueStausMonitorStrategy.valveStatusMonitorService.update(valveStatusMonitor, Wrappers.lambdaQuery(ValveStatusMonitor.class)
                        .eq(ValveStatusMonitor::getAppId, valveStatusMonitor.getAppId())
                        .eq(ValveStatusMonitor::getOriginId, valveStatusMonitor.getOriginId()));
            }
        }

        return null;
    }
}
