feiyu02
9 天以前 85ef942e7195abeb71466b7159c3ee30161e1e54
src/main/kotlin/cn/flightfeather/supervision/lightshare/service/impl/MediafileServiceImpl.kt
@@ -1,16 +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
@@ -20,7 +24,15 @@
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
@@ -30,6 +42,8 @@
    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 {
@@ -63,8 +77,8 @@
            throw BizException("巡查记录id和巡查任务id至少填写其中一个")
        }
        sceneId ?: throw BizException("记录对应的场景不存在")
        val sceneInfo = sceneRep.findScene(sceneId = sceneId)
        val mediaFileTypeList = Constant.MediaFileType.getList(sceneInfo?.typeid).map { it.value }
        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)
    }
@@ -96,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)
@@ -106,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
                }
@@ -196,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
    }
}