package com.xinelu.manage.controller.appointmentorder;


import com.xinelu.common.annotation.Log;
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.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.appointmentorder.AppointmentOrder;
import com.xinelu.manage.service.appointmentorder.IAppointmentOrderService;
import com.xinelu.manage.vo.appointmentorder.AppointmentOrderVO;
import com.xinelu.manage.vo.appointmentorder.PersonAndDepartmentVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 预约订单信息Controller
 *
 * @author 纪寒
 * @date 2022-09-05
 */
@RestController
@RequestMapping("/system/appointmentOrder")
public class AppointmentOrderController extends BaseController {
    @Resource
    private IAppointmentOrderService appointmentOrderService;

    /**
     * 查询预约订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:appointmentOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppointmentOrderVO appointmentOrder) {
        startPage();
        List<AppointmentOrderVO> list = appointmentOrderService.selectAppointmentOrderList(appointmentOrder);
        return getDataTable(list);
    }

    /**
     * 导出预约订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:appointmentOrder:export')")
    @Log(title = "预约订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppointmentOrderVO appointmentOrder) {
        List<AppointmentOrderVO> list = appointmentOrderService.selectAppointmentOrderList(appointmentOrder);
        ExcelUtil<AppointmentOrderVO> util = new ExcelUtil<>(AppointmentOrderVO.class);
        util.exportExcel(response, list, "预约订单信息数据");
    }

    /**
     * 获取预约订单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:appointmentOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(appointmentOrderService.selectAppointmentOrderById(id));
    }

    /**
     * 新增预约订单信息
     */
    @PreAuthorize("@ss.hasPermi('system:appointmentOrder:add')")
    @Log(title = "预约订单信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody AppointmentOrder appointmentOrder) {
        return toAjax(appointmentOrderService.insertAppointmentOrder(appointmentOrder));
    }

    /**
     * 修改预约订单信息
     */
    @PreAuthorize("@ss.hasPermi('system:appointmentOrder:edit')")
    @Log(title = "预约订单信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody AppointmentOrder appointmentOrder) {
        return toAjax(appointmentOrderService.updateAppointmentOrder(appointmentOrder));
    }

    /**
     * 逻辑删除预约订单信息
     */
    @PreAuthorize("@ss.hasPermi('system:appointmentOrder:remove')")
    @Log(title = "预约订单信息", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return appointmentOrderService.updateAppointmentOrderByIds(ids);
    }

    /**
     * 派单 通过订单id，护理站人员id修改订单信息
     *
     * @param appointmentOrder 订单信息
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('system:appointmentOrder:dispatch')")
    @PostMapping("/dispatch")
    public AjaxResult getDispatchStationPerson(@RequestBody AppointmentOrder appointmentOrder) {
        if (Objects.isNull(appointmentOrder.getNurseStationPersonId())) {
            return AjaxResult.error("护理站人员不能为空");
        }
        if (StringUtils.isBlank(appointmentOrder.getOrderNo())) {
            return AjaxResult.error("订单信息不能为空");
        }
        return appointmentOrderService.updateOrderStatusByOrderNo(appointmentOrder);
    }

    /**
     * 通过订单id查询护理所属护理站人员
     *
     * @param personAndDepartment 订单id
     * @return AjaxResult
     */
    @GetMapping("getPerson")
    public TableDataInfo getNurseStationPerson(PersonAndDepartmentVO personAndDepartment) {
        startPage();
        return getDataTable(appointmentOrderService.getNurseStationPerson(personAndDepartment));
    }

    /**
     * 通过护理员id查询护理所属护理站人员信息
     *
     * @param nurseStationPersonId 护理员id
     * @return AjaxResult
     */
    @GetMapping("getPersonInfo")
    public AjaxResult getNurseStationPersonInfo(Long nurseStationPersonId) {
        if (Objects.isNull(nurseStationPersonId)) {
            return AjaxResult.error("请选择护理员信息！");
        }
        return appointmentOrderService.getNurseStationPersonInfo(nurseStationPersonId);
    }

    /**
     * 确认取消订单（废弃）
     *
     * @param appointmentOrderId 订单id
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('system:appointmentOrder:confirmCancel')")
    @PostMapping("/confirmCancel")
    public AjaxResult updateAppointmentOrder(Long appointmentOrderId) {
        if (Objects.isNull(appointmentOrderId)) {
            return AjaxResult.error("订单信息为空，请联系管理员！");
        }
        return appointmentOrderService.updateAppointmentOrder(appointmentOrderId);
    }

    /**
     * 院内陪护确认完成
     *
     * @param orderNo     订单编号
     * @param orderStatus 订单状态
     * @return AjaxResult
     */
    @PostMapping("/updateCompanionStatus")
    public AjaxResult updateCompanionOrderStatus(String orderNo, String orderStatus) {
        if (StringUtils.isBlank(orderNo) || StringUtils.isBlank(orderStatus)) {
            return AjaxResult.error("订单信息为空，请重新选择！");
        }
        return appointmentOrderService.updateCompanionOrderStatus(orderNo, orderStatus);
    }
}
