package org.dromara.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.web.utils.SecurityUtils;
import org.dromara.work.domain.bo.TpAppealsBo;
import org.dromara.work.domain.bo.TpOrderBo;
import org.dromara.work.domain.vo.OrderCountVo;
import org.dromara.work.domain.vo.TpAppealsVo;
import org.dromara.work.domain.vo.TpOrderVo;
import org.dromara.work.service.ITpAppealsService;
import org.dromara.work.service.ITpNewOrderService;
import org.dromara.work.service.ITpOrderService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;

/**
 * 订单接口
 * @Author: Maosw
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/order")
@Tag(name = "订单接口")
public class ApiOrderController {

    private final ITpAppealsService tpAppealsService;

    private final ITpOrderService tpOrderService;

    private final ITpNewOrderService tpNewOrderService;

    /**
     * 新增申诉
     */
    @RepeatSubmit()
    @PostMapping("/addAppeals")
    public R<Boolean> addAppeals(@Validated(AddGroup.class) @RequestBody TpAppealsBo bo) {
        Long userId = SecurityUtils.getUserId();
        bo.setUserId(userId);
        return R.ok(tpAppealsService.insertByBo(bo));
    }

    /**
     * 根据ID删除申诉
     */
    @DeleteMapping("/deleteAppeals/{ids}")
    public R<Boolean> deleteAppeals(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return R.ok(tpAppealsService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 分页查询我的申诉列表
     */
    @GetMapping("/myAppealsList")
    public R<IPage<TpAppealsVo>> myAppealsList(TpAppealsBo bo, PageQuery pageQuery) {
        Long userId = SecurityUtils.getUserId();
        bo.setUserId(userId);
        return R.ok(tpAppealsService.selectPageList(bo, pageQuery));
    }

    /**
     * 根据ID查询申诉详情
     */
    @GetMapping("/getAppeals/{id}")
    public R<TpAppealsVo> getAppeals(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(tpAppealsService.queryById(id));
    }

    /**
     * 分页查询订单列表
     */
    @GetMapping("/orderList")
    public R<IPage<TpOrderVo>> orderList(TpOrderBo bo, PageQuery pageQuery) {
        return R.ok(tpNewOrderService.selectPageList(bo, pageQuery));
    }

    /**
     * 分页查询我的订单列表
     */
    @GetMapping("/myOrderList")
    public R<IPage<TpOrderVo>> myOrderList(TpOrderBo bo, PageQuery pageQuery) {
        Long userId = SecurityUtils.getUserId();
        bo.setSid(userId);
        return R.ok(tpNewOrderService.selectPageList(bo, pageQuery));
    }

    /**
     * 根据ID查询订单详情
     */
    @GetMapping("/getOrder/{id}")
    public R<TpOrderVo> getOrder(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(tpNewOrderService.queryById(id));
    }

    /**
     * 发布订单
     */
    @RepeatSubmit()
    @PostMapping("/publishOrder")
    public R<Boolean> publishOrder(@Validated(AddGroup.class) @RequestBody TpOrderBo bo) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(tpOrderService.insertByBo(bo));
    }

    /**
     * 修改订单信息
     */
    @RepeatSubmit()
    @PutMapping("/updateOrder")
    public R<Boolean> updateOrder(@RequestBody TpOrderBo bo) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(tpOrderService.updateByBo(bo));
    }

    /**
     * 客户删除订单
     */
    @DeleteMapping("/deleteOrder/{ids}")
    public R<Boolean> deleteOrder(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(tpOrderService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 客户取消订单
     */
    @RepeatSubmit()
    @PostMapping("/orderCancel/{id}")
    public R<Boolean> orderCancel(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(tpOrderService.orderCancel(id));
    }

    /**
     * 订单指派技术
     */
    @RepeatSubmit()
    @PostMapping("/orderAssign")
    public R<Boolean> orderAssign(@RequestBody TpOrderBo bo) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(tpOrderService.updateOrderAssign(bo.getOrderIds(),bo.getUserId()));
    }

    /**
     * 订单取消指派
     */
    @RepeatSubmit()
    @PostMapping("/orderCancelAssign/{id}")
    public R<Boolean> orderCancelAssign(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(tpOrderService.orderCancelAssign(id) > 0);
    }

    /**
     * 订单支付获取订单和余额信息
     */
    @GetMapping("/orderPay/{id}")
    public R<TpOrderVo> orderPay(@NotNull(message = "主键不能为空")  @PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(tpOrderService.queryOrderPay(id));
    }

    /**
     * 订单支付
     */
    @RepeatSubmit()
    @PostMapping("/orderPay")
    @Parameters({@Parameter(name = "orderId", description = "订单ID", required = true),@Parameter(name = "type", description = "类型：1-拉卡拉支付 2-余额", required = true)
        ,@Parameter(name = "price", description = "支付金额", required = true)
    })
    public R<JSONObject> pay(@RequestParam(value = "orderId") Long orderId, @RequestParam(value = "type") Integer type, @RequestParam(value = "price") BigDecimal price) throws Exception {
        Long userId = SecurityUtils.getUserId();
        return R.ok(tpOrderService.orderPay(orderId,type,price));
    }

    /**
     * 技术取消订单
     */
    @GetMapping("/orderFallback/{id}")
    public R<Boolean> orderFallback(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(tpOrderService.jsOrderFallback(id));
    }

    /**
     * 客户申请退款
     */
    @GetMapping("/orderRefund/{id}")
    public R<Boolean> orderRefund(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(tpOrderService.orderRefund(id));
    }

    /**
     * 客户取消退款
     */
    @GetMapping("/orderCancelRefund/{id}")
    public R<Boolean> orderCancelRefund(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(tpOrderService.orderCancelRefund(id));
    }

    /**
     * 技术处理订单退款
     */
    @RepeatSubmit()
    @PostMapping("/orderRefundJs")
    public R<Boolean> orderRefundJs(@RequestBody TpOrderBo bo) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(tpOrderService.orderRefundJs(bo));
    }

    /**
     * 技术接单
     */
    @GetMapping("/orderAccept/{id}")
    public R<Boolean> orderAccept(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(tpOrderService.orderAccept(id,userId));
    }

    /**
     * 客户确认订单
     */
    @GetMapping("/orderConfirm/{id}")
    public R<Boolean> orderConfirm(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(tpOrderService.orderConfirm(id));
    }

     /**
     * 根据用户ID统计订单信息
     */
    @GetMapping("/countOrders/{userId}")
    public R<OrderCountVo> countOrders(@NotNull(message = "用户ID不能为空") @PathVariable Long userId) {
        return R.ok(tpOrderService.orderCount(userId));
    }

    /**
     * 订单评论
     */
    @RepeatSubmit()
    @PostMapping("/addComment")
    public R<Boolean> addComment(@Validated(AddGroup.class) @RequestBody TpAppealsBo bo) {
        Long userId = SecurityUtils.getUserId();
        bo.setUserId(userId);
        return R.ok(tpOrderService.addComment(bo));
    }



}
