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.DangerousActRecordService;
import com.lutao.entity.DangerousActRecord;
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 DangerousActRecordStrategy implements CommandStrategy<DangerousActRecord> {

    @Resource
    private DangerousActRecordService dangerousActRecordService;

    private static DangerousActRecordStrategy dangerousActRecordStrategy;

    @PostConstruct
    public void init() {
        dangerousActRecordStrategy = this;
        dangerousActRecordStrategy.dangerousActRecordService = this.dangerousActRecordService;

    }

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


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

            log.info("DangerousActRecordStrategy 进行add操作 ......");
            dangerousActRecordStrategy.dangerousActRecordService.saveOrUpdate(dangerousActRecord,Wrappers.lambdaQuery(DangerousActRecord.class)
                    .eq(DangerousActRecord::getAppId,dangerousActRecord.getAppId())
                    .eq(DangerousActRecord::getOriginId,dangerousActRecord.getOriginId()));

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

            log.info("DangerousActRecordStrategy 进行del操作 .......");
            dangerousActRecordStrategy.dangerousActRecordService.remove(Wrappers.lambdaQuery(DangerousActRecord.class)
                    .eq(DangerousActRecord::getAppId, dangerousActRecord.getAppId())
                    .eq(DangerousActRecord::getOriginId, dangerousActRecord.getOriginId()));

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

            log.info("DangerousActRecordStrategy 进行upd操作 .......");
            dangerousActRecordStrategy.dangerousActRecordService.update(dangerousActRecord, Wrappers.lambdaQuery(DangerousActRecord.class)
                    .eq(DangerousActRecord::getAppId, dangerousActRecord.getAppId())
                    .eq(DangerousActRecord::getOriginId, dangerousActRecord.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("DangerousActRecordStrategy 进行find操作 .......");
            return MessageInfo.ok(dangerousActRecordStrategy.dangerousActRecordService.list(
                    Wrappers.lambdaQuery(DangerousActRecord.class).in(DangerousActRecord::getOriginId, idList)
                            .eq(DangerousActRecord::getAppId,dataAddVO.getAppId())
            ));
        }

        return MessageInfo.ok();
    }

    @Override
    public MessageInfo executeBatch(List<DataAddVO> dataAddVOS) {

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

        return null;
    }
}
