package com.ylx.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.core.domain.R;
import com.ylx.massage.service.AlarmService;
import com.ylx.order.domain.dto.*;
import com.ylx.order.domain.vo.merchant.MerchantCancelOrderDTO;
import com.ylx.order.domain.vo.merchant.MerchantOrderDetailVO;
import com.ylx.order.domain.vo.merchant.OrderCustomerPhoneVO;
import com.ylx.order.domain.vo.merchant.OrderPageVO;
import com.ylx.order.service.TOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/merchant/order")
@Api(tags = {"商户端订单模块"})
@Slf4j
@PreAuthorize("@merchantAuth.isMerchant()")
public class MerchantOrderController {

    @Resource
    private TOrderService orderService;
    @Resource
    private AlarmService alarmService;

    @ApiOperation("商户端查询订单分页列表")
    @PostMapping("/queryByDate")
    public R<Page<OrderPageVO>> queryOrderByDate(@Validated @RequestBody OrderDateQueryDTO dto) {
        Page<OrderPageVO> page = orderService.queryMerchantOrderList(dto);
        return R.ok(page);
    }

    @ApiOperation("商户端接单接口")
    @PostMapping("/accept")
    public R<Void> acceptOrder(@Validated @RequestBody OrderAcceptDTO dto) {
        this.orderService.acceptOrder(dto);
        return R.ok();
    }

    @ApiOperation("商户端拒绝接单接口")
    @PostMapping("/reject")
    public R<Void> rejectOrder(@Validated @RequestBody OrderRejectDTO dto) {
        this.orderService.rejectOrder(dto);
        return R.ok();
    }

    @PostMapping("/getCustomerPhone")
    @ApiOperation("根据订单ID获取客户完整手机号（用于电话联系）")
    public R<OrderCustomerPhoneVO> getCustomerPhone(@Validated @RequestBody OrderPhoneQueryDTO dto) {
        OrderCustomerPhoneVO vo = orderService.getCustomerPhoneByOrderId(dto);
        return R.ok(vo);
    }

    @PostMapping("/arriveSign")
    @ApiOperation("商户签到到达客户地址")
    public R<Void> orderArriveSign(@Validated @RequestBody MerchantOrderOperateDTO dto) {
        this.orderService.merchantArriveSign(dto);
        return R.ok();
    }

    @PostMapping("/cancelAfterReceive")
    @ApiOperation("商户接单后取消订单")
    public R<Void> merchantCancelOrder(@Validated @RequestBody MerchantCancelOrderDTO dto) {
        this.orderService.merchantCancelOrderToAfterSale(dto);
        return R.ok();
    }

    @PostMapping("/startService")
    @ApiOperation("商户开始服务，订单流转服务中")
    public R<Void> startService(@Validated @RequestBody MerchantOrderOperateDTO dto) {
        this.orderService.startService(dto);
        return R.ok();
    }

    @PostMapping("/finishService")
    @ApiOperation("商户完成服务，订单流转已完成")
    public R<Void> finishService(@Validated @RequestBody MerchantOrderOperateDTO dto) {
        this.orderService.finishService(dto);
        return R.ok();
    }

    @PostMapping("/detail")
    @ApiOperation("商户端订单详情接口")
    public R<MerchantOrderDetailVO> getMerchantOrderDetail(@Validated @RequestBody MerchantOrderDetailDTO dto) {
        MerchantOrderDetailVO vo = this.orderService.getMerchantOrderDetail(dto);
        return R.ok(vo);
    }

    @PostMapping("/alarm")
    @ApiOperation("商户端订单一键报警")
    public R<Boolean> submitAlarm(@Validated @RequestBody AlarmSubmitDTO dto) {
        Boolean result = this.alarmService.submitAlarm(dto);
        return R.ok(result);
    }

}
