// FPSMS-backend/src/main/java/com/ffii/fpsms/modules/pickOrder/entity/PickExecutionIssue.kt
package com.ffii.fpsms.modules.pickOrder.entity

import jakarta.persistence.*
import java.math.BigDecimal
import java.time.LocalDate
import java.time.LocalDateTime

@Entity
@Table(name = "pick_execution_issue")
class PickExecutionIssue(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Long? = null,

    @Column(name = "pick_order_id", nullable = true)
    val pickOrderId: Long? = null,

    @Column(name = "pick_order_code", length = 50, nullable = false)
    val pickOrderCode: String,

    @Column(name = "pick_order_create_date")
    val pickOrderCreateDate: LocalDate? = null,

    @Column(name = "pick_execution_date")
    val pickExecutionDate: LocalDate? = null,

    @Column(name = "Jo_pick_order_id")
val joPickOrderId: Long? = null,

@Column(name = "Do_pick_order_id")
val doPickOrderId: Long? = null,
    @Column(name = "pick_order_line_id", nullable = true)
    val pickOrderLineId: Long? = null,
    @Column(name = "issue_no", length = 50)
    val issueNo: String? = null,

    @Enumerated(EnumType.STRING)
    @Column(name = "issue_category")
    val issueCategory: IssueCategory = IssueCategory.lot_issue,
    @Column(name = "item_id", nullable = false)
    val itemId: Long,

    @Column(name = "item_code", length = 50)
    val itemCode: String? = null,

    @Column(name = "item_description", length = 255)
    val itemDescription: String? = null,

    @Column(name = "lot_id")
    val lotId: Long? = null,

    @Column(name = "lot_no", length = 50)
    val lotNo: String? = null,

    @Column(name = "store_location", length = 100)
    val storeLocation: String? = null,

    @Column(name = "required_qty", precision = 10, scale = 2)
    val requiredQty: BigDecimal? = null,

    @Column(name = "actual_pick_qty", precision = 10, scale = 2)
    val actualPickQty: BigDecimal? = null,

    @Column(name = "miss_qty", precision = 10, scale = 2)
    val missQty: BigDecimal = BigDecimal.ZERO,

    @Column(name = "bad_item_qty", precision = 10, scale = 2)
    val badItemQty: BigDecimal = BigDecimal.ZERO,
    @Column(name = "book_qty", precision = 10, scale = 2)
    val bookQty: BigDecimal = BigDecimal.ZERO,
    @Column(name = "issue_qty", precision = 10, scale = 2)
    val issueQty: BigDecimal = BigDecimal.ZERO,

    @Column(name = "issue_remark", columnDefinition = "TEXT")
    val issueRemark: String? = null,

    @Column(name = "picker_name", length = 100)
    val pickerName: String? = null,

    @Enumerated(EnumType.STRING)
    @Column(name = "handle_status")
    val handleStatus: HandleStatus = HandleStatus.pending,

    @Column(name = "handle_date")
    val handleDate: LocalDate? = null,

    @Column(name = "handled_by")
    val handledBy: Long? = null,

    @Column(name = "created", nullable = false)
    val created: LocalDateTime = LocalDateTime.now(),

    @Column(name = "createdBy", length = 30)
    val createdBy: String? = null,

    @Version
    @Column(name = "version", nullable = false)
    val version: Int = 0,

    @Column(name = "modified", nullable = false)
    val modified: LocalDateTime = LocalDateTime.now(),

    @Column(name = "modifiedBy", length = 30)
    val modifiedBy: String? = null,

    @Column(name = "deleted", nullable = false)
    val deleted: Boolean = false
) {
    //  添加默认构造函数
    constructor() : this(
        pickOrderId = null,
        pickOrderCode = "",
        pickOrderCreateDate = null,
        pickExecutionDate = null,
        pickOrderLineId = null,
        itemId = 0L,
        itemCode = null,
        itemDescription = null,
        lotId = null,
        issueNo = null,
        issueCategory = IssueCategory.lot_issue, 
        lotNo = null,
        storeLocation = null,
        requiredQty = null,
        actualPickQty = null,
        bookQty = BigDecimal.ZERO,
        issueQty = BigDecimal.ZERO,
        missQty = BigDecimal.ZERO,
        badItemQty = BigDecimal.ZERO,
        issueRemark = null,
        pickerName = null,
        handleStatus = HandleStatus.pending,
        handleDate = null,
        handledBy = null,
        created = LocalDateTime.now(),
        createdBy = null,
        version = 0,
        modified = LocalDateTime.now(),
        modifiedBy = null,
        deleted = false
    )
}

enum class HandleStatus {
    pending,
    completed
}
enum class IssueCategory {
    lot_issue,           // 正常 Issue Button 提交
    resuggest_issue,     // Resuggest 失败
    match_issue ,
    warehouse_issue,
    truck_issue // Matching 问题
}