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 |  178 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 168 insertions(+), 10 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 27ca9c2..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,20 @@
 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.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
@@ -15,13 +24,26 @@
 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,
     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 {
@@ -36,6 +58,28 @@
         val mediafilelist = mediafileMapper.select(mediafile)
         mediafilelist.sortBy { it.savetime }
         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鑾峰彇濯掍綋鏂囦欢
@@ -66,7 +110,9 @@
             mapper.readValue<List<MediaFileVo>>(mediafileVoList, object : TypeReference<List<MediaFileVo>>() {})
 
         if (mediaFileVos.isNotEmpty()) {
-            mediaFileVos.forEach {
+            mediaFileVos.forEachIndexed { i, it ->
+                if (i >= files.size) return@forEachIndexed
+
                 val mediafile = Mediafile()
                 BeanUtils.copyProperties(it, mediafile)
 
@@ -76,17 +122,15 @@
                 if (reme == null) {
                     mediafileMapper.insert(mediafile)
                 }
-            }
 
-            //鏍规嵁鎵╁睍瀛楁1鑾峰彇淇濆瓨璺緞
-            val path = mediaFileVos[0].extension1
-            for (image in files) {
-                val fileName = image.originalFilename
-
+                //鏍规嵁鎵╁睍瀛楁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
                 }
@@ -115,6 +159,26 @@
 
     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)
 
     //鍒犻櫎濯掍綋鏂囦欢
@@ -133,6 +197,7 @@
 
     override fun deleteList(idList: List<String>): Int {
         var res = 0
+        if (idList.isEmpty()) return res
         mediaFileRep.findList(idList).forEach {
             //鏈嶅姟鍣ㄤ繚瀛樿矾寰�
             val path = it?.extension1
@@ -145,4 +210,97 @@
         }
         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