@ -1,20 +1,44 @@
package cn.iocoder.yudao.module.system.service.bigviewdata ;
import cn.iocoder.yudao.module.system.controller.admin.bigviewdata.vo.* ;
import cn.iocoder.yudao.module.system.controller.admin.enterprise.vo.EnterprisePageReqVO ;
import cn.iocoder.yudao.module.system.controller.admin.enterprise.vo.LabelValueLongVO ;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.EnterpriseNumVO ;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeCountResVO ;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeGroupCountVO ;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeSelectVO ;
import cn.iocoder.yudao.module.system.controller.admin.taskinfo.vo.TaskInfoPageReqVO ;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataDO ;
import cn.iocoder.yudao.module.system.dal.dataobject.enterprise.EnterpriseDO ;
import cn.iocoder.yudao.module.system.dal.dataobject.enterpriseinspections.EnterpriseInspectionsDO ;
import cn.iocoder.yudao.module.system.dal.dataobject.qualification.EnterpriseQualificationDO ;
import cn.iocoder.yudao.module.system.dal.dataobject.taglibrary.TagLibraryDO ;
import cn.iocoder.yudao.module.system.dal.dataobject.taskinfo.TaskInfoDO ;
import cn.iocoder.yudao.module.system.dal.mysql.bigviewdata.BigViewDataMapper ;
import cn.iocoder.yudao.module.system.dal.mysql.enterprise.EnterpriseMapper ;
import cn.iocoder.yudao.module.system.dal.mysql.enterpriseinspections.EnterpriseInspectionsMapper ;
import cn.iocoder.yudao.module.system.dal.mysql.qualification.EnterpriseQualificationMapper ;
import cn.iocoder.yudao.module.system.dal.mysql.taskinfo.TaskInfoMapper ;
import cn.iocoder.yudao.module.system.service.dict.DictDataService ;
import cn.iocoder.yudao.module.system.service.enterprise.EnterpriseService ;
import cn.iocoder.yudao.module.system.service.enterpriseinspections.EnterpriseInspectionsService ;
import cn.iocoder.yudao.module.system.service.home.HomeService ;
import cn.iocoder.yudao.module.system.service.taglibrary.TagLibraryService ;
import cn.iocoder.yudao.module.system.service.taskinfo.TaskInfoService ;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper ;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper ;
import lombok.extern.slf4j.Slf4j ;
import org.springframework.stereotype.Service ;
import javax.annotation.Resource ;
import java.time.DayOfWeek ;
import java.time.LocalDate ;
import java.time.LocalDateTime ;
import java.util.ArrayList ;
import java.util.List ;
import java.time.LocalTime ;
import java.time.temporal.TemporalAdjusters ;
import java.util.* ;
import java.util.concurrent.CompletableFuture ;
import java.util.concurrent.ExecutionException ;
import java.util.stream.Collectors ;
@Service
@Slf4j
@ -26,80 +50,281 @@ public class BigViewDataServiceImpl implements BigViewDataService {
private TaskInfoMapper taskInfoMapper ;
@Resource
private HomeService homeService ;
@Resource
private DictDataService dictDataService ;
@Resource
private EnterpriseMapper enterpriseMapper ;
@Resource
private EnterpriseInspectionsMapper enterpriseInspectionsMapper ;
@Resource
private TagLibraryService tagLibraryService ;
@Override
public List < NameCountVO > getLeftFirst ( HomeSelectVO homeSelectVO ) {
public List < LeftFirstData VO> getLeftFirst ( HomeSelectVO homeSelectVO ) {
return bigViewDataMapper . selectTaskTypeGroupCount ( homeSelectVO ) ;
}
@Override
public List < BigViewGroupCountCompareVO > getRightFourth ( HomeSelectVO homeSelectVO ) {
//时间范围
// LocalDateTime startTime = LocalDateTime.now().minusMonths(1);
// LocalDateTime endTime = LocalDateTime.now();
// homeSelectVO.setStartTime(startTime);
// homeSelectVO.setEndTime(endTime);
//
BigViewQOQVO bigViewQOQVO = new BigViewQOQVO ( ) ;
LocalDate today = LocalDate . now ( ) ;
bigViewQOQVO . setCurrentStartTime ( today . withDayOfMonth ( 1 ) . toString ( ) ) ;
bigViewQOQVO . setCurrentEndTime ( today . toString ( ) ) ;
bigViewQOQVO . setPreviousStartTime ( today . withDayOfMonth ( 1 ) . minusMonths ( 1 ) . toString ( ) ) ;
bigViewQOQVO . setPreviousEndTime ( today . minusMonths ( 1 ) . toString ( ) ) ;
return bigViewDataMapper . getInspectionsTaskGroupCountCompare ( bigViewQOQVO ) ;
return bigViewDataMapper . getInspectionsTaskGroupCountCompare ( ) ;
}
//企业数量
public Long getEnterpriseCount ( HomeSelectVO homeSelectVO ) {
QueryWrapper < EnterpriseDO > wrapper = new QueryWrapper < > ( ) ;
wrapper . gt ( "status" , 1 ) ;
if ( homeSelectVO . getSelectWeek ( ) ! = null ) {
final LocalDateTime [ ] week = this . getWeek ( homeSelectVO ) ;
wrapper . between ( "create_time" , week [ 0 ] , week [ 1 ] ) ;
}
return enterpriseMapper . selectCount ( ) ;
}
//任务数量
public Long getTaskCount ( HomeSelectVO homeSelectVO ) {
QueryWrapper < TaskInfoDO > wrapper = new QueryWrapper < > ( ) ;
wrapper . gt ( "status" , 1 ) ;
if ( homeSelectVO . getSelectWeek ( ) ! = null ) {
final LocalDateTime [ ] week = this . getWeek ( homeSelectVO ) ;
wrapper . between ( "start_date" , week [ 0 ] , week [ 1 ] ) ;
}
return taskInfoMapper . selectCount ( wrapper ) ;
}
//执法数量
public Long getTaskInspection ( HomeSelectVO homeSelectVO ) {
QueryWrapper < EnterpriseInspectionsDO > wrapper = new QueryWrapper < > ( ) ;
wrapper . gt ( "status" , 1 ) ;
if ( homeSelectVO . getSelectWeek ( ) ! = null ) {
final LocalDateTime [ ] week = this . getWeek ( homeSelectVO ) ;
wrapper . between ( "create_time" , week [ 0 ] , week [ 1 ] ) ;
}
return enterpriseInspectionsMapper . selectCount ( ) ;
}
@Override
public HomeCountResVO getCenter ( HomeSelectVO homeSelectVO ) {
return homeService . appCount1 ( homeSelectVO ) ;
public BigViewCenterDataVO getCenter ( HomeSelectVO homeSelectVO ) {
BigViewCenterDataVO result = new BigViewCenterDataVO ( ) ;
result . setEnterpriseCount ( this . getEnterpriseCount ( homeSelectVO ) ) ;
result . setTaskCount ( this . getTaskCount ( homeSelectVO ) ) ;
result . setInspectionsCount ( this . getTaskInspection ( homeSelectVO ) ) ;
return result ;
}
@Override
public BigViewEnterpriseCountGroup getLeftSecond ( HomeSelectVO homeSelectVO ) {
BigViewEnterpriseCountGroup enterpriseCountGroup = new BigViewEnterpriseCountGroup ( ) ;
final List < NameCountVO > nameCountVOS = bigViewDataMapper . enterpriseCountGroup ( homeSelectVO ) ;
enterpriseCountGroup . setEnterpriseCountGroup ( nameCountVOS ) ;
List < Integer > countList = new ArrayList < > ( ) ;
if ( nameCountVOS ! = null ) {
nameCountVOS . forEach ( item - > {
countList . add ( item . getValue ( ) ) ;
} ) ;
public List < ReginCountVO > getMapData ( HomeSelectVO homeSelectVO ) {
BigViewCenterDataVO result = new BigViewCenterDataVO ( ) ;
// 1. 并行获取基础统计数据
final BigViewCenterDataVO center = this . getCenter ( homeSelectVO ) ;
// 2. 获取字典数据和区域统计
List < DictDataDO > regions = dictDataService . getDictDataListByDictType ( "enterprises_area" ) ;
// 3. 获取企业区域分布
QueryWrapper < EnterpriseDO > enterpriseDOQueryWrapper = new QueryWrapper < > ( ) ;
enterpriseDOQueryWrapper . select ( "region" , "count(id) as count" ) ;
enterpriseDOQueryWrapper . groupBy ( "region" ) ;
if ( homeSelectVO . getSelectWeek ( ) ! = null ) {
final LocalDateTime [ ] week = this . getWeek ( homeSelectVO ) ;
homeSelectVO . setTime ( week ) ;
enterpriseDOQueryWrapper . between ( "create_time" , week [ 0 ] , week [ 1 ] ) ;
}
final Integer count = countList . stream ( ) . reduce ( Integer : : sum ) . orElse ( 0 ) ;
enterpriseCountGroup . setEnterpriseCount ( count ) ;
return enterpriseCountGroup ;
Map < String , Long > enterpriseRegionMap = enterpriseMapper . selectList ( enterpriseDOQueryWrapper ) . stream ( ) . collect ( Collectors . toMap (
EnterpriseDO : : getRegion ,
EnterpriseDO : : getCount ,
( v1 , v2 ) - > v1
) ) ;
List < TaskCountVO > taskCounts = bigViewDataMapper . getTaskCount ( homeSelectVO ) ;
try {
// 4. 设置基础数据
Long enterpriseCount = center . getEnterpriseCount ( ) ;
Long taskCount = center . getTaskCount ( ) ;
Long inspectionsCount = center . getInspectionsCount ( ) ;
result . setEnterpriseCount ( enterpriseCount ) ;
result . setTaskCount ( taskCount ) ;
result . setInspectionsCount ( inspectionsCount ) ;
// 5. 计算区域比率
List < ReginCountVO > regionStats = regions . stream ( ) . map ( region - > {
ReginCountVO vo = new ReginCountVO ( ) ;
vo . setName ( region . getLabel ( ) ) ;
vo . setLocation ( region . getRemark ( ) ) ;
vo . setEnterpriseRate ( 0l ) ;
vo . setTaskRate ( 0l ) ;
vo . setTaskCount ( 0l ) ;
vo . setEnterpriseCount ( 0l ) ;
// 计算任务比率
taskCounts . stream ( )
. filter ( t - > region . getValue ( ) . equals ( t . getRegion ( ) . toString ( ) ) )
. findFirst ( )
. ifPresent ( t - > {
if ( t . getTaskCount ( ) > 0 & & taskCount > 0 ) {
// 计算原始比率(如 45.6)
double rawRate = ( double ) t . getTaskCount ( ) / taskCount * 100 ;
// 四舍五入到整数
long roundedRate = Math . round ( rawRate ) ;
vo . setTaskRate ( roundedRate ) ;
vo . setTaskCount ( t . getTaskCount ( ) ) ;
} else {
// 处理无效值(根据业务需求设置默认值)
vo . setTaskRate ( 0l ) ;
vo . setTaskCount ( 0l ) ;
}
} ) ;
// 计算企业比率
Optional . ofNullable ( enterpriseRegionMap . get ( region . getValue ( ) ) )
. ifPresent ( count - > {
if ( count > 0 & & enterpriseCount > 0 ) {
// 计算原始比率(如 45.6)
double rawRate = ( double ) count / enterpriseCount * 100 ;
// 四舍五入到整数
long roundedRate = Math . round ( rawRate ) ;
vo . setEnterpriseRate ( roundedRate ) ;
vo . setEnterpriseCount ( count ) ;
} else {
// 处理无效值(根据业务需求设置默认值)
vo . setEnterpriseRate ( 0l ) ;
vo . setEnterpriseCount ( 0L ) ;
}
} ) ;
return vo ;
} ) . collect ( Collectors . toList ( ) ) ;
result . setReginCountVOList ( regionStats ) ;
} catch ( Exception e ) {
Thread . currentThread ( ) . interrupt ( ) ;
throw new RuntimeException ( "数据获取失败" , e ) ;
}
return result . getReginCountVOList ( ) ;
}
// @Override
// public BigViewEnterpriseCountGroup getLeftSecond(HomeSelectVO homeSelectVO) {
// BigViewEnterpriseCountGroup enterpriseCountGroup = new BigViewEnterpriseCountGroup();
// final List<NameCountVO> nameCountVOS = bigViewDataMapper.enterpriseCountGroup(homeSelectVO);
// enterpriseCountGroup.setEnterpriseCountGroup(nameCountVOS);
// List<Integer> countList = new ArrayList<>();
// if (nameCountVOS != null) {
// nameCountVOS.forEach(item->{
// countList.add(item.getValue());
// });
// }
// final Integer count = countList.stream().reduce(Integer::sum).orElse(0);
// enterpriseCountGroup.setEnterpriseCount(count);
//
// return enterpriseCountGroup;
// }
@Override
public List < LabelValueLongVO > getLeftSecond ( HomeSelectVO homeSelectVO ) {
// 1. 获取标签库子项(父ID硬编码可改为动态参数)
List < TagLibraryDO > tagLibraries = tagLibraryService . childrenList ( 26L ) ;
// 2. 查询任务统计结果并转换为Map: taskType -> count
Map < Integer , Long > taskTypeCountMap = taskInfoMapper . selectList (
new QueryWrapper < TaskInfoDO > ( )
. select ( "task_type AS taskType" , "COUNT(*) AS count" ) // 优化COUNT(id)为COUNT(*)
. gt ( "status" , 1 )
. groupBy ( "task_type" )
) . stream ( )
. collect ( Collectors . toMap (
TaskInfoDO : : getTaskType ,
task - > task . getCount ( ) ! = null ? task . getCount ( ) : 0L , // 处理空值
( oldVal , newVal ) - > oldVal // 合并冲突策略(按需调整)
) ) ;
// 3. 合并标签与统计结果生成VO
List < LabelValueLongVO > resultList = new ArrayList < > ( ) ;
for ( TagLibraryDO tag : tagLibraries ) {
LabelValueLongVO vo = new LabelValueLongVO ( ) ;
vo . setLabel ( tag . getTagName ( ) ) ;
// 直接通过Map获取统计值,避免双重循环(O(1)查找)
vo . setValue ( taskTypeCountMap . getOrDefault ( tag . getId ( ) , 0L ) ) ;
resultList . add ( vo ) ;
}
return resultList ;
}
@Override
public List < HomeGroupCountVO > getLeftThird ( HomeSelectVO homeSelectVO ) {
return taskInfoMapper . selectHomeGroupCount ( homeSelectVO ) ;
public List < LeftThirdDataVO > getLeftThird ( HomeSelectVO homeSelectVO ) {
List < LeftThirdDataVO > thirdDataVOS = new ArrayList < > ( ) ;
final List < ReginCountVO > mapData = this . getMapData ( homeSelectVO ) ;
mapData . forEach ( item - > {
LeftThirdDataVO leftThirdDataVO = new LeftThirdDataVO ( ) ;
leftThirdDataVO . setName ( item . getName ( ) ) ;
if ( item . getTaskCount ( ) = = 0 | | item . getEnterpriseCount ( ) = = 0 ) {
leftThirdDataVO . setValue ( 0 . 0 ) ;
} else {
final double value = ( double ) item . getTaskCount ( ) / item . getEnterpriseCount ( ) * 100 ;
leftThirdDataVO . setValue ( Math . round ( value * 10 ) / 10 . 0 ) ;
}
leftThirdDataVO . setEnterpriseCount ( item . getEnterpriseCount ( ) ) ;
leftThirdDataVO . setTaskCount ( item . getTaskCount ( ) ) ;
thirdDataVOS . add ( leftThirdDataVO ) ;
} ) ;
List < LeftThirdDataVO > top5 = thirdDataVOS . stream ( )
. sorted ( Comparator . comparingDouble ( LeftThirdDataVO : : getTaskCount ) . reversed ( ) ) // 降序排序
. limit ( 5 ) // 取前五
. collect ( Collectors . toList ( ) ) ;
return top5 ;
}
@Override
public List < StatusCountVO > getRightFist ( HomeSelectVO homeSelectVO ) {
public List < MonthTaskInspection VO> getRightFist ( HomeSelectVO homeSelectVO ) {
return bigViewDataMapper . getInspectionsTaskGroupCount ( homeSelectVO ) ;
}
public LocalDateTime [ ] getWeek ( HomeSelectVO homeSelectVO ) {
if ( homeSelectVO . getSelectWeek ( ) ! = null ) {
LocalDateTime [ ] time = new LocalDateTime [ 2 ] ;
switch ( homeSelectVO . getSelectWeek ( ) ) {
case 1 :
time [ 0 ] = LocalDateTime . now ( ) . minusDays ( 7 ) ;
break ;
case 2 :
time [ 0 ] = LocalDateTime . now ( ) . minusMonths ( 1 ) ;
break ;
case 3 :
time [ 0 ] = LocalDateTime . now ( ) . minusYears ( 1 ) ;
break ;
}
time [ 1 ] = LocalDateTime . now ( ) ;
homeSelectVO . setTime ( time ) ;
}
if ( homeSelectVO . getSelectWeek ( ) ! = null ) {
LocalDateTime [ ] range = new LocalDateTime [ 2 ] ;
LocalDateTime now = LocalDateTime . now ( ) ;
switch ( homeSelectVO . getSelectWeek ( ) ) {
case 3 : // 本周
range [ 0 ] = now . with ( TemporalAdjusters . previousOrSame ( DayOfWeek . MONDAY ) )
. with ( LocalTime . MIN ) ; // 本周一 00:00
range [ 1 ] = now . with ( TemporalAdjusters . nextOrSame ( DayOfWeek . SUNDAY ) )
. with ( LocalTime . MAX ) ; // 本周日 23:59:59.999999999
break ;
case 2 : // 本月
range [ 0 ] = now . withDayOfMonth ( 1 )
. with ( LocalTime . MIN ) ; // 本月第一天 00:00
range [ 1 ] = now . with ( TemporalAdjusters . lastDayOfMonth ( ) )
. with ( LocalTime . MAX ) ; // 本月最后一天 23:59:59.999999999
break ;
case 1 : // 本年
range [ 0 ] = now . withDayOfYear ( 1 )
. with ( LocalTime . MIN ) ; // 本年第一天 00:00
range [ 1 ] = now . with ( TemporalAdjusters . lastDayOfYear ( ) )
. with ( LocalTime . MAX ) ; // 本年最后一天 23:59:59.999999999
break ;
default :
throw new IllegalArgumentException ( "无效的时间范围类型" ) ;
}
homeSelectVO . setTime ( range ) ;
}
return homeSelectVO . getTime ( ) ;
}
@ -127,8 +352,13 @@ public class BigViewDataServiceImpl implements BigViewDataService {
}
@Override
public List < EnterpriseNumVO > getRightThird ( HomeSelectVO homeSelectVO ) {
homeSelectVO . setType ( 1 ) ;
return homeService . appCount2 ( homeSelectVO ) ;
public List < RightThirdDataVO > getRightThird ( HomeSelectVO homeSelectVO ) {
if ( homeSelectVO . getSelectWeek ( ) ! = null ) {
final LocalDateTime [ ] week = this . getWeek ( homeSelectVO ) ;
homeSelectVO . setTime ( week ) ;
}
return bigViewDataMapper . getRightThird ( homeSelectVO ) ;
}
}