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

import java.util.ArrayList;
import java.util.List;

import com.bonus.canteen.core.health.domain.HealthMedicalReportDetail;
import com.bonus.canteen.core.health.domain.HealthMedicalReportDetails;
import com.bonus.canteen.core.health.domain.HealthPersonMedicalReportDetails;
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.HealthPersonMedicalReportMapper;
import com.bonus.canteen.core.health.domain.HealthPersonMedicalReport;
import com.bonus.canteen.core.health.service.IHealthPersonMedicalReportService;

/**
 * 人员体检报告Service业务层处理
 *
 * @author xsheng
 * @date 2025-05-25
 */
@Service
public class HealthPersonMedicalReportServiceImpl implements IHealthPersonMedicalReportService {
    @Autowired
    private HealthPersonMedicalReportMapper healthPersonMedicalReportMapper;

    /**
     * 查询人员体检报告
     *
     * @param id 人员体检报告主键
     * @return 人员体检报告
     */
    @Override
    public HealthPersonMedicalReport selectHealthPersonMedicalReportById(Long id) {
        return healthPersonMedicalReportMapper.selectHealthPersonMedicalReportById(id);
    }

    /**
     * 查询人员体检报告列表
     *
     * @param healthPersonMedicalReport 人员体检报告
     * @return 人员体检报告
     */
    @Override
    public List<HealthPersonMedicalReport> selectHealthPersonMedicalReportList(HealthPersonMedicalReport healthPersonMedicalReport) {
        return healthPersonMedicalReportMapper.selectHealthPersonMedicalReportList(healthPersonMedicalReport);
    }

    /**
     * 新增人员体检报告
     *
     * @param healthPersonMedicalReport 人员体检报告
     * @return 结果
     */
    @Override
    public int insertHealthPersonMedicalReport(HealthPersonMedicalReport healthPersonMedicalReport) {
        healthPersonMedicalReport.setCreateTime(DateUtils.getNowDate());
        try {
            return healthPersonMedicalReportMapper.insertHealthPersonMedicalReport(healthPersonMedicalReport);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }
    @Override
    public int insertHealthPersonMedicalReports(HealthPersonMedicalReportDetails healthPersonMedicalReport) {
        healthPersonMedicalReport.setCreateTime(DateUtils.getNowDate());
        try {
            healthPersonMedicalReport.setMedicalId(System.currentTimeMillis());
            healthPersonMedicalReportMapper.insertPersonReport(healthPersonMedicalReport);
            healthPersonMedicalReportMapper.updatePersonReport(healthPersonMedicalReport);
            List<HealthMedicalReportDetails> reportDatas=healthPersonMedicalReport.getReportDatas();
            if(reportDatas!=null&&reportDatas.size()>0){
                for(HealthMedicalReportDetails vo:reportDatas){
                    List<HealthMedicalReportDetail> details=vo.getHealthMedicalReportDetails();
                    vo.setMedicalId(healthPersonMedicalReport.getMedicalId());
                    healthPersonMedicalReportMapper.insertPersonReportProject(vo);
                    if(details!=null&&details.size()>0){
                        for(HealthMedicalReportDetail vo1:details){
                            vo1.setMedicalId(healthPersonMedicalReport.getMedicalId());
                            vo1.setMedicalProjectDetailId(vo1.getMedicalProjectId());
                            vo1.setMedicalProjectId(vo.getMedicalProjectId());
                            healthPersonMedicalReportMapper.insertPersonReportDetail(vo1);
                        }
                    }
                }
            }
            return 1;
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }
    @Override
    public int delHealthPersonMedicalReportList(HealthPersonMedicalReport healthPersonMedicalReport) {
        try{
             healthPersonMedicalReportMapper.delHealthPersonMedicalReportList(healthPersonMedicalReport);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 1;
    }

    /**
     * 修改人员体检报告
     *
     * @param healthPersonMedicalReport 人员体检报告
     * @return 结果
     */
    @Override
    public int updateHealthPersonMedicalReport(HealthPersonMedicalReport healthPersonMedicalReport) {
        healthPersonMedicalReport.setUpdateTime(DateUtils.getNowDate());
        try {
            return healthPersonMedicalReportMapper.updateHealthPersonMedicalReport(healthPersonMedicalReport);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除人员体检报告
     *
     * @param ids 需要删除的人员体检报告主键
     * @return 结果
     */
    @Override
    public int deleteHealthPersonMedicalReportByIds(Long[] ids) {
        return healthPersonMedicalReportMapper.deleteHealthPersonMedicalReportByIds(ids);
    }

    /**
     * 删除人员体检报告信息
     *
     * @param id 人员体检报告主键
     * @return 结果
     */
    @Override
    public int deleteHealthPersonMedicalReportById(Long id) {
        return healthPersonMedicalReportMapper.deleteHealthPersonMedicalReportById(id);
    }

    @Override
    public List<HealthMedicalReportDetails> getReportById(HealthMedicalReportDetails vo) {
        List<HealthMedicalReportDetails> detailsVos=new ArrayList<>();
        try{
            System.out.println("getMedicalId=============="+vo.getMedicalId());
            detailsVos=healthPersonMedicalReportMapper.getReportById(vo);
            System.out.println("detailsVos=============="+detailsVos.size());
            for(HealthMedicalReportDetails bean:detailsVos){
                List<HealthMedicalReportDetail> details=new  ArrayList<>();
                System.out.println("medicalProjectId=============="+bean.getMedicalProjectId());
                details=healthPersonMedicalReportMapper.getReportDetailById(bean);
                System.out.println("details=============="+details.size());
                bean.setHealthMedicalReportDetails(details);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return detailsVos;
    }
}
