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.HighRiskOperationService;
import com.lutao.entity.HighRiskOperation;
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/21
 */
@Component
@Slf4j
public class HighRiskOperationStrategy implements CommandStrategy<HighRiskOperation> {

    @Resource
    private HighRiskOperationService highRiskOperationService;

    private static HighRiskOperationStrategy highRiskOperationStrategy;

    @PostConstruct
    public void init() {
        highRiskOperationStrategy = this;
        highRiskOperationStrategy.highRiskOperationService = this.highRiskOperationService;
    }

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

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

            log.info("HighRiskOperationStrategy 进行add操作 ......");
            highRiskOperationStrategy.highRiskOperationService.saveOrUpdate(highRiskOperation,
                    Wrappers.lambdaQuery(HighRiskOperation.class)
                            .eq(HighRiskOperation::getAppId,highRiskOperation.getAppId())
                            .eq(HighRiskOperation::getOriginId,highRiskOperation.getOriginId()));

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

            log.info("HighRiskOperationStrategy 进行del操作 .......");
            highRiskOperationStrategy.highRiskOperationService.remove(Wrappers.lambdaQuery(HighRiskOperation.class)
                    .eq(HighRiskOperation::getAppId, highRiskOperation.getAppId())
                    .eq(HighRiskOperation::getOriginId, highRiskOperation.getOriginId()));

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

            log.info("HighRiskOperationStrategy 进行upd操作 .......");
            highRiskOperationStrategy.highRiskOperationService.update(highRiskOperation, Wrappers.lambdaQuery(HighRiskOperation.class)
                    .eq(HighRiskOperation::getAppId, highRiskOperation.getAppId())
                    .eq(HighRiskOperation::getOriginId, highRiskOperation.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("HighRiskOperationStrategy 进行find操作 .......");
            return MessageInfo.ok(highRiskOperationStrategy.highRiskOperationService.list(
                    Wrappers.lambdaQuery(HighRiskOperation.class).in(HighRiskOperation::getOriginId, idList)
                            .eq(HighRiskOperation::getAppId,dataAddVO.getAppId())
            ));
        }

        return MessageInfo.ok();
    }

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