package dev.zhub.mk.qtask;

import net.tccn.base.BaseService;
import net.tccn.base.Utils;
import org.redkale.net.http.RestHeader;
import org.redkale.net.http.RestMapping;
import org.redkale.net.http.RestService;
import org.redkale.service.RetResult;
import org.redkale.source.FilterNode;
import org.redkale.source.Flipper;
import org.redkale.util.AnyValue;
import org.redkale.util.Sheet;

import java.util.Map;

@RestService(name = "qtask", comment = "qtask服务")
public class QTaskService extends BaseService {

    @Override
    public void init(AnyValue config) {
        super.init(config);
        /*List<QTask> qTasks = metaSource.queryList(QTask.class);
        for (QTask xTask : qTasks) {
            System.out.println(JsonConvert.root().convertTo(xTask));
        }*/
    }

    @RestMapping(name = "list", comment = "qtask列表")
    public RetResult<Sheet<QTask>> list(@RestHeader(name = "platid") String platid,
                                        Flipper flipper, Map<String, String> params) {
        FilterNode node = FilterNode.create("platid", platid);
        Sheet<QTask> sheet = metaSource.querySheet(QTask.class, flipper, node);
        return RetResult.success(sheet);
    }

    @RestMapping(name = "create", comment = "qtask创建")
    public RetResult create(@RestHeader(name = "platid") String platid,
                            QTask bean) {
        // -- 数据检查 --
        // 1. name 不可为空、 content 不可以为空
        if (Utils.isEmpty(bean.getName())) {
            return retError("名称不能为空");
        }
        if (Utils.isEmpty(bean.getContent())) {
            return retError("内容不能为空");
        }

        // 2. bean.name 标识名称KEY唯一性检查
        FilterNode node = FilterNode.create("name", bean.getName())
                .and("platid", platid);
        if (metaSource.exists(QTask.class, node)) {
            return retError("名称重复");
        }

        // -- 数据保存 --
        metaSource.insert(bean);
        return render();
    }
}
