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.VideoEquipmentInfoService;
import com.lutao.entity.VideoEquipmentInfo;
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 VideoEquipmentInfoStrategy implements CommandStrategy<VideoEquipmentInfo>{

    @Resource
    private VideoEquipmentInfoService videoEquipmentInfoService;
    

    private static VideoEquipmentInfoStrategy videoEquipmentInfoStrategy;

    private List<VideoEquipmentInfo> videoEquipmentInfos = new ArrayList<>();


    @PostConstruct
    public void init() {
        videoEquipmentInfoStrategy = this;
        videoEquipmentInfoStrategy.videoEquipmentInfoService = this.videoEquipmentInfoService;
        videoEquipmentInfoStrategy.videoEquipmentInfos = this.videoEquipmentInfos;
    }

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

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

            log.info("videoEquipmentInfoStrategy 进行add操作 ......");
            videoEquipmentInfoStrategy.videoEquipmentInfoService.saveOrUpdate(videoEquipmentInfo,
                    Wrappers.lambdaQuery(VideoEquipmentInfo.class)
                            .eq(VideoEquipmentInfo::getAppId, videoEquipmentInfo.getAppId())
                            .eq(VideoEquipmentInfo::getOriginId, videoEquipmentInfo.getOriginId()));

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

            log.info("videoEquipmentInfoStrategy 进行del操作 .......");
            videoEquipmentInfoStrategy.videoEquipmentInfoService.remove( Wrappers.lambdaQuery(VideoEquipmentInfo.class)
                    .eq(VideoEquipmentInfo::getAppId, videoEquipmentInfo.getAppId())
                    .eq(VideoEquipmentInfo::getOriginId, videoEquipmentInfo.getOriginId()));

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

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

        return null;
    }

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

        List<VideoEquipmentInfo> videoEquipmentInfos = new ArrayList<>();

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

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

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

                videoEquipmentInfoStrategy.videoEquipmentInfoService.update(videoEquipmentInfo, Wrappers.lambdaQuery(VideoEquipmentInfo.class)
                        .eq(VideoEquipmentInfo::getAppId, videoEquipmentInfo.getAppId())
                        .eq(VideoEquipmentInfo::getOriginId, videoEquipmentInfo.getOriginId()));
            }
        }

        return null;
    }
}
