package dev.zhub.mk.qtask;

import lombok.Getter;
import lombok.Setter;
import net.tccn.base.BaseEntity;
import net.tccn.base.Kv;
import net.tccn.base.Utils;
import net.tccn.qtask.Task;
import org.redkale.annotation.Comment;
import org.redkale.convert.json.JsonConvert;
import org.redkale.persistence.Column;
import org.redkale.persistence.Entity;
import org.redkale.persistence.Id;

import java.util.Map;

@Getter
@Setter
@Entity
public class QTask extends BaseEntity {

    @Id
    @Comment("[记录ID]")
    private Integer qtaskid;

    @Comment("[查询默认参数]")
    private String para = "{}";

    @Comment("[CATALOG]")
    private String catalog = "";

    @Comment("[数据平台ID]")
    private int dbid;

    @Comment("[标识名称KEY]")
    // @Column(updatable = false)
    private String name = "";

    @Comment("[所属平台ID]")
    @Column(updatable = false)
    private Integer platid;

    @Comment("[业务名称]")
    private String title = "";

    @Comment("[SQL模板]")
    private String content;

    @Comment("[备注]")
    private String remark = "";

    @Comment("[状态]10正常，80删除")
    private int status = 10;

    public Task createTask(Kv<String, String> para) {
        // TODO :  QTask 作为 Task 的引用值，省去创建重复 Task 属性
        Task task = new Task();
        task.setName(name);
        task.setTitle(title);
        task.setContent(content);
        task.setDbid(dbid);
        task.setCatalog(catalog);

        Kv _para = Kv.of();
        if (!Utils.isEmpty(this.para)) {
            try {
                Map<String, String> map = JsonConvert.root().convertFrom(JsonConvert.TYPE_MAP_STRING_STRING, this.para);
                _para.putAll(map);
            } catch (Exception e) {
                new IllegalArgumentException(String.format("fromJson error:[%s]", this.para), e);
            }
        }
        _para.putAll(para); // 填充传入的参数，优先级最高
        task.setPara(_para);
        return task;
    }
}
