package com.boman.web.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.boman.web.core.mapper.StandardlyMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author shiqian
 * @date 2021年09月08日 10:40
 **/
@RestController
public class MoveYmjzDataController {

    @Resource
    private StandardlyMapper mapper;

    @GetMapping("/moveData/{limit}/{offset}")
    public String moveYmjzData(@PathVariable("limit") int limit, @PathVariable("offset") int offset) throws Exception {
//        List<String> idCardList = mapper.listIdCard(limit, offset);
        List<JSONObject> ymjzList = mapper.listInfo(limit, offset);
        int moveData = 0;
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < ymjzList.size(); i++) {
            JSONObject ymjz = ymjzList.get(i);
            String idCard = ymjz.getString("身份证");
            String 受种者编码 = ymjz.getString("受种者编码");
            String 工作单位 = ymjz.getString("工作单位");
            String 人群分类 = ymjz.getString("人群分类");
            String sql = String.format("UPDATE vaccine_info set code = '%s', work_unit = '%s', crowd_classification = '%s' where id_card = '%s'; -- %s \r\n", 受种者编码, 工作单位, 人群分类, idCard, i);
            stringBuilder.append(sql);

            System.err.println("count: " + i + ", " + sql);
            moveData++;
        }
        File file = new File("F:\\desk\\ymjz\\jmjl1.sql");
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fileWriter = new FileWriter(file, true);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(stringBuilder.toString());
        bufferedWriter.close();

        return "循环了 " + moveData + " 条";
    }

}
