package com.bonus.canteen.core.health.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.health.mapper.HealthPersonInfoMapper;
import com.bonus.canteen.core.health.domain.HealthPersonInfo;
import com.bonus.canteen.core.health.service.IHealthPersonInfoService;

/**
 * 人员健康情况Service业务层处理
 *
 * @author xsheng
 * @date 2025-05-25
 */
@Service
public class HealthPersonInfoServiceImpl implements IHealthPersonInfoService {
    @Autowired
    private HealthPersonInfoMapper healthPersonInfoMapper;

    /**
     * 查询人员健康情况
     *
     * @param id 人员健康情况主键
     * @return 人员健康情况
     */
    @Override
    public HealthPersonInfo selectHealthPersonInfoById(Long id) {
        return healthPersonInfoMapper.selectHealthPersonInfoById(id);
    }

    /**
     * 查询人员健康情况列表
     *
     * @param healthPersonInfo 人员健康情况
     * @return 人员健康情况
     */
    @Override
    public List<HealthPersonInfo> selectHealthPersonInfoList(HealthPersonInfo healthPersonInfo) {
        return healthPersonInfoMapper.selectHealthPersonInfoList(healthPersonInfo);
    }

    /**
     * 新增人员健康情况
     *
     * @param healthPersonInfo 人员健康情况
     * @return 结果
     */
    @Override
    public int insertHealthPersonInfo(HealthPersonInfo healthPersonInfo) {
        healthPersonInfo.setCreateTime(DateUtils.getNowDate());
        try {
            return healthPersonInfoMapper.insertHealthPersonInfo(healthPersonInfo);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改人员健康情况
     *
     * @param content 人员健康情况
     * @return 结果
     */
    @Override
    public int updateHealthPersonInfo(HealthPersonInfo content) {
        content.setUpdateTime(DateUtils.getNowDate());
        try {
            Long userId = content.getUserId();
            HealthPersonInfo chronicVo=new HealthPersonInfo();
            chronicVo.setChronicIds(content.getChronicIds());
            List<HealthPersonInfo> chronicVos=healthPersonInfoMapper.selectHealthChronicList(chronicVo);
            if(chronicVos!=null&&chronicVos.size()>0){
                /**
                 * 清空疾病信息
                 */
                healthPersonInfoMapper.deleteHealthInfoChronic(content);
                List<HealthPersonInfo> chronicfVos=new ArrayList<>();
                for(HealthPersonInfo vo:chronicVos){
                    HealthPersonInfo hfd=new HealthPersonInfo();
                    hfd.setUserId(content.getUserId());
                    hfd.setChronicIds(vo.getChronicIds());
                    hfd.setChronicNames(vo.getChronicNames());
                    chronicfVos.add(hfd);
                }
                /**
                 * 重新录入疾病信息
                 */
                healthPersonInfoMapper.insertHealthInfoChronic(chronicfVos);
            }
            healthPersonInfoMapper.delHealthInfo(content);
            /**
             * 重新录入健康信息
             */
            healthPersonInfoMapper.delHealthInfo(content);
            healthPersonInfoMapper.insertHealthInfo(content);
            healthPersonInfoMapper.updateHealthInfo(content);
            /**
             * 重新录入身体信息
             */
            int flag=healthPersonInfoMapper.getHealthBodyRecordNum(content);
            if(flag==0){
                healthPersonInfoMapper.insertHealthBodyRecord(content);
            }
            content.setBmi(getBMI(content.getWeight(),content.getHeight()));
            healthPersonInfoMapper.updateHealthBodyRecord(content);
            return 1;
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除人员健康情况
     *
     * @param ids 需要删除的人员健康情况主键
     * @return 结果
     */
    @Override
    public int deleteHealthPersonInfoByIds(Long[] ids) {
        return healthPersonInfoMapper.deleteHealthPersonInfoByIds(ids);
    }

    /**
     * 删除人员健康情况信息
     *
     * @param content 人员健康情况主键
     * @return 结果
     */
    @Override
    public int deleteHealthPersonInfoById(HealthPersonInfo content) {
        Long userId = content.getUserId();
        try{
            healthPersonInfoMapper.deleteHealthInfoChronic(content);
            healthPersonInfoMapper.delHealthInfo(content);
        }catch (Exception e){

        }
        return 1;
    }
    public static BigDecimal getBMI(BigDecimal weight, BigDecimal height) {
        if (!ObjectUtil.isNull(weight) && !ObjectUtil.isNull(height)) {
            if (NumberUtil.equals(height, BigDecimal.ZERO)) {
                return new BigDecimal(-1);
            } else {
                height = NumberUtil.div(height, BigDecimal.valueOf(100L), 2, RoundingMode.HALF_UP);
                return NumberUtil.div(weight, NumberUtil.mul(height, height), 2, RoundingMode.HALF_UP);
            }
        } else {
            return new BigDecimal(-1);
        }
    }
}
