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.WorkStatusClmService;
import com.lutao.entity.WorkStatusClm;
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 WorkStatusClmStrategy implements CommandStrategy<WorkStatusClm>{

    @Resource
    private WorkStatusClmService workStatusClmService;
    

    private static WorkStatusClmStrategy workStatusClmStrategy;

    private List<WorkStatusClm> workStatusClms = new ArrayList<>();


    @PostConstruct
    public void init() {
        workStatusClmStrategy = this;
        workStatusClmStrategy.workStatusClmService = this.workStatusClmService;
        workStatusClmStrategy.workStatusClms = this.workStatusClms;
    }

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

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

            log.info("workStatusClmStrategy 进行add操作 ......");
            workStatusClmStrategy.workStatusClmService.saveOrUpdate(workStatusClm,
                    Wrappers.lambdaQuery(WorkStatusClm.class)
                            .eq(WorkStatusClm::getAppId, workStatusClm.getAppId())
                            .eq(WorkStatusClm::getOriginId, workStatusClm.getOriginId()));

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

            log.info("workStatusClmStrategy 进行del操作 .......");
            workStatusClmStrategy.workStatusClmService.remove( Wrappers.lambdaQuery(WorkStatusClm.class)
                    .eq(WorkStatusClm::getAppId, workStatusClm.getAppId())
                    .eq(WorkStatusClm::getOriginId, workStatusClm.getOriginId()));

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

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

        return null;
    }

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

        List<WorkStatusClm> workStatusClms = new ArrayList<>();

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

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

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

                workStatusClmStrategy.workStatusClmService.update(workStatusClm, Wrappers.lambdaQuery(WorkStatusClm.class)
                        .eq(WorkStatusClm::getAppId, workStatusClm.getAppId())
                        .eq(WorkStatusClm::getOriginId, workStatusClm.getOriginId()));
            }
        }

        return null;
    }
}
