package com.xinelu.applet.controller.healthconsultation;

import com.xinelu.applet.service.healthconsultation.HealthConsultationService;
import com.xinelu.applet.vo.healthconsultation.HealthConsultationOrderDTO;
import com.xinelu.common.annotation.MobileRequestAuthorization;
import com.xinelu.common.annotation.RepeatSubmit;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.custominterface.Insert;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.manage.domain.hospitalpersoninfo.HospitalPersonInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @Description APP及小程序健康咨询接口
 * @Author zh
 * @Date 2023-03-07
 */
@Api(tags = "APP及小程序健康咨询接口")
@RestController
@RequestMapping("/nurseApp/healthConsultation")
public class HealthConsultationController extends BaseController {
    @Resource
    private HealthConsultationService healthConsultationService;

    /**
     * 健康咨询-预约医生
     */
    @MobileRequestAuthorization
    @GetMapping("/selectDepartment")
    public TableDataInfo selectDepartment() {
        return healthConsultationService.selectDepartment();
    }

    /**
     * 健康咨询-预约医生(无分页)
     */
    @MobileRequestAuthorization
    @GetMapping("/selectDepartmentNoPage")
    public AjaxResult selectDepartmentNoPage() {
        return AjaxResult.success(healthConsultationService.selectDepartmentNoPage());
    }

    /**
     * 健康咨询-预约医生-单查科室人员
     */
    @MobileRequestAuthorization
    @GetMapping("/selectHospitalPerson")
    public TableDataInfo selectHospitalPerson(HospitalPersonInfo hospitalPersonInfo) {
        startPage();
        List<HospitalPersonInfo> list = healthConsultationService.selectHospitalPerson(hospitalPersonInfo);
        return getDataTable(list);
    }

    /**
     * 健康咨询-信息确认
     */
    @MobileRequestAuthorization
    @GetMapping("/informationConfirmation")
    public AjaxResult informationConfirmation(Long patientId) {
        if (Objects.isNull(patientId)) {
            return AjaxResult.error("用户信息为空！");
        }
        return healthConsultationService.informationConfirmation(patientId);
    }


    /**
     * 手机App和微信小程序健康咨询确认订单方法
     *
     * @param healthConsultationOrderDTO 健康咨询订单
     * @return 结果
     */
    @ApiOperation(value = "手机App和微信小程序健康咨询确认订单方法")
    @MobileRequestAuthorization
    @RepeatSubmit
    @PostMapping("/addHealthConsultationOrder")
    public AjaxResult addHealthConsultationOrder(@Validated(Insert.class) @RequestBody HealthConsultationOrderDTO healthConsultationOrderDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new ServiceException(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        if (Objects.nonNull(healthConsultationOrderDTO.getTotalPrice()) && healthConsultationOrderDTO.getTotalPrice().compareTo(BigDecimal.ZERO) <= 0) {
            return AjaxResult.error("预约金额不正确，无法下单，请输入正确的预约金额！");
        }
        return healthConsultationService.insertHealthConsultationOrder(healthConsultationOrderDTO);
    }
}
