package net.tccn.plat;

import net.tccn.base.BaseService;
import net.tccn.base.JBean;
import net.tccn.base.MetaKit;
import net.tccn.base.Utils;
import net.tccn.base.dbq.jdbc.api.DbAccount;
import org.redkale.net.http.RestMapping;
import org.redkale.net.http.RestService;
import org.redkale.source.FilterBean;
import org.redkale.source.Flipper;
import org.redkale.util.Comment;
import org.redkale.util.Sheet;

@RestService(name = "plat", automapping = true, comment = "业务/数据平台")
public class PlatService extends BaseService {

    @RestMapping(name = "list", comment = "平台列表")
    public JBean list(MPlat plat, Flipper flipper) {
        JBean jBean = new JBean();

        //PageBean<SysPlat> page = SysPlat.dao.findPage(plat, flipper);
        /*List<MetaPlat> list = MetaKit.getSysPlats();
        PageBean page = PageBean.by(list, list.size());*/

        Sheet<MPlat> sheet = metaSource.querySheet(MPlat.class, flipper, (FilterBean) null);
        return jBean.setBody(sheet);
    }

    @Comment("平台信息保存")
    public JBean save(MPlat plat) {
        if (!Utils.isEmpty(plat.getPlatid())) {
            metaSource.update(plat);
        } else {
            metaSource.insert(plat);
        }
        return JBean.OK;
    }

    @RestMapping(name = "info", comment = "平台详情")
    public void info(int key) {

    }

    //------------------------

    @RestMapping(name = "db_list", comment = "数据源列表")
    public JBean dbList(DbAccount plat, Flipper flipper) {
        JBean jBean = new JBean();

        /*List<DbAccount> list = MetaKit.getDbPlats();
        PageBean page = PageBean.by(list, list.size());*/

        Sheet<DbAccount> sheet = metaSource.querySheet(DbAccount.class, flipper, (FilterBean) null);
        return jBean.setBody(sheet);
    }

    @RestMapping(name = "db_save", comment = "数据源信息保存")
    public JBean dbSave(DbAccount plat) {
        // MetaKit.save(plat);
        if (!Utils.isEmpty(plat.getDbid()) || plat.getDbid() != 0) {
            if (Utils.isEmpty(plat.getPwd())) {
                DbAccount account = MetaKit.getDbPlat(plat.getDbid());
                plat.setPwd(account.getPwd());
            }

            metaSource.update(plat);
        } else {
            metaSource.insert(plat);
        }

        return JBean.OK;
    }


}
