package com.xinelu.applet.controller.nurseapplogin;

import com.xinelu.applet.dto.appletlogin.StationItemInfoDTO;
import com.xinelu.applet.service.nurseapplogin.NurseAppLoginService;
import com.xinelu.applet.vo.nursepersonapplogin.OrderAndItemVO;
import com.xinelu.common.annotation.MobileRequestAuthorization;
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.Query;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

;

/**
 * @Description APP登录注册控制器
 * @Author zh
 * @Date 2022-10-12
 */
@RestController
@RequestMapping("/nurseApp/login")
public class NurseAppLoginController extends BaseController {

    @Resource
    private NurseAppLoginService nurseAppLoginService;


    /**
     * 判断用户是否完善信息-微信小程序和APP共用
     *
     * @param patientId 用户id
     * @return 结果
     */
    @MobileRequestAuthorization
    @GetMapping("/AppIdentification")
    public AjaxResult getIdentification(Long patientId) {
        if (Objects.isNull(patientId)) {
            return AjaxResult.error("当前用户信息不存在，无法完善个人信息！");
        }
        return nurseAppLoginService.getIdentification(patientId);
    }

    /**
     * 查询护理人列表信息-护理服务查询个人信息使用（会员小程序和App共用）
     *
     * @param patientId 用户id
     * @return 护理人列表集合
     */
    @MobileRequestAuthorization
    @GetMapping("/getAppPatientList")
    public AjaxResult getPatientList(Long patientId) {
        if (Objects.isNull(patientId)) {
            return AjaxResult.error("用户信息不能为空!");
        }
        return nurseAppLoginService.getPatientVOList(patientId);
    }

    /**
     * 根据护理站id和护理站项目id查询对应的护理项目信息（会员App和小程序共用）
     *
     * @param itemInfoDTO 输入参数
     * @return 护理站项目信息集合
     */
    @MobileRequestAuthorization
    @GetMapping("/getAppStationItemInfo")
    public AjaxResult getStationItemAndConsumableInfo(@Validated(Query.class) StationItemInfoDTO itemInfoDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new ServiceException(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        if (Objects.isNull(itemInfoDTO)) {
            return AjaxResult.error("护理站信息不能为空！");
        }
        return nurseAppLoginService.getAppStationItemAndConsumableInfo(itemInfoDTO);
    }

    /**
     * APP个人中心查询
     *
     * @param patientId 用户编号id
     * @return 结果
     */
    @MobileRequestAuthorization
    @GetMapping("/appPersonal")
    public AjaxResult nurseAppPersonal(Long patientId, @RequestHeader("region") String region) {
        if (Objects.isNull(patientId)) {
            return AjaxResult.error("用户信息不能为空!");
        }
        return nurseAppLoginService.nurseAppPersonal(patientId, region);
    }

    /**
     * App查询预约服务订单
     *
     * @param patientId   用户id
     * @param orderStatus 预约订单状态
     * @return 结果
     */
    @MobileRequestAuthorization
    @GetMapping("/appServiceOrder")
    public TableDataInfo selectAppServiceOrder(Long patientId, String orderStatus) {
        startPage();
        List<OrderAndItemVO> appointmentOrderDetails = nurseAppLoginService.selectAppServiceOrderItem(patientId, orderStatus);
        return getDataTable(appointmentOrderDetails);
    }

    /**
     * 小程序取消订单
     *
     * @param appointmentOrderId      预约订单id主键
     * @param cancelAppointmentReason 取消预约原因
     * @return 结果
     */
    @MobileRequestAuthorization
    @PostMapping("/cancellationOrder")
    public AjaxResult updateAppointmentOrder(Long appointmentOrderId, String cancelAppointmentReason) {
        if (Objects.isNull(appointmentOrderId)) {
            return AjaxResult.error("预约订单信息不能为空!");
        }
        return nurseAppLoginService.updateAppointmentOrder(appointmentOrderId, cancelAppointmentReason);
    }
}
