package cn.sagacloud.server.algorithm.services.group

import cn.sagacloud.server.algorithm.models.entities.Project
import cn.sagacloud.server.algorithm.models.group.GroupProjectDTO
import cn.sagacloud.server.algorithm.models.project.ProjectDTO
import cn.sagacloud.server.algorithm.models.request.BaseProjectRequest
import com.sybotan.database.SFilter
import com.sybotan.mybatis.SMybatisDao
import com.sybotan.service.SObjectService
import org.springframework.stereotype.Service

@Service
class GroupProjectService {

    val projectService = SObjectService(SMybatisDao(Project::class.java))

    /**
     * 查询集团项目列表
     */
    fun queryGroupProjectList(request: BaseProjectRequest):List<GroupProjectDTO> {
        val projectList:ArrayList<Project>
        if (!request.projectIds.isNullOrEmpty()){
            projectList = projectService.select(SFilter.Companion.inList("id", ArrayList(request.projectIds!!))).exec()
        }else{
            projectList = projectService.selectAll().exec()
        }
        val list:ArrayList<GroupProjectDTO> = ArrayList<GroupProjectDTO>()
        val map = projectList.groupBy { project: Project -> project.groupCode }
        map.forEach { (t, u) ->
            list.add(GroupProjectDTO(t,null,u.map {project -> ProjectDTO(project.id,project.name)})) }
        return list
    }
}