package com.eversec.service;

import com.eversec.common.JBean;
import com.eversec.kit.creator.Runner;
import com.lxyer.excel.poi.ExcelKit;
import org.redkale.net.http.RestMapping;
import org.redkale.net.http.RestService;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by liangxianyou at 2019/2/21 11:08.
 */
@RestService(name = "abc", automapping = true)
public class AbcService extends BaseService {

    @RestMapping(name = "sheets", comment = "得到所有的sheetName")
    public List<String> sheets(String fileName) {
        List<String> sheets = new ArrayList<>();
        try {
            File file = new File(apphome, "upload/" + fileName);
            if(file.exists()) {
                sheets = ExcelKit.getSheetNames(file);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sheets.stream().filter(x -> {
            return !x.contains("版本记录") && !x.contains("表说明") && !x.contains("表名称");
        }).collect(Collectors.toList());
    }

    @RestMapping(name = "creater", comment = "构建代码")
    public JBean creater(CfgBean cfgBean) {
        JBean jBean = new JBean();
        try {
            cfgBean.setXlsPath(new File(apphome, "upload/" + cfgBean.getFileName()).getPath());
            cfgBean.appHome = apphome.getPath();

            String res = Runner.run(cfgBean);
            res = res.replace("\n", "<br>").replace("\t", "&nbsp;&nbsp;&nbsp;");
            jBean.setBody(res);
        } catch (IOException e) {
            e.printStackTrace();
            jBean.set(-1, e.getMessage());
        }

        return jBean;
    }
}
