package org.dromara.work.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.servlet.http.HttpServletResponse;
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.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.work.domain.bo.TpOrderBo;
import org.dromara.work.domain.vo.CustomerOrderSumVo;
import org.dromara.work.domain.vo.CustomerOrderVo;
import org.dromara.work.domain.vo.TpOrderVo;
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.List;

/**
 * 订单管理(客服)
 *
 * @author Maosw
 * @date 2024-08-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/customer/order")
public class CustomerOrderController extends BaseController {

    private final ITpOrderService tpOrderService;

    /**
     * 查询订单管理列表
     */
    @SaCheckPermission("work:customerOrder:list")
    @GetMapping("/list")
    public TableDataInfo<CustomerOrderVo> list(TpOrderBo bo, PageQuery pageQuery) {
        return tpOrderService.queryCustomerPageList(bo, pageQuery);
    }

    /**
     * 查询订单统计（客服）
     */
    @SaCheckPermission("work:customerOrder:sum")
    @GetMapping("/sum")
    public R<CustomerOrderSumVo> sum(TpOrderBo bo) {
        return R.ok(tpOrderService.queryCustomerSum(bo));
    }


    /**
     * 导出客服订单
     */
    @SaCheckPermission("work:customerOrder:export")
    @Log(title = "导出客服订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpOrderBo bo, HttpServletResponse response) {
        List<TpOrderVo> list = tpOrderService.queryListKF(bo);
        ExcelUtil.exportExcel(list, "导出客服订单", TpOrderVo.class, response);
    }

    /**
     * 获取订单管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:customerOrder:query")
    @GetMapping("/{id}")
    public R<TpOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpOrderService.queryById(id));
    }

    /**
     * 新增订单管理
     */
    @SaCheckPermission("work:customerOrder:add")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpOrderBo bo) {
        return toAjax(tpOrderService.insertByBo(bo));
    }

    /**
     * 修改订单管理
     */
    @SaCheckPermission("work:customerOrder:edit")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpOrderBo bo) {
        return toAjax(tpOrderService.updateByBo(bo));
    }

    /**
     * 删除订单管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("work:customerOrder:remove")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tpOrderService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 订单指派技术
     */
    @SaCheckPermission("work:customerOrder:assign")
    @Log(title = "订单指派技术", businessType = BusinessType.OTHER)
//    @Parameters({@Parameter(name = "orderIds", description = "订单ID", required = true),@Parameter(name = "userId", description = "用户ID", required = true)})
    @PostMapping("/assign")
    public R<Void> assign(@RequestBody TpOrderBo bo) {
        return toAjax(tpOrderService.updateOrderAssign(bo.getOrderIds(),bo.getUserId()));
    }

    /**
     * 订单取消指派
     */
    @SaCheckPermission("work:customerOrder:cancelAssign")
    @Log(title = "订单取消指派", businessType = BusinessType.OTHER)
    @Parameter(name = "orderId", description = "订单ID", required = true)
    @PostMapping("/cancelAssign")
    public R<Void> cancelAssign(@RequestParam(value = "orderId") Long orderId) {
        return toAjax(tpOrderService.orderCancelAssign(orderId));
    }


    /**
     * 订单支付获取订单和余额信息
     * @param orderId 主键
     */
    @SaCheckPermission("work:customerOrder:queryOrderPay")
    @GetMapping("queryOrderPay/{orderId}")
    public R<TpOrderVo> queryOrderPay(@NotNull(message = "orderId主键不能为空") @PathVariable Long orderId) {
        return R.ok(tpOrderService.queryOrderPay(orderId));
    }

    /**
     * 订单支付
     */
    @SaCheckPermission("work:customerOrder:pay")
    @Log(title = "订单支付", businessType = BusinessType.OTHER)
    @PostMapping("/pay")
    @Parameters({@Parameter(name = "orderId", description = "订单ID", required = true),@Parameter(name = "type", description = "类型：1客户 2客服", required = true)
        ,@Parameter(name = "price", description = "支付金额", required = true)
    })
    public R<Void> pay(@RequestParam(value = "orderId") Long orderId,@RequestParam(value = "type") Integer type,@RequestParam(value = "price") BigDecimal price) {
        return toAjax(tpOrderService.orderPay(orderId,type,price));
    }


    /**
     * 订单回退获取订单和退款金额信息
     * @param orderId 主键
     */
    @SaCheckPermission("work:customerOrder:orderFallback")
    @GetMapping("orderFallback/{orderId}")
    public R<TpOrderVo> orderFallback(@NotNull(message = "orderId主键不能为空") @PathVariable Long orderId) {
        return R.ok(tpOrderService.orderFallback(orderId));
    }


    /**
     * 订单回退
     */
    @SaCheckPermission("work:customerOrder:fallback")
    @Log(title = "订单回退", businessType = BusinessType.OTHER)
    @PostMapping("/fallback")
    @Parameters({@Parameter(name = "orderId", description = "订单ID", required = true),@Parameter(name = "type", description = "类型：1客户 2客服", required = true)
        ,@Parameter(name = "price", description = "回退金额", required = true)
    })
    public R<Void> fallback(@RequestParam(value = "orderId") Long orderId,@RequestParam(value = "type") Integer type,@RequestParam(value = "price") BigDecimal price) {
        return toAjax(tpOrderService.fallback(orderId,type,price));
    }


    /**
     * 订单拆单获取订单和金额信息
     * @param orderId 主键
     */
    @SaCheckPermission("work:customerOrder:cdOrderInfo")
    @GetMapping("cdOrderInfo/{orderId}")
    public R<TpOrderVo> cdOrderInfo(@NotNull(message = "orderId主键不能为空") @PathVariable Long orderId) {
        return R.ok(tpOrderService.cdOrderInfo(orderId));
    }

    /**
     * 订单拆单
     */
    @SaCheckPermission("work:customerOrder:cdOrder")
    @Log(title = "订单拆单", businessType = BusinessType.OTHER)
    @PostMapping("/cdOrder")
    @Parameters({@Parameter(name = "orderId", description = "订单ID", required = true),@Parameter(name = "price", description = "拆单金额", required = true)})
    public R<Void> cdOrder(@RequestParam(value = "orderId") Long orderId,@RequestParam(value = "price") BigDecimal price) {
        return toAjax(tpOrderService.cdOrder(orderId,price));
    }

    /**
     * 订单改价获取订单和金额信息
     * @param orderId 主键
     */
    @SaCheckPermission("work:customerOrder:gjOrderInfo")
    @GetMapping("gjOrderInfo/{orderId}")
    public R<TpOrderVo> gjOrderInfo(@NotNull(message = "orderId主键不能为空") @PathVariable Long orderId) {
        return R.ok(tpOrderService.gjOrderInfo(orderId));
    }

    /**
     * 订单改价
     */
    @SaCheckPermission("work:customerOrder:gjOrder")
    @Log(title = "订单改价", businessType = BusinessType.OTHER)
    @PostMapping("/gjOrder")
    @Parameters({@Parameter(name = "orderId", description = "订单ID", required = true),@Parameter(name = "price", description = "拆单金额", required = true)})
    public R<Void> gjOrder(@RequestParam(value = "orderId") Long orderId,@RequestParam(value = "price") BigDecimal price) {
        return toAjax(tpOrderService.gjOrder(orderId,price));
    }

}
