package cn.flightfeather.supervision.lightshare.service.impl
|
|
import cn.flightfeather.supervision.business.bgtask.ReportTaskCtrl
|
import cn.flightfeather.supervision.business.report.DataSource
|
import cn.flightfeather.supervision.business.report.DbMapper
|
import cn.flightfeather.supervision.business.report.file.ReportOne
|
import cn.flightfeather.supervision.business.report.file.ReportThree
|
import cn.flightfeather.supervision.business.report.file.ReportTwo
|
import cn.flightfeather.supervision.common.exception.BizException
|
import cn.flightfeather.supervision.domain.ds1.repository.TaskRep
|
import cn.flightfeather.supervision.lightshare.service.DataProductService
|
import cn.flightfeather.supervision.lightshare.vo.AreaVo
|
import cn.flightfeather.supervision.lightshare.vo.ExcelConfigVo
|
import org.springframework.beans.factory.annotation.Value
|
import org.springframework.http.HttpHeaders
|
import org.springframework.http.MediaType
|
import org.springframework.stereotype.Service
|
import java.io.File
|
import java.util.*
|
import javax.servlet.http.HttpServletResponse
|
|
/**
|
*
|
* @date 2024/10/18
|
* @author feiyu02
|
*/
|
@Service
|
class DataProductServiceImpl(
|
private val taskRep: TaskRep,
|
private val dbMapper: DbMapper,
|
@Value("\${filePath}") private val filePath: String,
|
private val reportTaskCtrl: ReportTaskCtrl,
|
) : DataProductService {
|
|
override fun downloadProduct(
|
areaVo: AreaVo, type: Int, forceUpdate: Boolean, response: HttpServletResponse,
|
): Boolean {
|
areaVo.scensetypeid ?: throw BizException("必须选择一个场景类型")
|
val topTask = taskRep.findOneTask(areaVo) ?: throw BizException("未找到符合条件的顶层任务")
|
val config = ExcelConfigVo(
|
topTask.tguid ?: "",
|
topTask.starttime,
|
topTask.endtime,
|
topTask.provincecode,
|
topTask.citycode,
|
topTask.districtcode,
|
topTask.towncode,
|
areaVo.scensetypeid?.toInt(),
|
forceUpdate = forceUpdate
|
)
|
val dataSource = DataSource(config, dbMapper)
|
val t = when (type) {
|
1 -> ReportOne(dataSource)
|
2 -> ReportTwo(dataSource)
|
3 -> ReportThree(dataSource)
|
else -> throw BizException("未指定数据产品类型,无法下载")
|
}
|
val fileName = t.getReportName()
|
val p = "$filePath/autoscore/"
|
val file = File(p + fileName)
|
if (config.forceUpdate || !file.exists()) {
|
val downloadUrl = "/autoscore/${fileName}"
|
reportTaskCtrl.startTask(t, downloadUrl)
|
return false
|
} else {
|
val fName = Base64.getEncoder().encodeToString(fileName.toByteArray())
|
response.apply {
|
setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=$fName")
|
setHeader("fileName", fName)
|
addHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "fileName")
|
contentType = "application/vnd.ms-excel;charset=UTF-8"
|
// contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE
|
setHeader(HttpHeaders.PRAGMA, "no-cache")
|
setHeader(HttpHeaders.CACHE_CONTROL, "no-cache")
|
setDateHeader(HttpHeaders.EXPIRES, 0)
|
}
|
response.outputStream.write(file.readBytes())
|
return true
|
}
|
}
|
}
|