package net.tccn.service;

import net.tccn.base.*;
import net.tccn.dbq.DbExecutors;
import net.tccn.dbq.fbean.FBean;
import org.redkale.net.http.HttpScope;
import org.redkale.net.http.RestMapping;
import org.redkale.net.http.RestParam;
import org.redkale.net.http.RestService;
import org.redkale.util.Comment;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * 数据服务 对外提供服务
 * @author: liangxianyou at 2019/1/6 20:46.
 */
@RestService(name = "data", automapping = true, comment = "数据服务")
public class DataService extends BaseService {

    @Resource
    private MetadataService metadataService;


    @RestMapping(name = "list", auth = false, comment = "数据分页列表")
    public JBean findList(FBean fBean, @RestParam(name = "platToken") String token) {
        JBean jBean = new JBean();
        try {
            fBean.setPlatToken(token);
            fBean.setType("list");
            PageBean page = DbExecutors.findPage(fBean);
            jBean.setBody(page);
        } catch (Exception e) {
            jBean.set(-1, "数据查询失败");
            e.printStackTrace();
        }
        return jBean;
    }

    @RestMapping(name = "save", comment = "数据保存")
    public JBean save(String name, Map<String, String> data, @RestParam(name = "platToken") String token) {
        JBean jBean = new JBean();
        try {
            DbExecutors.save(name, data, token);
        } catch (Exception e) {
            throw new CfgException("保存数据失败");
        }

        return jBean;
    }

    @Comment("数据导出excel")
    public HttpScope export(FBean fBean, String fileName, @RestParam(name = "platToken") String token) {
        try {
            fBean.setPlatToken(token);
            fBean.setType("export");
            PageBean page = DbExecutors.findPage(fBean);
            Kv heads = MetaKit.cfgExport(fBean.getName(), token);

            if (isEmpty.test(fileName)) {
                fileName = String.format("export_excel_%s", System.currentTimeMillis());
            }

            Kv attrs = Kv.of();
            attrs.put("data", page.getRows());
            attrs.put("heads", heads);
            attrs.put("fileName", fileName);

            return HttpScope.refer("excel").attr(attrs);
        } catch (ExecutionException | InterruptedException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return HttpScope.refer("excel");
    }

    @RestMapping(name = "del", auth = false, comment = "数据删除")
    public JBean del(String name, Map<String, String> data, @RestParam(name = "platToken") String token) {
        JBean jBean = new JBean();
        try {
            DbExecutors.del(name, data, token);
        } catch (Exception e) {
            jBean.set(-1, "删除数据失败!");
        }
        return jBean;
    }
}
