src/main/kotlin/cn/flightfeather/supervision/datafetch/FetchNightConstruction.kt
@@ -4,9 +4,14 @@
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 org.springframework.transaction.annotation.Transactional
import tk.mybatis.mapper.entity.Example
import java.util.*
import javax.annotation.PostConstruct
@@ -27,28 +32,37 @@
        private const val CITY_NAME = "上海市"
        private const val DISTRICT_NAME = "静安区"
        private const val DEFAULT_TIME = "2020-12-01 00:00:00"
        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
    private val dateUtil = DateUtil()
    @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)
        nightConstructionMapper.selectByExample(Example(NightConstruction::class.java).apply {
        val res = nightConstructionMapper.selectByExample(Example(NightConstruction::class.java).apply {
            createCriteria().andEqualTo("ncProvinceCode", PROVINCE_CODE)
                    .andEqualTo("ncCityCode", CITY_CODE)
                    .andEqualTo("ncDistrictCode", DISTRICT_CODE)
                .andEqualTo("ncCityCode", CITY_CODE)
                .andEqualTo("ncDistrictCode", DISTRICT_CODE)
            orderBy("ncCreateTime").desc()
        }).let {
        })
        res.let {
            val timeStr = if (it.isNotEmpty()) {
                val cal = Calendar.getInstance().apply { time = it[0]?.ncCreateTime }
                cal.add(Calendar.DAY_OF_MONTH, -1)
@@ -56,6 +70,7 @@
            } else {
                DEFAULT_TIME
            }
//            val timeStr = DEFAULT_TIME
            try {
@@ -82,19 +97,41 @@
                            ncDistrictCode = DISTRICT_CODE
                            ncDistrictName = DISTRICT_NAME
                        }
                        nightConstructionMapper.selectByExample(Example(NightConstruction::class.java).apply {
                            createCriteria().andEqualTo("ncNum", nightConstruction.ncNum)
                        }).run {
                            nightConstructionMapper.selectByExample(Example(NightConstruction::class.java).apply {
                        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)
                            }).takeIf { l-> l.isNotEmpty() }?.get(0)?.let { r->
                                nightConstruction.ncUserId = r.ncUserId
                                nightConstruction.ncSceneId = r.ncSceneId
                            }
                            if (isEmpty()) {
                                nightConstructionMapper.insert(nightConstruction)
                            })
                        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) {