package com.changhu.module.superManagement.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.changhu.module.superManagement.pojo.vo.EnterprisesUnitServiceProjectSecurityUserTotalVo;
import com.changhu.module.superManagement.service.SuperIndexService;
import com.changhu.pojo.entity.EnterprisesUnit;
import com.changhu.pojo.entity.SecurityUser;
import com.changhu.pojo.entity.ServiceProject;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.concurrent.CountDownLatch;

/**
 * @author 20252
 * @createTime 2024/11/21 上午11:31
 * @desc SuperIndexServiceImpl...
 */
@Service
public class SuperIndexServiceImpl implements SuperIndexService {

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @SneakyThrows
    @Override
    public EnterprisesUnitServiceProjectSecurityUserTotalVo statistics() {
        CountDownLatch countDownLatch = new CountDownLatch(3);
        EnterprisesUnitServiceProjectSecurityUserTotalVo result = new EnterprisesUnitServiceProjectSecurityUserTotalVo();
        threadPoolTaskExecutor.execute(() -> {
            try {
                result.setEnterprisesUnitTotal(Db.lambdaQuery(EnterprisesUnit.class).count().intValue());
            } finally {
                countDownLatch.countDown();
            }
        });
        threadPoolTaskExecutor.execute(() -> {
            try {
                result.setServiceProjectTotal(Db.lambdaQuery(ServiceProject.class).count().intValue());
            } finally {
                countDownLatch.countDown();
            }
        });

        threadPoolTaskExecutor.execute(() -> {
            try {
                result.setSecurityUserTotal(Db.lambdaQuery(SecurityUser.class).count().intValue());
            } finally {
                countDownLatch.countDown();
            }
        });

        countDownLatch.await();
        return result;
    }
}
