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.MinisteriaSupervisionDetailService;
import com.lutao.entity.MinisteriaSupervisionDetail;
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/20
 */
@Component
@Slf4j
public class MinisteriaSupervisionDetailStrategy implements CommandStrategy<MinisteriaSupervisionDetail> {

    @Resource
    private MinisteriaSupervisionDetailService ministeriaSupervisionDetailService;

    private static MinisteriaSupervisionDetailStrategy ministeriaSupervisionDetailStrategy;

    @PostConstruct
    public void init() {
        ministeriaSupervisionDetailStrategy = this;
        ministeriaSupervisionDetailStrategy.ministeriaSupervisionDetailService = this.ministeriaSupervisionDetailService;
    }

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

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

            log.info("ministeriaSupervisionDetailStrategy 进行add操作 ......");
            ministeriaSupervisionDetailStrategy.ministeriaSupervisionDetailService.saveOrUpdate(ministeriaSupervisionDetail,
                    Wrappers.lambdaQuery(MinisteriaSupervisionDetail.class)
                            .eq(MinisteriaSupervisionDetail::getAppId, ministeriaSupervisionDetail.getAppId())
                            .eq(MinisteriaSupervisionDetail::getOriginId, ministeriaSupervisionDetail.getOriginId()));

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

            log.info("ministeriaSupervisionDetailStrategy 进行del操作 .......");
            ministeriaSupervisionDetailStrategy.ministeriaSupervisionDetailService.remove(Wrappers.lambdaQuery(MinisteriaSupervisionDetail.class)
                    .eq(MinisteriaSupervisionDetail::getAppId, ministeriaSupervisionDetail.getAppId())
                    .eq(MinisteriaSupervisionDetail::getOriginId, ministeriaSupervisionDetail.getOriginId()));

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

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

        return MessageInfo.ok();
    }

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

        return null;
    }
}
