package gaf3.core.services.code.service.impl

import gaf3.core.exception.BusinessError
import gaf3.core.services.code.dao.GafCodeItemDAO
import gaf3.core.services.code.domain.CodeItem
import gaf3.core.services.code.service.CodeService
import gaf3.core.services.code.service.GafCodeStore
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.data.domain.Sort
import org.springframework.stereotype.Service

@Service("dbCodeStore")
//@ConditionalOnProperty(prefix = "gaf.code", name = ["store-type"], havingValue = "db", matchIfMissing = true)
class DBCodeStore (@Autowired private val itemDao: GafCodeItemDAO): GafCodeStore {
    override fun load(type: String): List<CodeItem> {
        val item = itemDao.findOneByTypeAndAlias("group", type) ?: itemDao.findOneByTypeAndCode("group", type)
        ?: throw BusinessError(BusinessError.ERR_BUSINESS, "字典类型不存在")
        val rs = itemDao.findAllByType(item.code ?: item.name ?: item.id!!, Sort.by(Sort.Direction.ASC, "code"))
        return rs.map {
            p->CodeService.convertDomain(p)
        }
    }
}