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.ContractorManageService;
import com.lutao.entity.ContractorManage;
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 ContractorManageStrategy implements CommandStrategy<ContractorManage> {

    @Resource
    private ContractorManageService contractorManageService;

    private static ContractorManageStrategy contractorManageStrategy;

    @PostConstruct
    public void init() {
        contractorManageStrategy = this;
        contractorManageStrategy.contractorManageService = this.contractorManageService;
    }

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

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

            log.info("contractorManageStrategy 进行add操作 ......");
            contractorManageStrategy.contractorManageService.saveOrUpdate(contractorManage,
                    Wrappers.lambdaQuery(ContractorManage.class)
                            .eq(ContractorManage::getAppId, contractorManage.getAppId())
                            .eq(ContractorManage::getOriginId, contractorManage.getOriginId()));

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

            log.info("contractorManageStrategy 进行del操作 .......");
            contractorManageStrategy.contractorManageService.remove(Wrappers.lambdaQuery(ContractorManage.class)
                    .eq(ContractorManage::getAppId, contractorManage.getAppId())
                    .eq(ContractorManage::getOriginId, contractorManage.getOriginId()));

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

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

        return MessageInfo.ok();
    }

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

        return null;
    }
}
