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.MattersManagerService;
import com.lutao.entity.MattersManager;
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 MattersManagerStrategy implements CommandStrategy<MattersManager> {

    @Resource
    private MattersManagerService mattersManagerService;

    private static MattersManagerStrategy mattersManagerStrategy;

    @PostConstruct
    public void init() {
        mattersManagerStrategy = this;
        mattersManagerStrategy.mattersManagerService = this.mattersManagerService;
    }

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

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

            log.info("MattersManagerStrategy 进行add操作 ......");
            mattersManagerStrategy.mattersManagerService.saveOrUpdate(mattersManager,
                    Wrappers.lambdaQuery(MattersManager.class)
                            .eq(MattersManager::getAppId,mattersManager.getAppId())
                            .eq(MattersManager::getOriginId,mattersManager.getOriginId()));

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

            log.info("MattersManagerStrategy 进行del操作 .......");
            mattersManagerStrategy.mattersManagerService.remove(Wrappers.lambdaQuery(MattersManager.class)
                    .eq(MattersManager::getAppId, mattersManager.getAppId())
                    .eq(MattersManager::getOriginId, mattersManager.getOriginId()));

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

            log.info("MattersManagerStrategy 进行upd操作 .......");
            mattersManagerStrategy.mattersManagerService.update(mattersManager, Wrappers.lambdaQuery(MattersManager.class)
                    .eq(MattersManager::getAppId, mattersManager.getAppId())
                    .eq(MattersManager::getOriginId, mattersManager.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("MattersManagerStrategy 进行find操作 .......");
            return MessageInfo.ok(mattersManagerStrategy.mattersManagerService.list(
                    Wrappers.lambdaQuery(MattersManager.class).in(MattersManager::getOriginId, idList)
                            .eq(MattersManager::getAppId,dataAddVO.getAppId())
            ));
        }

        return MessageInfo.ok();

    }

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