| | |
| | | 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 |
| | |
| | | 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) |
| | | |
| | |
| | | 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和巡查任务id至少填写其中一个") |
| | | } |
| | | 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 |
| | | } |
| | | |
| | | //新增媒体文件 |
| | | override fun addMedifile(mediafileVoList: String, files: Array<MultipartFile>) { |
| | | logging.info("上传文件方法调用") |
| | | logging.info("mediafileVoList:" + mediafileVoList) |
| | | // logging.info("上传文件方法调用") |
| | | // logging.info("mediafileVoList:" + mediafileVoList) |
| | | val mapper = ObjectMapper() |
| | | |
| | | //json转object |
| | | 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) |
| | | |
| | |
| | | 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 |
| | | } |
| | |
| | | |
| | | |
| | | //根据业务类型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) |
| | |
| | | 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) |
| | | |
| | |
| | | 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 |
| | | } |
| | | } |