package com.xinelu.applet.controller.consultationInfo;

import com.xinelu.manage.dto.consultationInfo.ConsultationInfoDTO;
import com.xinelu.manage.service.consultationInfo.IConsultationInfoService;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.domain.R;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.custominterface.Insert;
import com.xinelu.manage.domain.consultationInfo.ConsultationInfo;
import com.xinelu.manage.domain.hospitalpersoninfo.HospitalPersonInfo;
import com.xinelu.manage.service.hospitalpersoninfo.IHospitalPersonInfoService;
import com.xinelu.manage.vo.consultationInfo.ConsultationInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
 * 在线问诊-问诊信息Controller
 *
 * @author xinelu
 * @date 2023-09-25
 */
@Api(tags = "在线问诊控制器")
@RestController
@RequestMapping("/nurseApplet/consultationInfo")
public class ConsultationInfoController extends BaseController {
    @Autowired
    private IConsultationInfoService consultationInfoService;
    @Autowired
    private IHospitalPersonInfoService hospitalPersonInfoService;

    /**
     * 查询在线问诊-问诊信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ConsultationInfoDTO consultationInfoDTO) {
        startPage();
        List<ConsultationInfoVO> list = consultationInfoService.selectConsultationInfoList(consultationInfoDTO);
        return getDataTable(list);
    }

    /**
     * 获取在线问诊-问诊信息详细信息
     */
    @ApiOperation(value = "获取在线问诊-问诊信息详细信息")
    @GetMapping(value = "/getHospitalPersonInfo")
    public R<HospitalPersonInfo> getHospitalPersonInfo(@RequestParam("personCode") String personCode,
                                                       @RequestParam("status") String status) {
        return R.ok(hospitalPersonInfoService.getByPersonCode(personCode,status));
    }

    /**
     * 获取在线问诊-问诊信息详细信息
     */
    @ApiOperation(value = "获取在线问诊-问诊信息详细信息")
    @GetMapping(value = "/{id}")
    public R<ConsultationInfoDTO> getInfo(@PathVariable("id") Long id) {
        return R.ok(consultationInfoService.selectConsultationInfoById(id));
    }

    /**
     * 新增在线问诊-问诊信息
     */
    @PostMapping
    public AjaxResult add(@Validated(Insert.class) @RequestBody ConsultationInfoDTO consultationInfoDTO) {
        return consultationInfoService.insertConsultationInfo(consultationInfoDTO);
    }

    /**
     * 修改在线问诊-问诊信息(修改问诊状态)
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ConsultationInfo consultationInfo) {
        return toAjax(consultationInfoService.updateConsultationInfo(consultationInfo));
    }

    /**
     * 问诊资料上传接口
     */
    @PostMapping("/uploadConsultationFile")
    public AjaxResult uploadConsultationFile(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        if (Objects.isNull(multipartFile) || StringUtils.isBlank(multipartFile.getOriginalFilename())) {
            return AjaxResult.error("当前文件不存在，无法上传！");
        }
        if (multipartFile.getOriginalFilename().contains(Constants.EMPTY)) {
            return AjaxResult.error("当前文件名含有空格，请先去除空格在上传！");
        }
        return consultationInfoService.uploadConsultationFile(multipartFile);
    }
}
