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.PersonInfoService;
import com.lutao.entity.PersonInfo;
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/21
 */
@Component
@Slf4j
public class PersonInfoStrategy implements CommandStrategy<PersonInfo> {

    @Resource
    private PersonInfoService personInfoService;

    private static PersonInfoStrategy personInfoStrategy;

    @PostConstruct
    public void init() {
        personInfoStrategy = this;
        personInfoStrategy.personInfoService = this.personInfoService;
    }

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

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

            log.info("PersonInfoStrategy 进行add操作 ......");
            personInfoStrategy.personInfoService.saveOrUpdate(personInfo,
                    Wrappers.lambdaQuery(PersonInfo.class)
                            .eq(PersonInfo::getAppId, personInfo.getAppId())
                            .eq(PersonInfo::getOriginId, personInfo.getOriginId()));

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

            log.info("PersonInfoStrategy 进行del操作 .......");
            personInfoStrategy.personInfoService.remove(Wrappers.lambdaQuery(PersonInfo.class)
                    .eq(PersonInfo::getAppId, personInfo.getAppId())
                    .eq(PersonInfo::getOriginId, personInfo.getOriginId()));

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

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

        return MessageInfo.ok();
    }

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