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.ProblemRectificationDetailService;
import com.lutao.entity.ProblemRectificationDetail;
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 ProblemRectificationDetailStrategy implements CommandStrategy<ProblemRectificationDetail> {

    @Resource
    private ProblemRectificationDetailService problemRectificationDetailService;

    private static ProblemRectificationDetailStrategy problemRectificationDetailStrategy;

    @PostConstruct
    public void init() {
        problemRectificationDetailStrategy = this;
        problemRectificationDetailStrategy.problemRectificationDetailService = this.problemRectificationDetailService;
    }

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

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

            log.info("ProblemRectificationDetailStrategy 进行add操作 ......");
            problemRectificationDetailStrategy.problemRectificationDetailService.saveOrUpdate(problemRectificationDetail,
                    Wrappers.lambdaQuery(ProblemRectificationDetail.class)
                            .eq(ProblemRectificationDetail::getAppId,problemRectificationDetail.getAppId())
                            .eq(ProblemRectificationDetail::getOriginId,problemRectificationDetail.getOriginId()));

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

            log.info("ProblemRectificationDetailStrategy 进行del操作 .......");
            problemRectificationDetailStrategy.problemRectificationDetailService.remove(Wrappers.lambdaQuery(ProblemRectificationDetail.class)
                    .eq(ProblemRectificationDetail::getAppId, problemRectificationDetail.getAppId())
                    .eq(ProblemRectificationDetail::getOriginId, problemRectificationDetail.getOriginId()));

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

            log.info("ProblemRectificationDetailStrategy 进行upd操作 .......");
            problemRectificationDetailStrategy.problemRectificationDetailService.update(problemRectificationDetail, Wrappers.lambdaQuery(ProblemRectificationDetail.class)
                    .eq(ProblemRectificationDetail::getAppId, problemRectificationDetail.getAppId())
                    .eq(ProblemRectificationDetail::getOriginId, problemRectificationDetail.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("ProblemRectificationDetailStrategy 进行find操作 .......");
            return MessageInfo.ok(problemRectificationDetailStrategy.problemRectificationDetailService.list(
                    Wrappers.lambdaQuery(ProblemRectificationDetail.class).in(ProblemRectificationDetail::getOriginId, idList)
                            .eq(ProblemRectificationDetail::getAppId,dataAddVO.getAppId())
            ));
        }

        return MessageInfo.ok();
    }

    @Override
    public MessageInfo executeBatch(List<DataAddVO> dataAddVOS) {
        return null;
    }
}
