package gaf3.core.services.bff.service

import gaf3.core.data.PagedData
import gaf3.core.jpa.extension.findAll
import gaf3.core.services.bff.dao.GafBffUserDeptDAO
import gaf3.core.services.bff.entity.BffUserDept
import gaf3.core.services.user.domain.UserInfo
import gaf3.core.services.user.entity.GafUserInfo
import gaf3.core.services.user.service.UserService
import org.springframework.data.domain.Example
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Service

@Service("gafBffUserDeptService")
class BffUserDeptService(val bffDao: GafBffUserDeptDAO, val userService: UserService) {

    @JvmOverloads
    fun find(filter: GafUserInfo, deptId: String?, skip: Int = 0, limit: Int = 100, exact: Boolean = true): PagedData<UserInfo> {
        val example = BffUserDept()
        example.user = filter
        example.target = deptId
        val page:Int = skip/limit
        val paged = bffDao.findAll(example, PageRequest.of(page, limit), exact)
        val rs = paged.content.map { p ->
            if(null == p.user) {
                UserInfo(p.target)
            } else {
                userService.convertDomain(p.user!!, p.acct)
            }
        }

        return PagedData(rs, paged.totalElements.toInt())
    }
}