package org.dromara.pay.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
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.pay.domain.bo.PayOrderBo;
import org.dromara.pay.domain.vo.PayOrderSubmitRespVO;
import org.dromara.pay.domain.vo.PayOrderVo;
import org.dromara.pay.service.PayOrderService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 支付订单
 *
 * @author pengles
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pay/order")
public class PayOrderController extends BaseController {

    private final PayOrderService payOrderService;

    /**
     * 查询支付订单列表
     */
    @SaCheckPermission("pay:order:list")
    @GetMapping("/list")
    public TableDataInfo<PayOrderVo> list(PayOrderBo bo, PageQuery pageQuery) {
        return payOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取支付订单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("pay:order:query")
    @GetMapping("/{id}")
    public R<PayOrderVo> getInfo(@PathVariable Long id) {
        return R.ok(payOrderService.queryById(id));
    }

    /**
     * 新增支付订单
     */
    @SaCheckPermission("pay:order:add")
    @Log(title = "支付订单", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody PayOrderBo bo) {
        return toAjax(payOrderService.insertByBo(bo));
    }

    /**
     * 提交支付订单
     */
    @SaCheckPermission("pay:order:submit")
    @Log(title = "支付订单", businessType = BusinessType.OTHER)
    @PostMapping("/submit")
    public R<PayOrderSubmitRespVO> submit(@Validated @RequestBody PayOrderBo bo) {
        return R.ok(payOrderService.submitOrder(bo));
    }

    /**
     * 修改支付订单
     */
    @SaCheckPermission("pay:order:edit")
    @Log(title = "支付订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody PayOrderBo bo) {
        return toAjax(payOrderService.updateByBo(bo));
    }

    /**
     * 删除支付订单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("pay:order:remove")
    @Log(title = "支付订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(payOrderService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
