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.common.core.kafka.MessageCommandEnum;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.collect.vo.DataAddVO;
import com.lutao.cloud.consume.service.ChangeInfoReviewService;
import com.lutao.entity.ChangeInfoReview;
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 ChangeInfoReviewStrategy implements CommandStrategy<ChangeInfoReview> {

    @Resource
    private ChangeInfoReviewService changeInfoReviewService;

    private static ChangeInfoReviewStrategy changeInfoReviewStrategy;


    @PostConstruct
    public void init() {
        changeInfoReviewStrategy = this;
        changeInfoReviewStrategy.changeInfoReviewService = this.changeInfoReviewService;
    }

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

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

            log.info("ChangeInfoReviewStrategy 进行add操作 ......");
            changeInfoReviewStrategy.changeInfoReviewService.saveOrUpdate(changeInfoReview,
                    Wrappers.lambdaQuery(ChangeInfoReview.class)
                            .eq(ChangeInfoReview::getAppId, changeInfoReview.getAppId())
                            .eq(ChangeInfoReview::getOriginId, changeInfoReview.getOriginId()));

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

            log.info("ChangeInfoReviewStrategy 进行del操作 .......");
            changeInfoReviewStrategy.changeInfoReviewService.remove(Wrappers.lambdaQuery(ChangeInfoReview.class)
                    .eq(ChangeInfoReview::getAppId, changeInfoReview.getAppId())
                    .eq(ChangeInfoReview::getOriginId, changeInfoReview.getOriginId()));

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

            log.info("ChangeInfoReviewStrategy 进行upd操作 .......");
            changeInfoReviewStrategy.changeInfoReviewService.update(changeInfoReview, Wrappers.lambdaQuery(ChangeInfoReview.class)
                    .eq(ChangeInfoReview::getAppId, changeInfoReview.getAppId())
                    .eq(ChangeInfoReview::getOriginId, changeInfoReview.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("ChangeInfoReviewStrategy 进行find操作 .......");
            List<ChangeInfoReview> changeInfoReviews = changeInfoReviewStrategy.changeInfoReviewService.list(
                    Wrappers.lambdaQuery(ChangeInfoReview.class).in(ChangeInfoReview::getOriginId, idList)
                            .eq(ChangeInfoReview::getAppId, dataAddVO.getAppId())
            );
            return MessageInfo.ok(changeInfoReviews);
        }

        return null;
    }

    @Override
    public MessageInfo executeBatch(List<DataAddVO> dataAddVOS) {
        String command = dataAddVOS.get(0).getCommand();

        List<ChangeInfoReview> changeInfoReviews = new ArrayList<>();

        for (DataAddVO dataAddVO : dataAddVOS) {
            ChangeInfoReview changeInfoReview = JSONUtil.toBean(dataAddVO.getJsonDate().toString(), ChangeInfoReview.class);
            changeInfoReview.setAppId(dataAddVO.getAppId());
            changeInfoReviews.add(changeInfoReview);
        }

        if (MessageCommandEnum.MESSAGE_ADD.getCommand().equals(command)) {
            log.info("ChangeInfoReviewStrategy 进行add-batch操作 ......添加数量为：" + changeInfoReviews.size());
            for (ChangeInfoReview changeInfoReview : changeInfoReviews) {
                changeInfoReviewStrategy.changeInfoReviewService.saveOrUpdate(changeInfoReview,
                        Wrappers.lambdaQuery(ChangeInfoReview.class)
                                .eq(ChangeInfoReview::getAppId, changeInfoReview.getAppId())
                                .eq(ChangeInfoReview::getOriginId, changeInfoReview.getOriginId()));
            }
        } else if (MessageCommandEnum.MESSAGE_DEL.getCommand().equals(command)) {
            log.info("ChangeInfoReviewStrategy 进行del-batch操作 .......删除数量为：" + changeInfoReviews.size());
            for (ChangeInfoReview changeInfoReview : changeInfoReviews) {

                changeInfoReviewStrategy.changeInfoReviewService.remove(Wrappers.lambdaQuery(ChangeInfoReview.class)
                        .eq(ChangeInfoReview::getAppId, changeInfoReview.getAppId())
                        .eq(ChangeInfoReview::getOriginId, changeInfoReview.getOriginId()));
            }
        } else if (MessageCommandEnum.MESSAGE_upd.getCommand().equals(command)) {
            log.info("ChangeInfoReviewStrategy 进行upd-batch操作 .......更新数量为：" + changeInfoReviews.size());
            for (ChangeInfoReview changeInfoReview : changeInfoReviews) {

                changeInfoReviewStrategy.changeInfoReviewService.update(changeInfoReview, Wrappers.lambdaQuery(ChangeInfoReview.class)
                        .eq(ChangeInfoReview::getAppId, changeInfoReview.getAppId())
                        .eq(ChangeInfoReview::getOriginId, changeInfoReview.getOriginId()));
            }
        }

        return null;
    }
}
