From 85ef942e7195abeb71466b7159c3ee30161e1e54 Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期二, 07 四月 2026 08:51:01 +0800
Subject: [PATCH] 2026.4.7
---
src/main/kotlin/cn/flightfeather/supervision/lightshare/service/impl/MediafileServiceImpl.kt | 236 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 208 insertions(+), 28 deletions(-)
diff --git a/src/main/kotlin/cn/flightfeather/supervision/lightshare/service/impl/MediafileServiceImpl.kt b/src/main/kotlin/cn/flightfeather/supervision/lightshare/service/impl/MediafileServiceImpl.kt
index 4c5d8c6..b06ee08 100644
--- a/src/main/kotlin/cn/flightfeather/supervision/lightshare/service/impl/MediafileServiceImpl.kt
+++ b/src/main/kotlin/cn/flightfeather/supervision/lightshare/service/impl/MediafileServiceImpl.kt
@@ -1,11 +1,21 @@
package cn.flightfeather.supervision.lightshare.service.impl
+import cn.flightfeather.supervision.business.autooutput.datasource.AopDbMapper
+import cn.flightfeather.supervision.common.exception.BizException
+import cn.flightfeather.supervision.common.utils.*
import cn.flightfeather.supervision.domain.ds1.entity.Mediafile
import cn.flightfeather.supervision.domain.ds1.mapper.MediafileMapper
-import cn.flightfeather.supervision.common.utils.Constant
-import cn.flightfeather.supervision.common.utils.FileUtil
+import cn.flightfeather.supervision.domain.ds1.entity.Inspection
+import cn.flightfeather.supervision.domain.ds1.entity.Subtask
+import cn.flightfeather.supervision.domain.ds1.repository.InspectionRep
+import cn.flightfeather.supervision.domain.ds1.repository.MediaFileRep
+import cn.flightfeather.supervision.domain.ds1.repository.SceneRep
+import cn.flightfeather.supervision.domain.ds1.repository.SubTaskRep
+import cn.flightfeather.supervision.lightshare.service.DomainitemService
import cn.flightfeather.supervision.lightshare.service.MediafileService
-import cn.flightfeather.supervision.lightshare.vo.MediafileVo
+import cn.flightfeather.supervision.lightshare.vo.AreaVo
+import cn.flightfeather.supervision.lightshare.vo.ExcelConfigVo
+import cn.flightfeather.supervision.lightshare.vo.MediaFileVo
import com.fasterxml.jackson.core.type.TypeReference
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Service
@@ -13,11 +23,30 @@
import com.fasterxml.jackson.databind.ObjectMapper
import org.slf4j.Logger
import org.slf4j.LoggerFactory
+import org.springframework.beans.factory.annotation.Value
+import tk.mybatis.mapper.entity.Example
import java.io.File
+import java.io.FileInputStream
+import java.io.FileOutputStream
+import java.nio.charset.StandardCharsets
+import java.time.LocalDateTime
+import java.time.ZoneId
+import java.util.*
+import javax.servlet.http.HttpServletResponse
@Service
-class MediafileServiceImpl(val mediafileMapper: MediafileMapper) : MediafileService {
+class MediafileServiceImpl(
+ val mediafileMapper: MediafileMapper,
+ private val mediaFileRep: MediaFileRep,
+ private val inspectionRep: InspectionRep,
+ private val subTaskRep: SubTaskRep,
+ private val sceneRep: SceneRep,
+ private val domainItemService: DomainitemService,
+ private val dbMapper: AopDbMapper,
+ @Value("\${filePath}") var filePath: String,
+ @Value("\${imgPath}") var imgPath: String,
+) : MediafileService {
private val logging: Logger = LoggerFactory.getLogger(MediafileServiceImpl::class.java)
@@ -31,21 +60,43 @@
return mediafilelist
}
+ /**
+ * 鑾峰彇鎵�鏈変换鎰忔媿甯歌璁板綍鍥剧墖
+ * @param iGuid
+ */
+ override fun getRoutineRecord(iGuid: String?, stGuid: String?): List<Mediafile?> {
+ var inspectionGuid = iGuid
+ val sceneId = if (iGuid != null) {
+ inspectionRep.findOne(iGuid)?.sguid
+ } else if (stGuid != null) {
+ val s = subTaskRep.findOne(stGuid)
+ val ins = inspectionRep.findOne(Inspection().apply { stguid = stGuid })
+ inspectionGuid = ins?.guid
+ s?.scenseid
+ } else {
+ throw BizException("宸℃煡璁板綍id鍜屽贰鏌ヤ换鍔d鑷冲皯濉啓鍏朵腑涓�涓�")
+ }
+ sceneId ?: throw BizException("璁板綍瀵瑰簲鐨勫満鏅笉瀛樺湪")
+ val sceneInfo = sceneRep.findScene(sceneId = sceneId) ?: throw BizException("鍦烘櫙涓嶅瓨鍦紝鑾峰彇鍦烘櫙鍥剧墖澶辫触")
+ val mediaFileTypeList = domainItemService.getMediaFileType(sceneInfo.typeid?.toInt()).entries.map { it.key?.toInt() }
+ return mediaFileRep.findList(inspectionGuid, mediaFileTypeList)
+ }
+
//鏍规嵁闂id鑾峰彇濯掍綋鏂囦欢
- override fun findByProblemID(id: String): List<MediafileVo> {
- val mediafileVos = mutableListOf<MediafileVo>()
+ override fun findByProblemID(id: String): List<MediaFileVo> {
+ val mediaFileVos = mutableListOf<MediaFileVo>()
val mediafile = Mediafile()
mediafile.iguid = id
val mediafiles = mediafileMapper.select(mediafile)
if (mediafiles.isNotEmpty()) {
mediafiles.forEach {
- val mediafileVo = MediafileVo()
+ val mediafileVo = MediaFileVo()
BeanUtils.copyProperties(it, mediafileVo)
- mediafileVos.add(mediafileVo)
+ mediaFileVos.add(mediafileVo)
}
}
- mediafileVos.sortBy { it.savetime }
- return mediafileVos
+ mediaFileVos.sortBy { it.savetime }
+ return mediaFileVos
}
//鏂板濯掍綋鏂囦欢
@@ -55,11 +106,13 @@
val mapper = ObjectMapper()
//json杞琽bject
- val mediafileVos =
- mapper.readValue<List<MediafileVo>>(mediafileVoList, object : TypeReference<List<MediafileVo>>() {})
+ val mediaFileVos =
+ mapper.readValue<List<MediaFileVo>>(mediafileVoList, object : TypeReference<List<MediaFileVo>>() {})
- if (mediafileVos.isNotEmpty()) {
- mediafileVos.forEach {
+ if (mediaFileVos.isNotEmpty()) {
+ mediaFileVos.forEachIndexed { i, it ->
+ if (i >= files.size) return@forEachIndexed
+
val mediafile = Mediafile()
BeanUtils.copyProperties(it, mediafile)
@@ -69,17 +122,15 @@
if (reme == null) {
mediafileMapper.insert(mediafile)
}
- }
- //鏍规嵁鎵╁睍瀛楁1鑾峰彇淇濆瓨璺緞
- val path = mediafileVos[0].extension1
- for (image in files) {
- val fileName = image.originalFilename
-
- val filePath = "${Constant.DEFAULT_FILE_PATH}/images/$path/"
+ //鏍规嵁鎵╁睍瀛楁1鑾峰彇淇濆瓨璺緞
+ val path = it.extension1
+ val fileName = it.guid + ".jpg"
+ val image = files[i]
+ val filePath = "${imgPath}/$path/"
try {
//璋冪敤鏂囦欢淇濆瓨鏂规硶
- FileUtil.uploadFile(image.bytes, filePath, fileName!!)
+ FileUtil.uploadFile(image.bytes, filePath, fileName)
} catch (e: Exception) {
// TODO: handle exception
}
@@ -89,17 +140,17 @@
//鏍规嵁涓氬姟绫诲瀷ID鑾峰彇濯掍綋鏂囦欢
- override fun findByBusinessGUID(businessGuid: String): List<MediafileVo> {
- val mediafileVoList = mutableListOf<MediafileVo>()
+ override fun findByBusinessGUID(businessGuid: String): List<MediaFileVo> {
+ val mediaFileVoList = mutableListOf<MediaFileVo>()
val mediafile = Mediafile()
mediafile.businessguid = businessGuid
val mediafiles = mediafileMapper.select(mediafile)
mediafiles.forEach {
- val mediafileVo = MediafileVo()
+ val mediafileVo = MediaFileVo()
BeanUtils.copyProperties(it, mediafileVo)
- mediafileVoList.add(mediafileVo)
+ mediaFileVoList.add(mediafileVo)
}
- return mediafileVoList
+ return mediaFileVoList
}
override fun findOne(id: String): Mediafile = mediafileMapper.selectByPrimaryKey(id)
@@ -107,6 +158,26 @@
override fun findAll(): MutableList<Mediafile> = mediafileMapper.selectAll()
override fun save(mediafile: Mediafile): Int = mediafileMapper.updateByPrimaryKey(mediafile)
+
+ override fun saveMediaFile(files: Array<MultipartFile>, getMediaFile: () -> Mediafile): Int {
+ var res = 0
+ // 淇濆瓨鍥剧墖
+ for (image in files) {
+ val mediaFile = getMediaFile()
+ res += mediafileMapper.insert(mediaFile)
+
+ val path = mediaFile.extension1
+ val fileName = mediaFile.guid + ".jpg"
+ val filePath = "${imgPath}/$path/"
+ try {
+ //璋冪敤鏂囦欢淇濆瓨鏂规硶
+ FileUtil.uploadFile(image.bytes, filePath, fileName)
+ } catch (e: Exception) {
+ // TODO: handle exception
+ }
+ }
+ return res
+ }
override fun update(mediafile: Mediafile): Int = mediafileMapper.updateByPrimaryKey(mediafile)
@@ -117,10 +188,119 @@
val path = mediafile.extension1
//File("/Users/liwei/images/${path}/${id}.jpg").delete()
//鍏堝垹鍥剧墖
- val f = File("${Constant.DEFAULT_FILE_PATH}/images/$path/$id.jpg")
+ val f = File("${imgPath}/$path/$id.jpg")
if (f.exists()) {
f.delete()
}
return mediafileMapper.deleteByPrimaryKey(id)
}
+
+ override fun deleteList(idList: List<String>): Int {
+ var res = 0
+ if (idList.isEmpty()) return res
+ mediaFileRep.findList(idList).forEach {
+ //鏈嶅姟鍣ㄤ繚瀛樿矾寰�
+ val path = it?.extension1
+ //鍏堝垹鍥剧墖
+ val f = File("${imgPath}/$path/${it?.guid}.jpg")
+ if (f.exists()) {
+ f.delete()
+ }
+ res += mediaFileRep.deleteOne(it?.guid)
+ }
+ return res
+ }
+
+ override fun downloadPic(config: ExcelConfigVo, response: HttpServletResponse): HttpServletResponse {
+ val subSum = dbMapper.subtaskMapper.getSummaryByArea(AreaVo().apply {
+ provincecode = config.provinceCode
+ citycode = config.cityCode
+ districtcode = config.districtCode
+ starttime = LocalDateTime.ofInstant(config.startTime?.toInstant(), ZoneId.systemDefault())
+ endtime = LocalDateTime.ofInstant(config.endTime?.toInstant(), ZoneId.systemDefault())
+ scensetypeid = config.sceneType.toString()
+ })
+ if (subSum.isEmpty()) return response
+
+ val mediaList = mediafileMapper.selectByExample(Example(Mediafile::class.java).apply {
+ createCriteria().andIn("iguid", subSum.map { it.insGuid })
+ .apply {
+ if (!config.fileType.isNullOrEmpty()) {
+ andIn("businesstypeid", config.fileType)
+ }
+ }
+ })
+
+ //寤虹珛绗竴灞傜洰褰曪紝鍖呭惈鎵�鏈夌殑浠诲姟
+ val time = DateUtil.DateToString(Date(), "yyyy-MM-dd_HH-mm-ss")
+ val basePath = imgPath + File.separator + "temp" + File.separator + time
+ FileUtil.mkDirs(basePath)
+
+ //寰幆寤虹珛绗簩灞傜洰褰曪紝鎸夌収鏃ユ湡鍒嗙被
+ subSum.groupBy { it.stPlanTime }.forEach { (date, sList) ->
+ //寤虹珛涓�涓棩鏈熸枃浠跺す
+ val datePath = "${basePath}${File.separator}${DateUtil.DateToString(date, DateUtil.DateStyle.YYYY_MM_DD)}"
+ FileUtil.mkDirs(datePath)
+
+ //寰幆寤虹珛绗笁灞傜洰褰曪紝鎸夌収宸℃煡浠诲姟鍒嗙被
+ sList.forEach {s ->
+ //寤虹珛涓�涓瓙浠诲姟鏂囦欢澶�
+ val subTaskFilePath = "${datePath}${File.separator}${StringUtil.numberFormat(s.sceneIndex, 3)} ${s.sceneName}"
+ FileUtil.mkDirs(subTaskFilePath)
+
+ //寰幆寤虹珛绗洓灞傜洰褰曪紝鎸夌収鍥剧墖绫诲瀷鍒嗙被
+ mediaList.filter { m-> m.iguid == s.insGuid }.groupBy { m-> m.businesstype }.forEach { (type, mList) ->
+ val mediaTypeFilePath = "${subTaskFilePath}${File.separator}${type}"
+ FileUtil.mkDirs(mediaTypeFilePath)
+
+ // 鍐欏叆鍥剧墖
+ mList.forEachIndexed { i, m->
+ val fromPicName = m.guid + ".jpg"
+ val picPath =
+ imgPath + File.separator + m.extension1 + fromPicName
+ val fromFile = File(picPath)
+ val picName = if (m.description != null) {
+ m.description!!.replace("/", "-")
+ .replace("\\", "-")
+ .replace("\n", "")
+ .replace(".", "(${i + 1}).")
+ } else {
+ fromPicName
+ }
+// val picName = fromPicName
+ val toFile = File(mediaTypeFilePath + File.separator + picName)
+ if (fromFile.exists()) {
+ FileUtil.copy(fromFile, toFile)
+ }
+ }
+ }
+ }
+ }
+
+ response.apply {
+ setHeader("Content-Disposition", "attachment;filename=$time.zip")
+ contentType = "application/octet-stream"
+ characterEncoding = StandardCharsets.UTF_8.name()
+ }
+
+ val zipFile = File("$basePath.zip")
+ val fos1 = FileOutputStream(zipFile)
+ ZipUtils.toZip(basePath, fos1, true)
+
+ val out = response.outputStream
+ val fis = FileInputStream(zipFile)
+
+ val buf = ByteArray(2 * 1024)
+ var len = fis.read(buf)
+ while (len != -1) {
+ out.write(buf, 0, len)
+ len = fis.read(buf)
+ }
+ out.flush()
+ out.close()
+ fis.close()
+ fos1.close()
+
+ return response
+ }
}
\ No newline at end of file
--
Gitblit v1.9.3