package cn.lihongjie.coal.deviceWorkOrder.entity;

import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import cn.lihongjie.coal.device.entity.DeviceEntity;
import cn.lihongjie.coal.deviceWorkOrderDetail.entity.DeviceWorkOrderDetailEntity;
import cn.lihongjie.coal.user.entity.UserEntity;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;

import lombok.Data;

import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.Formula;

import java.time.LocalDateTime;
import java.util.List;

@Data
@Entity
public class DeviceWorkOrderEntity extends OrgCommonEntity {



    @Comment("提单人名称")
    private String proposerName;

    @Comment("提单人联系方式")
    private String proposerContact;


    @ManyToOne
    private DeviceEntity device;

    @Comment("工单时间")
    private LocalDateTime time;

    @Comment("工单类型")
    private String type;

    @Formula(
            "(select i.name\n"
                    + "from t_dictionary d,\n"
                    + "     t_dictionary_item i\n"
                    + "where d.id = i.dictionary_id\n"
                    + "  and d.code = 'device.workOrder.type'\n"
                    + "  and i.code = type)")
    private String typeName;

    @Comment("工单状态")
    private String orderStatus;

    @Formula(
            "(select i.name\n"
                    + "from t_dictionary d,\n"
                    + "     t_dictionary_item i\n"
                    + "where d.id = i.dictionary_id\n"
                    + "  and d.code = 'device.workOrder.status'\n"
                    + "  and i.code = order_status)")
    private String orderStatusName;

    @Comment("归档状态")
    @ColumnDefault("'0'")
    private String archiveStatus = "0";

    @Formula(
            "(select i.name\n"
                    + "from t_dictionary d,\n"
                    + "     t_dictionary_item i\n"
                    + "where d.id = i.dictionary_id\n"
                    + "  and d.code = 'archiveStatus'\n"
                    + "  and i.code = archive_status)")
    private String archiveStatusName;


    @OneToMany(mappedBy = "workOrder", cascade = {CascadeType.ALL})
    private List<DeviceWorkOrderDetailEntity> details;


    @OneToMany
    private List<UserEntity> workers;

}
