package com.boman.web.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.boman.domain.VaccineInfoOperation;
import com.boman.web.core.mapper.StandardlyMapper;
import com.boman.web.core.service.vaccineInfo.IVaccineInfoService;
import com.boman.web.core.service.vaccineInfo.impl.VaccineInfoServiceImpl;
import com.boman.web.core.utils.IdUtils;
import com.google.common.base.Strings;
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;
    @Resource
    private IVaccineInfoService service;

    @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 + " 条";
    }

    @GetMapping("/moveData1")
    public String moveYmjzData() throws Exception {
        List<JSONObject> ymjzList = mapper.selectAll();
//        List<JSONObject> idCardUserNameList = mapper.listVacc();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 30; i++) {
            JSONObject ymjz = ymjzList.get(i);
            String 受种者编码 = ymjz.getString("受种者编码");
            String 受种者姓名 = ymjz.getString("受种者姓名");
            String 受种者性别 = ymjz.getString("受种者性别");
            String 出生日期 = ymjz.getString("出生日期");
            String idCard = ymjz.getString("身份证");
            String 电话号码 = ymjz.getString("电话号码");
            String 工作单位 = ymjz.getString("工作单位");
            String 人群分类 = ymjz.getString("人群分类");
            String 接种单位 = ymjz.getString("接种单位");
            String 接种疫苗 = ymjz.getString("接种疫苗");
            String 生产厂家 = ymjz.getString("生产厂家");
            String 接种针次 = ymjz.getString("接种针次");
            String 接种日期 = ymjz.getString("接种日期");
//            VaccineInfoOperation info = mapper.getByIdCard(idCard);
            String[] split = idCard.split("\\**\\**\\**\\**\\*");
            VaccineInfoOperation info = mapper.getByCode(受种者编码, "%" + split[1] + "%", "%" + 受种者姓名 + "%");
            String progress = VaccineInfoServiceImpl.getIsSuccess(生产厂家, 接种针次);
            if (info == null) {
//                insert
                Long maxId = IdUtils.getMaxId("vaccine_info", "id");
                String insert = String.format("insert into vaccine_info (`id`, `user_name`, `gender`, `id_card`" +
                                ", `phone_num`, `is_vaccination`, `vaccine_name`, `jici`, `vaccination_time`" +
                                ", `vaccination_place`, `should_be`, `progress`, `should_slow` " +
                                ", `code`, `birthday`, `work_unit`, `crowd_classification`, `manufacturer`, `age`, `dept_id`, `status`) " +
                                "VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s'); -- %s \r\n"
                        , maxId, 受种者姓名, 受种者性别, ymjz.getString("身份证")
                        , 电话号码, "是", 生产厂家, 接种针次, 接种日期
                        , 接种单位, "是", progress, "否"
                        , 受种者编码, 出生日期, 工作单位, 人群分类, 生产厂家, 0, 1, 2
                        , i);
                stringBuilder.append(insert);
                System.err.println("count: " + i + ", " + insert);
            } else {
                idCard = info.getIdCard();
                Integer age = 0;
                try {
                    age = VaccineInfoServiceImpl.getAge(info.getIdCard());
                } catch (Exception e) {
                    age = null;
                }
                VaccineInfoServiceImpl.shouldSlow(info);
                String update = String.format("UPDATE vaccine_info " +
                                "set code = '%s', crowd_classification = '%s', vaccine_name = '%s', manufacturer = '%s'" +
                                ", jici = '%s', vaccination_time = '%s', age = '%s', birthday = '%s', progress = '%s'" +
                                ", dept_id = '%s', `is_vaccination` = '是', `should_be` = '%s', `should_slow` = '%s' " +
                                "where code = '%s' or (id_card like '%s' and user_name like '%s' ); -- %s \r\n"
                        , 受种者编码, 人群分类, 生产厂家, 生产厂家
                        , 接种针次, 接种日期, age, 出生日期, progress
                        , info.getDeptId(), Strings.nullToEmpty(info.getShouldBe()), Strings.nullToEmpty(info.getShouldSlow()),
                        受种者编码, "%" + split[1] + "%", "%" + 受种者姓名 + "%",i);
                stringBuilder.append(update);
                System.err.println("count: " + i + ", " + update);
            }

            Long maxId = IdUtils.getMaxId("vaccine_info_user", "id");
            String userInsert = String.format("insert into vaccine_info_user (id, id_card, vaccine_name, jici, vaccination_time, vaccination_place) " +
                    "values('%s', '%s', '%s', '%s', '%s', '%s');\r\n", maxId, idCard, 生产厂家, 接种针次, 接种日期, 接种单位);
            stringBuilder.append(userInsert);
            System.err.println("vaccine_info_user count: " + i + ", " + userInsert);
        }

//        File file = new File("F:\\desk\\ymjz\\jmjl2021-09-18.sql");
        File file = new File("/usr/local/sql/jmjl2021-09-24.sql");
        if (!file.exists()) {
            file.createNewFile();
        }

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

        return "";
    }

}
