package com.ruoyi.api.controller.info;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.FilePathSplicingType;
import com.ruoyi.common.filepathsplicing.FilePathSplicing;
import com.ruoyi.common.utils.MapTools;
import com.ruoyi.info.hospital.domain.bo.HospitalBo;
import com.ruoyi.info.hospital.domain.vo.HospitalVo;
import com.ruoyi.info.hospital.service.IHospitalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Comparator;
import java.util.List;

/**
 * @author fucheng.bai
 * @date 2025/9/22
 */
@Validated
@Api(value = "API 医院管理控制器", tags = {"医院管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/info/hospital")
public class ApiHospitalController {

    private final IHospitalService iHospitalService;

    /**
     * 获取所有医院列表
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("查询医院信息列表")
    @GetMapping("/list")
    public R<List<HospitalVo>> list(@ApiParam("经纬度，以逗号隔开") @RequestParam(required = false) String coordinates) {
        HospitalBo hospitalBo = new HospitalBo();
        hospitalBo.setRackStatus("0");
        List<HospitalVo> hospitalVoList = iHospitalService.queryList(hospitalBo);
        if (!StrUtil.isEmpty(coordinates)) {
            // 如果坐标不为空，则计算相对距离
            for (HospitalVo hospitalVo : hospitalVoList) {
                if (StrUtil.isEmpty(hospitalVo.getCoordinates())) {
                    continue;
                }

                // 获取两个点位的相对距离，单位：米
                double distance = MapTools.calculate(coordinates.split(",")[0], coordinates.split(",")[1],
                    hospitalVo.getCoordinates().split(",")[0], hospitalVo.getCoordinates().split(",")[1]);

//                BigDecimal relativeDistance = BigDecimal.valueOf(distance).abs().divide(BigDecimal.valueOf(1000),  1, RoundingMode.HALF_UP);
                hospitalVo.setRelativeDistance(BigDecimal.valueOf(distance).abs().setScale(0, RoundingMode.DOWN));
                hospitalVo.setDistanceUnit("m");
            }
            // 根据相对距离排序，从小到大
            hospitalVoList.sort(Comparator.comparing(HospitalVo::getRelativeDistance));

            for (HospitalVo hospitalVo : hospitalVoList) {
                // 如果相对距离大于1000米，则转换为km，保留一位小数
                if (hospitalVo.getRelativeDistance().compareTo(BigDecimal.valueOf(1000)) > 0) {
                    BigDecimal relativeDistance = hospitalVo.getRelativeDistance().divide(BigDecimal.valueOf(1000),  1, RoundingMode.HALF_UP);
                    hospitalVo.setRelativeDistance(relativeDistance);
                    hospitalVo.setDistanceUnit("km");
                }
            }
        }

        return R.ok(hospitalVoList);
    }

    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("获取医院信息详细信息")
    @GetMapping("/info/{id}")
    public R<HospitalVo> getInfo(@ApiParam("主键")
                                 @NotNull(message = "主键不能为空")
                                 @PathVariable("id") Long id) {
        return R.ok(iHospitalService.queryById(id));
    }
}
