package com.persagy.server.services

import com.persagy.server.datacenter.models.entities.task.CategoryStatistics
import com.persagy.server.datacenter.models.entities.task.EquipmentTaskTwo
import com.persagy.server.datacenter.models.entities.task.ModelScanTaskQuery
import com.persagy.server.services.task.EquipScanTaskQueryService
import com.persagy.server.services.task.ModelScanTaskQueryService
import com.persagy.server.services.task.ModelScanTaskService
import org.apache.ibatis.annotations.Select
import org.springframework.stereotype.Service

/**
 * Description
 *
 * @author:linhuili
 * @date:2021/10/27
 */
@Service
class StatisticsCountService {
    /**
     * 分类统计
     */
    fun categoryStatistics(projectId: String, id: String, equipId: String): ArrayList<CategoryStatistics> {
        //"SELECT class_code as code,(SELECT name FROM dict.def_class WHERE code = class_code  LIMIT 1 ) as name, COUNT(class_code) FROM scantask.equip_scan_task WHERE task_parent_id = #{id} AND parent_id = #{equipId} GROUP BY class_code"
        return EquipScanTaskQueryService.categoryStatistics(projectId, id, equipId)
    }

    /**
     * 统计模型设备部件类型以及数量
     */
    fun categoryStatisticsModel(projectId: String, id: String, equipId: String): ArrayList<CategoryStatistics> {
        //"SELECT mt.class_code as code,(SELECT name FROM dict.def_class WHERE code = mt.class_code LIMIT 1 ) as name, COUNT(class_code) FROM scantask.model_scan_task mt WHERE mt.task_parent_id = #{id} AND mt.parent_id = #{equipId} GROUP BY mt.class_code"
        return ModelScanTaskQueryService.categoryStatisticsModel(projectId, id, equipId)
    }

    /**
     * 查询 当前模型文件下指定元空间内的设备
     */
    fun equipInISpace(projectId: String, modelId: String, bimId: String): ArrayList<EquipmentTaskTwo>? {
        //@Select("SELECT id,name,local_id as localId ,local_name as localName ,cad_id as cadId ,bim_id as bimId ,class_code as classCode FROM equipment e WHERE project_id = #{projectId} AND bim_location  IS NOT null  AND model_id = #{modelId} AND is_point_in_polygon_v2_str(split_part(e.bim_location, E',', 1) , split_part(e.bim_location, E',', 2) , (SELECT outline FROM zone_ispace WHERE project_id= #{projectId} AND bim_id = #{bimId})) AND (e.task_state IS null OR e.task_state = 0 OR task_state = -1) ORDER BY create_time , e.id")
        return ModelScanTaskService.equipInISpace(projectId, modelId, bimId)
    }

    /**
     * 查询 当前模型文件下指定元空间内的设备
     */
    fun equipInISpaceTwo(projectId: String, modelId: String, bimId: String, category: String): ArrayList<EquipmentTaskTwo>? {
    //@Select("SELECT id,name,local_id as localId ,local_name as localName ,cad_id as cadId ,bim_id as bimId ,class_code as classCode FROM equipment e WHERE project_id = #{projectId} AND bim_location  IS NOT null  AND model_id = #{modelId} AND is_point_in_polygon_v2_str(split_part(e.bim_location, E',', 1) , split_part(e.bim_location, E',', 2) , (SELECT outline FROM zone_ispace WHERE project_id= #{projectId} AND bim_id = #{bimId})) AND (e.task_state IS null OR e.task_state = 0 OR task_state = -1) ORDER BY create_time , e.id")
        return ModelScanTaskService.equipInISpaceTwo(projectId, modelId, bimId,category)
    }
}
