package test.com.izouma.admintemplate.datasource;


import com.izouma.awesomeadmin.model.GenCode;
import com.izouma.awesomeadmin.model.TableField;
import com.izouma.awesomeadmin.util.NetUtils;
import com.izouma.codegenerator.EditViewGenerator;
import com.izouma.codegenerator.GeneratorTool;
import com.izouma.codegenerator.ListViewGenerator;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.text.CaseUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 配置spring和junit整合，junit启动时加载springIOC容器
 * spring-test,junit
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/applicationContext.xml"})
public class TMSGenVueServiceTest {


    @Test
    public void test() throws IOException {

        String baseUrl = "http://211.144.95.51/api/MetadataArea/MetadataAreaList";
        String talbeName = "MetadataArea";
        String javaPath = "D:\\项目资料\\物流前端\\GEN";

        //String result = NetUtils.getUrlResponse(baseUrl, "");

        String result = "{\n" +
                "\t\"error\": \"\",\n" +
                "\t\"data\": [{\n" +
                "\t\t\"area_id\": 1,\n" +
                "\t\t\"area_code\": \"110000\",\n" +
                "\t\t\"area_name\": \"北京市\",\n" +
                "\t\t\"area_type\": 1,\n" +
                "\t\t\"parent_area_id\": 0,\n" +
                "\t\t\"area_lng\": null,\n" +
                "\t\t\"area_lat\": null,\n" +
                "\t\t\"create_user_id\": null,\n" +
                "\t\t\"create_time\": \"2018-06-27 09:54:34\",\n" +
                "\t\t\"create_org_id\": null,\n" +
                "\t\t\"edit_user_id\": null,\n" +
                "\t\t\"edit_time\": \"2018-06-27 09:54:42\",\n" +
                "\t\t\"edit_org_id\": null,\n" +
                "\t\t\"area_type_id\": 1,\n" +
                "\t\t\"area_type_name\": \"省\"\n" +
                "\t}]\n" +
                "}";

        JSONObject job = JSONObject.fromObject(result);
        JSONArray jsonArray = JSONArray.fromObject(job.get("data"));

//测试数据
        GenCode genCode = new GenCode();
        genCode.setTableName(talbeName);
        genCode.setRemark("TMS-GEN-vue");
        genCode.setJavaPath(javaPath);
        genCode.setViewPath(javaPath);
        List<TableField> tableFields = new ArrayList<>();

        if (jsonArray.size() > 0) {
            JSONObject jobInfo = jsonArray.getJSONObject(0);

            Iterator it = jobInfo.keys();
            List<String> keyListstr = new ArrayList<String>();
            while (it.hasNext()) {
                String key = (String) it.next();
                String value = jobInfo.getString(key);

                TableField tableField = new TableField();

                tableField.setName(key);
                tableField.setJdbcType("varchar");
                tableField.setNotNull(false);
                tableField.setPrimaryKey(false);
                tableField.setAutoIncrease(false);
                tableField.setRemark(key);
                tableField.setLength(50);
                tableField.setShowInList(true);
                tableField.setShowInForm(true);
                tableField.setFormType("singleLineText");
                tableField.setModelName(key);

                tableFields.add(tableField);

            }


            genCode.setFields(tableFields);


            genCode.setClassName(CaseUtils.toCamelCase(genCode.getTableName(), true, '_'));

            ListViewGenerator.GenListView(genCode);
            EditViewGenerator.GenEditView(genCode);
        }


    }


}



