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.TagAlarmClmService;
import com.lutao.entity.TagAlarmClm;
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 TagAlarmClmStrategy implements CommandStrategy<TagAlarmClm>{

    @Resource
    private TagAlarmClmService tagAlarmClmService;


    private static TagAlarmClmStrategy tagAlarmClmStrategy;

    private List<TagAlarmClm> tagAlarmClms = new ArrayList<>();


    @PostConstruct
    public void init() {
        tagAlarmClmStrategy = this;
        tagAlarmClmStrategy.tagAlarmClmService = this.tagAlarmClmService;
        tagAlarmClmStrategy.tagAlarmClms = this.tagAlarmClms;
    }

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

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

            log.info("tagAlarmClmStrategy 进行add操作 ......");
            tagAlarmClmStrategy.tagAlarmClmService.saveOrUpdate(tagAlarmClm,
                    Wrappers.lambdaQuery(TagAlarmClm.class)
                            .eq(TagAlarmClm::getAppId, tagAlarmClm.getAppId())
                            .eq(TagAlarmClm::getOriginId, tagAlarmClm.getOriginId()));

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

            log.info("tagAlarmClmStrategy 进行del操作 .......");
            tagAlarmClmStrategy.tagAlarmClmService.remove( Wrappers.lambdaQuery(TagAlarmClm.class)
                    .eq(TagAlarmClm::getAppId, tagAlarmClm.getAppId())
                    .eq(TagAlarmClm::getOriginId, tagAlarmClm.getOriginId()));

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

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

        return null;
    }

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

        List<TagAlarmClm> tagAlarmClms = new ArrayList<>();

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

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

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

                tagAlarmClmStrategy.tagAlarmClmService.update(tagAlarmClm, Wrappers.lambdaQuery(TagAlarmClm.class)
                        .eq(TagAlarmClm::getAppId, tagAlarmClm.getAppId())
                        .eq(TagAlarmClm::getOriginId, tagAlarmClm.getOriginId()));
            }
        }

        return null;
    }
}
