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.EmergencyRecordService;
import com.lutao.entity.EmergencyRecord;
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 EmergencyRecordStrategy implements CommandStrategy<EmergencyRecord> {

    @Resource
    private EmergencyRecordService emergencyRecordService;

    private static EmergencyRecordStrategy emergencyRecordStrategy;

    @PostConstruct
    public void init() {
        emergencyRecordStrategy = this;
        emergencyRecordStrategy.emergencyRecordService = this.emergencyRecordService;
    }

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

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

            log.info("EmergencyRecordStrategy 进行add操作 ......");
            emergencyRecordStrategy.emergencyRecordService.saveOrUpdate(emergencyRecord, Wrappers.lambdaQuery(EmergencyRecord.class)
                    .eq(EmergencyRecord::getAppId, emergencyRecord.getAppId())
                    .eq(EmergencyRecord::getOriginId, emergencyRecord.getOriginId()));

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

            log.info("EmergencyRecordStrategy 进行del操作 .......");
            emergencyRecordStrategy.emergencyRecordService.remove(Wrappers.lambdaQuery(EmergencyRecord.class)
                    .eq(EmergencyRecord::getAppId, emergencyRecord.getAppId())
                    .eq(EmergencyRecord::getOriginId, emergencyRecord.getOriginId()));

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

            log.info("EmergencyRecordStrategy 进行upd操作 .......");
            emergencyRecordStrategy.emergencyRecordService.update(emergencyRecord, Wrappers.lambdaQuery(EmergencyRecord.class)
                    .eq(EmergencyRecord::getAppId, emergencyRecord.getAppId())
                    .eq(EmergencyRecord::getOriginId, emergencyRecord.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("EmergencyRecordStrategy 进行find操作 .......");
            return MessageInfo.ok(emergencyRecordStrategy.emergencyRecordService.list(
                    Wrappers.lambdaQuery(EmergencyRecord.class).in(EmergencyRecord::getOriginId, idList)
                            .eq(EmergencyRecord::getAppId,dataAddVO.getAppId())
            ));
        }

        return MessageInfo.ok();
    }

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

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

        return null;
    }
}
