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.ValveStatusClmService;
import com.lutao.entity.ValveStatusClm;
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 ValueStausClmStrategy implements CommandStrategy<ValveStatusClm>{

    @Resource
    private ValveStatusClmService valveStatusClmService;
    

    private static ValueStausClmStrategy valueStausClmStrategy;

    private List<ValveStatusClm> valveStatusClms = new ArrayList<>();


    @PostConstruct
    public void init() {
        valueStausClmStrategy = this;
        valueStausClmStrategy.valveStatusClmService = this.valveStatusClmService;
        valueStausClmStrategy.valveStatusClms = this.valveStatusClms;
    }

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

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

            log.info("valueStausClmStrategy 进行add操作 ......");
            valueStausClmStrategy.valveStatusClmService.saveOrUpdate(valveStatusClm,
                    Wrappers.lambdaQuery(ValveStatusClm.class)
                            .eq(ValveStatusClm::getAppId, valveStatusClm.getAppId())
                            .eq(ValveStatusClm::getOriginId, valveStatusClm.getOriginId()));

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

            log.info("valueStausClmStrategy 进行del操作 .......");
            valueStausClmStrategy.valveStatusClmService.remove( Wrappers.lambdaQuery(ValveStatusClm.class)
                    .eq(ValveStatusClm::getAppId, valveStatusClm.getAppId())
                    .eq(ValveStatusClm::getOriginId, valveStatusClm.getOriginId()));

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

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

        return null;
    }

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

        List<ValveStatusClm> valveStatusClms = new ArrayList<>();

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

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

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

                valueStausClmStrategy.valveStatusClmService.update(valveStatusClm, Wrappers.lambdaQuery(ValveStatusClm.class)
                        .eq(ValveStatusClm::getAppId, valveStatusClm.getAppId())
                        .eq(ValveStatusClm::getOriginId, valveStatusClm.getOriginId()));
            }
        }

        return null;
    }
}
