package com.xinelu.manage.controller.appointmentorderdetails;


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.appointmentorderdetails.AppointmentOrderDetails;
import com.xinelu.manage.dto.appointmentorderdetails.AppointmentOrderDetailsDTO;
import com.xinelu.manage.service.appointmentorderdetails.IAppointmentOrderDetailsService;
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/appointmentOrderDetails")
public class AppointmentOrderDetailsController extends BaseController {
    @Resource
    private IAppointmentOrderDetailsService appointmentOrderDetailsService;

    /**
     * 查询预约订单明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:appointmentOrderDetails:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppointmentOrderDetails appointmentOrderDetails) {
        startPage();
        List<AppointmentOrderDetails> list = appointmentOrderDetailsService.selectAppointmentOrderDetailsList(appointmentOrderDetails);
        return getDataTable(list);
    }

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

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

    /**
     * 新增预约订单明细
     */
    @PreAuthorize("@ss.hasPermi('system:appointmentOrderDetails:add')")
    @Log(title = "预约订单明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody AppointmentOrderDetails appointmentOrderDetails) {
        return toAjax(appointmentOrderDetailsService.insertAppointmentOrderDetails(appointmentOrderDetails));
    }

    /**
     * 修改预约订单明细
     */
    @PreAuthorize("@ss.hasPermi('system:appointmentOrderDetails:edit')")
    @Log(title = "预约订单明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody AppointmentOrderDetails appointmentOrderDetails) {
        return toAjax(appointmentOrderDetailsService.updateAppointmentOrderDetails(appointmentOrderDetails));
    }

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

    /**
     * 获取预约订单明细详细信息
     */
    @GetMapping(value = "/detailed")
    public AjaxResult getAppointmentOrderDetailsByDetailed(String orderNo) {
        if (StringUtils.isEmpty(orderNo)) {
            return AjaxResult.error("订单编号不能为空");
        }
        return AjaxResult.success(appointmentOrderDetailsService.getAppointmentOrderDetailsByDetailed(orderNo));
    }

    /**
     * 根据订单编号查询预约订单信息
     *
     * @param orderNo 订单编号
     * @return 信息
     */
    @GetMapping("/getCancelOrderInfo")
    public AjaxResult getCancelAppointmentOrderInfo(String orderNo) {
        if (StringUtils.isBlank(orderNo)) {
            return AjaxResult.error("请选择预约订单信息！");
        }
        return appointmentOrderDetailsService.getCancelAppointmentOrderInfo(orderNo);
    }


    /**
     * 修改陪诊陪护预约订单信息
     *
     * @param appointmentOrderDetailsDTO 订单信息
     * @return com.xinyilu.common.core.domain.AjaxResult
     */
    @PostMapping("/updateAppointmentOrderInfo")
    public AjaxResult updateAppointmentOrderInfo(@RequestBody AppointmentOrderDetailsDTO appointmentOrderDetailsDTO) {
        if (Objects.isNull(appointmentOrderDetailsDTO)) {
            return AjaxResult.error("保存预约订单信息不能为空！");
        }
        if (Objects.isNull(appointmentOrderDetailsDTO.getAppointOrderId()) || Objects.isNull(appointmentOrderDetailsDTO.getAppointOrderDetailsId())) {
            return AjaxResult.error("保存预约订单信息id不能为空！");
        }
        return appointmentOrderDetailsService.updateAppointmentOrderInfo(appointmentOrderDetailsDTO);
    }
}
