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.PlatformPersonInfoService;
import com.lutao.entity.PlatformPersonInfo;
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 PlatformPersonInfoStrategy implements CommandStrategy<PlatformPersonInfo>{
    @Resource
    private PlatformPersonInfoService platformPersonInfoService;


    private static PlatformPersonInfoStrategy platformPersonInfoStrategy;

    private List<PlatformPersonInfo> platformPersonInfos = new ArrayList<>();


    @PostConstruct
    public void init() {
        platformPersonInfoStrategy = this;
        platformPersonInfoStrategy.platformPersonInfoService = this.platformPersonInfoService;
        platformPersonInfoStrategy.platformPersonInfos = this.platformPersonInfos;
    }

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

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

            log.info("platformPersonInfoStrategy 进行add操作 ......");
            platformPersonInfoStrategy.platformPersonInfoService.saveOrUpdate(platformPersonInfo,
                    Wrappers.lambdaQuery(PlatformPersonInfo.class)
                            .eq(PlatformPersonInfo::getAppId, platformPersonInfo.getAppId())
                            .eq(PlatformPersonInfo::getPlatformCode, platformPersonInfo.getPlatformCode())
                            .eq(PlatformPersonInfo::getDataDate, platformPersonInfo.getDataDate())
                            .eq(PlatformPersonInfo::getPersonType, platformPersonInfo.getPersonType())
                            );

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

            log.info("platformPersonInfoStrategy 进行del操作 .......");
            platformPersonInfoStrategy.platformPersonInfoService.remove( Wrappers.lambdaQuery(PlatformPersonInfo.class)
                    .eq(PlatformPersonInfo::getAppId, platformPersonInfo.getAppId())
                    .eq(PlatformPersonInfo::getOriginId, platformPersonInfo.getOriginId()));

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

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

        return null;
    }

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

        List<PlatformPersonInfo> platformPersonInfos = new ArrayList<>();

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

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

                platformPersonInfoStrategy.platformPersonInfoService.remove(Wrappers.lambdaQuery(PlatformPersonInfo.class)
                        .eq(PlatformPersonInfo::getAppId, platformPersonInfo.getAppId())
                        .eq(PlatformPersonInfo::getOriginId, platformPersonInfo.getOriginId()));
            }
        } else if (MessageCommandEnum.MESSAGE_upd.getCommand().equals(command)) {
            log.info("platformPersonInfoStrategy 进行upd-batch操作 .......更新数量为：" + platformPersonInfos.size());
            for (PlatformPersonInfo platformPersonInfo : platformPersonInfos) {

                platformPersonInfoStrategy.platformPersonInfoService.update(platformPersonInfo, Wrappers.lambdaQuery(PlatformPersonInfo.class)
                        .eq(PlatformPersonInfo::getAppId, platformPersonInfo.getAppId())
                        .eq(PlatformPersonInfo::getOriginId, platformPersonInfo.getOriginId()));
            }
        }

        return null;
    }
}
