package com.bonus.canteen.core.nutrition.mobile.controller;

import cn.hutool.core.text.CharSequenceUtil;
import com.bonus.canteen.core.nutrition.common.dto.HealthCustMedicalReportEditDTO;
import com.bonus.canteen.core.nutrition.common.vo.HealthCustMedicalReportProjectVO;
import com.bonus.canteen.core.nutrition.common.vo.HealthMedicalReportProjectVO;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.canteen.core.common.utils.HeaderFetchUtil;
import com.bonus.canteen.core.nutrition.common.service.HealthCustMedicalReportService;
import com.bonus.canteen.core.nutrition.common.service.HealthMedicalReportTmplService;
import com.bonus.canteen.core.nutrition.common.vo.HealthCustMedicalReportVO;
import com.bonus.canteen.core.nutrition.common.vo.HealthMedicalReportTmplVO;
import com.bonus.common.core.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping({"/health/mobile/medicalreport"})
@Api(
   tags = {"mobile-移动端人员体检报告"}
)
public class HealthMobileMedicalReportController {
   @Autowired
   @Lazy
   private HealthCustMedicalReportService healthCustMedicalReportService;
   @Autowired
   @Lazy
   private HealthMedicalReportTmplService healthMedicalReportTmplService;

   @PostMapping({"/list-medicalreport"})
//   @RequiresGuest
   @ApiOperation("查询人员体检报告记录")
   public List<HealthCustMedicalReportVO> listMedicalReport(@RequestHeader Map<String, String> headers) {
      return this.healthCustMedicalReportService.listMedicalReport(this.getOpenidByHeaders(headers), this.getSourceTypeByHeaders(headers));
   }

   @PostMapping({"/get-medicalreport-info"})
//   @RequiresGuest
   @ApiOperation("根据体检id查询报告详情")
   public List<HealthCustMedicalReportProjectVO> getMedicalReportInfo(@RequestBody HealthCustMedicalReportEditDTO dto) {
      return this.healthCustMedicalReportService.getMedicalReportInfo(dto.getMedicalId());
   }

   @PostMapping({"/list-template"})
//   @RequiresGuest
   @ApiOperation("查询体检报告模板")
   public List<HealthMedicalReportTmplVO> listTemplate() {
      return this.healthMedicalReportTmplService.listTemplate();
   }

   @PostMapping({"/get-template-info"})
//   @RequiresGuest
   @ApiOperation("根据体检报告模板id查询模板详情")
   public List<HealthMedicalReportProjectVO> getTemplateInfo(@RequestBody HealthMedicalReportTmplVO vo) {
      return this.healthMedicalReportTmplService.getTemplateInfoByTmplId(vo.getMedicalTemplateId());
   }

   @PostMapping({"/edit-medicalreport"})
//   @RequiresGuest
   @ApiOperation("编辑人员体检报告")
   public AjaxResult editMedicalReport(@RequestBody HealthCustMedicalReportEditDTO dto) {
      return AjaxResult.success(this.healthCustMedicalReportService.editMedicalReport(dto));
   }

   @PostMapping({"/delete-medicalreport"})
//   @RequiresGuest
   @ApiOperation("删除人员体检报告")
   public void deleteMedicalReport(@RequestBody HealthCustMedicalReportEditDTO dto) {
      this.healthCustMedicalReportService.deleteMedicalReport(dto.getMedicalId());
   }

   protected String getOpenidByHeaders(Map<String, String> headers) {
      return HeaderFetchUtil.getValueFromHeadersIgnoreCase(headers, "openid");
   }

   protected Integer getSourceTypeByHeaders(Map<String, String> headers) {
      String value = HeaderFetchUtil.getValueFromHeadersIgnoreCase(headers, "source-type");
      return CharSequenceUtil.isNotBlank(value) ? Integer.valueOf(value) : null;
   }
}
