package com.boman.gen.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.boman.gen.mapper.MoveDataMapper;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.List;

/**
 * @author shiqian
 * @date 2021年09月06日 18:24
 **/
@RestController
@RequestMapping
public class MoveDataController {

    @Resource
    MoveDataMapper moveDataMapper;


    @GetMapping("/hj/{limit}/{offset}")
    public String moveData(@PathVariable("limit") int limit, @PathVariable("offset") int offset) {
        List<JSONObject> select = moveDataMapper.select(limit, offset);
        return JSON.toJSONString(select);
    }

    @GetMapping("/selectY/{limit}/{offset}")
    public String selectY(@PathVariable("limit") int limit, @PathVariable("offset") int offset) {
        List<JSONObject> select = moveDataMapper.selectY(limit, offset);
        return JSON.toJSONString(select);
    }

    /**
     * 功能描述:
     *
     *       <dependency>
     *             <groupId>com.google.code.gson</groupId>
     *             <artifactId>gson</artifactId>
     *             <version>2.7</version>
     *         </dependency>
     *
     * @param
     * @return void
     */
    private static void jsonToExcel() throws Exception {
        JsonParser jsonParser = new JsonParser();
        JsonArray asJsonArray = (JsonArray) jsonParser.parse(new FileReader("F:\\desk\\ymjz\\jmjz_all.json"));
        for (int i = 0; i < asJsonArray.size(); i++) {
            JsonElement jsonElement = asJsonArray.get(i);
            JsonObject featuresObj = jsonElement.getAsJsonObject();
            String ID = getString(featuresObj, "ID");
            String 受种者编码 = getString(featuresObj, "受种者编码");
            String 受种者姓名 = getString(featuresObj, "受种者姓名");
            String 受种者性别 = getString(featuresObj, "受种者性别");
            String 出生日期 = getString(featuresObj, "出生日期");
            String 身份证 = getString(featuresObj, "身份证");
            String 电话号码 = getString(featuresObj, "电话号码");
            String 工作单位 = getString(featuresObj, "工作单位");
            String 人群分类 = getString(featuresObj, "人群分类");
            String 接种单位 = getString(featuresObj, "接种单位");
            String 接种疫苗 = getString(featuresObj, "接种疫苗");
            String 生产厂家 = getString(featuresObj, "生产厂家");
            String 接种针次 = getString(featuresObj, "接种针次");
            String 接种日期 = getString(featuresObj, "接种日期");

            String sqlStr = "insert into ymjz (ID, 受种者编码, 受种者姓名, 受种者性别, 出生日期, 身份证,电话号码,工作单位, 人群分类,接种单位,接种疫苗,生产厂家,接种针次, 接种日期) " +
                    "values (" + ID + ", " + 受种者编码 + ", " + 受种者姓名 + ", " + 受种者性别 + ", " + 出生日期 + ", " + 身份证 + ", "
                    + 电话号码 + ", " + 工作单位 + ", " + 人群分类 + " , " + 接种单位 + ", " + 接种疫苗 + ", " + 生产厂家 + ", " + 接种针次
                    + ", " + 接种日期 + "); -- " + i + "\r\n";
            System.err.println("count: " + i + ", " + sqlStr);
            File file = new File("F:\\desk\\ymjz\\jmjz_all.sql");
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(file, true);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(sqlStr);
            bufferedWriter.close();
        }
    }

    private static String getString(JsonObject featuresObj, String type) {
        JsonElement zjlx = featuresObj.get(type);
        if (null == zjlx) {
            return "''";
        }
        return zjlx.toString();
    }

    public static void main(String[] args) throws Exception {
        System.out.println();
        jsonToExcel();
    }
}