package com.docus.server.archive.controller;

import com.docus.core.util.Func;
import com.docus.infrastructure.web.api.CommonResult;
import com.docus.server.archive.service.PatientInfoSyncService;
import com.docus.server.fsy.entity.JswzhPatientInfoView;
import com.docus.server.fsy.mapper.JswzhPatientInfoViewMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author YongBin Wen
 * @date 2024/3/8 15:21
 */
@Slf4j
@RequestMapping("/fssy-collect")
@Api(tags = "佛山三院同步接口")
@RestController
public class FoShanSySyncController {
    @Resource
    private PatientInfoSyncService patientInfoSyncService;

    @Resource
    private JswzhPatientInfoViewMapper jswzhPatientInfoViewMapper;

    @ApiOperation(value = "根据时间进行同步出院数据 yyyy-MM-dd")
    @GetMapping("/sync/discharge")
    public CommonResult<String> syncDischarge(@RequestParam("startDate") String startDate, @RequestParam("endDate") String endDate) {
        String pattern = "yyyy-MM-dd";
        int patternLen = pattern.length();
        if (startDate.length() != patternLen || endDate.length() != patternLen) {
            return CommonResult.failed("请传入正确的时间格式 yyyy-MM-dd");
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            sdf.parse(startDate);
            sdf.parse(endDate);
        } catch (Exception ex) {
            return CommonResult.failed("请传入正确的时间格式 yyyy-MM-dd");
        }
        int page = 1;
        int size = 100;
        int total = 0;
        try {
            while (true) {
                int count = patientInfoSyncService.syncDischargeData(startDate + " 00:00:00", endDate + " 23:59:59", page, size);
                total += count;
                if (count == 0) {
                    break;
                }
                page += 1;
            }
        } catch (Exception ex) {
            log.error("同步出院数据接口出错了！" + ex.getMessage(), ex);
            return CommonResult.success("同步失败");
        }
        return CommonResult.success("同步完成,共同步患者条数：" + total);
    }

    @ApiOperation(value = "同步在院数据")
    @GetMapping("/sync/inHospital")
    public CommonResult<String> syncInHospital() {
        try {
            patientInfoSyncService.syncInHospitalData();
            return CommonResult.success("同步完成");
        } catch (Exception ex) {
            log.error("同步在院数据接口出错！" + ex.getMessage(), ex);
            return CommonResult.success("同步出错！");
        }

    }

    @ApiOperation(value = "视图数据获取测试,根据时间范围获取100条出院数据(都传或者都不传) ，如果不传递时间，则获取在院数据")
    @GetMapping("/test/patientInfoViewGet")
    public CommonResult<List<JswzhPatientInfoView>> viewGet(@RequestParam(value = "startDate", required = false) String startDate, @RequestParam(value = "endDate", required = false) String endDate) {
        if (Func.isBlank(startDate) && Func.isBlank(endDate)) {
            List<JswzhPatientInfoView> inHospital = jswzhPatientInfoViewMapper.getInHospital();
            return CommonResult.success(inHospital);
        }
        if (Func.isBlank(startDate) || Func.isBlank(endDate)) {
            return CommonResult.failed("出院数据查询查询，时间都不为空！");
        }
        String pattern = "yyyy-MM-dd";
        int patternLen = pattern.length();
        if (startDate.length() != patternLen || endDate.length() != patternLen) {
            return CommonResult.failed("请传入正确的时间格式 yyyy-MM-dd");
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            sdf.parse(startDate);
            sdf.parse(endDate);
        } catch (Exception ex) {
            return CommonResult.failed("请传入正确的时间格式 yyyy-MM-dd");
        }
        List<JswzhPatientInfoView> discharge = jswzhPatientInfoViewMapper.getDischarge(startDate + " 00:00:00", endDate + " 23:59:59", 1, 100);
        return CommonResult.success(discharge);
    }

}
