feiyu02
2025-07-23 517296b16b1faf07bc389809387b1937f9415746
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package cn.flightfeather.supervision.business.datafetch
 
import cn.flightfeather.supervision.common.net.NCHttpService
import cn.flightfeather.supervision.domain.ds1.entity.NightConstruction
import cn.flightfeather.supervision.domain.ds1.mapper.NightConstructionMapper
import cn.flightfeather.supervision.common.utils.DateUtil
import cn.flightfeather.supervision.domain.ds1.entity.Scense
import cn.flightfeather.supervision.domain.ds1.entity.Userinfo
import cn.flightfeather.supervision.domain.ds1.mapper.ScenseMapper
import cn.flightfeather.supervision.domain.ds1.mapper.UserinfoMapper
import com.github.pagehelper.PageHelper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import tk.mybatis.mapper.entity.Example
import java.util.*
import javax.annotation.PostConstruct
 
/**
 * 获取静安区夜间施工许可证信息
 */
@Component
class FetchNightConstruction : FetchData() {
 
    companion object {
        private lateinit var instance: FetchNightConstruction
 
        private const val PROVINCE_CODE = "31"
        private const val CITY_CODE = "3100"
        private const val DISTRICT_CODE = "310106"
        private const val PROVINCE_NAME = "上海市"
        private const val CITY_NAME = "上海市"
        private const val DISTRICT_NAME = "静安区"
 
        private const val DEFAULT_TIME = "2022-01-01 00:00:00"
    }
 
    @Autowired
    lateinit var nightConstructionMapper: NightConstructionMapper
 
    @Autowired
    lateinit var scenseMapper: ScenseMapper
 
    @Autowired
    lateinit var userinfoMapper: UserinfoMapper
 
    @PostConstruct
    fun init() {
        instance = this
        this.nightConstructionMapper = instance.nightConstructionMapper
        this.scenseMapper = instance.scenseMapper
        this.userinfoMapper = instance.userinfoMapper
    }
 
    override fun fetch() {
        PageHelper.startPage<NightConstruction>(1, 1)
        val res = nightConstructionMapper.selectByExample(Example(NightConstruction::class.java).apply {
            createCriteria().andEqualTo("ncProvinceCode", PROVINCE_CODE)
                .andEqualTo("ncCityCode", CITY_CODE)
                .andEqualTo("ncDistrictCode", DISTRICT_CODE)
            orderBy("ncCreateTime").desc()
        })
        res.let {
            val timeStr = if (it.isNotEmpty()) {
                val cal = Calendar.getInstance().apply { time = it[0]?.ncCreateTime }
                cal.add(Calendar.DAY_OF_MONTH, -1)
                DateUtil.DateToString(cal.time, DateUtil.DateStyle.YYYY_MM_DD_HH_MM_SS) ?: DEFAULT_TIME
            } else {
                DEFAULT_TIME
            }
//            val timeStr = DEFAULT_TIME
 
 
            try {
                NCHttpService.getFile(timeStr)?.forEach {e ->
                    if (e.isJsonObject) {
                        val vo = e.asJsonObject
                        val nightConstruction = NightConstruction().apply {
                            ncNum = vo["nightallnum"].asString
                            ncItemName = vo["itemName"].asString
                            ncItemUnit = vo["itemUnit"].asString
                            ncConstructionUnit = vo["constructionUnit"].asString
                            ncPerson = vo["person"].asString
                            ncApplyContent = vo["applyContent"].asString
                            ncStartDate = DateUtil.StringToDate(vo["startDate"].asString)
                            ncEndDate = DateUtil.StringToDate(vo["endDate"].asString)
                            ncFileName = vo["filename"].asString
                            ncCreateTime = DateUtil.StringToDate(vo["createtime"].asString)
                            ncUrl = vo["url"].asString
                            val url = ncUrl.split("://")[1].split("/")[0]
                            ncUrl = ncUrl.replace(url, "${NCHttpService.IP}:${NCHttpService.PORT}")
 
                            ncProvinceCode = PROVINCE_CODE
                            ncProvinceName = PROVINCE_NAME
                            ncCityCode = CITY_CODE
                            ncCityName = CITY_NAME
                            ncDistrictCode = DISTRICT_CODE
                            ncDistrictName = DISTRICT_NAME
                        }
                        val history = nightConstructionMapper.selectByExample(Example(NightConstruction::class.java)
                            .apply {
                                createCriteria().andEqualTo("ncNum", nightConstruction.ncNum)
                            })
                        // 判断该夜施文件是否存在
                        if (history.isNotEmpty()) return@forEach
 
                        var sceneId: String? = null
                        var userId: String? = null
                        // 获取该夜施对应企业的历史记录
                        val records = nightConstructionMapper.selectByExample(Example(NightConstruction::class.java)
                            .apply {
                                createCriteria().andEqualTo("ncItemName", nightConstruction.ncItemName)
                            })
                        if (records.isNotEmpty()) {
                            sceneId = records[0]?.ncSceneId
                            userId = records[0]?.ncUserId
                        }
                        // 若无历史记录,查找场景表,判断是否有相同名称的场景
                        else {
                            val scenes = scenseMapper.selectByExample(Example(Scense::class.java).apply {
                                createCriteria().andEqualTo("name", nightConstruction.ncItemName)
                            })
                            if (scenes.isNotEmpty()) {
                                sceneId = scenes[0].guid
                                val user = userinfoMapper.selectByExample(Example(Userinfo::class.java).apply {
                                    createCriteria().andEqualTo("dGuid", sceneId)
                                })
                                if (user.isNotEmpty()) userId = user[0].guid
                            }
                        }
                        nightConstruction.ncUserId = userId
                        nightConstruction.ncSceneId = sceneId
 
                        nightConstructionMapper.insert(nightConstruction)
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
}