package cc.smtweb.system.bpm.test;

import cc.smtweb.framework.core.common.R;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.system.bpm.spring.BpmApplication;
import cc.smtweb.system.bpm.web.design.form.ModelForm;
import cc.smtweb.system.bpm.web.design.form.ModelFormHelper;
import cc.smtweb.system.bpm.web.design.form.ModelFormService;
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Akmm at 2022/4/26 9:46
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BpmApplication.class)
public class ModelFormTest {
    @Test
    public void testLoad() {
        SwMap params = new SwMap();
        params.put("pageId", 1);
        params.put("dataset", "modelProject");
        SwMap filter = new SwMap();
        filter.put("prj_name", "测试");
        params.put("filter", filter);
        DynPageService service = new DynPageService();
        R r = service.load(params, null);
        System.out.println(r.readSuccess());
    }

    @Test
    public void testSave() {
        //{pageId,dataset:"", data:{form:{},list: {total:0,rows:[]}}, filter:{}}
        SwMap params = new SwMap();
        params.put("pageId", 1);
        params.put("dataset", "modelProject");
        SwMap data = new SwMap();
        SwMap form = new SwMap();
        form.put("prj_id", "12345");
        form.put("prj_name", "test12345");
        form.put("prj_desc", "this is test12345");
        data.put("form", form);
        params.put("data", data);
        SwMap filter = new SwMap();
        filter.put("prj_name", "测试");
        params.put("filter", filter);
        DynPageService service = new DynPageService();
        R r = service.saveOne(params, null);
        System.out.println(r.readSuccess());
    }

    @Test
    public void testDel() {
        //{pageId,dataset:"", data:{form:{},list: {total:0,rows:[]}}, filter:{}}
        SwMap params = new SwMap();
        params.put("pageId", 1);
        params.put("id", 12345);

        DynPageService service = new DynPageService();
        R r = service.del(params, null);
        System.out.println(r.readSuccess());
    }

    @Test
    public void testBuildModelByTmpl() {
        ModelForm form = new ModelForm();
        form.setTitle("测试呀");
        form.setDataset("[{\"id\":\"ds_18147381016\",\"name\":\"master\",\"label\":\"主数据集\",\"type\":\"form\",\"masterTable\":\"2\",\"masterTable_text\":\"目录\",\"idField\":\"mc_id\",\"lazy\":false,\"canEdit\":true,\"fields\":[{\"id\":\"id_1814738101b\",\"table\":\"2\",\"field\":\"mc_id\",\"name\":\"mc_id\",\"fieldType\":1,\"notNull\":1,\"link\":\"0\"},{\"id\":\"id_1814738101c\",\"table\":\"2\",\"field\":\"mc_parent_id\",\"name\":\"mc_parent_id\",\"fieldType\":4,\"notNull\":0,\"link\":\"2\"},{\"id\":\"id_1814738101d\",\"table\":\"2\",\"field\":\"mc_level_code\",\"name\":\"mc_level_code\",\"fieldType\":5,\"notNull\":0,\"link\":\"0\"},{\"id\":\"id_1814738101e\",\"table\":\"2\",\"field\":\"mc_prj_id\",\"name\":\"mc_prj_id\",\"fieldType\":0,\"notNull\":1,\"link\":\"1\"},{\"id\":\"id_1814738101f\",\"table\":\"2\",\"field\":\"mc_code\",\"name\":\"mc_code\",\"fieldType\":2,\"notNull\":0,\"link\":\"0\"},{\"id\":\"id_18147381020\",\"table\":\"2\",\"field\":\"mc_name\",\"name\":\"mc_name\",\"fieldType\":3,\"notNull\":0,\"link\":\"0\"}],\"filters\":[],\"data\":[],\"dynCond\":{\"opt\":\"and\"},\"sortFields\":[]}]");
        form.setTmpl("{\"param\":{},\"layout\":{\"c1\":[{\"id\":\"id18147381019\",\"name\":\"group1\",\"label\":\"分组1\",\"type\":\"card\",\"fields\":[{\"field\":\"id_1814738101c\",\"field_text\":\"mc_parent_id\",\"field_label\":\"父ID\",\"dataset\":\"ds_18147381016\",\"dataset_text\":\"master\"},{\"field\":\"id_1814738101d\",\"field_text\":\"mc_level_code\",\"field_label\":\"级次码\",\"dataset\":\"ds_18147381016\",\"dataset_text\":\"master\"},{\"field\":\"id_1814738101e\",\"field_text\":\"mc_prj_id\",\"field_label\":\"所属项目\",\"dataset\":\"ds_18147381016\",\"dataset_text\":\"master\"},{\"field\":\"id_1814738101f\",\"field_text\":\"mc_code\",\"field_label\":\"编码\",\"dataset\":\"ds_18147381016\",\"dataset_text\":\"master\"},{\"field\":\"id_18147381020\",\"field_text\":\"mc_name\",\"field_label\":\"编码\",\"dataset\":\"ds_18147381016\",\"dataset_text\":\"master\"}],\"sfilters\":[],\"cfilters\":[]}]}}");
        ModelFormHelper.buildSaveModelByTmpl(form, "model_simple");
        System.out.println(form.getContent());
    }

    @Test
    public void testBuildJavaBean() {
        //{pageId,dataset:"", data:{form:{},list: {total:0,rows:[]}}, filter:{}}
        SwMap params = new SwMap();
        params.put("pageId", 718479207767740416L);
        params.put("service", true);
        List<SwMap> table = new ArrayList<>();
        params.put("table", table);
        SwMap map = new SwMap();
        map.put("tableId", 718391823709507584L);
        map.put("bean", true);
        map.put("cache", true);
        table.add(map);

        ModelFormService service = new ModelFormService();
        R r = service.buildJavaCode(params, null);
        System.out.println(r.readSuccess());
    }
}
