package shop.alien.lawyer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LawyerConsultationOrder;
import shop.alien.entity.store.dto.LawyerConsultationOrderDto;
import shop.alien.entity.store.dto.PayStatusRequest;
import shop.alien.entity.store.vo.LawyerConsultationOrderVO;
import shop.alien.lawyer.service.LawyerConsultationOrderService;
import shop.alien.lawyer.service.OrderExpirationService;
import shop.alien.util.myBaticsPlus.QueryBuilder;

import java.util.List;
import java.util.Map;

/**
 * 咨询订单 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"律师平台-咨询订单(用户端)"})
@ApiSort(14)
@CrossOrigin
@RestController
@RequestMapping("/lawyer/consultationOrder")
@RequiredArgsConstructor
public class LawyerConsultationOrderController {

    @Value("${order.coefficient}")
    private String coefficient;
    @Value("${order.coefficients}")
    private String coefficients;

    private final LawyerConsultationOrderService consultationOrderService;
    private final OrderExpirationService orderExpirationService;


    @ApiOperation("分页查询咨询订单列表（支持按律师姓名关联查询，返回结果包含律师信息）")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页容（默认10）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "orderNumber", value = "订单编号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "lawyerName", value = "律师姓名（支持模糊查询，关联查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态, 0:待支付, 1:已支付, 2:进行中, 3:已完成, 4:已取消", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getConsultationOrderList")
    public R<IPage<LawyerConsultationOrderVO>> getConsultationOrderList(@RequestParam(defaultValue = "1") int page,
                                                                        @RequestParam(defaultValue = "10") int size,
                                                                        @RequestParam(required = false) String orderNumber,
                                                                        @RequestParam(required = false) Integer clientUserId,
                                                                        @RequestParam(required = false) Integer lawyerUserId,
                                                                        @RequestParam(required = false) String lawyerName,
                                                                        @RequestParam(required = false) Integer orderStatus) {
        log.info("LawyerConsultationOrderController.getConsultationOrderList?page={},size={},orderNumber={},clientUserId={},lawyerUserId={},lawyerName={},orderStatus={}",
                page, size, orderNumber, clientUserId, lawyerUserId, lawyerName, orderStatus);
        int pageNum = page > 0 ? page : 1;
        int pageSize = size > 0 ? size : 10;
        return consultationOrderService.getConsultationOrderList(pageNum, pageSize, orderNumber, clientUserId, lawyerUserId, lawyerName, orderStatus);
    }

    @ApiOperation("新增咨询订单")
    @ApiOperationSupport(order = 2)
    @PostMapping("/addConsultationOrder")
    public R<LawyerConsultationOrder> addConsultationOrder(@RequestBody LawyerConsultationOrder consultationOrder) {
        log.info("LawyerConsultationOrderController.addConsultationOrder?consultationOrder={}", consultationOrder);
        return consultationOrderService.addConsultationOrder(consultationOrder);
    }

    @ApiOperation("编辑咨询订单")
    @ApiOperationSupport(order = 3)
    @PostMapping("/editConsultationOrder")
    public R<LawyerConsultationOrder> editConsultationOrder(@RequestBody LawyerConsultationOrder consultationOrder) {
        log.info("开始编辑咨询订单，订单ID={}, 订单编号={}",
                consultationOrder != null ? consultationOrder.getId() : null,
                consultationOrder != null ? consultationOrder.getOrderNumber() : null);

        // 参数校验：订单ID不能为空
        if (consultationOrder == null || consultationOrder.getId() == null) {
            log.warn("编辑咨询订单失败：订单ID为空");
            return R.fail("订单ID不能为空");
        }

        // 记录修改前的关键信息
        Integer orderId = consultationOrder.getId();
        Integer newOrderStatus = consultationOrder.getOrderStatus();
        Integer newPaymentStatus = consultationOrder.getPaymentStatus();
        log.info("编辑咨询订单，订单ID={}, 新订单状态={}, 新支付状态={}",
                orderId, newOrderStatus, newPaymentStatus);

        R<LawyerConsultationOrder> result = consultationOrderService.editConsultationOrder(consultationOrder);

        if (result.isSuccess()) {
            log.info("编辑咨询订单成功，订单ID={}, 订单编号={}",
                    orderId, consultationOrder.getOrderNumber());
        } else {
            log.warn("编辑咨询订单失败，订单ID={}, 失败原因={}",
                    orderId, result.getMsg());
        }

        return result;
    }

    @ApiOperation("删除咨询订单（用户端）")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", dataType = "Integer", paramType = "query", required = true)
    })
    @DeleteMapping("/deleteConsultationOrder")
    public R<Boolean> deleteConsultationOrder(@RequestParam(value = "id", required = true) Integer id) {
        log.info("删除咨询订单，订单ID={}", id);
        if (id == null) {
            log.warn("删除咨询订单失败：订单ID为空");
            return R.fail("订单ID不能为空");
        }
        return consultationOrderService.deleteConsultationOrder(id);
    }

    @ApiOperation("保存或更新咨询订单")
    @ApiOperationSupport(order = 5)
    @PostMapping("/saveOrUpdate")
    public R<LawyerConsultationOrder> saveOrUpdate(@RequestBody LawyerConsultationOrder consultationOrder) {
        log.info("LawyerConsultationOrderController.saveOrUpdate?consultationOrder={}", consultationOrder);
        boolean result = consultationOrderService.saveOrUpdate(consultationOrder);
        if (result) {
            return R.data(consultationOrder);
        }
        return R.fail("操作失败");
    }

    @ApiOperation("通用列表查询")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "orderNumber", value = "订单编号（支持模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "problemScenarioId", value = "法律问题场景ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态, 0:待支付, 1:已支付, 2:进行中, 3:已完成, 4:已取消", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "paymentStatus", value = "支付状态, 0:未支付, 1:已支付", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_Start", value = "创建时间开始（范围查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_End", value = "创建时间结束（范围查询）", dataType = "String", paramType = "query")
    })
    @GetMapping("/getList")
    public R<List<LawyerConsultationOrder>> getList(@ModelAttribute LawyerConsultationOrder consultationOrder) {
        log.info("LawyerConsultationOrderController.getList?consultationOrder={}", consultationOrder);
        List<LawyerConsultationOrder> list = QueryBuilder.of(consultationOrder)
                .likeFields("orderNumber")  // 订单编号支持模糊查询
                .build()
                .list(consultationOrderService);
        return R.data(list);
    }

    @ApiOperation("通用分页查询")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页容（默认10）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "orderNumber", value = "订单编号（支持模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "problemScenarioId", value = "法律问题场景ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态, 0:待支付, 1:已支付, 2:进行中, 3:已完成, 4:已取消", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "paymentStatus", value = "支付状态, 0:未支付, 1:已支付", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_Start", value = "创建时间开始（范围查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_End", value = "创建时间结束（范围查询）", dataType = "String", paramType = "query")
    })
    @GetMapping("/getPage")
    public R<IPage<LawyerConsultationOrder>> getPage(@ModelAttribute LawyerConsultationOrder consultationOrder,
                                                     @RequestParam(defaultValue = "1") int page,
                                                     @RequestParam(defaultValue = "10") int size) {
        log.info("LawyerConsultationOrderController.getPage?consultationOrder={},page={},size={}", consultationOrder, page, size);
        int pageNum = page > 0 ? page : 1;
        int pageSize = size > 0 ? size : 10;
        IPage<LawyerConsultationOrder> pageResult = QueryBuilder.of(consultationOrder)
                .likeFields("orderNumber")  // 订单编号支持模糊查询
                .page(pageNum, pageSize)
                .build()
                .page(consultationOrderService);
        return R.data(pageResult);
    }

    /*@ApiOperation("开始咨询律师")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lawyerId", value = "律师ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "question", value = "咨询问题（可选）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sessionId", value = "关联的AI会话ID（可选）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID（可选）", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "problemScenarioId", value = "法律问题场景ID（可选）", dataType = "Integer", paramType = "query")
    })
    @PostMapping("/start")
    public R<Map<String, Object>> startConsultation(
            @RequestParam Integer lawyerId,
            @RequestParam(required = false) String question,
            @RequestParam(required = false) String sessionId,
            @RequestParam(required = false) Integer clientUserId,
            @RequestParam(required = false) Integer problemScenarioId) {
        log.info("LawyerConsultationOrderController.startConsultation?lawyerId={},question={},sessionId={},clientUserId={},problemScenarioId={}",
                lawyerId, question, sessionId, clientUserId, problemScenarioId);
        return consultationOrderService.startConsultation(lawyerId, question, sessionId, clientUserId, problemScenarioId);
    }*/


    @ApiOperation("立即咨询（用户端）")
    @ApiOperationSupport(order = 9)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "problemScenarioId", value = "法律问题场景ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "problemDescription", value = "问题描述", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "alipayNo", value = "支付宝订单编号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "orderStr", value = "支付宝订单字符串", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "payType", value = "支付方式 1支付宝 2微信", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "chargeMinute", value = "分钟收费-钱数", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "minuteNum", value = "分钟收费-分钟数", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "chargeTime", value = "次数收费-钱数", dataType = "Integer", paramType = "query"),
//            @ApiImplicitParam(name = "lawyerName", value = "律师姓名（支持模糊查询，关联查询）", dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "consultationFee", value = "咨询费用,单位分", dataType = "Integer", paramType = "query"),
//            @ApiImplicitParam(name = "startTime", value = "咨询开始时间", dataType = "Date", paramType = "query"),
//            @ApiImplicitParam(name = "endTime", value = "咨询结束时间", dataType = "Date", paramType = "query"),
//            @ApiImplicitParam(name = "validityPeriod", value = "订单有效期", dataType = "Date", paramType = "query"),
//            @ApiImplicitParam(name = "orderStatus", value = "订单状态, 0:待支付, 1:已支付, 2:进行中, 3:已完成, 4:已取消", dataType = "Integer", paramType = "query"),
//            @ApiImplicitParam(name = "paymentStatus", value = "支付状态, 0:未支付, 1:已支付", dataType = "Integer", paramType = "query"),
    })
    @PostMapping("/consultNow")
    public R<LawyerConsultationOrderDto> startConsultation(@RequestBody LawyerConsultationOrder lawyerConsultationOrder) {
        log.info("LawyerConsultationOrderController.consultNow?lawyerConsultationOrder={}", lawyerConsultationOrder);
        //创建订单时，存储redis中，设置过期时间
        R<LawyerConsultationOrderDto> layerConsultationOrderDto = consultationOrderService.consultNow(lawyerConsultationOrder);
        orderExpirationService.setOrderPaymentTimeout(layerConsultationOrderDto.getData().getOrderNumber(), 60 * Long.parseLong(coefficient));
        return layerConsultationOrderDto;
    }


    @ApiOperation("是否支付成功更改订单表状态")
    @ApiOperationSupport(order = 10)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNumber", value = "订单编号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态, 0:待支付, 1:待接单, 2:进行中, 3:已完成, 4:已取消", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "paymentStatus", value = "支付状态, 0:未支付, 1:已支付", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "payType", value = "支付方式 1支付宝 2微信", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderStr", value = "支付宝或VX的str", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "alipayNo", value = "支付宝/VX订单编号", dataType = "String", paramType = "query"),
    })
    @PostMapping("/payStatus")
    public R<LawyerConsultationOrderDto> payStatus(@RequestBody PayStatusRequest request) {
        log.info("LawyerConsultationOrderController.payStatus?orderNumber={},paymentStatus={},orderStatus={}",
                request.getOrderNumber(), request.getPaymentStatus(), request.getOrderStatus());
        //如果订单已支付，关闭redis中的记时
        if (request.getPaymentStatus() != null && request.getPaymentStatus() == 1) {
            orderExpirationService.cancelOrderPaymentTimeout(request.getOrderNumber());
            orderExpirationService.setOrderAcceptTimeout(request.getOrderNumber(), Long.parseLong(coefficients)*60);
        }
        return consultationOrderService.payStatus(request);
    }

    @ApiOperation("根据用户ID查询律师咨询订单(用户端)")
    @ApiOperationSupport(order = 11)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页容（默认10）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "客户端用户ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态, 0:待支付,1.待接单 2:进行中, 3:已完成, 4:已取消，5.已退款", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "lawyerName", value = "律师姓名（支持模糊查询，关联查询）", dataType = "String", paramType = "query")
    })
    @GetMapping("/getConsultationOrderListById")
    public R<Map<String, Object>> getConsultationOrderListById(@RequestParam(defaultValue = "1") int page,
                                                                            @RequestParam(defaultValue = "10") int size,
                                                                            @RequestParam(required = true) String userId,
                                                                            @RequestParam(required = false) String orderStatus,
                                                                            @RequestParam(required = false) String lawyerName) {
        log.info("LawyerConsultationOrderController.getConsultationOrderListById?pageNum={},pageSize={},userId={},orderStatus={},lawyerName={}",
                page, size, userId, orderStatus, lawyerName);
        return R.data(consultationOrderService.getConsultationOrderListById(page, size, userId, orderStatus, lawyerName));
    }

    @ApiOperation("查询订单详情")
    @ApiOperationSupport(order = 12)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lawyerOrderId", value = "法律订单ID", dataType = "String", paramType = "query")
    })
    @GetMapping("/getConsultationOrderDetail")
    public R<LawyerConsultationOrderVO> getConsultationOrderDetail(@RequestParam(required = false) String lawyerOrderId) {
        log.info("LawyerConsultationOrderController.getConsultationOrderDetail?lawyerOrderId={}",
                lawyerOrderId);
        return R.data(consultationOrderService.getConsultationOrderDetail(lawyerOrderId));
    }

    /**
     * 取消律师咨询订单（用户端）
     * <p>
     * 支持取消待支付和待接单状态的订单：
     * - 待支付订单：取消后订单状态变更为已取消，同时取消订单支付超时计时器
     * - 待接单订单：取消后订单状态变更为已退款
     * 已取消或已完成的订单不允许再次取消
     * </p>
     *
     * @param id 订单ID
     * @return 取消结果，true表示成功，false表示失败
     */
    @ApiOperation("取消律师咨询订单（用户端）")
    @ApiOperationSupport(order = 13)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", dataType = "String", paramType = "query", required = true)
    })
    @PostMapping("/cancelOrder")
    public R<Boolean> cancelOrder(@RequestParam(value = "id", required = true) String id) {
        log.info("LawyerConsultationOrderController.cancelOrder?id={}", id);

        // 参数校验
        if (id == null || id.trim().isEmpty()) {
            log.warn("取消订单失败：订单ID为空");
            return R.fail("订单ID不能为空");
        }

        try {
            boolean success = consultationOrderService.cancelOrder(id);
            if (success) {
                log.info("取消订单成功，订单ID={}", id);
                return R.data(true);
            } else {
                log.warn("取消订单失败，订单ID={}", id);
                return R.fail("取消订单失败，请检查订单状态");
            }
        } catch (Exception e) {
            log.error("取消订单异常，订单ID={}，异常信息：{}", id, e.getMessage(), e);
            return R.fail("取消订单失败：" + e.getMessage());
        }
    }

    @ApiOperation("查询咨询订单信息（律师端）")
    @ApiOperationSupport(order = 14)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页容（默认10）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "clientUserName", value = "用户姓名", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态（2 进行中，3 已完成）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "lawyerId", value = "律师ID", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/getLawyerConsultationOrderInfo")
    public R<Map<String, Object>> getLawyerConsultationOrderInfo(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) String clientUserName,
            @RequestParam(required = false) String orderStatus,
            @RequestParam(required = true) String lawyerId) {
        log.info("LawyerConsultationOrderController.getLawyerConsultationOrderInfo?page={},size={},startDate={},endDate={},clientUserName={},orderStatus={},lawyerId={}",
                page, size, startDate, endDate, clientUserName, orderStatus, lawyerId);

        // 参数校验
        if (lawyerId == null || lawyerId.trim().isEmpty()) {
            log.warn("查询咨询订单信息失败：律师ID为空");
            return R.fail("律师ID不能为空");
        }

        // 分页参数校验和规范化
        int pageNum = page > 0 ? page : 1;
        int pageSize = size > 0 ? size : 10;

        return R.data(consultationOrderService.getLawyerConsultationOrderInfo(pageNum, pageSize, startDate, endDate, clientUserName, orderStatus, lawyerId));
    }


    @ApiOperation("创建订单前的校验")
    @ApiOperationSupport(order = 15)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", dataType = "Integer", paramType = "query", required = true),
    })
    @PostMapping("/checkOrder")
    public R<Map<String, String>> checkOrder(@RequestParam Integer clientUserId, @RequestParam Integer lawyerUserId) {
        log.info("LawyerConsultationOrderController.checkOrder?clientUserId={},lawyerUserId={}", clientUserId, lawyerUserId);
        
        // 参数校验
        if (clientUserId == null) {
            log.warn("创建订单前校验失败：客户端用户ID为空");
            return R.fail("客户端用户ID不能为空");
        }
        
        if (lawyerUserId == null) {
            log.warn("创建订单前校验失败：律师用户ID为空");
            return R.fail("律师用户ID不能为空");
        }
        
        return consultationOrderService.checkOrder(clientUserId, lawyerUserId);
    }

    @ApiOperation("订单收益详情接口")
    @ApiOperationSupport(order = 16)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "String", paramType = "query")
    })
    @PostMapping("/getOrderIncome")
    public R<Map<String, Object>> getOrderIncome(@RequestBody LawyerConsultationOrderVO lawyerConsultationOrderVO) {
        log.info("LawyerConsultationOrderController.getOrderIncome?lawyerConsultationOrderVO{}", lawyerConsultationOrderVO);
        return consultationOrderService.getOrderIncome(lawyerConsultationOrderVO);
    }

    @ApiOperation("用户申请退款")
    @ApiOperationSupport(order = 17)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "applyRefundReason", value = "申请退款原因", dataType = "String", paramType = "query", required = true)
    })
    @PostMapping("/applyRefund")
    public R<Boolean> applyRefund(@RequestParam String clientUserId, @RequestParam String orderId, @RequestParam String applyRefundReason) {
        log.info("LawyerConsultationOrderController.applyRefund?clientUserId={},orderId{}", clientUserId, orderId);
        return consultationOrderService.applyRefund(clientUserId, orderId, applyRefundReason);
    }

    @ApiOperation("用户完成订单")
    @ApiOperationSupport(order = 18)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "String", paramType = "query", required = true)
    })
    @PostMapping("/completeOrder")
    public R<Boolean> completeOrder(@RequestParam String clientUserId, @RequestParam String orderId) {
        log.info("LawyerConsultationOrderController.completeOrder?clientUserId={},orderId{}", clientUserId, orderId);
        return consultationOrderService.completeOrder(clientUserId, orderId);
    }


    @ApiOperation("查看申请退款详情")
    @ApiOperationSupport(order = 19)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "法律订单ID", dataType = "String", paramType = "query")
    })
    @GetMapping("/getApplyRefundDetail")
    public R<LawyerConsultationOrder> getApplyRefundDetail(@RequestParam(required = false) String orderId) {
        log.info("LawyerConsultationOrderController.getApplyRefundDetail?orderId={}",
                orderId);
        return R.data(consultationOrderService.getApplyRefundDetail(orderId));
    }
}

