1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package cn.flightfeather.supervision.domain.ds1.repository
 
import cn.flightfeather.supervision.common.utils.Constant
import cn.flightfeather.supervision.domain.ds1.entity.*
import cn.flightfeather.supervision.domain.ds1.mapper.*
import org.apache.commons.lang3.ObjectUtils
import org.springframework.stereotype.Repository
import tk.mybatis.mapper.entity.Example
import java.util.*
 
@Repository
class DataProductRep(
    private val dataProductMapper: DataProductMapper,
    private val dataProductProDetailMapper: DataProductProDetailMapper,
    private val dataProductTownProAnalysisMapper: DataProductTownProAnalysisMapper,
    private val provinceMapper: ProvinceMapper,
    private val townMapper: TownMapper,
    private val cityMapper: CityMapper,
    private val districtMapper: DistrictMapper
 
) {
 
    /**
     * 插入数据产品中间结果基本信息和具体信息
     */
    fun insertDataProduct(dataProduct: DataProduct, specificEntities: List<Any>) {
        // 根据类型插入指定数据库表
        if (specificEntities.isNotEmpty()) {
            when (specificEntities.first()::class) {
                DataProductProDetail::class -> {
                    (specificEntities as? List<DataProductProDetail>)?.let { let_it ->
                        specificEntities.forEach { for_it ->
                            for_it.dpGuid = dataProduct.guid
                        }
                        dataProduct.typeId = Constant.DataProductType.PRO_DETAIL_SUMMARY.value
                        insertProDetailSpecificInfo(let_it)
                    }
                }
                DataProductTownProAnalysis::class -> {
                    (specificEntities as? List<DataProductTownProAnalysis>)?.let { let_it ->
                        specificEntities.forEach { for_it ->
                            for_it.dpGuid = dataProduct.guid
                        }
                        dataProduct.typeId = Constant.DataProductType.PRO_ANALYSIS_SUMMARY.value
                        insertTownProAnalysisSpecificInfo(let_it)
                    }
                }
            }
        }
        insertDataProductBaseInfo(dataProduct)
    }
 
    private fun insertProDetailSpecificInfo(specificEntities: List<DataProductProDetail>) {
        dataProductProDetailMapper.insertList(specificEntities)
    }
 
    private fun insertTownProAnalysisSpecificInfo(specificEntities: List<DataProductTownProAnalysis>) {
        dataProductTownProAnalysisMapper.insertList(specificEntities)
    }
 
     private fun insertDataProductBaseInfo(dataProduct: DataProduct) {
         // 行政区域name填充
         if (dataProduct.townCode != null) {
             townMapper.selectByExample(Example(Town::class.java).apply {
                 createCriteria().andEqualTo("towncode", dataProduct.townCode)
             })?.takeIf { it.isNotEmpty() }?.get(0)?.let { dataProduct.townName = it.townname ?: "" }
         }
         if (dataProduct.provinceCode != null) {
             provinceMapper.selectByExample(Example(Province::class.java).apply {
                 createCriteria().andEqualTo("provincecode", dataProduct.provinceCode)
             })?.takeIf { it.isNotEmpty() }?.get(0)?.let { dataProduct.provinceName = it.provincename ?: "" }
         }
         if (dataProduct.cityCode != null) {
             cityMapper.selectByExample(Example(City::class.java).apply {
                 createCriteria().andEqualTo("citycode", dataProduct.cityCode)
             })?.takeIf { it.isNotEmpty() }?.get(0)?.let { dataProduct.cityName = it.cityname ?: "" }
         }
         if (dataProduct.districtCode != null) {
             districtMapper.selectByExample(Example(District::class.java).apply {
                 createCriteria().andEqualTo("districtcode", dataProduct.districtCode)
             })?.takeIf { it.isNotEmpty() }?.get(0)?.let { dataProduct.districtName = it.districtname ?: "" }
         }
         dataProduct.createTime = Date()
         dataProductMapper.insert(dataProduct)
    }
}