package cc.smtweb.system.bpm.test;

import cc.smtweb.framework.core.common.SwEnum;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityDao;
import cc.smtweb.framework.core.db.cache.ModelTableCache;
import cc.smtweb.framework.core.db.vo.ModelField;
import cc.smtweb.framework.core.db.vo.ModelTable;
import cc.smtweb.framework.core.util.JsonUtil;
import cc.smtweb.system.bpm.spring.BpmApplication;
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.Collection;
import java.util.List;

/**
 * Created by Akmm at 2022/5/24 18:40
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BpmApplication.class)
public class ToolTest {
    @Test
    public void resetTable() {
        ModelTableCache cache = ModelTableCache.getInstance();
        EntityDao<ModelTable> dao = DbEngine.getInstance().findDao(ModelTable.class);
        Collection<ModelTable> list = new ArrayList<>(cache.getAll());
        for (ModelTable table: list) {
            table.setName(table.getName().toUpperCase());
            List<ModelField> fields = table.getFields();
            for (ModelField field: fields) {
                field.setName(field.getName().toLowerCase());
                if ("COMMENT".equalsIgnoreCase(field.getDataType())) field.setDataType(SwEnum.DataType.REMARK.value);
                SwEnum.DataTypeBean dtb = SwEnum.DataType.instance.getByValue(field.getDataType());
                if (dtb == null) {
                    System.out.println(table.getName() + "." + field.getName() + "字段类型错误：[" + field.getDataType() + "]");
                    continue;
                }
                field.setDataType(dtb.value);
            }
            SwMap map = new SwMap();
            map.put("fields", table.getFields());
            map.put("indexes", table.getIndexes());
            map.put("caches", table.getCaches());
            table.getData().put("tb_content", JsonUtil.encodeString(map));
            dao.updateEntity(table);
            cache.put(table);
        }
    }
}
