package com.changhu.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.changhu.common.exception.MessageException;
import com.changhu.common.pojo.vo.SelectNodeVo;
import com.changhu.module.management.pojo.entity.EnterprisesUnit;
import com.changhu.module.management.pojo.entity.PoliceUnit;
import com.changhu.module.management.pojo.entity.SecurityUnit;
import com.changhu.module.management.service.ServiceProjectService;
import com.changhu.module.miniProgram.pojo.entity.SecurityUser;
import com.changhu.pojo.dto.DataViewDTO;
import com.changhu.pojo.dto.EnterprisesUnitDetailDTO;
import com.changhu.service.OpenApiService;
import com.changhu.support.mybatisplus.pojo.entity.BaseEntity;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @author 20252
 * @createTime 2024/10/9 下午5:29
 * @desc OpenApiServiceImpl...
 */
@Service
public class OpenApiServiceImpl implements OpenApiService {

    @Autowired
    private ServiceProjectService serviceProjectService;

    @Override
    public List<SelectNodeVo<Long>> getEnterprisesUnit(String policeCode) {
        PoliceUnit policeUnit = Db.lambdaQuery(PoliceUnit.class).eq(PoliceUnit::getCode, policeCode).oneOpt().orElseThrow(() -> new MessageException("当前系统没有该派出所"));
        return Db.lambdaQuery(EnterprisesUnit.class)
                .eq(EnterprisesUnit::getPoliceUnitId, policeUnit.getSnowFlakeId())
                .list()
                .stream()
                .map(item -> SelectNodeVo.<Long>builder()
                        .value(item.getSnowFlakeId())
                        .label(item.getName())
                        .build())
                .toList();
    }

    @Override
    public EnterprisesUnitDetailDTO enterprisesUnitDetailById(Long enterprisesUnitId) {
        return Db.lambdaQuery(EnterprisesUnit.class)
                .eq(BaseEntity::getSnowFlakeId, enterprisesUnitId)
                .oneOpt()
                .map(item -> EnterprisesUnitDetailDTO.builder()
                        .snowFlakeId(item.getSnowFlakeId())
                        .name(item.getName())
                        .address(item.getAddress())
                        .contactPersonInfo(item.getContactPersonInfo())
                        .remark(item.getRemark())
                        .serviceProjectList(serviceProjectService.getServiceProjectByEnterprisesUnitId(item.getSnowFlakeId()))
                        .createTime(item.getCreateTime())
                        .build())
                .orElseThrow(() -> new MessageException("企事业单位不存在"));
    }

    @SneakyThrows
    @Override
    public DataViewDTO dataView() {
        DataViewDTO dataViewDTO = new DataViewDTO();
        CountDownLatch countDownLatch = new CountDownLatch(5);
        try {
            dataViewDTO.setPoliceUnitCount(Db.lambdaQuery(PoliceUnit.class).count().intValue());
        } finally {
            countDownLatch.countDown();
        }
        try {
            dataViewDTO.setSecurityUnitCount(Db.lambdaQuery(SecurityUnit.class).count().intValue());
        } finally {
            countDownLatch.countDown();
        }
        try {
            dataViewDTO.setEnterprisesUnitCount(Db.lambdaQuery(EnterprisesUnit.class).count().intValue());
        } finally {
            countDownLatch.countDown();
        }
        try {
            dataViewDTO.setSecurityUserTotal(Db.lambdaQuery(SecurityUser.class).count().intValue());
        } finally {
            countDownLatch.countDown();
        }
        try {
            dataViewDTO.setNoCardSecurityUserCount(Db.lambdaQuery(SecurityUser.class)
                    .isNull(SecurityUser::getSecurityNumber)
                    .or()
                    .eq(SecurityUser::getSecurityNumber, "")
                    .count().intValue());
        } finally {
            countDownLatch.countDown();
        }
        countDownLatch.await();
        return dataViewDTO;
    }
}
