feiyu02
2025-07-31 6688232eaa889eeb6c58d0d804b587699db55ec2
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
package cn.flightfeather.supervision.business.import
 
import cn.flightfeather.supervision.common.exception.BizException
import cn.flightfeather.supervision.common.utils.Constant
import cn.flightfeather.supervision.common.utils.ExcelUtil
import cn.flightfeather.supervision.domain.ds1.entity.Scense
import cn.flightfeather.supervision.domain.ds1.repository.RegionRep
import org.apache.poi.ss.usermodel.CellType
import org.springframework.stereotype.Component
import java.io.InputStream
import java.util.*
 
/**
 * 场景信息导入工具
 */
@Component
class SceneImport(
    private val regionRep: RegionRep,
) {
    private val headers =
        listOf(
            ExcelUtil.MyHeader("编号(若不填写,则按照顺序自动添加)", CellType.NUMERIC),
            ExcelUtil.MyHeader("名称", CellType.STRING),
            ExcelUtil.MyHeader("类型", CellType.STRING),
            ExcelUtil.MyHeader("地址", CellType.STRING),
            ExcelUtil.MyHeader("经度(高德)", CellType.NUMERIC),
            ExcelUtil.MyHeader("纬度(高德)", CellType.NUMERIC),
            ExcelUtil.MyHeader("省", CellType.STRING),
            ExcelUtil.MyHeader("市", CellType.STRING),
            ExcelUtil.MyHeader("区", CellType.STRING),
            ExcelUtil.MyHeader("街镇", CellType.STRING),
            ExcelUtil.MyHeader("联系人", CellType.STRING),
            ExcelUtil.MyHeader("联系电话", CellType.STRING)
        )
 
    /**
     * 通过文件读取场景信息
     */
    fun readFromFile(file: InputStream): List<Scense> {
        val result = mutableListOf<Scense>()
        try {
            ExcelUtil.readXLXS(file, headers) {
                val data = Scense().apply {
                    guid = null
                    name = it.getCell(1)?.stringCellValue?.trim()
                    typeid = null
                    type = it.getCell(2)?.stringCellValue?.trim()
                    scensesubtypeid = null
                    scensesubtype = null
                    location = it.getCell(3)?.stringCellValue?.trim()
                    longitude = it.getCell(4)?.numericCellValue?.toBigDecimal()
                    latitude = it.getCell(5)?.numericCellValue?.toBigDecimal()
                    provincecode = null
                    provincename = it.getCell(6)?.stringCellValue?.trim()
                    citycode = null
                    cityname = it.getCell(7)?.stringCellValue?.trim()
                    districtcode = null
                    districtname = it.getCell(8)?.stringCellValue?.trim()
                    towncode = null
                    townname = it.getCell(9)?.stringCellValue?.trim()
                    contactst = it.getCell(11)?.stringCellValue?.trim()
                    contactswx = null
                    contacts = it.getCell(10)?.stringCellValue?.trim()
                    createdate = Date()
                    updatedate = Date()
                    extension1 = "1"
                    extension2 = null
                    index = it.getCell(5)?.stringCellValue?.trim()?.toInt()
                    remark = null
                }
                data.typeid = Constant.SceneType.getByName(data.type)?.value?.toByte()
                    ?: throw BizException(errorStr(it.rowNum + 1, 2, "场景类型不存在"))
                data.provincecode = regionRep.findProvince(data.provincename)?.provincecode
                    ?: throw BizException(errorStr(it.rowNum + 1, 6, "省份错误或系统未曾配置"))
                data.citycode = regionRep.findCity(data.cityname)?.citycode
                    ?: throw BizException(errorStr(it.rowNum + 1, 7, "城市错误或系统未曾配置"))
                data.districtcode = regionRep.findDistrict(data.districtname)?.districtcode
                    ?: throw BizException(errorStr(it.rowNum + 1, 8, "区县错误或系统未曾配置"))
                data.towncode = regionRep.findTown(data.townname)?.towncode
                    ?: throw BizException(errorStr(it.rowNum + 1, 9, "街镇错误或系统未曾配置"))
 
                result.add(data)
            }
        } catch (e: IllegalStateException) {
            throw BizException("excel文件内容错误,数据转换失败!", e)
        }
        return result
    }
 
    private fun errorStr(row: Int, col: Int, str: String): String {
        return "第${row}行,第${col}列单元格错误,${str}"
    }
}