package thyyxxk.webserver.scheduled;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import thyyxxk.webserver.dao.his.reports.PatientDistributionAddressDao;
import thyyxxk.webserver.entity.inpatient.patient.Patient;
import thyyxxk.webserver.utils.BaiduMapGeocoderUtil;
import thyyxxk.webserver.utils.ListUtil;
import thyyxxk.webserver.utils.StringUtil;

import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class UpdatePatientLatitudeAndLongitude {

    @Value("${execute-scheduled}")
    private Boolean executeScheduled;

    private final PatientDistributionAddressDao dao;

    public UpdatePatientLatitudeAndLongitude(PatientDistributionAddressDao dao) {
        this.dao = dao;
    }

    /**
     * 每天一点执行定时任务
     * 患者地址转坐标
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void UpdatePatientLatitudeAndLongitude() {
        if (!executeScheduled) {
            return;
        }
        String tableName = "a_patient_mi";
        String patNo = "inpatient_no";
        List<Patient> list = dao.timedTaskPatientData();

        if (ListUtil.isBlank(list)) {
            tableName = "mz_patient_mi";
            patNo = "patient_id";
            list = dao.mzTimedTaskPatientData();
        }


        if (ListUtil.notBlank(list)) {
            for (Patient patient : list) {
                if (StringUtil.notBlank(patient.getHomeStreet())) {
                    String[] address = BaiduMapGeocoderUtil.getlocation(patient.getHomeStreet());
                    if (address == null) {
                        patient.setAddrTransedFlag(1);
                        patient.setLatitude(null);
                        patient.setLongitude(null);
                    } else {
                        patient.setAddrTransedFlag(0);
                        patient.setLongitude(new BigDecimal(address[0]));
                        patient.setLatitude(new BigDecimal(address[1]));
                    }
                    log.info("住院号:{},地址:{},经度:{},纬度:{}", patient.getInpatientNo(),
                            patient.getHomeStreet(),
                            patient.getLongitude(), patient.getLatitude());
                }
            }

            List<List<Patient>> performAnUpdate = ListUtils.partition(list, 50);

            for (List<Patient> patients : performAnUpdate) {
                dao.updatePatientAddress(patients, tableName, patNo);
            }

        }
    }

    public void test() throws IOException {
        String fileName = "D:\\world\\server\\web-server\\serverlog\\temp.txt";
        Path path = Paths.get(fileName);
        List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);

        List<Patient> updated = new ArrayList<>();

        Integer 数量 = 0;

        for (String allLine : allLines) {
            Patient patient = new Patient();

            Integer 位置 = allLine.indexOf("住院号:");

            if (位置 > -1) {
                String str = allLine.substring(位置);
                String[] list = str.split(",");

                for (int i = 0; i < list.length; i++) {
                    String 信息 = list[i].split(":")[1];
                    if ("null".equals(信息)) {
                        信息 = null;
                    }
                    switch (i) {
                        case 0:
                            patient.setInpatientNo(信息);
                            break;
                        case 2:
                            patient.setLongitude(信息 == null ? null : new BigDecimal(信息));
                            break;
                        case 3:
                            patient.setLatitude(信息 == null ? null : new BigDecimal(信息));
                            break;
                        default:
                            break;
                    }
                }
                log.info("转化信息:{}", JSON.toJSONString(patient));
                updated.add(patient);
            }
        }

        List<List<Patient>> performAnUpdate = ListUtils.partition(updated, 50);
        for (List<Patient> patients : performAnUpdate) {
//            dao.updatePatientAddress(patients);
        }

    }

}

