package cn.flightfeather.supervision.domain.ds1.repository
|
|
import cn.flightfeather.supervision.domain.ds1.entity.City
|
import cn.flightfeather.supervision.domain.ds1.entity.District
|
import cn.flightfeather.supervision.domain.ds1.entity.Province
|
import cn.flightfeather.supervision.domain.ds1.entity.Town
|
import cn.flightfeather.supervision.domain.ds1.mapper.CityMapper
|
import cn.flightfeather.supervision.domain.ds1.mapper.DistrictMapper
|
import cn.flightfeather.supervision.domain.ds1.mapper.ProvinceMapper
|
import cn.flightfeather.supervision.domain.ds1.mapper.TownMapper
|
import org.springframework.stereotype.Repository
|
|
/**
|
* 地域信息相关数据库操作
|
*/
|
@Repository
|
class RegionRep(
|
private val provinceMapper: ProvinceMapper,
|
private val cityMapper: CityMapper,
|
private val districtMapper: DistrictMapper,
|
private val townMapper: TownMapper,
|
) {
|
private val cacheMap = mutableMapOf<String, Any?>()
|
|
private fun<T : Any?> findCache(key: String?, findDb: () -> T?): T? {
|
key ?: return null
|
val s = cacheMap[key]
|
return if (s == null) {
|
val value = findDb()
|
cacheMap[key] = value
|
value
|
} else {
|
try {
|
s as T
|
} catch (e: Exception) {
|
null
|
}
|
}
|
}
|
|
fun findProvince(name: String?): Province? {
|
return findCache(name) {
|
provinceMapper.selectOne(Province().apply { provincename = name })
|
}
|
}
|
|
fun findCity(name: String?): City? {
|
return findCache(name) {
|
cityMapper.selectOne(City().apply { cityname = name })
|
}
|
}
|
|
fun findDistrict(name: String?): District? {
|
return findCache(name) {
|
districtMapper.selectOne(District().apply { districtname = name })
|
}
|
}
|
|
fun findTown(name: String?): Town? {
|
return findCache(name) {
|
townMapper.selectOne(Town().apply { townname = name })
|
}
|
}
|
}
|