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.PersonOutInfoService;
import com.lutao.cloud.consume.service.PlatformPersonTimeInfoService;
import com.lutao.entity.PersonOutInfo;
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;

/**
 * 人员离平台策略
 *
 * @author yx
 * @date 2023/07/21
 */
@Component
@Slf4j
public class PersonOutInfoStrategy implements CommandStrategy<PersonOutInfo> {


    @Resource
    private PersonOutInfoService personOutInfoService;

    @Resource
    private PlatformPersonTimeInfoService platformPersonTimeInfoService;

    private static PersonOutInfoStrategy personOutInfoStrategy;

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

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

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

            log.info("PersonOutInfoStrategy 进行add操作 ......");
            personOutInfoStrategy.personOutInfoService.saveOrUpdate(personOutInfo,
                    Wrappers.lambdaQuery(PersonOutInfo.class)
                            .eq(PersonOutInfo::getAppId,personOutInfo.getAppId())
                            .eq(PersonOutInfo::getOriginId,personOutInfo.getOriginId()));

            //删除当天在平台人员
            if (personOutInfo.getPersonId() == null){
                //通过人员姓名删除
                personOutInfoStrategy.platformPersonTimeInfoService.remove(
                        Wrappers.lambdaQuery(PlatformPersonTimeInfo.class)
                                .eq(PlatformPersonTimeInfo::getPersonName,personOutInfo.getPersonName())
                                .eq(PlatformPersonTimeInfo::getDataDate,personOutInfo.getOutTime().toLocalDate())
                                .eq(PlatformPersonTimeInfo::getAppId,personOutInfo.getAppId())
                );
            }else {
                //通过人员id删除
                personOutInfoStrategy.platformPersonTimeInfoService.remove(
                        Wrappers.lambdaQuery(PlatformPersonTimeInfo.class)
                                .eq(PlatformPersonTimeInfo::getPersonId,personOutInfo.getPersonId())
                                .eq(PlatformPersonTimeInfo::getDataDate,personOutInfo.getOutTime().toLocalDate())
                                .eq(PlatformPersonTimeInfo::getAppId,personOutInfo.getAppId())
                );
            }





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

            log.info("PersonOutInfoStrategy 进行del操作 .......");
            personOutInfoStrategy.personOutInfoService.remove(Wrappers.lambdaQuery(PersonOutInfo.class)
                    .eq(PersonOutInfo::getAppId, personOutInfo.getAppId())
                    .eq(PersonOutInfo::getOriginId, personOutInfo.getOriginId()));

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

            log.info("PersonOutInfoStrategy 进行upd操作 .......");
            personOutInfoStrategy.personOutInfoService.update(personOutInfo, Wrappers.lambdaQuery(PersonOutInfo.class)
                    .eq(PersonOutInfo::getAppId, personOutInfo.getAppId())
                    .eq(PersonOutInfo::getOriginId, personOutInfo.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("PersonOutInfoStrategy 进行find操作 .......");
            return MessageInfo.ok(personOutInfoStrategy.personOutInfoService.list(
                    Wrappers.lambdaQuery(PersonOutInfo.class).in(PersonOutInfo::getOriginId, idList)
                            .eq(PersonOutInfo::getAppId,dataAddVO.getAppId())
            ));
        }

        return MessageInfo.ok();
    }

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