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.PlatformPersonTimeInfoService;
import com.lutao.entity.PlatformPersonTimeInfo;
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 PlatformPersonTimeInfoStrategy implements CommandStrategy<PlatformPersonTimeInfo>{

    @Resource
    private PlatformPersonTimeInfoService platformPersonTimeInfoService;

    private static PlatformPersonTimeInfoStrategy platformPersonTimeInfoStrategy;

    @PostConstruct
    public void init() {
        platformPersonTimeInfoStrategy = this;
        platformPersonTimeInfoStrategy.platformPersonTimeInfoService = this.platformPersonTimeInfoService;
    }


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

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

            log.info("PlatformPersonTimeInfoStrategy 进行add操作 ......");
            platformPersonTimeInfoStrategy.platformPersonTimeInfoService.saveOrUpdate(platformPersonTimeInfo,
                    Wrappers.lambdaQuery(PlatformPersonTimeInfo.class)
                            .eq(PlatformPersonTimeInfo::getAppId,platformPersonTimeInfo.getAppId())
                            .eq(PlatformPersonTimeInfo::getPersonId,platformPersonTimeInfo.getPersonId())
                            .eq(PlatformPersonTimeInfo::getDataDate,platformPersonTimeInfo.getDataDate()));

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

            log.info("PlatformPersonTimeInfoStrategy 进行del操作 .......");
            platformPersonTimeInfoStrategy.platformPersonTimeInfoService.remove(Wrappers.lambdaQuery(PlatformPersonTimeInfo.class)
                    .eq(PlatformPersonTimeInfo::getAppId, platformPersonTimeInfo.getAppId())
                    .eq(PlatformPersonTimeInfo::getOriginId, platformPersonTimeInfo.getOriginId()));

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

            log.info("PlatformPersonTimeInfoStrategy 进行upd操作 .......");
            platformPersonTimeInfoStrategy.platformPersonTimeInfoService.update(platformPersonTimeInfo, Wrappers.lambdaQuery(PlatformPersonTimeInfo.class)
                    .eq(PlatformPersonTimeInfo::getAppId,platformPersonTimeInfo.getAppId())
                    .eq(PlatformPersonTimeInfo::getPersonId,platformPersonTimeInfo.getPersonId())
                    .eq(PlatformPersonTimeInfo::getDataDate,platformPersonTimeInfo.getDataDate()));
        } 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("PlatformPersonTimeInfoStrategy 进行find操作 .......");
            return MessageInfo.ok(platformPersonTimeInfoStrategy.platformPersonTimeInfoService.list(
                    Wrappers.lambdaQuery(PlatformPersonTimeInfo.class).in(PlatformPersonTimeInfo::getOriginId, idList)
                            .eq(PlatformPersonTimeInfo::getAppId,dataAddVO.getAppId())
            ));
        }

        return MessageInfo.ok();
    }

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