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.WorkStatusMonitorService;
import com.lutao.entity.WorkStatusMonitor;
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 WorkStatusMonitorStrategy implements CommandStrategy<WorkStatusMonitor>{

    @Resource
    private WorkStatusMonitorService workStatusMonitorService;
    

    private static WorkStatusMonitorStrategy workStatusMonitorStrategy;

    private List<WorkStatusMonitor> workStatusMonitors = new ArrayList<>();


    @PostConstruct
    public void init() {
        workStatusMonitorStrategy = this;
        workStatusMonitorStrategy.workStatusMonitorService = this.workStatusMonitorService;
        workStatusMonitorStrategy.workStatusMonitors = this.workStatusMonitors;
    }

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

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

            log.info("workStatusMonitorStrategy 进行add操作 ......");
            workStatusMonitorStrategy.workStatusMonitorService.saveOrUpdate(workStatusMonitor,
                    Wrappers.lambdaQuery(WorkStatusMonitor.class)
                            .eq(WorkStatusMonitor::getAppId, workStatusMonitor.getAppId())
                            .eq(WorkStatusMonitor::getOriginId, workStatusMonitor.getOriginId()));

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

            log.info("workStatusMonitorStrategy 进行del操作 .......");
            workStatusMonitorStrategy.workStatusMonitorService.remove( Wrappers.lambdaQuery(WorkStatusMonitor.class)
                    .eq(WorkStatusMonitor::getAppId, workStatusMonitor.getAppId())
                    .eq(WorkStatusMonitor::getOriginId, workStatusMonitor.getOriginId()));

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

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

        return null;
    }

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

        List<WorkStatusMonitor> workStatusMonitors = new ArrayList<>();

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

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

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

                workStatusMonitorStrategy.workStatusMonitorService.update(workStatusMonitor, Wrappers.lambdaQuery(WorkStatusMonitor.class)
                        .eq(WorkStatusMonitor::getAppId, workStatusMonitor.getAppId())
                        .eq(WorkStatusMonitor::getOriginId, workStatusMonitor.getOriginId()));
            }
        }

        return null;
    }
}
