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.RelationOneToMany;
import com.yice.common.core.annotation.RelationOneToManyAggregation;
import com.yice.common.core.base.mapper.BaseModelMapper;
import com.yice.common.core.base.model.BaseModel;
import com.yice.common.core.constant.AggregationType;
import com.yice.common.core.util.MyCommonUtil;
import com.yice.webadmin.app.vo.TuningTaskVo;
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.List;
import java.util.Map;

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

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

    /**
     * 任务名称。
     */
    private String taskName;

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

    /**
     * 任务描述。
     */
    private String taskDescribe;

    /**
     * task_name LIKE搜索字符串。
     */
    @TableField(exist = false)
    private String searchString;

    public void setSearchString(String searchString) {
        this.searchString = MyCommonUtil.replaceSqlWildcard(searchString);
    }

    /**
     * 运行数量 (一对多聚合计算字段)。
     */
    @RelationOneToManyAggregation(
            masterIdField = "taskId",
            slaveModelClass = TuningRun.class,
            slaveIdField = "taskId",
            aggregationType = AggregationType.COUNT,
            aggregationField = "taskId")
    @TableField(exist = false)
    private Integer runCount = 0;
    /**
     * TuningRun 的一对多关联表数据对象。
     * 通常在一对多的关联中，我们基于从表数据过滤主表数据，此时需要先对从表数据进行嵌套子查询过滤，并将从表过滤数据列表集成到该字段。
     */
    @RelationOneToMany(
            masterIdField = "taskId",
            slaveModelClass = TuningRun.class,
            slaveIdField = "taskId")
    @TableField(exist = false)
    private List<TuningRun> tuningRunList;

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

    @Mapper
    public interface TuningTaskModelMapper extends BaseModelMapper<TuningTaskVo, TuningTask> {
        /**
         * 转换Vo对象到实体对象。
         *
         * @param tuningTaskVo 域对象。
         * @return 实体对象。
         */
        @Mapping(target = "tuningRunList", expression = "java(mapToBean(tuningTaskVo.getTuningRunList(), com.yice.webadmin.app.model.TuningRun.class))")
        @Override
        TuningTask toModel(TuningTaskVo tuningTaskVo);

        /**
         * 转换实体对象到VO对象。
         *
         * @param tuningTask 实体对象。
         * @return 域对象。
         */
        @Mapping(target = "tuningRunList", expression = "java(beanToMap(tuningTask.getTuningRunList(), false))")
        @Override
        TuningTaskVo fromModel(TuningTask tuningTask);
    }

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