package com.bonus.canteen.core.nutrition.common.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bonus.canteen.core.nutrition.common.dto.HealthCustMedicalReportEditDTO;
import com.bonus.canteen.core.nutrition.common.mapper.HealthCustMedicalReportDetailMapper;
import com.bonus.canteen.core.nutrition.common.mapper.HealthCustMedicalReportProMapper;
import com.bonus.canteen.core.nutrition.common.model.HealthCustMedicalReport;
import com.bonus.canteen.core.nutrition.common.model.HealthCustMedicalReportDetail;
import com.bonus.canteen.core.nutrition.common.model.HealthCustMedicalReportProject;
import com.bonus.canteen.core.nutrition.common.vo.HealthCustMedicalReportDetailVO;
import com.bonus.canteen.core.nutrition.common.vo.HealthCustMedicalReportProjectVO;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.canteen.core.customer.api.CustCasualApi;
import com.bonus.canteen.core.nutrition.common.mapper.HealthCustMedicalReportMapper;
import com.bonus.canteen.core.nutrition.common.vo.HealthCustMedicalReportVO;
import com.bonus.common.houqin.i18n.I18n;
import com.bonus.common.houqin.utils.LeBeanUtil;
import com.bonus.common.houqin.utils.id.Id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

@Service
public class HealthCustMedicalReportService {
   @Autowired
   private CustCasualApi custCasualApi;
   @Resource
   private HealthCustMedicalReportMapper healthCustMedicalReportMapper;
   @Resource
   private HealthCustMedicalReportProMapper healthCustMedicalReportProMapper;
   @Resource
   private HealthCustMedicalReportDetailMapper healthCustMedicalReportDetailMapper;

   public List<HealthCustMedicalReportVO> listMedicalReport(String openid, Integer sourceType) {
      Long custId = this.custCasualApi.getCustIdByOpenidAndSourceType(openid, sourceType);
      if (ObjectUtil.isEmpty(custId)) {
         throw new ServiceException(I18n.getMessage("nutrition.no-cust", new Object[0]));
      } else {
         List<HealthCustMedicalReportVO> reportVOList = this.healthCustMedicalReportMapper.listMedicalReport(custId);
         return reportVOList;
      }
   }

   public List<HealthCustMedicalReportProjectVO> getMedicalReportInfo(Long medicalId) {
      List<HealthCustMedicalReportProjectVO> reportProjectVOS = this.healthCustMedicalReportMapper.listCustReport(medicalId);
      if (CollectionUtil.isEmpty(reportProjectVOS)) {
         return reportProjectVOS;
      } else {
         Iterator var3 = reportProjectVOS.iterator();

         while(var3.hasNext()) {
            HealthCustMedicalReportProjectVO project = (HealthCustMedicalReportProjectVO)var3.next();
            List<HealthCustMedicalReportDetailVO> reportDetailVOS = this.healthCustMedicalReportDetailMapper.listCustReportDetail(project.getMedicalProjectId(), medicalId);
            project.setMedicalReportDetailVOList(reportDetailVOS);
         }

         return reportProjectVOS;
      }
   }

   public Long editMedicalReport(HealthCustMedicalReportEditDTO reportEditDTO) {
      Long count = this.healthCustMedicalReportMapper.selectCount(Wrappers.lambdaQuery(HealthCustMedicalReport.class)
              .eq(HealthCustMedicalReport::getMedicalDate, reportEditDTO.getMedicalDate())
              .eq(HealthCustMedicalReport::getMedicalTemplateId, reportEditDTO.getMedicalTemplateId())
              .eq(HealthCustMedicalReport::getCustId, reportEditDTO.getCustId()));
      Long medicalId = reportEditDTO.getMedicalId();
      if (ObjectUtil.isNotEmpty(medicalId)) {
         if (count > 1L) {
            throw new ServiceException(I18n.getMessage("nutrition.report-existing", new Object[0]));
         }

         this.deleteMedicalReport(medicalId);
      } else {
         if (count > 0L) {
            throw new ServiceException(I18n.getMessage("nutrition.report-existing", new Object[0]));
         }

         medicalId = Id.next();
         reportEditDTO.setMedicalId(medicalId);
      }

      HealthCustMedicalReport report = (HealthCustMedicalReport) LeBeanUtil.copyCreateProperties(reportEditDTO, HealthCustMedicalReport.class);
      this.healthCustMedicalReportMapper.insert(report);
      if (ObjectUtil.isEmpty(reportEditDTO.getReportProjectVOS())) {
         return medicalId;
      } else {
         Iterator var5 = reportEditDTO.getReportProjectVOS().iterator();

         while(true) {
            HealthCustMedicalReportProjectVO projectEditDTO;
            do {
               if (!var5.hasNext()) {
                  return medicalId;
               }

               projectEditDTO = (HealthCustMedicalReportProjectVO)var5.next();
               HealthCustMedicalReportProject reportProject = new HealthCustMedicalReportProject();
               reportProject.setMedicalId(medicalId);
               reportProject.setMedicalProjectId(projectEditDTO.getMedicalProjectId());
               reportProject.setMedicalProjectName(projectEditDTO.getMedicalProjectName());
               this.healthCustMedicalReportProMapper.insert(reportProject);
            } while(CollectionUtil.isEmpty(projectEditDTO.getMedicalReportDetailVOList()));

            Iterator var8 = projectEditDTO.getMedicalReportDetailVOList().iterator();

            while(var8.hasNext()) {
               HealthCustMedicalReportDetailVO detailEditDTO = (HealthCustMedicalReportDetailVO)var8.next();
               HealthCustMedicalReportDetail reportDetail = new HealthCustMedicalReportDetail();
               reportDetail.setMedicalId(medicalId);
               reportDetail.setMedicalProjectId(projectEditDTO.getMedicalProjectId());
               reportDetail.setMedicalProjectDetailId(detailEditDTO.getMedicalProjectDetailId());
               reportDetail.setMedicalProjectDetailName(detailEditDTO.getMedicalProjectDetailName());
               reportDetail.setResult(detailEditDTO.getResult());
               this.healthCustMedicalReportDetailMapper.insert(reportDetail);
            }
         }
      }
   }

   public void deleteMedicalReport(Long medicalId) {
      this.healthCustMedicalReportMapper.deleteCustReport(medicalId);
      this.healthCustMedicalReportProMapper.delete((Wrapper)Wrappers.lambdaQuery(HealthCustMedicalReportProject.class)
              .eq(HealthCustMedicalReportProject::getMedicalId, medicalId));
      this.healthCustMedicalReportMapper.delete((Wrapper)Wrappers.lambdaQuery(HealthCustMedicalReport.class).eq(HealthCustMedicalReport::getMedicalId, medicalId));
   }

}
