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.GiveupMineReportService;
import com.lutao.entity.GiveupMineReport;
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 GiveUpMineReportStrategy implements CommandStrategy<GiveupMineReport> {

    @Resource
    private GiveupMineReportService giveupMineReportService;

    private static GiveUpMineReportStrategy giveUpMineReportStrategy;

    @PostConstruct
    public void init() {
        giveUpMineReportStrategy = this;
        giveUpMineReportStrategy.giveupMineReportService = this.giveupMineReportService;
    }

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

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

            log.info("GiveUpMineReportStrategy 进行add操作 ......");
            giveUpMineReportStrategy.giveupMineReportService.saveOrUpdate(giveupMineReport,
                    Wrappers.lambdaQuery(GiveupMineReport.class)
                            .eq(GiveupMineReport::getAppId,giveupMineReport.getAppId())
                            .eq(GiveupMineReport::getOriginId,giveupMineReport.getOriginId()));

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

            log.info("GiveUpMineReportStrategy 进行del操作 .......");
            giveUpMineReportStrategy.giveupMineReportService.remove(Wrappers.lambdaQuery(GiveupMineReport.class)
                    .eq(GiveupMineReport::getAppId, giveupMineReport.getAppId())
                    .eq(GiveupMineReport::getOriginId, giveupMineReport.getOriginId()));

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

            log.info("GiveUpMineReportStrategy 进行upd操作 .......");
            giveUpMineReportStrategy.giveupMineReportService.update(giveupMineReport, Wrappers.lambdaQuery(GiveupMineReport.class)
                    .eq(GiveupMineReport::getAppId, giveupMineReport.getAppId())
                    .eq(GiveupMineReport::getOriginId, giveupMineReport.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("GiveUpMineReportStrategy 进行find操作 .......");
            return MessageInfo.ok(giveUpMineReportStrategy.giveupMineReportService.list(
                    Wrappers.lambdaQuery(GiveupMineReport.class).in(GiveupMineReport::getOriginId, idList)
                            .eq(GiveupMineReport::getAppId,dataAddVO.getAppId())
            ));
        }

        return MessageInfo.ok();
    }

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

    }
}
