package com.persagy.revit.services.endpoint

import com.persagy.database.SFilter
import com.persagy.revit.models.modelFile.ModelFileFolder
import com.persagy.revit.services.BaseDataService
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SBaseResponse
import org.springframework.stereotype.Component

/**
 *
 *
 * @author jxing
 */
@Component
object ModelFileFolderService {
//    private var folderService = SObjectService(SMybatisDao(ModelFileFolder::class.java))
//    private var floorModelService = SObjectService(SMybatisDao(FloorModel::class.java))

    val dataService = BaseDataService

    /**
     * 删除建筑文件夹
     */
    fun deleteFolder(folderList: ArrayList<ModelFileFolder>): SBaseResponse {
        val idList = ArrayList<String>()
        try {
            folderList.stream().map { modelFileFolder: ModelFileFolder? -> modelFileFolder!!.id }.distinct().forEach { id: String? -> if (id != null) idList.add(id) }
            val modelFloor = dataService.floorModelService.select(SFilter.inList("folderId", idList)).exec()
            val floorIdList = ArrayList<String>()
            modelFloor.forEach { floor ->
                if(floor.id != null)
                    floorIdList.add(floor.id!!)
            }
            var count = 0L
            if(floorIdList.isNotEmpty()) {
                count = dataService.modelFileService.select(SFilter.and(SFilter.inList("floorModelId", floorIdList), SFilter.eq("removed", false))).execCount()
            }
            if (count > 0L)
                return SBaseResponse(SResponseType.failure, "删除失败, 文件夹中有模型文件不允许删除!")
            else {
                val result = dataService.folderService.delete(SFilter.inList("id", idList))
                if (result) {
                    dataService.floorModelService.delete(SFilter.inList("folderId", idList))
                    return SBaseResponse(SResponseType.success, "删除成功")
                } else
                    return SBaseResponse(SResponseType.failure, "删除失败")
            }
        }catch (ex : Exception){
            return SBaseResponse(SResponseType.failure, ex.message ?: "")
        }
    }
}