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.PersonInInfoService;
import com.lutao.entity.PersonInInfo;
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 PersonIniInfoStrategy implements CommandStrategy<PersonInInfo> {

    @Resource
    private PersonInInfoService personInInfoService;

    private static PersonIniInfoStrategy personIniInfoStrategy;

    @PostConstruct
    public void init() {
        personIniInfoStrategy = this;
        personIniInfoStrategy.personInInfoService = this.personInInfoService;
    }

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

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

            log.info("PersonIniInfoStrategy 进行add操作 ......");
            personIniInfoStrategy.personInInfoService.saveOrUpdate(personInInfo,
                    Wrappers.lambdaQuery(PersonInInfo.class).eq(PersonInInfo::getAppId,personInInfo.getAppId())
                            .eq(PersonInInfo::getOriginId,personInInfo.getOriginId()));

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

            log.info("PersonIniInfoStrategy 进行del操作 .......");
            personIniInfoStrategy.personInInfoService.remove(Wrappers.lambdaQuery(PersonInInfo.class)
                    .eq(PersonInInfo::getAppId, personInInfo.getAppId())
                    .eq(PersonInInfo::getOriginId, personInInfo.getOriginId()));

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

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

        return MessageInfo.ok();
    }

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