package com.ylx.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.core.domain.R;
import com.ylx.common.exception.ServiceException;
import com.ylx.order.domain.dto.AdminOrderDispatchDTO;
import com.ylx.order.domain.dto.AdminOrderDispatchMerchantQueryDTO;
import com.ylx.order.domain.dto.AdminOrderQueryDTO;
import com.ylx.order.domain.vo.AdminOrderDetailVO;
import com.ylx.order.domain.vo.AdminOrderDispatchMerchantVO;
import com.ylx.order.domain.vo.AdminOrderPageVO;
import com.ylx.order.domain.vo.AdminOrderServiceCategoryVO;
import com.ylx.order.service.AdminOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
@RequestMapping("/admin/order")
@Api(tags = {"后台服务订单"})
public class AdminOrderController {

    @Resource
    private AdminOrderService adminOrderService;

    /**
     * 分页查询服务订单
     *
     * @param page
     * @param dto
     * @return R<Page<AdminOrderPageVO>>
     */
    @GetMapping("/page")
    @ApiOperation("分页查询服务订单")
    public R<Page<AdminOrderPageVO>> page(Page<AdminOrderPageVO> page, AdminOrderQueryDTO dto) {
        try {
            return R.ok(adminOrderService.page(page, dto));
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("分页查询服务订单异常", e);
            return R.fail("分页查询服务订单失败");
        }
    }

    /**
     * 分页查询回收站服务订单
     *
     * @param page
     * @param dto
     * @return R<Page<AdminOrderPageVO>>
     */
    @GetMapping("/recycle/page")
    @ApiOperation("分页查询回收站服务订单")
    public R<Page<AdminOrderPageVO>> recyclePage(Page<AdminOrderPageVO> page, AdminOrderQueryDTO dto) {
        try {
            return R.ok(adminOrderService.recyclePage(page, dto));
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("分页查询回收站服务订单异常", e);
            return R.fail("分页查询回收站服务订单失败");
        }
    }

    /**
     * 查询服务分类选项
     *
     * @return R<List<AdminOrderServiceCategoryVO>>
     */
    @GetMapping("/serviceCategory/options")
    @ApiOperation("查询服务分类选项")
    public R<List<AdminOrderServiceCategoryVO>> serviceCategoryOptions() {
        try {
            return R.ok(adminOrderService.listServiceCategoryOptions());
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("查询服务分类选项异常", e);
            return R.fail("查询服务分类选项失败");
        }
    }

    /**
     * 根据订单ID逻辑删除服务订单
     *
     * @param id 订单ID
     * @return R<?>
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据订单ID逻辑删除服务订单")
    public R<?> delete(@PathVariable("id") Long id) {
        try {
            adminOrderService.deleteById(id);
            return R.ok("删除成功");
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("逻辑删除服务订单异常，订单ID：{}", id, e);
            return R.fail("删除服务订单失败");
        }
    }

    /**
     * 根据订单ID恢复回收站服务订单
     *
     * @param id 订单ID
     * @return R<?>
     */
    @PostMapping("/{id}/restore")
    @ApiOperation("根据订单ID恢复回收站服务订单")
    public R<?> restore(@PathVariable("id") Long id) {
        try {
            adminOrderService.restoreById(id);
            return R.ok("恢复成功");
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("恢复回收站服务订单异常，订单ID：{}", id, e);
            return R.fail("恢复服务订单失败");
        }
    }

    /**
     * 根据订单ID发起退款申请
     *
     * @param id 订单ID
     * @return R<?>
     */
    @PostMapping("/{id}/refund/apply")
    @ApiOperation("根据订单ID发起退款申请")
    public R<?> applyRefund(@PathVariable("id") Long id) {
        try {
            adminOrderService.applyRefund(id);
            return R.ok("退款申请已发起");
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("发起退款申请异常，订单ID：{}", id, e);
            return R.fail("发起退款申请失败");
        }
    }

    /**
     * 根据订单ID查询后台订单详情
     *
     * @param id 订单ID
     * @return R<AdminOrderDetailVO>
     */
    @GetMapping("/{id}/detail")
    @ApiOperation("根据订单ID查询后台订单详情")
    public R<AdminOrderDetailVO> detail(@PathVariable("id") Long id) {
        try {
            return R.ok(adminOrderService.detail(id));
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("查询后台订单详情异常，订单ID：{}", id, e);
            return R.fail("查询后台订单详情失败");
        }
    }

    /**
     * 查询订单派单候选商户列表
     *
     * @param id 订单ID
     * @param dto 查询参数
     * @return R<List<AdminOrderDispatchMerchantVO>>
     */
    @GetMapping("/{id}/dispatch/merchant/list")
    @ApiOperation("查询订单派单候选商户列表")
    public R<List<AdminOrderDispatchMerchantVO>> dispatchMerchantList(@PathVariable("id") Long id, AdminOrderDispatchMerchantQueryDTO dto) {
        try {
            return R.ok(adminOrderService.listDispatchMerchants(id, dto));
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("查询订单派单候选商户列表异常，订单ID：{}", id, e);
            return R.fail("查询派单候选商户列表失败");
        }
    }

    /**
     * 确认派单
     *
     * @param id 订单ID
     * @param dto 派单参数
     * @return R<?>
     */
    @PostMapping("/{id}/dispatch")
    @ApiOperation("确认派单")
    public R<?> dispatch(@PathVariable("id") Long id, @RequestBody AdminOrderDispatchDTO dto) {
        try {
            adminOrderService.dispatch(id, dto);
            return R.ok("派单成功");
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("确认派单异常，订单ID：{}", id, e);
            return R.fail("确认派单失败");
        }
    }
}
