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.ChangeInfoService;
import com.lutao.entity.ChangeInfo;
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;

/**
 * 改变信息策略
 *
 * @author yx
 * @date 2023/07/19
 */

@Slf4j
@Component
public class ChangeInfoStrategy implements CommandStrategy<ChangeInfo> {

    @Resource
    private ChangeInfoService changeInfoService;

    private static ChangeInfoStrategy changeInfoStrategy;


    //通过@PostConstruct实现初始化bean之前进行的操作
    @PostConstruct
    public void init() {
        changeInfoStrategy = this;
        changeInfoStrategy.changeInfoService = this.changeInfoService;

    }

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

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

            log.info("ChangeInfoStrategy 进行add操作 ......");
            changeInfoStrategy.changeInfoService.saveOrUpdate(changeInfo,
                    Wrappers.lambdaQuery(ChangeInfo.class)
                            .eq(ChangeInfo::getAppId, changeInfo.getAppId())
                            .eq(ChangeInfo::getOriginId, changeInfo.getOriginId()));

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

            log.info("ChangeInfoStrategy 进行del操作 .......");
            changeInfoStrategy.changeInfoService.remove(Wrappers.lambdaQuery(ChangeInfo.class)
                    .eq(ChangeInfo::getAppId, changeInfo.getAppId())
                    .eq(ChangeInfo::getOriginId, changeInfo.getOriginId()));

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

            log.info("ChangeInfoStrategy 进行upd操作 .......");
            changeInfoStrategy.changeInfoService.update(changeInfo, Wrappers.lambdaQuery(ChangeInfo.class)
                    .eq(ChangeInfo::getAppId, changeInfo.getAppId())
                    .eq(ChangeInfo::getOriginId, changeInfo.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("ChangeInfoStrategy 进行find操作 .......");
            return MessageInfo.ok(changeInfoStrategy.changeInfoService.list(
                    Wrappers.lambdaQuery(ChangeInfo.class).in(ChangeInfo::getOriginId, idList)
                            .eq(ChangeInfo::getAppId, dataAddVO.getAppId())
            ));
        }

        return MessageInfo.ok();
    }

    @Override
    public MessageInfo executeBatch(List<DataAddVO> dataAddVOS) {
        String command = dataAddVOS.get(0).getCommand();
        List<ChangeInfo> changeInfos = new ArrayList<>();
        for (DataAddVO dataAddVO : dataAddVOS) {
            ChangeInfo changeInfo = JSONUtil.toBean(dataAddVO.getJsonDate().toString(), ChangeInfo.class);
            changeInfo.setAppId(dataAddVO.getAppId());
            changeInfos.add(changeInfo);
        }
        if (MessageCommandEnum.MESSAGE_ADD.getCommand().equals(command)) {
            log.info("ChangeInfoStrategy 进行add-batch操作 ......添加数量为：" + changeInfos.size());
            for (ChangeInfo changeInfo : changeInfos) {
                changeInfoStrategy.changeInfoService.saveOrUpdate(changeInfo,
                        Wrappers.lambdaQuery(ChangeInfo.class)
                                .eq(ChangeInfo::getAppId, changeInfo.getAppId())
                                .eq(ChangeInfo::getOriginId, changeInfo.getOriginId()));
            }
        } else if (MessageCommandEnum.MESSAGE_DEL.getCommand().equals(command)) {
            log.info("ChangeInfoStrategy 进行del-batch操作 .......删除数量为：" + changeInfos.size());
            for (ChangeInfo changeInfo : changeInfos) {
                changeInfoStrategy.changeInfoService.remove(Wrappers.lambdaQuery(ChangeInfo.class)
                        .eq(ChangeInfo::getAppId, changeInfo.getAppId())
                        .eq(ChangeInfo::getOriginId, changeInfo.getOriginId()));
            }
        } else if (MessageCommandEnum.MESSAGE_upd.getCommand().equals(command)) {
            log.info("ChangeInfoStrategy 进行upd-batch操作 .......更新数量为：" + changeInfos.size());
            for (ChangeInfo changeInfo : changeInfos) {
                changeInfoStrategy.changeInfoService.update(changeInfo, Wrappers.lambdaQuery(ChangeInfo.class)
                        .eq(ChangeInfo::getAppId, changeInfo.getAppId())
                        .eq(ChangeInfo::getOriginId, changeInfo.getOriginId()));
            }
        }

        return null;
    }
}
