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.ProfEquipmentInfoService;
import com.lutao.entity.ProfEquipmentInfo;
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 ProEquipmentInfoStrategy implements CommandStrategy<ProfEquipmentInfo> {

    @Resource
    private ProfEquipmentInfoService profEquipmentInfoService;


    private static ProEquipmentInfoStrategy proEquipmentInfoStrategy;

    private List<ProfEquipmentInfo> profEquipmentInfos = new ArrayList<>();


    @PostConstruct
    public void init() {
        proEquipmentInfoStrategy = this;
        proEquipmentInfoStrategy.profEquipmentInfoService = this.profEquipmentInfoService;
        proEquipmentInfoStrategy.profEquipmentInfos = this.profEquipmentInfos;
    }

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

        if (MessageCommandEnum.MESSAGE_ADD.getCommand().equals(command)) {
            ProfEquipmentInfo profEquipmentInfo = JSONUtil.toBean(data.toString(), ProfEquipmentInfo.class);

            String profEquipType = profEquipmentInfo.getProfEquipType();
            //如果专业设备类型 不是 1，2，3，4，5，6，7，8，9，10，11 则不进行添加
            if (!"1".equals(profEquipType) &&
                    !"2".equals(profEquipType) && !"3".equals(profEquipType) &&
                    !"4".equals(profEquipType) && !"5".equals(profEquipType) &&
                    !"6".equals(profEquipType) && !"7".equals(profEquipType) &&
                    !"8".equals(profEquipType) && !"9".equals(profEquipType) &&
                    !"10".equals(profEquipType) && !"11".equals(profEquipType)) {
                return null;
            }


            profEquipmentInfo.setAppId(dataAddVO.getAppId());

            log.info("proEquipmentInfoStrategy 进行add操作 ......");
            proEquipmentInfoStrategy.profEquipmentInfoService.saveOrUpdate(profEquipmentInfo,
                    Wrappers.lambdaQuery(ProfEquipmentInfo.class)
                            .eq(ProfEquipmentInfo::getAppId, profEquipmentInfo.getAppId())
                            .eq(ProfEquipmentInfo::getOriginId, profEquipmentInfo.getOriginId()));

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

            log.info("proEquipmentInfoStrategy 进行del操作 .......");
            proEquipmentInfoStrategy.profEquipmentInfoService.remove(Wrappers.lambdaQuery(ProfEquipmentInfo.class)
                    .eq(ProfEquipmentInfo::getAppId, profEquipmentInfo.getAppId())
                    .eq(ProfEquipmentInfo::getOriginId, profEquipmentInfo.getOriginId()));

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

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

        return null;
    }

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

        List<ProfEquipmentInfo> profEquipmentInfos = new ArrayList<>();

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

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

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

                proEquipmentInfoStrategy.profEquipmentInfoService.update(profEquipmentInfo, Wrappers.lambdaQuery(ProfEquipmentInfo.class)
                        .eq(ProfEquipmentInfo::getAppId, profEquipmentInfo.getAppId())
                        .eq(ProfEquipmentInfo::getOriginId, profEquipmentInfo.getOriginId()));
            }
        }

        return null;
    }
}
