feiyu02
2024-11-08 d2727f231319a48019bc3b87439136ab49b97b9b
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
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
        }
    }
}