package com.xinelu.applet.service.nurseappletpersonworkorder.Impl;


import com.xinelu.applet.dto.nursepersonapplogin.OrderFallbackDTO;
import com.xinelu.applet.service.nurseappletpersonworkorder.NurseAppletPersonWorkOrderService;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.enums.OrderStatusEnum;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.manage.mapper.appointmentorder.AppointmentOrderMapper;
import com.xinelu.manage.mapper.appointmentorderdetails.AppointmentOrderDetailsMapper;
import com.xinelu.manage.vo.appointmentorder.AppointmentReceivingOrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 护理员工单ServiceImpl
 *
 * @author zh
 * @date 2023-03-30
 */
@Slf4j
@Service
public class NurseAppletPersonWorkOrderServiceImpl implements NurseAppletPersonWorkOrderService {
    @Resource
    private AppointmentOrderMapper appointmentOrderMapper;
    @Resource
    private AppointmentOrderDetailsMapper appointmentOrderDetailsMapper;

    /**
     * 接单
     *
     * @param orderFallbackDTO 订单信息
     * @return AjaxResult
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult receiveOrders(OrderFallbackDTO orderFallbackDTO) {
        AppointmentReceivingOrderVO appointmentReceivingOrder = appointmentOrderDetailsMapper.getAppointmentReceivingOrder(orderFallbackDTO.getAppointmentOrderNo());
        if (Objects.isNull(appointmentReceivingOrder)) {
            return AjaxResult.error("当前订单不存在，无法接单！");
        }
        int update = appointmentOrderMapper.updateAppointmentOrderStatus(OrderStatusEnum.NOT_FINISH.getInfo(), orderFallbackDTO.getAppointmentOrderNo());
        if (update < 0) {
            throw new ServiceException("接单失败，请联系管理员！");
        }
        return AjaxResult.success();
    }
}
