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.DivisionInspectionDetailService;
import com.lutao.entity.DivisionInspectionDetail;
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 DivisionInspectionDetailStrategy implements CommandStrategy<DivisionInspectionDetail> {

    @Resource
    private DivisionInspectionDetailService divisionInspectionDetailService;

    private static DivisionInspectionDetailStrategy divisionInspectionDetailStrategy;

    @PostConstruct
    public void init() {
        divisionInspectionDetailStrategy = this;
        divisionInspectionDetailStrategy.divisionInspectionDetailService = this.divisionInspectionDetailService;
    }

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

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

            log.info("divisionInspectionDetailStrategy 进行add操作 ......");
            divisionInspectionDetailStrategy.divisionInspectionDetailService.saveOrUpdate(divisionInspectionDetail,
                    Wrappers.lambdaQuery(DivisionInspectionDetail.class)
                            .eq(DivisionInspectionDetail::getAppId, divisionInspectionDetail.getAppId())
                            .eq(DivisionInspectionDetail::getOriginId, divisionInspectionDetail.getOriginId()));

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

            log.info("divisionInspectionDetailStrategy 进行del操作 .......");
            divisionInspectionDetailStrategy.divisionInspectionDetailService.remove(Wrappers.lambdaQuery(DivisionInspectionDetail.class)
                    .eq(DivisionInspectionDetail::getAppId, divisionInspectionDetail.getAppId())
                    .eq(DivisionInspectionDetail::getOriginId, divisionInspectionDetail.getOriginId()));

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

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

        return MessageInfo.ok();
    }

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


        return null;
    }
}
