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.PersonCertificateService;
import com.lutao.entity.PersonCertificate;
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 PersonCertificateStrategy implements CommandStrategy<PersonCertificate> {

    @Resource
    private PersonCertificateService personCertificateService;

    private static PersonCertificateStrategy personCertificateStrategy;

    @PostConstruct
    public void init() {
        personCertificateStrategy = this;
        personCertificateStrategy.personCertificateService = this.personCertificateService;
    }

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

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

            log.info("PersonCertificateStrategy 进行add操作 ......");
            personCertificateStrategy.personCertificateService.saveOrUpdate(personCertificate,
                    Wrappers.lambdaQuery(PersonCertificate.class)
                            .eq(PersonCertificate::getAppId,personCertificate.getAppId())
                            .eq(PersonCertificate::getPersonId,personCertificate.getPersonId())
                            .eq(PersonCertificate::getCertificateType,personCertificate.getCertificateType()));

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

            log.info("PersonCertificateStrategy 进行del操作 .......");
            personCertificateStrategy.personCertificateService.remove(Wrappers.lambdaQuery(PersonCertificate.class)
                    .eq(PersonCertificate::getAppId, personCertificate.getAppId())
                    .eq(PersonCertificate::getOriginId, personCertificate.getOriginId()));

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

            log.info("PersonCertificateStrategy 进行upd操作 .......");
            personCertificateStrategy.personCertificateService.update(personCertificate, Wrappers.lambdaQuery(PersonCertificate.class)
                    .eq(PersonCertificate::getAppId, personCertificate.getAppId())
                    .eq(PersonCertificate::getOriginId, personCertificate.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("PersonCertificateStrategy 进行find操作 .......");
            return MessageInfo.ok(personCertificateStrategy.personCertificateService.list(
                    Wrappers.lambdaQuery(PersonCertificate.class).in(PersonCertificate::getOriginId, idList)
                            .eq(PersonCertificate::getAppId,dataAddVO.getAppId())
            ));
        }

        return MessageInfo.ok();
    }

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