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

import cn.hutool.core.lang.Dict;
import cn.hutool.core.lang.func.LambdaUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.changhu.common.pojo.vo.SelectNodeVo;
import com.changhu.common.utils.UserUtil;
import com.changhu.module.management.mapper.ServiceProjectMapper;
import com.changhu.module.management.pojo.entity.EnterprisesUnit;
import com.changhu.module.management.pojo.entity.ServiceProject;
import com.changhu.module.miniProgram.pojo.entity.SecurityUser;
import com.changhu.module.miniProgram.pojo.vo.IndexDataStatisticsVo;
import com.changhu.module.miniProgram.pojo.vo.IndexServiceProjectListVo;
import com.changhu.module.miniProgram.service.MPoliceService;
import com.changhu.support.mybatisplus.pojo.entity.BaseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 20252
 * @createTime 2024/9/11 上午10:53
 * @desc MPoliceServiceImpl...
 */
@Service
public class MPoliceServiceImpl implements MPoliceService {

    @Autowired
    private ServiceProjectMapper serviceProjectMapper;

    @Override
    public IndexDataStatisticsVo dataStatistics() {
        Long unitId = UserUtil.getUnitId();
        IndexDataStatisticsVo indexDataStatisticsVo = new IndexDataStatisticsVo();
        List<Long> enterprisesUnitIds = Db.lambdaQuery(EnterprisesUnit.class)
                .eq(EnterprisesUnit::getPoliceUnitId, unitId)
                .list()
                .stream().map(BaseEntity::getSnowFlakeId)
                .toList();
        if (enterprisesUnitIds.isEmpty()) {
            return indexDataStatisticsVo;
        }
        indexDataStatisticsVo.setEnterprisesUnitCount(enterprisesUnitIds.size());

        List<Long> serviceProjectIds = Db.lambdaQuery(ServiceProject.class)
                .in(ServiceProject::getEnterprisesUnitId, enterprisesUnitIds)
                .list()
                .stream()
                .map(BaseEntity::getSnowFlakeId)
                .toList();
        if (serviceProjectIds.isEmpty()) {
            return indexDataStatisticsVo;
        }
        indexDataStatisticsVo.setServiceProjectCount(serviceProjectIds.size());

        Long securityUserCount = Db.lambdaQuery(SecurityUser.class)
                .in(SecurityUser::getServiceProjectId, serviceProjectIds)
                .count();
        indexDataStatisticsVo.setSecurityUserCount(securityUserCount.intValue());

        Long noCardSecurityUserCount = Db.lambdaQuery(SecurityUser.class)
                .in(SecurityUser::getServiceProjectId, serviceProjectIds)
                .isNull(SecurityUser::getSecurityNumber)
                .count();
        indexDataStatisticsVo.setNoCardSecurityUserCount(noCardSecurityUserCount.intValue());

        return indexDataStatisticsVo;
    }

    @Override
    public List<IndexServiceProjectListVo> getUnitServiceProjectList() {
        return serviceProjectMapper.getServiceProjectList(UserUtil.getUnitId(), null);
    }

    @Override
    public List<SelectNodeVo<Long>> getUnitEnterprisesUnitList() {
        return Db.lambdaQuery(EnterprisesUnit.class)
                .eq(EnterprisesUnit::getPoliceUnitId, UserUtil.getUnitId())
                .list()
                .stream()
                .map(item -> SelectNodeVo.<Long>builder()
                        .value(item.getSnowFlakeId())
                        .label(item.getName())
                        .extData(Dict.of(LambdaUtil.getFieldName(EnterprisesUnit::getType), item.getType()))
                        .build())
                .toList();
    }
}
