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.*
|
import cn.flightfeather.supervision.domain.ds1.repository.RegionRep
|
import cn.flightfeather.supervision.lightshare.vo.SceneDetail
|
import com.google.gson.Gson
|
import org.apache.poi.ss.usermodel.Cell
|
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, true),
|
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)
|
)
|
|
// 工地信息
|
private val headersType1 = listOf(
|
ExcelUtil.MyHeader("工程类型", CellType.STRING),
|
ExcelUtil.MyHeader("建设状态", CellType.STRING),
|
ExcelUtil.MyHeader("施工阶段", CellType.STRING),
|
ExcelUtil.MyHeader("开工时间", CellType.STRING),
|
ExcelUtil.MyHeader("完工时间", CellType.STRING),
|
ExcelUtil.MyHeader("剩余工期", CellType.STRING),
|
ExcelUtil.MyHeader("占地面积", CellType.NUMERIC),
|
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),
|
ExcelUtil.MyHeader("安全员", CellType.STRING),
|
ExcelUtil.MyHeader("安全员电话", CellType.STRING),
|
)
|
|
/**
|
* 通过文件读取场景信息
|
*/
|
fun readFromFile(file: InputStream): List<SceneDetail> {
|
val result = mutableListOf<SceneDetail>()
|
try {
|
ExcelUtil.readXLXS(file, headers) {
|
val data = SceneDetail().apply {
|
scense = 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(0)?.numericCellValue?.toInt()
|
remark = null
|
}
|
if (scense?.name.isNullOrBlank()) throw BizException(errorStr(it.rowNum + 1, 2, "场景名称不能空白"))
|
scense?.typeid = Constant.SceneType.getByName(scense?.type)?.value?.toByte()
|
?: throw BizException(errorStr(it.rowNum + 1, 3, "场景类型不存在"))
|
if (scense?.location.isNullOrBlank()) throw BizException(errorStr(it.rowNum + 1, 4, "场景地址不能空白"))
|
scense?.longitude ?: throw BizException(errorStr(it.rowNum + 1, 5, "经度不能空白"))
|
scense?.latitude ?: throw BizException(errorStr(it.rowNum + 1, 6, "纬度不能空白"))
|
scense?.provincecode = regionRep.findProvince(scense?.provincename)?.provincecode
|
?: throw BizException(errorStr(it.rowNum + 1, 7, "省份错误或系统未曾配置"))
|
scense?.citycode = regionRep.findCity(scense?.cityname)?.citycode
|
?: throw BizException(errorStr(it.rowNum + 1, 8, "城市错误或系统未曾配置"))
|
scense?.districtcode = regionRep.findDistrict(scense?.districtname)?.districtcode
|
?: throw BizException(errorStr(it.rowNum + 1, 9, "区县错误或系统未曾配置"))
|
scense?.towncode = regionRep.findTown(scense?.townname)?.towncode
|
?: throw BizException(errorStr(it.rowNum + 1, 10, "街镇错误或系统未曾配置"))
|
|
when (scense?.typeid.toString()) {
|
Constant.SceneType.TYPE1.value -> {
|
subScene = SceneConstructionSite().apply {
|
csProjectType = getCellValueToString(it.getCell(12))
|
csStatus = getCellValueToString(it.getCell(13))
|
siExtension1 = getCellValueToString(it.getCell(14))
|
csStartTime = getCellValueToString(it.getCell(15))
|
csEndTime = getCellValueToString(it.getCell(16))
|
csLeftTime = getCellValueToString(it.getCell(17))
|
csFloorSpace = getCellValueToString(it.getCell(18))
|
csConstructionArea = getCellValueToString(it.getCell(19))
|
csConstructionAreaPerMonth = getCellValueToString(it.getCell(20))
|
csEmployerUnit = getCellValueToString(it.getCell(21))
|
csEmployerContacts = getCellValueToString(it.getCell(22))
|
csEmployerContactsTel = getCellValueToString(it.getCell(23))
|
csConstructionUnit = getCellValueToString(it.getCell(24))
|
csConstructionContacts = getCellValueToString(it.getCell(25))
|
csConstructionContactsTel = getCellValueToString(it.getCell(26))
|
csSecurityOfficer = getCellValueToString(it.getCell(27))
|
csSecurityOfficerTel = getCellValueToString(it.getCell(28))
|
}
|
}
|
Constant.SceneType.TYPE2.value -> {
|
|
}
|
Constant.SceneType.TYPE3.value -> {
|
|
}
|
Constant.SceneType.TYPE14.value -> {
|
|
}
|
}
|
}
|
|
result.add(data)
|
}
|
} catch (e: IllegalStateException) {
|
throw BizException(e.message, e)
|
}
|
return result
|
}
|
|
private fun errorStr(row: Int, col: Int, str: String): String {
|
return "第${row}行,第${col}列单元格错误,${str}"
|
}
|
|
private fun getCellValueToString(cell: Cell?): String {
|
cell ?: return ""
|
return when (cell.cellType) {
|
CellType.STRING -> cell.stringCellValue.trim()
|
CellType.NUMERIC -> cell.numericCellValue.toString().trim()
|
CellType.BLANK -> cell.stringCellValue.trim()
|
CellType.BOOLEAN -> cell.booleanCellValue.toString().trim()
|
CellType.ERROR -> cell.errorCellValue.toString().trim()
|
else -> throw BizException(errorStr(cell.rowIndex + 1, cell.columnIndex + 1, "单元格格式不是常见格式,无法识别"))
|
}
|
}
|
}
|