package com.ffii.fpsms.modules.deliveryOrder.service

import com.ffii.fpsms.m18.entity.M18DataLogRepository
import com.ffii.fpsms.m18.service.M18DataLogService
import com.ffii.fpsms.modules.deliveryOrder.entity.DeliveryOrder
import com.ffii.fpsms.modules.deliveryOrder.entity.models.DeliveryOrderInfo
import com.ffii.fpsms.modules.deliveryOrder.enums.DeliveryOrderStatus
import com.ffii.fpsms.modules.deliveryOrder.web.models.SaveDeliveryOrderRequest
import com.ffii.fpsms.modules.deliveryOrder.web.models.SaveDeliveryOrderResponse
import com.ffii.fpsms.modules.deliveryOrder.web.models.SaveDeliveryOrderStatusRequest
import com.ffii.fpsms.modules.master.service.CurrencyService
import com.ffii.fpsms.modules.master.service.ShopService
import com.ffii.fpsms.modules.user.entity.UserRepository
import com.ffii.fpsms.modules.user.service.UserService
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.io.IOException
import kotlin.jvm.optionals.getOrDefault
import kotlin.jvm.optionals.getOrNull
import com.ffii.fpsms.modules.deliveryOrder.web.models.DoDetailResponse
import com.ffii.fpsms.modules.deliveryOrder.web.models.DoDetailLineResponse
import com.ffii.fpsms.modules.deliveryOrder.web.models.ReleaseDoRequest
import com.ffii.fpsms.modules.pickOrder.web.models.SavePickOrderRequest
import com.ffii.fpsms.modules.pickOrder.web.models.SavePickOrderLineRequest
import com.ffii.fpsms.modules.pickOrder.enums.PickOrderType
import com.ffii.fpsms.modules.pickOrder.service.PickOrderService
import com.ffii.fpsms.modules.deliveryOrder.entity.DoPickOrder
import com.ffii.fpsms.modules.deliveryOrder.entity.DoPickOrderRepository
import com.ffii.fpsms.modules.deliveryOrder.enums.DoPickOrderStatus
import java.math.BigDecimal
import com.ffii.fpsms.modules.master.web.models.MessageResponse
import java.time.LocalDateTime
import com.ffii.fpsms.modules.deliveryOrder.web.models.AssignByStoreRequest
import com.ffii.fpsms.modules.deliveryOrder.entity.DoPickOrderRecord
import com.ffii.fpsms.modules.deliveryOrder.entity.DoPickOrderRecordRepository
import com.ffii.fpsms.modules.deliveryOrder.web.models.*  // ✅ 导入
import com.ffii.fpsms.modules.pickOrder.entity.PickOrderRepository
import com.ffii.fpsms.modules.pickOrder.enums.PickOrderStatus
import com.ffii.fpsms.modules.pickOrder.entity.TruckRepository
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import com.ffii.core.support.JdbcDao
import com.ffii.fpsms.modules.pickOrder.entity.Truck
import com.ffii.fpsms.modules.deliveryOrder.entity.DoPickOrderLineRepository
import com.ffii.fpsms.modules.deliveryOrder.entity.DeliveryOrderRepository
import com.ffii.fpsms.modules.deliveryOrder.entity.DoPickOrderLineRecordRepository
import com.ffii.fpsms.modules.deliveryOrder.entity.DoPickOrderLineRecord
import org.springframework.context.annotation.Lazy
@Service
open class DoPickOrderService(
    private val doPickOrderRepository: DoPickOrderRepository,
    private val doPickOrderRecordRepository: DoPickOrderRecordRepository,
    private val userRepository: UserRepository,
    private val pickOrderRepository: PickOrderRepository,
    private val jdbcDao: JdbcDao,  // ✅ 添加这行
    private val truckRepository: TruckRepository, 
    private val doPickOrderLineRepository: DoPickOrderLineRepository,
    @Lazy private val deliveryOrderRepository: DeliveryOrderRepository,
    private val doPickOrderLineRecordRepository: DoPickOrderLineRecordRepository
) {
    fun findReleasedDoPickOrders(): List<DoPickOrder> {
        return doPickOrderRepository.findByTicketStatusIn(
            listOf(DoPickOrderStatus.released, DoPickOrderStatus.pending)
        )
    }

    fun getNextTicketNumber(datePrefix: String, storeId: String): String {
        println("🔍 DEBUG: Getting next ticket number for date prefix: $datePrefix, store: $storeId")
        try {
            val sanitizedStoreId = storeId.replace("/", "")
            val shortDatePrefix = if (datePrefix.length == 8) {
                datePrefix.substring(2)  // 从 20251016 变成 251016
            } else {
                datePrefix
            }
            // ✅ 修改搜索模式为新格式
            val searchPattern = "TI-${shortDatePrefix}-${sanitizedStoreId}-"  // T-20250915-4F-
            val todayTickets = doPickOrderRepository.findByTicketNoStartingWith(searchPattern)
            println("🔍 DEBUG: Found ${todayTickets.size} existing tickets with prefix $searchPattern")
            todayTickets.forEach { ticket ->
                println("🔍 DEBUG: Existing ticket: ${ticket.ticketNo}, Status: ${ticket.ticketStatus}")
            }
            val nextNumber = (todayTickets.size + 1).toString().padStart(3, '0')
            // ✅ 修改生成格式
            val ticketNumber = "TI-${datePrefix}-${sanitizedStoreId}-${nextNumber}"  // T-20250915-4F-001
            println("🔍 DEBUG: Generated ticket number: $ticketNumber")
            return ticketNumber
        } catch (e: Exception) {
            println("❌ ERROR in getNextTicketNumber: ${e.message}")
            e.printStackTrace()
            throw e
        }
    }

    fun save(record: DoPickOrder): DoPickOrder {
        return doPickOrderRepository.save(record)
    }

    fun findByStoreIdOrderByTruckDepartureTime(storeId: String): List<DoPickOrder> {
        return doPickOrderRepository.findByStoreIdAndTicketStatusOrderByTruckDepartureTimeAsc(
            storeId, DoPickOrderStatus.pending
        )
    }

    // Add these missing methods
    fun assignByStore(request: AssignByStoreRequest): MessageResponse {
        // TODO: Implement store-based assignment logic
        return MessageResponse(
            id = null,
            code = null,
            name = null,
            type = null,
            message = "Store-based assignment not implemented yet",
            errorPosition = null,
            entity = null
        )
    }

    fun releaseAssignedByStore(request: AssignByStoreRequest): MessageResponse {
        // TODO: Implement store-based release logic
        return MessageResponse(
            id = null,
            code = null,
            name = null,
            type = null,
            message = "Store-based release not implemented yet",
            errorPosition = null,
            entity = null
        )
    }

    // ✅ Updated method to set ticketReleaseTime when assigning order to user
    fun updateHandledByForPickOrder(pickOrderId: Long, userId: Long): List<DoPickOrder> {
        val doPickOrders = doPickOrderRepository.findByPickOrderId(pickOrderId)
        val user = userRepository.findById(userId).orElse(null)  // ✅ 改用 orElse(null)
        val handlerName = user?.name ?: "Unknown"
        doPickOrders.forEach {
            it.handledBy = userId
            it.handlerName = handlerName
            it.ticketStatus = DoPickOrderStatus.released
            it.ticketReleaseTime = LocalDateTime.now()
        }
        return doPickOrderRepository.saveAll(doPickOrders)
    }

    fun completeDoPickOrdersForPickOrder(pickOrderId: Long): List<DoPickOrder> {
        val doPickOrders = doPickOrderRepository.findByPickOrderId(pickOrderId)
        doPickOrders.forEach {
            it.ticketStatus = DoPickOrderStatus.completed
            it.ticketCompleteDateTime = LocalDateTime.now()
        }
        val savedDoPickOrders = doPickOrderRepository.saveAll(doPickOrders)
        
        // ✅ 同步更新相关的delivery_order状态
        savedDoPickOrders.forEach { doPickOrder ->
            if (doPickOrder.doOrderId != null) {
                val deliveryOrder = deliveryOrderRepository.findByIdAndDeletedIsFalse(doPickOrder.doOrderId!!)
                if (deliveryOrder != null && deliveryOrder.status != DeliveryOrderStatus.COMPLETED) {
                    deliveryOrder.status = DeliveryOrderStatus.COMPLETED
                    deliveryOrderRepository.save(deliveryOrder)
                    println("✅ Updated delivery order ${doPickOrder.doOrderId} status to completed")
                }
            }
        }
        
        return savedDoPickOrders
    }

    // ✅ New method to remove do_pick_order records when auto-assigning by store
    // ✅ 修改方法：先复制记录到record表，再删除原记录
    @Transactional
    fun removeDoPickOrdersForPickOrder(pickOrderId: Long): Int {
        val doPickOrders = doPickOrderRepository.findByPickOrderId(pickOrderId)
        var deletedCount = 0
        
        doPickOrders.forEach { doPickOrder ->
            // ✅ 第一步：复制 do_pick_order 到 do_pick_order_record
            val doPickOrderRecord = DoPickOrderRecord(
                storeId = doPickOrder.storeId?: "",
                ticketNo = doPickOrder.ticketNo?: "",
                ticketStatus = DoPickOrderStatus.completed, // 设置为completed状态
                truckId = doPickOrder.truckId,
                pickOrderId = doPickOrder.pickOrderId,
                truckDepartureTime = doPickOrder.truckDepartureTime,
                shopId = doPickOrder.shopId,
                handledBy = doPickOrder.handledBy,
                handlerName = doPickOrder.handlerName,
                doOrderId = doPickOrder.doOrderId,
                pickOrderCode = doPickOrder.pickOrderCode,
                deliveryOrderCode = doPickOrder.deliveryOrderCode,
                loadingSequence = doPickOrder.loadingSequence,
                ticketReleaseTime = doPickOrder.ticketReleaseTime,
                ticketCompleteDateTime = LocalDateTime.now(), // 设置完成时间
                truckLanceCode = doPickOrder.truckLanceCode,
                shopCode = doPickOrder.shopCode,
                shopName = doPickOrder.shopName,
                requiredDeliveryDate = doPickOrder.requiredDeliveryDate
            )
            val savedRecord = doPickOrderRecordRepository.save(doPickOrderRecord)
            println("✅ Copied do_pick_order ${doPickOrder.id} to do_pick_order_record")
            
            // ✅ 第二步：复制 do_pick_order_line 到 do_pick_order_line_record
            // ✅ 第二步：复制 do_pick_order_line 到 do_pick_order_line_record
            val doPickOrderLines = doPickOrderLineRepository.findByDoPickOrderIdAndDeletedFalse(doPickOrder.id!!)
            doPickOrderLines.forEach { line ->
                val doPickOrderLineRecord = DoPickOrderLineRecord() // ✅ 使用默认构造函数
                doPickOrderLineRecord.doPickOrderId = savedRecord.id // ✅ 设置属性
                doPickOrderLineRecord.pickOrderId = line.pickOrderId
                doPickOrderLineRecord.doOrderId = line.doOrderId
                doPickOrderLineRecord.pickOrderCode = line.pickOrderCode
                doPickOrderLineRecord.deliveryOrderCode = line.deliveryOrderCode
                doPickOrderLineRecord.status = line.status
                doPickOrderLineRecordRepository.save(doPickOrderLineRecord)
                println("✅ Copied do_pick_order_line ${line.id} to do_pick_order_line_record")
            }
            
            // ✅ 第三步：删除原始的 do_pick_order_line 记录
            if (doPickOrderLines.isNotEmpty()) {
                doPickOrderLineRepository.deleteAll(doPickOrderLines)
                println("✅ Deleted ${doPickOrderLines.size} do_pick_order_line records")
            }
            
            // ✅ 第四步：删除原始的 do_pick_order 记录
            doPickOrderRepository.delete(doPickOrder)
            deletedCount++
            println("✅ Deleted do_pick_order ${doPickOrder.id}")
        }
        
        return deletedCount
    }

    fun saveRecord(record: DoPickOrderRecord): DoPickOrderRecord {
        return doPickOrderRecordRepository.save(record)
    }

    // ✅ Add method to update DoPickOrderRecord status
    fun updateRecordHandledByForPickOrder(pickOrderId: Long, userId: Long): List<DoPickOrderRecord> {
        val doPickOrderRecords = doPickOrderRecordRepository.findByPickOrderId(pickOrderId)
        val user = userRepository.findById(userId).orElse(null)  // ✅ 改用 orElse(null)
        val handlerName = user?.name ?: "Unknown"
        doPickOrderRecords.forEach {
            it.handledBy = userId
            it.handlerName = handlerName
            it.ticketStatus = DoPickOrderStatus.released
            it.ticketReleaseTime = LocalDateTime.now()
        }
        return doPickOrderRecordRepository.saveAll(doPickOrderRecords)
    }

    // ✅ Add method to complete DoPickOrderRecord
    fun completeDoPickOrderRecordsForPickOrder(pickOrderId: Long): List<DoPickOrderRecord> {
        val doPickOrderRecords = doPickOrderRecordRepository.findByPickOrderId(pickOrderId)
        doPickOrderRecords.forEach {
            it.ticketStatus = DoPickOrderStatus.completed
            it.ticketCompleteDateTime = LocalDateTime.now()  // ✅ 设置完成时间
        }
        return doPickOrderRecordRepository.saveAll(doPickOrderRecords)
    }

    // Add method to find do_pick_order records by pick order ID
    fun findByPickOrderId(pickOrderId: Long): List<DoPickOrder> {
        return doPickOrderRepository.findByPickOrderId(pickOrderId)
    }

    fun updateDoOrderIdForPickOrder(pickOrderId: Long, doOrderId: Long): List<DoPickOrder> {
        val doPickOrders = doPickOrderRepository.findByPickOrderId(pickOrderId)
        doPickOrders.forEach {
            it.doOrderId = doOrderId
        }
        return doPickOrderRepository.saveAll(doPickOrders)
    }
    fun getSummaryByStore(storeId: String, requiredDate: LocalDate?): StoreLaneSummary {
        val targetDate = requiredDate ?: LocalDate.now()
        println("🔍 DEBUG: Getting summary for store=$storeId, date=$targetDate")
        
        // ✅ 修复格式转换：保持原始格式
        val actualStoreId = when (storeId) {
            "2/F" -> "2/F"  // ✅ 保持原格式
            "4/F" -> "4/F"  // ✅ 保持原格式
            else -> storeId
        }
        println("🔍 DEBUG: Using storeId: '$actualStoreId'")
        
        // ✅ 直接查询 do_pick_order 表
        val allRecords = doPickOrderRepository.findByStoreIdAndRequiredDeliveryDateAndTicketStatusIn(
            actualStoreId,
            targetDate,
            listOf(DoPickOrderStatus.pending, DoPickOrderStatus.released, DoPickOrderStatus.completed)
        )
    
        println("🔍 DEBUG: Found ${allRecords.size} records for date $targetDate")
        
        // ✅ 过滤掉所有 do_pick_order_line 都是 "issue" 状态的 shop
        val filteredRecords = allRecords.filter { doPickOrder ->
            val hasNonIssueLines = checkDoPickOrderHasNonIssueLines(doPickOrder.id!!)
            if (!hasNonIssueLines) {
                println("🔍 DEBUG: Filtering out DoPickOrder ${doPickOrder.id} - all lines are issues")
            }
            hasNonIssueLines
        }
        
        println("🔍 DEBUG: After filtering, ${filteredRecords.size} records remain")
        
        // ✅ 使用过滤后的记录
        val grouped = filteredRecords.groupBy { it.truckDepartureTime to it.truckLanceCode }
            .mapValues { (_, list) ->
                LaneBtn(
                    truckLanceCode = list.first().truckLanceCode ?: "",
                    unassigned = list.count { it.handledBy == null },
                    total = list.size
                )
            }
    
        val timeGroups = grouped.entries
            .groupBy { it.key.first }
            .mapValues { (_, entries) ->
                entries.map { it.value }
                    .sortedByDescending { it.unassigned }
                    .take(3)
            }
            .filterValues { lanes -> lanes.any { it.unassigned > 0 } }
            .toSortedMap(compareBy { it })
            .entries.take(4)
            .map { (time, lanes) ->
                LaneRow(
                    truckDepartureTime = time?.toString() ?: "",
                    lanes = lanes
                )
            }
    
        return StoreLaneSummary(storeId = storeId, rows = timeGroups)
    }
    private fun checkDoPickOrderHasNonIssueLines(doPickOrderId: Long): Boolean {
        try {
            // 1. 获取该 do_pick_order 的所有 do_pick_order_line 数量
            val totalLinesSql = """
                SELECT COUNT(*) as total_lines
                FROM fpsmsdb.do_pick_order_line dpol
                WHERE dpol.do_pick_order_id = :doPickOrderId 
                AND dpol.deleted = 0
            """.trimIndent()
            
            val totalLinesResult = jdbcDao.queryForList(totalLinesSql, mapOf("doPickOrderId" to doPickOrderId))
            val totalLines = (totalLinesResult.firstOrNull()?.get("total_lines") as? Number)?.toInt() ?: 0
            
            if (totalLines == 0) {
                return true  // 没有 lines，不算过滤
            }
            
            // 2. 获取非 "issue" 状态的 lines 数量
            val nonIssueLinesSql = """
                SELECT COUNT(*) as non_issue_lines
                FROM fpsmsdb.do_pick_order_line dpol
                WHERE dpol.do_pick_order_id = :doPickOrderId 
                AND dpol.deleted = 0
                AND (dpol.status IS NULL OR dpol.status != 'issue')
            """.trimIndent()
            
            val nonIssueLinesResult = jdbcDao.queryForList(nonIssueLinesSql, mapOf("doPickOrderId" to doPickOrderId))
            val nonIssueLines = (nonIssueLinesResult.firstOrNull()?.get("non_issue_lines") as? Number)?.toInt() ?: 0
            
            // 3. 只有当所有 lines 都是 "issue" 状态时才过滤掉
            val hasNonIssueLines = nonIssueLines > 0
            
            println("🔍 DEBUG: DoPickOrder $doPickOrderId - Total lines: $totalLines, Non-issue lines: $nonIssueLines, Has non-issue lines: $hasNonIssueLines")
            
            return hasNonIssueLines
            
        } catch (e: Exception) {
            println("❌ Error checking non-issue lines for do_pick_order $doPickOrderId: ${e.message}")
            return true  // 出错时不过滤
        }
    }
    // ✅ 修复：把 assignByLane 移到类里面
    fun assignByLane(request: AssignByLaneRequest): MessageResponse {
        val user = userRepository.findById(request.userId).orElse(null)
            ?: return MessageResponse(
                id = null, code = "USER_NOT_FOUND", name = null, type = null,
                message = "User not found", errorPosition = null, entity = null
            )
    
        // ✅ 转换 storeId 格式：'2/F' -> '2F/F', '4/F' -> '4F/F'
        val actualStoreId = when (request.storeId) {
            "2/F" -> "2/F"  // ✅ 保持原格式
            "4/F" -> "4/F"  // ✅ 保持原格式
            else -> request.storeId
        }
        println("🔍 DEBUG: assignByLane - Converting storeId from '${request.storeId}' to '$actualStoreId'")
    
        val candidates = doPickOrderRepository
            .findByStoreIdAndTicketStatusOrderByTruckDepartureTimeAsc(
                actualStoreId,
                DoPickOrderStatus.pending
            )
            .filter { it.truckLanceCode == request.truckLanceCode }
    
        if (candidates.isEmpty()) {
            return MessageResponse(
                id = null, code = "NO_ORDERS", name = null, type = null,
                message = "No available pick order(s) for this lane.", errorPosition = null, entity = null
            )
        }
    
        val firstOrder = candidates.first()
        
        // ✅ 更新 do_pick_order
        firstOrder.handledBy = request.userId
        firstOrder.handlerName = user.name
        firstOrder.ticketStatus = DoPickOrderStatus.released
        firstOrder.ticketReleaseTime = LocalDateTime.now()
        doPickOrderRepository.save(firstOrder)
    
        // ✅ 关键修改：获取这个 do_pick_order 下的所有 pick orders 并分配给用户
        val doPickOrderLines = doPickOrderLineRepository.findByDoPickOrderIdAndDeletedFalse(firstOrder.id!!)
        println("🔍 DEBUG: Found ${doPickOrderLines.size} pick orders in do_pick_order ${firstOrder.id}")
        
        doPickOrderLines.forEach { line ->
            if (line.pickOrderId != null) {
                val pickOrder = pickOrderRepository.findById(line.pickOrderId!!).orElse(null)
                if (pickOrder != null) {
                    pickOrder.assignTo = user
                    pickOrder.status = PickOrderStatus.RELEASED
                    pickOrderRepository.save(pickOrder)
                    println("🔍 DEBUG: Assigned pick order ${line.pickOrderId} to user ${request.userId}")
                } else {
                    println("⚠️ WARNING: Pick order ${line.pickOrderId} not found")
                }
            }
        }
    
        // ✅ 同步更新 do_pick_order_record（如果有的话）
        doPickOrderLines.forEach { line ->
            if (line.pickOrderId != null) {
                val records = doPickOrderRecordRepository.findByPickOrderId(line.pickOrderId!!)
                records.forEach { record ->
                    record.handledBy = request.userId
                    record.handlerName = user.name
                    record.ticketStatus = DoPickOrderStatus.released
                    record.ticketReleaseTime = LocalDateTime.now()
                }
                if (records.isNotEmpty()) {
                    doPickOrderRecordRepository.saveAll(records)
                    println("🔍 DEBUG: Updated ${records.size} do_pick_order_record for pick order ${line.pickOrderId}")
                }
            }
        }
    
        return MessageResponse(
            id = firstOrder.id,
            code = "SUCCESS",
            name = null,
            type = null,
            message = "Assigned ${doPickOrderLines.size} pick order(s) from lane ${request.truckLanceCode}",
            errorPosition = null,
            entity = mapOf(
                "doPickOrderId" to firstOrder.id,
                "ticketNo" to firstOrder.ticketNo,
                "numberOfPickOrders" to doPickOrderLines.size,
                "pickOrderIds" to doPickOrderLines.mapNotNull { it.pickOrderId }
            )
        )
    }

    // 在 DoPickOrderService 类中添加这个方法
    open fun determineStoreId(doOrderId: Long): String {
        val sql = """
        SELECT 
            dol.deliveryOrderId,
            w.store_id,
            SUM(ill.inQty - COALESCE(ill.outQty, 0) - COALESCE(ill.holdQty, 0)) AS total_inventory
        FROM fpsmsdb.delivery_order_line dol
        LEFT JOIN fpsmsdb.inventory_lot il ON il.itemId = dol.itemId AND il.deleted = 0
        LEFT JOIN fpsmsdb.inventory_lot_line ill ON ill.inventoryLotId = il.id AND ill.deleted = 0
        LEFT JOIN fpsmsdb.warehouse w ON w.id = ill.warehouseId AND w.deleted = 0
        WHERE dol.deleted = 0 AND dol.deliveryOrderId = :doOrderId
        GROUP BY dol.deliveryOrderId, w.store_id
        ORDER BY total_inventory DESC
        LIMIT 1
    """.trimIndent()

        val results = jdbcDao.queryForList(sql, mapOf("doOrderId" to doOrderId))
        return if (results.isNotEmpty()) {
            val storeId = results.first()["store_id"] as? String ?: "2F"
            when (storeId) {
                "2F" -> "2/F"
                "4F" -> "4/F"
                "3F" -> "3/F"
                else -> "2/F"
            }
        } else {
            "2/F" // 默认值
        }
    }

    open fun selectTruck(shopId: Long?, storeId: String): Truck? {
        if (shopId == null) return null

        val storeIdInt = when (storeId) {
            "2/F" -> 2
            "4/F" -> 4
            "3/F" -> 3
            else -> 2
        }

        val trucks = truckRepository.findByShopIdAndDeletedFalse(shopId)
        return trucks.find { it.storeId == storeIdInt } ?: trucks.firstOrNull()
    }

    open fun finishDoPickOrder(doPickOrderId: Long): MessageResponse {
        val doPickOrder = doPickOrderRepository.findById(doPickOrderId).orElse(null)
            ?: return MessageResponse(
                id = null,
                code = "NOT_FOUND",
                name = null,
                type = null,
                message = "DO Pick Order not found",
                errorPosition = null,
                entity = null
            )

        try {
            // 确定 store_id 和 truck
            val storeId = determineStoreId(doPickOrder.doOrderId ?: 0L)
            val truck = selectTruck(doPickOrder.shopId, storeId)

            // 生成 ticket 编号
            val targetDate = doPickOrder.requiredDeliveryDate ?: LocalDate.now()
            val datePrefix = targetDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"))
            val ticketNumber = getNextTicketNumber(datePrefix, storeId)

            // 更新 do_pick_order
            doPickOrder.storeId = storeId
            doPickOrder.ticketNo = ticketNumber
            doPickOrder.truckId = truck?.id
            doPickOrder.truckDepartureTime = truck?.departureTime
            doPickOrder.truckLanceCode = truck?.truckLanceCode

            val updatedDoPickOrder = doPickOrderRepository.save(doPickOrder)

            // 创建 do_pick_order_record
            val doPickOrderRecord = DoPickOrderRecord(
                storeId = storeId,
                ticketNo = ticketNumber,
                ticketStatus = DoPickOrderStatus.pending,
                truckId = truck?.id,
                pickOrderId = doPickOrder.pickOrderId,
                truckDepartureTime = truck?.departureTime,
                shopId = doPickOrder.shopId,
                handledBy = null,
                doOrderId = doPickOrder.doOrderId,
                pickOrderCode = doPickOrder.pickOrderCode,
                deliveryOrderCode = doPickOrder.deliveryOrderCode,
                loadingSequence = doPickOrder.loadingSequence,
                truckLanceCode = truck?.truckLanceCode,
                shopCode = doPickOrder.shopCode,
                shopName = doPickOrder.shopName,
                requiredDeliveryDate = targetDate
            )

            doPickOrderRecordRepository.save(doPickOrderRecord)

            return MessageResponse(
                id = updatedDoPickOrder.id,
                code = "SUCCESS",
                name = null,
                type = null,
                message = "DO Pick Order finished successfully",
                errorPosition = null,
                entity = mapOf("ticketNo" to ticketNumber)
            )
        } catch (e: Exception) {
            return MessageResponse(
                id = null,
                code = "ERROR",
                name = null,
                type = null,
                message = "Error finishing DO Pick Order: ${e.message}",
                errorPosition = null,
                entity = null
            )
        }
    }
}// ✅ 类结束