package com.ruoyi.web.controller.app;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.ss.timeBill.domain.TimeBillVO;
import com.ruoyi.ss.timeBill.domain.dto.TimeBillAddOrderDTO;
import com.ruoyi.ss.timeBill.domain.dto.TimeBillPayDTO;
import com.ruoyi.ss.timeBill.service.TimeBillService;
import com.ruoyi.ss.timeBill.service.TimeBillValidator;
import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillPayType;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author wjh
 * 2024/7/24
 */
@RestController
@RequestMapping("/app/timeBill")
public class AppTimeBillController extends BaseController {

    @Autowired
    private TimeBillService timeBillService;

    @Autowired
    private TimeBillValidator timeBillValidator;

    @ApiOperation("下单使用设备")
    @PostMapping
    public AjaxResult add(@RequestBody @Validated TimeBillAddOrderDTO dto) {
        dto.setUserId(getUserId());
        return AjaxResult.success("下单成功", timeBillService.addOrder(dto));
    }

    @ApiOperation("结束使用订单")
    @PutMapping("/{billId}/close")
    public AjaxResult close(@PathVariable Long billId) {
        TimeBillVO bill = timeBillService.selectTimeBillByBillId(billId);
        if (!timeBillValidator.canClose(bill, getUserId())) {
            return error("您无权结束该订单");
        }
        return toAjax(timeBillService.close(bill));
    }

}
