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

import com.ffii.fpsms.modules.deliveryOrder.entity.*
import com.ffii.fpsms.modules.deliveryOrder.enums.DeliveryOrderStatus
import com.ffii.fpsms.modules.deliveryOrder.enums.DoPickOrderStatus
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime

@Service
open class DoPickOrderCompletionService(
    private val doPickOrderRepository: DoPickOrderRepository,
    private val doPickOrderRecordRepository: DoPickOrderRecordRepository,
    private val doPickOrderLineRepository: DoPickOrderLineRepository,
    private val doPickOrderLineRecordRepository: DoPickOrderLineRecordRepository,
    private val deliveryOrderRepository: DeliveryOrderRepository
) {
    
    @Transactional
    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
    }
    /* 
    @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,
                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
            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
    }
    */
}