package net.tccn.qtask;

import dev.zhub.mk.qtask.QTask;
import net.tccn.base.BaseService;
import net.tccn.base.JBean;
import org.redkale.net.http.RestBody;
import org.redkale.net.http.RestHeader;
import org.redkale.net.http.RestParam;
import org.redkale.net.http.RestService;
import org.redkale.service.RetResult;
import org.redkale.source.ColumnValue;
import org.redkale.source.FilterNode;
import org.redkale.source.Flipper;
import org.redkale.util.Comment;
import org.redkale.util.Sheet;

@RestService(automapping = true)
public class _QtaskService extends BaseService {

    @Comment("qtask列表")
    public RetResult<Sheet<QTask>> list(Flipper flipper, @RestHeader(name = "plattoken") String token) {
        flipper.sort("qtaskid DESC");
        /*if (task == null) {
            task = new DbTask();
        }
        task.setplatid(platId(token));


        PageBean<DbTask> page = DbTask.dao.findPage(task, flipper);*/

        FilterNode node = FilterNode.create("platid", platId(token)).and("status", 10);
        Sheet<QTask> sheet = metaSource.querySheet(QTask.class, flipper, node);
        return RetResult.success(sheet);
    }

    public RetResult create(@RestHeader(name = "plattoken") String token,
                            @RestBody QTask task) {

        FilterNode node = FilterNode.create("name", task.getName()).and("platid", platId(token));
        if (metaSource.exists(QTask.class, node)) {
            return retError("qtask 标识不能重复");
        }
        task.setPlatid(platId(token));

        metaSource.insert(task);

        // 刷新缓存
        metaKit.reloadxAsync(QTask.class);
        return render();
    }

    public RetResult update(@RestHeader(name = "plattoken") String token,
                            @RestBody QTask task) {

        metaSource.update(task);

        // 刷新缓存
        metaKit.reloadxAsync(QTask.class);
        return render();
    }

    public RetResult delete(@RestHeader(name = "plattoken") String token,
                            @RestBody QTask task) {

        metaSource.updateColumn(QTask.class, task.getQtaskid(), ColumnValue.create("status", 80));

        // 刷新缓存
        metaKit.reloadxAsync(QTask.class);
        return render();
    }



    /*@Comment("qtask保存")
    public JBean save(DbTask task, @RestParam(name = "plattoken") String token) {
        JBean jBean = new JBean();

        do {
            if (isEmpty(task.getName())) {
                jBean.set(-1, "任务标识码不能为空");
                break;
            }

            // 同平台name 唯一校验
            DbTask bean = new DbTask();
            bean.setplatid(platId(token));
            bean.setName(task.getName());

            DbTask entity = DbTask.dao.findFirst(bean);
            if (entity != null && !entity.getKey().equals(task.getKey())) {
                jBean.set(-1, "任务标识码被占用");
                break;
            }

            if (task.getKey() != null) {
                task.update();
            } else {
                task.setplatid(platId(token));
                task.save();
            }
            MetaKit.reload(task);
        } while (false);



        return jBean;
    }*/

    @Comment("debug调试接口")
    public JBean debug(QTask task, @RestParam(name = "plattoken") String token) {
        JBean jBean = new JBean();

        Object res = TaskKit.taskRun(task);
        jBean.setBody(res);
        return jBean;
    }

}
