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/Activity";
        String talbeName = "Activity";
        String javaPath = "D:\\项目资料\\物流前端\\GEN";

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

        String result = "{\n" +
                "\t\"error\": \"\",\n" +
                "\t\"data\": [ {\n" +
                "                \"activity_id\": 1,\n" +
                "                \"activity_name\": \"\uD83D\uDC09\uD83D\uDC0E精神 ⭐\uD83D\uDD25传承\",\n" +
                "                \"activity_type\": \"在线活动\",\n" +
                "                \"activity_theme\": \"宪法宣传\",\n" +
                "                \"activity_sponsor\": \"上海市XX局\",\n" +
                "                \"activity_on_off\": true,\n" +
                "                \"create_time\": \"2018-12-13 14:14:47\"\n" +
                "            }]\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.GenListView1(genCode);
            EditViewGenerator.GenEditView(genCode);
        }


    }


}



