/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .F88X
 *        X8888Y
 *      .}888888N;
 *        i888888N;        .:!              .I$WI:
 *          R888888I      .'N88~            i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
 *          .R888888I    .;N8888~          .X8'  "8I.!,/8"  !%NY8`"8I8~~8>,88I
 *            +888888N;  .8888888Y                                  "&&8Y.}8,
 *            ./888888N;  .R888888Y        .'}~    .>}'.`+>  i}!    "i'  +/'  .'i~  !11,.:">,  .~]!  .i}i
 *              ~888888%:  .I888888l      .]88~`1/iY88Ii+1'.R$8$8]"888888888>  Y8$  W8E  X8E  W8888'188Il}Y88$*
 *              18888888    E8888881    .]W%8$`R8X'&8%++N8i,8N%N8+l8%`  .}8N:.R$RE%N88N%N$K$R  188,FE$8%~Y88I
 *            .E888888I  .i8888888'      .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
 *            8888888I  .,N888888~        ~88i"8W,!N8*.I88.}888%F,i$88"F88"  888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
 *          i888888N'      I888Y          ]88;/EX*IFKFK88X  K8R  .l8W  88Y  ~88}'88E&%8W.X8N``]88!.$8K  .:W8I
 *        .i888888N;        I8Y          .&8$  .X88!  i881.:%888>I88  ;88]  +88+.';;;;:.Y88X  18N.,88l  .+88/
 *      .:R888888I
 *      .&888888I                                          Copyright (c) 2009-2020.  博锐尚格科技股份有限公司
 *        ~8888'
 *        .!88~                                                                     All rights reserved.
 *
 * *********************************************************************************************************************
 */

package com.persagy.server.services.task

import cn.hutool.core.collection.CollUtil
import cn.sagacloud.server.scanbuilding.User
import com.alibaba.fastjson.JSONObject
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.persagy.base.extensions.toJson
import com.persagy.database.SFilter
import com.persagy.database.SPageQueryInfo
import com.persagy.database.extensions.keyMap
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.Opts
import com.persagy.server.datacenter.models.entities.assistant.TaskPic
import com.persagy.server.datacenter.models.entities.dict.EquipCategory
import com.persagy.server.datacenter.models.entities.dictnew.DefClass
import com.persagy.server.datacenter.models.entities.objects.Equipment
import com.persagy.server.datacenter.models.entities.task.*
import com.persagy.server.datacenter.models.requests.ModelScanSQueryRequest
import com.persagy.server.mappers.StatisticsCountMapper
import com.persagy.server.scanbuilding.UserLog
import com.persagy.server.services.AdmBaseService
import com.persagy.server.services.DmpBaseService
import com.persagy.server.services.base.RService
import com.persagy.server.util.AdmEntityTransferUtil
import com.persagy.server.util.IdUtils
import com.persagy.service.SBaseService
import com.persagy.service.SObjectService
import com.persagy.service.SPageContext
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.requests.SCreateRequest
import com.persagy.service.models.requests.SQueryRequest
import com.persagy.service.models.responses.SQueryResponse
import com.persagy.service.utils.SSpringContextUtil
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestBody
import java.util.*
import java.util.stream.Collectors


/**
 * 模型任务
 *
 * @author  邓婷婷
 */
object ModelScanTaskService : RService<ModelScanTask>(SMybatisDao(ModelScanTask::class.java))  {

    // 日志
    private val logger = LoggerFactory.getLogger(ModelScanTaskService::class.java)


    val modelScanTaskService = SBaseService(SMybatisDao(ModelScanTask::class.java))
    /** 设备类型 */
   // val equipCategoryService = SObjectService(SMybatisDao(EquipCategory::class.java))
    /** 扫楼日志 */
    var userLogService = SObjectService(SMybatisDao(UserLog::class.java))
    /** 用户 */
    var userService = SObjectService(SMybatisDao(User::class.java))

    @Autowired
    private val admBaseService: AdmBaseService? = AdmBaseService()

    @Autowired
    private val dmpBaseService: DmpBaseService? = DmpBaseService()


    /** 实例化 mapper 对象 */
    val mapper by lazy {
        SSpringContextUtil.getBean(StatisticsCountMapper::class.java) as StatisticsCountMapper
    }


    /**
     * 插入之前先过滤已经存在未验证任务的对象，返回新列表
     */
    fun createBefore(@RequestBody request: SCreateRequest<ModelScanTask>): SCreateRequest<ModelScanTask> {
        val modelList = request.content;
        var newList = arrayListOf<ModelScanTask>()
        for(model in modelList){
          //  val modelTask = ModelScanTaskService.select("equipId" to model.equipId!!,"taskState" to 1).entity()
            val modelTask = ModelScanTaskService.select(SFilter.eq("equipId", model.equipId!!),SFilter.eq("taskState", 1)).entity()
            if(modelTask==null){//表示当前设备不存在未验证的任务
                newList.add(model)
            }
        }

        request.content = newList
        println(newList.size)
        return request
    } // Fun createBefore

    /**
     * 创建之前操作
     */
    override fun onCreateBefore(modelScanTask: ModelScanTask): Boolean  {
        try {
                val gson = Gson()
                var flag = false
                if (modelScanTask.taskState == -1) {//前台传数据过来状态为-1 表示当前任务是未找到状态 重新生成任务，将之前的设备任务状态和部件任务改为已完成
                    flag = true
                    resetTaskStatus(modelScanTask)
                }
                val projectId = SPageContext.getHeader("projectId")
//                if (modelScanTask.id.isNullOrEmpty()) {
                    modelScanTask.id = UUID.randomUUID().toString()
//                }
                if (!projectId.isNullOrEmpty()) {
                    modelScanTask.projectId = projectId
                }
                modelScanTask.taskSource = 1//数据源为模型
                modelScanTask.taskState = 1//任务状态为待验证
                modelScanTask.taskType = "1"

                if (!modelScanTask.equipId.isNullOrEmpty()) {//当设备id不为空时，去设备表查询对应的详细信息
                    //val equipmentService = SBaseService(SMybatisDao(Equipment::class.java))
                   //val equipment = equipmentService.select("id" to modelScanTask.equipId!!).entity()
                   // val equipment = equipmentService.select(SFilter.eq("id", modelScanTask.equipId!!)).entity()

                    //2021.10.26调整为从中间件查询设备信息
                    var equipRequest = SQueryRequest()
                    equipRequest.filters = "id = '"+modelScanTask.equipId+"'"
                    val equipmentList = admBaseService?.queryEquip(equipRequest)

                    if(CollUtil.isEmpty(equipmentList)){
                        return false
                        logger.info("设备不存在 id "+modelScanTask.equipId)
                    }
                    val equipment = equipmentList?.get(0)
                    if (modelScanTask.schemeId.isNullOrEmpty()){
                        modelScanTask.schemeId = SchemeUtils.setScheme(equipment!!.classCode!!)
                    }

//                equipment.ledgerParam.PhotoDoc.Pic
                    if(equipment!!.infos != null){//设备图片信息
                        val photoDoc = equipment!!.infos!!.get("photoDoc")
                        if( photoDoc != null){
                            val obj = JSONObject.parseObject(photoDoc!!.toJson())
                            val pic = obj.getString("pic")

                            val type = object : TypeToken<ArrayList<TaskPic>>() {
                            }.type
                            val siInSpList = gson.fromJson<ArrayList<TaskPic>>(pic, type)
                            modelScanTask.taskPicList = siInSpList
                        }

                        val containsKey = equipment.infos!!.containsKey(Opts.INSTALLLOCATION)
                        if (containsKey){
                            modelScanTask.installLocation = equipment.infos!![Opts.INSTALLLOCATION].toString()
                        }
                    }

                    try {
                        if (!equipment.infos.isNullOrEmpty()){

                            val containsKey = equipment.infos!!.containsKey(Opts.MANUFACTURER)
                            if (containsKey){
                                modelScanTask.manufacturer = equipment.infos!![Opts.MANUFACTURER].toString()
                            }
                            val containsKey2 = equipment.infos!!.containsKey(Opts.SPECIFICATION)
                            if(containsKey2) {
                                modelScanTask.specification = equipment.infos!![Opts.SPECIFICATION].toString()
                            }

                            val containsKeys = equipment.infos!!.containsKey(Opts.SUPPLIER)
                            if (containsKeys){
                                modelScanTask.supplier =  equipment.infos!![Opts.SUPPLIER].toString()
                            }
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }

                    if (flag) {
                        modelScanTask.equipName = equipment!!.name
                        modelScanTask.localId = equipment!!.localId
                        modelScanTask.localName = equipment!!.localName
                        modelScanTask.bimId = equipment!!.bimId
                        modelScanTask.classCode = equipment!!.classCode
                        modelScanTask.buildingId = equipment!!.buildingId
                        modelScanTask.floorId = equipment!!.floorId
                        modelScanTask.cadId = equipment!!.cadId
                        modelScanTask.bimLocation = equipment.bimLocation

                        if (equipment.codeType!=null){
                            modelScanTask.taskType = equipment.codeType.toString()
                        }else{
                            modelScanTask.taskType = "1"
                        }
                    } else {

                        if (modelScanTask.equipName.isNullOrEmpty()) {
                            modelScanTask.equipName = equipment!!.name
                        }
                        if (modelScanTask.localId.isNullOrEmpty()) {
                            modelScanTask.localId = equipment!!.localId
                        }
                        if (modelScanTask.localName.isNullOrEmpty()) {
                            modelScanTask.localName = equipment!!.localName
                        }
                        if (modelScanTask.bimId.isNullOrEmpty()) {
                            modelScanTask.bimId = equipment!!.bimId
                        }
                        if (modelScanTask.classCode.isNullOrEmpty()) {
                            modelScanTask.classCode = equipment!!.classCode
                        }
                        if (modelScanTask.buildingId.isNullOrEmpty()) {
                            modelScanTask.buildingId = equipment!!.buildingId
                        }
                        if (modelScanTask.floorId.isNullOrEmpty()) {
                            modelScanTask.floorId = equipment!!.floorId
                        }
                        if (modelScanTask.cadId.isNullOrEmpty()) {
                            modelScanTask.cadId = equipment!!.cadId
                        }

                        if (modelScanTask.bimLocation.isNullOrEmpty()){
                            modelScanTask.bimLocation = equipment.bimLocation
                        }

                        if (modelScanTask.taskType.isNullOrEmpty()){
                            if (equipment.codeType!=null){
                                modelScanTask.taskType = equipment.codeType.toString()
                            }else{
                                modelScanTask.taskType = "1"
                            }
                        }
                    }
                }
            } catch (e: Exception) {
                    e.printStackTrace()
            }
      return true
    } // Function onCreateBefore()

    /**
     * 创建设备台账任务成功之后，设备级联部件，所有部件也增加任务
     */
    override fun onCreateSuccess(modelScanTaskList: ArrayList<ModelScanTask>) {
        val projectId = SPageContext.getHeader("projectId")

        for(task in modelScanTaskList){
            //val equipmentService = SBaseService(SMybatisDao(Equipment::class.java))
           // val componentList = equipmentService.select(SFilter.eq("parentId", task.equipId!!)).exec()
            val componentList = task.equipId?.let { EquipScanTaskService.queryComponyByEquipId(it) }
            if (componentList != null) {
                for(equipment in componentList){
                    val modelScanTask = ModelScanTask()
                    modelScanTask.id = IdUtils.uuidCreate()
                    if(!task.projectId.isNullOrEmpty()){
                        modelScanTask.projectId = task.projectId
                    }else{
                        modelScanTask.projectId = projectId
                    }
                    modelScanTask.taskSource = 4//数据源为模型任务
                    modelScanTask.taskState = 1//任务状态为待验证
                    modelScanTask.parentId = task.equipId
                    modelScanTask.equipId = equipment!!.id
                    modelScanTask.equipName = equipment!!.name
                    modelScanTask.localId = equipment!!.localId
                    modelScanTask.localName = equipment!!.localName
                    modelScanTask.bimId = equipment!!.bimId
                    modelScanTask.classCode = equipment!!.classCode
                    modelScanTask.buildingId = equipment!!.buildingId
                    modelScanTask.floorId = equipment!!.floorId
                    modelScanTask.cadId = equipment!!.cadId
                    modelScanTask.bimLocation = equipment.bimLocation
                    modelScanTask.taskParentId = task.id
                    if (equipment.codeType!=null){
                        modelScanTask.taskType = equipment.codeType.toString()
                    }else{
                        modelScanTask.taskType = "1"
                    }
                    ModelScanTaskService.insert(modelScanTask)
                }
            }
            resetObjStatus(task)
        }
    } // Fun onCreateSuccess



    /**
     * 状态修改完成之后，若修改为  已完成状态  则级联部件任务状态全部修改为  已完成
     */
    override fun onUpdateSuccess(list: ArrayList<ModelScanTask>) {

        val projectId = SPageContext.getHeader("projectId")

        for (task in list) {
          //  val equipmentService = SBaseService(SMybatisDao(Equipment::class.java))
            if(task.equipId == null ){

                val modelScanTaskScanTask = ModelScanTaskService.select(SFilter.eq("id", task.id!!)).entity()
                task.equipId = modelScanTaskScanTask!!.equipId
            }

            if (!task.confirmingPersonId.isNullOrEmpty()&&task.taskState == 0){
                task.taskState = -1
                //更改部件对象状态
                updateComponetObjStatus(task)
                //更改部件任务状态
                updateComponetTaskStatus(task)
            }else if(task.confirmingPersonId.isNullOrEmpty()&&task.taskState == 0) {
                task.taskState = 0
                //更改部件对象状态
                updateComponetObjStatus(task)
                //更改部件任务状态
                updateComponetTaskStatus(task)
            }
            //更改设备对象的状态
            val equipmentOld = admBaseService?.queryEquipById(task.equipId!!)?.get(0)
            if(equipmentOld!!.taskState != -1){//未找到
                val equip = Equipment()
                equip.id = task.equipId!!
               /* equip.taskState = task.taskState
                equipmentService.update(equip)*/
                val infos = HashMap<String, Any?>()
                infos.put("taskState",task.taskState!!)
                equip.infos = infos

                admBaseService?.updateEquip(arrayListOf(equip))
            }
            try {
                if (!Opts.coming.isNullOrEmpty()&&Opts.coming == "app"){

                        val entity = userService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("phone", Opts.account!!)).entity()
                        val userLog = UserLog()
                        userLog.comming = "app"
                        userLog.action = "修改任务"
                        userLog.id = IdUtils.uuidCreate()
                        userLog.note = "确认模型设备任务"
                        userLog.phone = entity?.phone
                        userLog.projectId = projectId
                        userLog.userId = entity!!.id
                        userLog.name = entity.name
                        userLog.objectId = task.equipId
                        userLogService.insert(userLog)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    } // Fun onUpdateSuccess

    /**
     * 更新设备对象状态时，对应更改部件对象状态，当为未找到状态时，特殊处理
     */
    fun updateComponetObjStatus(task: ModelScanTask){
      //  val equipmentService = SBaseService(SMybatisDao(Equipment::class.java))
      //  val componentList = equipmentService.select(SFilter.eq("parentId", task.equipId!!)).exec()
        val componentList = task.equipId?.let { EquipScanTaskService.queryComponyByEquipId(it) }
        if(task.taskState == 1 || task.taskState == -1){ // 待验证和未找到
            if (componentList != null) {
                for(component in componentList){
                    component.taskState = task.taskState
                   // equipmentService.update(component)
                    val infos = HashMap<String, Any?>()
                    infos.put("taskState",task.taskState!!)
                    component.infos = infos

                    admBaseService?.updateComponent(arrayListOf(component))
                }
            }
        }else if(task.taskState == 0){ // 已完成
            if (componentList != null) {
                for(component in componentList){
                    if(component.taskState != -1){//如果部件状态为未找到，则不改变状态
                        component.taskState = task.taskState
                        //equipmentService.update(component)

                        val infos = HashMap<String, Any?>()
                        infos.put("taskState",task.taskState!!)
                        component.infos = infos

                        admBaseService?.updateComponent(arrayListOf(component))
                    }
                }
            }
        }
    }

    /**
     * 更新设备任务状态时，对应更改部件任务状态
     */
    fun updateComponetTaskStatus(task: ModelScanTask){
        val equipmentService = SBaseService(SMybatisDao(Equipment::class.java))
        val modelScanTaskService = SBaseService(SMybatisDao(ModelScanTask::class.java))

        val componentTaskList = modelScanTaskService.select(SFilter.eq("parentId", task.equipId!!)).exec()

        for(childTask in componentTaskList){
            childTask.taskState = task.taskState
            ModelScanTaskService.update(childTask)
        }
    } // Fun updateStatus

    /**
     * 更新设备对象状态时，对应更改部件对象状态
     */
    fun resetComponetObjStatus(task: ModelScanTask){
       // val equipmentService = SBaseService(SMybatisDao(Equipment::class.java))
       // val componentList = equipmentService.select(SFilter.eq("parentId", task.equipId!!)).exec()
        val componentList = EquipScanTaskService.queryComponyByEquipId(task!!.equipId!!)
        if (componentList != null) {
            for(component in componentList){
                /*component.taskState = task.taskState
                equipmentService.update(component)*/
                val infos = HashMap<String, Any?>()
                infos.put("taskState",task.taskState!!)
                component.infos = infos
                //equipmentService.update(component)
                admBaseService?.updateComponent(arrayListOf(component))
            }
        }
    }

    /**
     * 重新生成任务时，将任务状态改为已完成
     */
    fun resetTaskStatus(task: ModelScanTask) {
        //val equipmentService = SBaseService(SMybatisDao(Equipment::class.java))
        val modelScanTaskService = SBaseService(SMybatisDao(ModelScanTask::class.java))

        val resetTask = modelScanTaskService.select(SFilter.eq("equipId", task.equipId!!), SFilter.eq("taskState", -1)).entity()
        if (resetTask != null) {
            resetTask.taskState = 0
            modelScanTaskService.update(resetTask)//更改设备任务状态
            updateComponetTaskStatus(resetTask)//更改部件任务状态
        }
    }
    /**
     * 重新生成任务时，将设备和部件状态改变为待验证
     */
    fun resetObjStatus(task: ModelScanTask){
        /*val equipmentService = SBaseService(SMybatisDao(Equipment::class.java))

        //更改设备对象状态
        val equip = Equipment()
        equip.id = task.equipId!!
        equip.taskState = task.taskState
        equipmentService.update(equip)
        //更改部件对象状态
        resetComponetObjStatus(task)*/

        val equip = Equipment()
        equip.id = task.equipId!!

        val infos = HashMap<String, Any?>()
        infos.put("taskState",task.taskState!!)
        equip.infos = infos

        admBaseService?.updateEquip(arrayListOf(equip))
        //更改部件对象状态
       resetComponetObjStatus(task)
    }


    /**
     * 查询所有模型文件夹和文件
     */
    fun modelFileQuery(): SQueryResponse<ModelFolder> {
        val pageQuery = SPageQueryInfo<ModelFolder>(1, 1000)
        val gson = Gson()
        return try {
            var request = SQueryRequest()
            request.filters = "ProjectId = '${SPageContext.getHeader("ProjectId")}'";
            request.distinct = true
            val response = pageQuery(request, null);

            //文件夹集合
            val folderMap = HashMap<String,String>()
            //集合封装:文件夹->模型文件
           /* val modelFiles = arrayListOf<ModelFile>()*/
            val folderFileMap = HashMap<String,ArrayList<ModelFile>>()
            for(folder in response.content!!){
                folder.folderId?.let { folder.folderName?.let { it1 -> folderMap.put(it, it1) } }
                //获取文件夹中的所有文件
                var modelFiles: ArrayList<ModelFile>? =  folderFileMap.get(folder.folderId)
                if (CollUtil.isEmpty(modelFiles)) {
                    modelFiles =ArrayList<ModelFile>()
                }
                val modelFile = ModelFile()
                modelFile.fileId = folder.fileId
                modelFile.fileName = folder.fileName

                val fileIds = modelFiles?.stream()?.map(ModelFile::fileId)?.collect(Collectors.toList<String>())
                if (fileIds != null) {
                    if(fileIds.contains(folder.fileId)){
                       continue
                    }
                }
                if (modelFiles != null) {
                    modelFiles.add(modelFile)
                }
                folder.folderId?.let { folderFileMap.put(it, modelFiles as ArrayList<ModelFile>) }
            }

            //结果集封装:文件夹->模型文件
            val modelFolders = arrayListOf<ModelFolder>()
            for ((folderId, folderName) in folderMap.entries) {
                val fileList = folderFileMap.get(folderId)

                val modelFolder = ModelFolder()
                modelFolder.folderId = folderId
                modelFolder.folderName = folderName

                modelFolder.fileList = fileList
                modelFolder.fileListStr = null
                modelFolders.add(modelFolder)
            }

            //历史逻辑：
            /*val modelFolderService = SBaseService(SMybatisDao(ModelFolder::class.java))
            val modelFolderQueryBuilder = modelFolderService.select(request.filters)
            modelFolderQueryBuilder.tableName = "scantask.f_get_modelfile('$projectId')"
            pageQuery = modelFolderQueryBuilder.pageQuery(1, 1000)

            for(folder in pageQuery.content!!){
                if(folder.fileListStr != null){
                    val listStr = folder.fileListStr!!.replace("/\\","")
                    val newStr = "["+listStr.substring(1,listStr.length-1)+"]"
                    val type = object : TypeToken<ArrayList<ModelFile>>() {
                    }.type
                    val siInSpList = gson.fromJson<ArrayList<ModelFile>>(newStr, type)
                    folder.fileList = siInSpList
                    folder.fileListStr = null
                }
            }*/
            pageQuery.content = modelFolders;
            SQueryResponse(pageQuery)
        } catch (e: Exception) {
            e.printStackTrace()
            SQueryResponse(SResponseType.failure, e.message!!)
        }
    } //  Fun modelFileQuery

    /**
     * 根据元空间id查询设备
     */
    fun  equipQuery(request: ModelScanSQueryRequest): SQueryResponse<EquipmentTaskTwo>{
        return  try{
            val projectId = SPageContext.getHeader("projectId")
            val modelScanTaskService = SBaseService(SMybatisDao(EquipmentTaskTwo::class.java))
            if (!request.modelId.isNullOrEmpty()&&!request.idList.isNullOrEmpty()&&request.idList!!.size>0){
                val list = ArrayList<EquipmentTaskTwo>()
                for ( bimId in request.idList!!){
                    if (request.category.isNullOrEmpty()){
                        val equipInISpaceList = mapper.equipInISpace(projectId!!, request.modelId!!, bimId)
                        if (equipInISpaceList.size>0){
                            logger.debug("数据22222 = ${equipInISpaceList.toJson()}")
                            list.addAll(equipInISpaceList)
                        }
                    }else{
                        val equipInISpaceList = mapper.equipInISpaceTwo(projectId!!, request.modelId!!, bimId,request.category!!)
                        if (equipInISpaceList.size>0){
                            logger.debug("数据111 = ${equipInISpaceList.toJson()}")
                            list.addAll(equipInISpaceList)
                        }
                    }
                }
                if (list.size>0){
                    val pageNumber = request.pageNumber
                    val pageSize = request.pageSize
                    /** 开始条数 */
                    var fromIndex = pageSize* (pageNumber-1)
                    var toIndex = fromIndex + pageSize
                    if (toIndex>=list.size){
                        toIndex = list.size
                    }

                    val sQueryResponse = SQueryResponse<EquipmentTaskTwo>(SResponseType.success)
                    sQueryResponse.total = list.size.toLong()
                    val subList = list.subList(fromIndex, toIndex)
                    if (subList.size>0){
                        for (equip in subList){
                            logger.debug("数据 = ${equip.toJson()}")
                            try {
                                /*val entity = equipCategoryService.select(SFilter.eq("code", equip.classCode!!)).entity()
                                if (entity!=null){
                                    equip.equipCategory = entity
                                }*/
                                val equipCategoryList = dmpBaseService?.getEquipCategory(equip.classCode!!)
                                if (equipCategoryList != null) {
                                    equip.equipCategory = equipCategoryList.get(0)
                                }

                                val exec = modelScanTaskService.select(SFilter.eq("parentId", equip.id!!)).exec()
                                if (exec.size>0){
                                    equip.component = exec
                                }
                                //val entity1 = EquipmentService.select(SFilter.eq("id", equip.id!!)).entity()
                                val entity1 = admBaseService?.queryEquipById(equip.id!!)?.get(0)
                                equip.bimId = entity1!!.bimId
                                equip.localId = entity1.localId
                                equip.localName = entity1.localName
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }
                        }
                    }
                    sQueryResponse.content = subList
                    sQueryResponse.pageSize = pageSize
                    sQueryResponse.pageNumber = pageNumber
                    sQueryResponse
                }else{
                    val sQueryResponse = SQueryResponse<EquipmentTaskTwo>(SResponseType.success)
                    sQueryResponse.content = ArrayList<EquipmentTaskTwo>()
                    sQueryResponse.pageNumber = request.pageNumber
                    sQueryResponse.pageSize = request.pageSize
                    sQueryResponse
                }
            }else{
                val sQueryResponse = SQueryResponse<EquipmentTaskTwo>(SResponseType.failure)
                sQueryResponse.message = "入参错误"
                sQueryResponse
            }

//            if (!request.idList.isNullOrEmpty()){
//                return EquipmentService.pageQuery(request, arrayListOf(SFilter.inList(SEndWithChar("bimId",":"),request.idList!!) ,
//                        SFilter.eq("projectId",projectId!!)))
//            }else{
//                return EquipmentService.pageQuery(request, arrayListOf(
//                        SFilter.eq("projectId",projectId!!)))
//            }

//           return EquipmentService.pageQuery(request, arrayListOf(SFilter.inList(SEndWithChar("bimId",":"),listId) ,
//                   SFilter.eq("projectId",projectId!!)))

    } catch (e: Exception) {
        e.printStackTrace()
        SQueryResponse(SResponseType.failure, e.message!!)
    }
    }

//    private fun getDatas(list: ArrayList<Equipment>,firstIndex: Int, lastIndex: Int): List<Equipment> {
//        val resList = ArrayList<Equipment>()
//        for (i in firstIndex until lastIndex) {
//            if (i < list.size) {
//                resList.add(list[i])
//            }
//        }
//        return resList
//    }




    override fun onDeleteBefore(entity: ModelScanTask): Boolean {

        try {
            val entity2 = EquipScanTaskService.select(SFilter.eq("id", entity.id!!)).entity()
            if(entity2 == null){
                return false
            }
            //val entity1 = EquipmentService.select(SFilter.eq("id", entity2!!.equipId!!)).entity()
            val entity1 = admBaseService?.queryEquipById(entity2!!.equipId!!)?.get(0)
            if (entity1!=null){
                if (entity1.taskState == 1){
                  /*  entity1.taskState = null
                    EquipmentService.update(entity1, arrayListOf("TaskState"))*/
                    val infos = HashMap<String, Any?>()
                    infos.put("taskState",null)
                    entity1.infos = infos

                    admBaseService?.updateEquip(arrayListOf(entity1))
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return super.onDeleteBefore(entity)
    }

    /**
     * 统计设备下部件的类型以及数量
     */
    fun categoryCounts(equipScanTask: ModelScanTask): SQueryResponse<CategoryStatistics>{
        try {
            val projectId = SPageContext.getHeader("projectId")
            if (!equipScanTask.id.isNullOrEmpty()&&!equipScanTask.equipId.isNullOrEmpty()){
                val categoryStatistics = mapper.categoryStatisticsModel(projectId!!,equipScanTask.id!!, equipScanTask.equipId!!)
                val sQueryResponse = SQueryResponse<CategoryStatistics>()
                sQueryResponse.total = categoryStatistics.size.toLong()
                sQueryResponse.content = categoryStatistics
                sQueryResponse.result = SResponseType.success
                return sQueryResponse
            }else{
                val sQueryResponse = SQueryResponse<CategoryStatistics>(SResponseType.failure)
                sQueryResponse.message = "入参错误"
                return sQueryResponse
            }
        } catch (e: Exception) {
            e.printStackTrace()
            return SQueryResponse(SResponseType.failure,e.message.toString())
        }
    } // Fun

} // Class ModelScanTaskService