package com.yice.webadmin.app.model;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.yice.common.core.annotation.RelationDict;
import com.yice.common.core.annotation.RelationOneToOne;
import com.yice.common.core.base.mapper.BaseModelMapper;
import com.yice.common.core.base.model.BaseModel;
import com.yice.webadmin.app.vo.ModelTaskVo;
import com.yice.webadmin.upms.model.SysUser;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.Date;
import java.util.Map;

/**
 * ModelTask实体对象。
 *
 * @author linking
 * @date 2023-04-13
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName(value = "lmp_model_task")
public class ModelTask extends BaseModel {

    /**
     * 任务ID。
     */
    @TableId(value = "task_id")
    private Long taskId;

    /**
     * 模型ID。
     */
    private Long modelId;

    /**
     * 版本ID。
     */
    private Long versionId;

    /**
     * 版本名称。
     */
    private String versionName;

    /**
     * 任务类型。
     */
    private Integer taskType;

    /**
     * 任务状态。
     */
    private Integer taskStatus;

    /**
     * 完成时间。
     */
    private Date completeTime;

    /**
     * 模型版本号。
     */
    private Integer modelVersion;

    @RelationOneToOne(
            masterIdField = "taskId",
            slaveModelClass = ModelEstimate.class,
            slaveIdField = "taskId")
    @TableField(exist = false)
    private ModelEstimate modelEstimate;

    @RelationOneToOne(
            masterIdField = "taskId",
            slaveModelClass = ModelCompress.class,
            slaveIdField = "taskId")
    @TableField(exist = false)
    private ModelCompress modelCompress;

    /**
     * 创建人名称字典。
     */
    @RelationDict(
            masterIdField = "createUserId",
            slaveModelClass = SysUser.class,
            slaveIdField = "userId",
            slaveNameField = "showName")
    @TableField(exist = false)
    private Map<String, Object> createUserIdDictMap;

    @Mapper
    public interface ModelTaskModelMapper extends BaseModelMapper<ModelTaskVo, ModelTask> {
        /**
         * 转换Vo对象到实体对象。
         *
         * @param modelTaskVo 域对象。
         * @return 实体对象。
         */
        @Mapping(target = "modelEstimate", expression = "java(mapToBean(modelTaskVo.getModelEstimate(), com.yice.webadmin.app.model.ModelEstimate.class))")
        @Mapping(target = "modelCompress", expression = "java(mapToBean(modelTaskVo.getModelCompress(), com.yice.webadmin.app.model.ModelCompress.class))")
        @Override
        ModelTask toModel(ModelTaskVo modelTaskVo);

        /**
         * 转换实体对象到VO对象。
         *
         * @param modelTask 实体对象。
         * @return 域对象。
         */
        @Mapping(target = "modelEstimate", expression = "java(beanToMap(modelTask.getModelEstimate(), false))")
        @Mapping(target = "modelCompress", expression = "java(beanToMap(modelTask.getModelCompress(), false))")
        @Override
        ModelTaskVo fromModel(ModelTask modelTask);
    }

    public static final ModelTaskModelMapper INSTANCE = Mappers.getMapper(ModelTaskModelMapper.class);
}
