package cn.iocoder.yudao.module.system.service.biz;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyVitalSignsRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.VitalSignsPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.VitalSignsSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyVitalSignsDO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyInfoMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyVitalSignsMapper;
import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper;
import cn.iocoder.yudao.module.system.enums.VitalSignsEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 生命体征 Service 实现类
 *
 * @author 源码
 */
@Service
@Validated
public class ElderlyVitalSignsServiceImpl implements ElderlyVitalSignsService {

    @Resource
    private ElderlyInfoMapper elderlyInfoMapper;
    @Resource
    private ElderlyVitalSignsMapper vitalSignsMapper;

    @Resource
    private BuildService buildService;
    @Resource
    private TenantMapper tenantMapper;


    @Override
    public void createVitalSigns(VitalSignsSaveReqVO createReqVO) {
        List<ElderlyVitalSignsDO> vitalSigns = createReqVO.getVitalSigns();
        for (ElderlyVitalSignsDO vitalSign : vitalSigns) {
            vitalSign.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
            vitalSign.setCreatedTime(new Date());
            vitalSign.setDataSourceType(1); //人工
            vitalSign.setTenantId(vitalSign.getTenantId() == null ? TenantContextHolder.getTenantId() : vitalSign.getTenantId());
            vitalSignsMapper.insert(vitalSign);
        }
    }

    @Override
    public ElderlyVitalSignsDO getVitalSigns(Long id) {
        ElderlyVitalSignsDO elderlyVitalSigns = vitalSignsMapper.selectById(id);
        if (elderlyVitalSigns != null) {
            ElderlyInfoDO elderlyInfo = elderlyInfoMapper.selectById(elderlyVitalSigns.getElderId());
            assert elderlyInfo != null;
            elderlyVitalSigns.setElderName(elderlyInfo.getElderName());
            elderlyVitalSigns.setBedName(buildService.getFullBedName(elderlyInfo.getId()));

            //判断生命体征范围是否正常
            checkVitalSigns(elderlyVitalSigns);

        }
        return elderlyVitalSigns;
    }

    private static void checkVitalSigns(ElderlyVitalSignsDO elderlyVitalSigns) {
        if (StringUtils.isNotBlank(elderlyVitalSigns.getTemperature())) {
            VitalSignsEnum vitalSignsEnum = VitalSignsEnum.TEMPERATURE;
            double v = Double.parseDouble(elderlyVitalSigns.getTemperature());
            if (v >= vitalSignsEnum.getMaxValue()) {
                elderlyVitalSigns.setTemperature(String.format("%s%s", elderlyVitalSigns.getTemperature(), "(偏高)"));
            } else if (v < vitalSignsEnum.getMinValue()) {
                elderlyVitalSigns.setTemperature(String.format("%s%s", elderlyVitalSigns.getTemperature(), "(偏低)"));
            } else {
                elderlyVitalSigns.setTemperature(String.format("%s%s", elderlyVitalSigns.getTemperature(), "(正常)"));
            }
        }

        if (StringUtils.isNotBlank(elderlyVitalSigns.getPulse())) {
            VitalSignsEnum vitalSignsEnum = VitalSignsEnum.PULSE;
            double v = Double.parseDouble(elderlyVitalSigns.getPulse());

            if (v >= vitalSignsEnum.getMaxValue()) {
                elderlyVitalSigns.setPulse(String.format("%s%s", elderlyVitalSigns.getPulse(), "(偏高)"));
            } else if (v < vitalSignsEnum.getMinValue()) {
                elderlyVitalSigns.setPulse(String.format("%s%s", elderlyVitalSigns.getPulse(), "(偏低)"));
            } else {
                elderlyVitalSigns.setPulse(String.format("%s%s", elderlyVitalSigns.getPulse(), "(正常)"));
            }
        }


        if (StringUtils.isNotBlank(elderlyVitalSigns.getRespiratoryRate())) {
            VitalSignsEnum vitalSignsEnum = VitalSignsEnum.RESPIRATORY_RATE;
            double v = Double.parseDouble(elderlyVitalSigns.getRespiratoryRate());

            if (v >= vitalSignsEnum.getMaxValue()) {
                elderlyVitalSigns.setRespiratoryRate(String.format("%s%s", elderlyVitalSigns.getRespiratoryRate(), "(偏高)"));
            } else if (v < vitalSignsEnum.getMinValue()) {
                elderlyVitalSigns.setRespiratoryRate(String.format("%s%s", elderlyVitalSigns.getRespiratoryRate(), "(偏低)"));
            } else {
                elderlyVitalSigns.setRespiratoryRate(String.format("%s%s", elderlyVitalSigns.getRespiratoryRate(), "(正常)"));
            }
        }

        if (StringUtils.isNotBlank(elderlyVitalSigns.getSystolicPressure())) {
            VitalSignsEnum vitalSignsEnum = VitalSignsEnum.SYSTOLIC_PRESSURE;
            double v = Double.parseDouble(elderlyVitalSigns.getSystolicPressure());

            if (v >= vitalSignsEnum.getMaxValue()) {
                elderlyVitalSigns.setSystolicPressure(String.format("%s%s", elderlyVitalSigns.getSystolicPressure(), "(偏高)"));
            } else if (v < vitalSignsEnum.getMinValue()) {
                elderlyVitalSigns.setSystolicPressure(String.format("%s%s", elderlyVitalSigns.getSystolicPressure(), "(偏低)"));
            } else {
                elderlyVitalSigns.setSystolicPressure(String.format("%s%s", elderlyVitalSigns.getSystolicPressure(), "(正常)"));
            }
        }


        if (StringUtils.isNotBlank(elderlyVitalSigns.getDiastolicPressure())){
            VitalSignsEnum vitalSignsEnum = VitalSignsEnum.DIASTOLIC_PRESSURE;
            double v = Double.parseDouble(elderlyVitalSigns.getDiastolicPressure());

            if (v >= vitalSignsEnum.getMaxValue()) {
                elderlyVitalSigns.setDiastolicPressure(String.format("%s%s", elderlyVitalSigns.getDiastolicPressure(), "(偏高)"));
            } else if (v < vitalSignsEnum.getMinValue()) {
                elderlyVitalSigns.setDiastolicPressure(String.format("%s%s", elderlyVitalSigns.getDiastolicPressure(), "(偏低)"));
            } else {
                elderlyVitalSigns.setDiastolicPressure(String.format("%s%s", elderlyVitalSigns.getDiastolicPressure(), "(正常)"));
            }
        }


        if (StringUtils.isNotBlank(elderlyVitalSigns.getBloodOxygenSaturation())){
            VitalSignsEnum vitalSignsEnum = VitalSignsEnum.BLOOD_OXYGEN_SATURATION;
            double v = Double.parseDouble(elderlyVitalSigns.getBloodOxygenSaturation());

            if (v >= vitalSignsEnum.getMaxValue()) {
                elderlyVitalSigns.setBloodOxygenSaturation(String.format("%s%s", elderlyVitalSigns.getBloodOxygenSaturation(), "(偏高)"));
            } else if (v < vitalSignsEnum.getMinValue()) {
                elderlyVitalSigns.setBloodOxygenSaturation(String.format("%s%s", elderlyVitalSigns.getBloodOxygenSaturation(), "(偏低)"));
            } else {
                elderlyVitalSigns.setBloodOxygenSaturation(String.format("%s%s", elderlyVitalSigns.getBloodOxygenSaturation(), "(正常)"));
            }
        }

        if (StringUtils.isNotBlank(elderlyVitalSigns.getBloodSugar())){
            VitalSignsEnum vitalSignsEnum = VitalSignsEnum.BLOOD_SUGAR;
            double v = Double.parseDouble(elderlyVitalSigns.getBloodSugar());

            if (v >= vitalSignsEnum.getMaxValue()) {
                elderlyVitalSigns.setBloodSugar(String.format("%s%s", elderlyVitalSigns.getBloodSugar(), "(偏高)"));
            } else if (v < vitalSignsEnum.getMinValue()) {
                elderlyVitalSigns.setBloodSugar(String.format("%s%s", elderlyVitalSigns.getBloodSugar(), "(偏低)"));
            } else {
                elderlyVitalSigns.setBloodSugar(String.format("%s%s", elderlyVitalSigns.getBloodSugar(), "(正常)"));
            }
        }
    }

    @Override
    public void deleteVitalSigns(Long[] ids) {
        vitalSignsMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<ElderlyVitalSignsRespVO> exportVitalSignsExcel(VitalSignsPageReqVO pageReqVO) {
        Page<ElderlyVitalSignsDO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<ElderlyVitalSignsDO> list = vitalSignsMapper.getVitalSignsPage(page, pageReqVO);
        return list.stream().map(item -> {
            checkVitalSigns(item);
            ElderlyVitalSignsRespVO respVO = new ElderlyVitalSignsRespVO();
            BeanUtils.copyProperties(item, respVO);
            respVO.setMeasuringDate(LocalDate.from(item.getMeasuringDate().toInstant().atZone(ZoneId.systemDefault())));
            respVO.setBedName(buildService.getFullBedName(item.getElderId()));
            return respVO;
        }).collect(Collectors.toList());

    }

    @Override
    public PageResult<ElderlyVitalSignsDO> getVitalSignsPage(VitalSignsPageReqVO pageReqVO) {
        Page<ElderlyVitalSignsDO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<ElderlyVitalSignsDO> list = vitalSignsMapper.getVitalSignsPage(page, pageReqVO);
        for (ElderlyVitalSignsDO elderlyVitalSignsDO : list) {
            elderlyVitalSignsDO.setBedName(buildService.getFullBedName(elderlyVitalSignsDO.getElderId()));
            TenantDO tenantDO = tenantMapper.selectById(elderlyVitalSignsDO.getElderId());
            elderlyVitalSignsDO.setTenantName(tenantDO == null ? null : tenantDO.getName());
            //判断生命体征范围是否正常
            checkVitalSigns(elderlyVitalSignsDO);

        }
        return new PageResult<>(list, page.getTotal());
    }
}