src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/LedgerServiceImpl.kt
@@ -11,6 +11,7 @@
import cn.flightfeather.supervision.domain.repository.LedgerMediaFileRep
import cn.flightfeather.supervision.domain.repository.LedgerRep
import cn.flightfeather.supervision.domain.repository.UserInfoRep
import cn.flightfeather.supervision.domain.repository.UserSettingRep
import cn.flightfeather.supervision.infrastructure.utils.DateUtil
import cn.flightfeather.supervision.infrastructure.utils.FileUtil
import cn.flightfeather.supervision.infrastructure.utils.UUIDGenerator
@@ -18,9 +19,11 @@
import cn.flightfeather.supervision.lightshare.vo.*
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.github.pagehelper.PageHelper
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.multipart.MultipartFile
import tk.mybatis.mapper.entity.Example
import java.io.File
@@ -43,6 +46,7 @@
    private val ledgerMediaFileRep: LedgerMediaFileRep,
    private val userInfoRep: UserInfoRep,
    private val ledgerRep: LedgerRep,
    private val userSettingRep: UserSettingRep,
) : LedgerService {
    @Value("\${imgPath}")
@@ -95,6 +99,7 @@
                sceneType = it.getlScenetype(),
                iconUrl = it.getlIconurl(),
                realTime = it.getlRealTime(),
                copy = it.getlAutoCopy() ?: false,
                description = it.getlDescription(),
                notRelated = it.getlNotRelatedSwitch() ?: true,
                multigroup = it.getlMultiGroup() ?: false
@@ -186,6 +191,9 @@
                ledgerType = type?.get(0)?.getlTypename()
                ledgerFinished = true
                isUpLoad = true
                this.year = it.lrYear
                this.month = it.lrMonth
                this.day = it.lrDay
                updateDate = it.lrSubmitdate
                updateType = it.lrUpdatetype
                fileType = it.lrEasubmitkind.toInt()
@@ -246,6 +254,9 @@
                ledgerType = type?.get(0)?.getlTypename()
                ledgerFinished = true
                isUpLoad = true
                this.year = it.lrYear
                this.month = it.lrMonth
                this.day = it.lrDay
                updateDate = it.lrSubmitdate
                updateType = it.lrUpdatetype
                fileType = it.lrEasubmitkind.toInt()
@@ -296,11 +307,21 @@
        return true
    }
    override fun uploadLedger(userId: String, ledgerVoList: String, files: Array<MultipartFile>): Boolean {
    @Transactional
    override fun uploadLedger(
        userId: String, ledgerVoList: String,
        fileNames: String?, files: Array<MultipartFile>,
    ): Boolean {
        val mapper = ObjectMapper()
        val ledgerVos = mapper.readValue<List<LedgerVo>>(ledgerVoList, object : TypeReference<List<LedgerVo>>() {})
        val ledgerVos = mapper.readValue(ledgerVoList, object : TypeReference<List<LedgerVo>>() {})
        val fileNamePair = if (fileNames.isNullOrBlank()) {
            emptyList()
        } else {
            mapper.readValue(fileNames, object : TypeReference<List<Map<String, String>>>() {})?.map {
                it["first"] to it["second"]
            }
        }
        ledgerVos.forEach {
            it.id = it.id ?: UUIDGenerator.generate16ShortUUID()
@@ -347,8 +368,14 @@
                val record = tmp[0]
                it.id = record.lrGuid
                ledgerMedia = deleteLedgerFile(record.lrGuid)
                // 去除不涉及标志或复制标志
                record.lrExtension2 = null
                record.apply {
                    lrYear = updateYear
                    lrMonth = updateMonth.toByte()
                    lrDay = updateDay.toByte()
                    lrSubmitdate = Date()
                    // 去除不涉及标志或复制标志
                    lrExtension2 = null
                }
                ledgerRecordMapper.updateByPrimaryKey(record)
            }
@@ -356,9 +383,9 @@
            var picPath = ""
            val time = DateUtil.DateToString(Date(), DateUtil.DateStyle.YYYY_MM)
            files.forEach { file ->
                val fileName = file.originalFilename
                //TODO 此处的文件路径需要修改为动态配置
//                val basePath = "D:/02product/05ledger/images/"
                var fileName = file.originalFilename
                // 根据传入的文件配队信息获取原始文件名
                fileNamePair?.find { p-> p.first == file.originalFilename }?.let { p-> fileName = p.second }
                val basePath = imgPath
                val path = "$time/$userId/${it.ledgerName}/"
                picPath += if (picPath.isEmpty()) {
@@ -509,6 +536,7 @@
        sceneType: Int,
        time: String,
    ): List<LedgerRecord> {
        // 查询相关的所有台账类型
        val ledgerSubTypes = ledgerSubTypeMapper.selectByExample(Example(LedgerSubType::class.java).apply {
            if (ledgerSubTypeId != null) {
                createCriteria().andEqualTo("lsSubtypeid", ledgerSubTypeId)
@@ -516,6 +544,17 @@
                createCriteria().andEqualTo("lScenetype", sceneType)
            }
        })
        // 2026.1.20 新增自巡查承诺后,自巡查的有效周期延长至一年(当年内)
        val promised = userSettingRep.checkIsSelfPatrolPromised(userId)
        if (promised) {
            ledgerSubTypes.onEach {
                if (it.getlTypeid() == -1) {
                    it.setlPeriod(12)
                }
            }
        }
        val c = Calendar.getInstance().apply { this.time = DateUtil.StringToDate(time) }
        val year = c.get(Calendar.YEAR)
        val month = c.get(Calendar.MONTH) + 1
@@ -529,10 +568,11 @@
        }
        val records = mutableListOf<LedgerRecord>()
        map.forEach { (p, v) ->
            // 按照周期分组,统一查询相同周期的所有台账
            // FIXME: 2020/11/10  此处根据周期和当前月份计算得到当前月份所在周期的始末月,只适用于周期小于等于12个月的情况。后续待修改
            val startMon = ceil(month.toDouble() / p).toInt().minus(1).times(p).plus(1)
            var endMon = startMon + p - 1
            if (endMon > month) endMon = month
//            if (endMon > month) endMon = month
            val r = ledgerRecordMapper.selectByExample(Example(LedgerRecord::class.java).apply {
                createCriteria().andEqualTo("lrSubmitid", userId)
                    .andEqualTo("lrYear", year)
@@ -540,6 +580,7 @@
                    .andLessThanOrEqualTo("lrMonth", endMon.toByte())
                    .andIn("lsSubtypeid", v)
            })
            // 筛选每种台账记录中的最新一条
            val monMap = mutableMapOf<Int, LedgerRecord>()
            r.forEach {
                if (monMap.containsKey(it.lsSubtypeid)) {
@@ -560,8 +601,8 @@
    }
    override fun copyLedger(userId: String, time: String, copyLedgerList: List<CopyLedgerVo>): BaseResponse<String> {
        val year = time.split("-")[0]
        val month = time.split("-")[1]
        val year = time.split("-")[0].toIntOrNull() ?: return BaseResponse(false, "年份格式错误")
        val month = time.split("-")[1].toIntOrNull() ?: return BaseResponse(false, "月份格式错误")
        val date = DateUtil.StringToDate(time, DateUtil.DateStyle.YYYY_MM)
        copyLedgerList.forEach {
            //去重判断
@@ -575,14 +616,17 @@
                return@forEach
            }
            val y = it.time?.split("-")?.get(0) ?: return@forEach
            val m = it.time?.split("-")?.get(1) ?: return@forEach
            val y = it.time?.split("-")?.get(0)
            val m = it.time?.split("-")?.get(1)
            ledgerRecordMapper.selectByExample(Example(LedgerRecord::class.java).apply {
                createCriteria().andEqualTo("lrSubmitid", userId)
                    .andEqualTo("lsSubtypeid", it.subTypeId)
                    .andEqualTo("lrYear", y)
                    .andEqualTo("lrMonth", m)
            }).forEach record@{ lr ->
                orderBy("lrYear").desc()
                    .orderBy("lrMonth").desc()
                    .orderBy("lrSubmitdate").desc()
            }).takeIf { list-> list.isNotEmpty() }?.get(0)?.let record@{ lr ->
                //获取记录对应的文件信息
                val fileList = ledgerMediaFileMapper.selectByExample(Example(LedgerMediaFile::class.java).apply {
                    createCriteria().andEqualTo("lrGuid", lr.lrGuid)