package cn.iocoder.yudao.module.system.controller.admin.rental;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.customer.RentalCustomerPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.customer.RentalCustomerRespVO;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.customer.RentalCustomerSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.itemsrecord.RentalItemsCountReqVO;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.itemsrecord.RentalItemsRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.itemsrecord.RentalItemsRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.itemsrecord.RentalItemsRecordSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.rental.RentalCustomerDO;
import cn.iocoder.yudao.module.system.dal.dataobject.rental.RentalItemsRecordDO;
import cn.iocoder.yudao.module.system.service.rental.RentalItemsRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 租赁物品记录")
@RestController
@RequestMapping("/system/rental-items")
@Validated
public class RentalItemsRecordController {

    @Resource
    private RentalItemsRecordService rentalItemsRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建租赁物品记录")
    @PreAuthorize("@ss.hasPermission('system:rental-items:create')")
    public CommonResult<Long> createRentalItemsRecord(@Valid @RequestBody RentalItemsRecordSaveReqVO createReqVO) {
        return success(rentalItemsRecordService.createRentalItemsRecord(createReqVO));
    }

    @PostMapping("/createBatch")
    @Operation(summary = "批量创建租赁物品记录")
    @PreAuthorize("@ss.hasPermission('system:rental-items:create')")
    public CommonResult<Boolean> createRentalItemsRecord(@Valid @RequestBody List<RentalItemsRecordSaveReqVO> createReqVO) {
        rentalItemsRecordService.createRentalItemsRecordBath(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新租赁物品记录")
    @PreAuthorize("@ss.hasPermission('system:rental-items:update')")
    public CommonResult<Boolean> updateRentalItemsRecord(@Valid @RequestBody RentalItemsRecordSaveReqVO updateReqVO) {
        rentalItemsRecordService.updateRentalItemsRecord(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取租赁物品记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:rental-items:query')")
    public CommonResult<RentalItemsRecordRespVO> getRentalItemsRecord(@RequestParam("id") Long id) {

        return success(BeanUtils.toBean(rentalItemsRecordService.getRentalItemsRecord(id), RentalItemsRecordRespVO.class));
    }

    @GetMapping("/get-count")
    @Operation(summary = "获取指定订单剩余租赁物品数量")
    @Parameter(name = "orderNo", description = "订单编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:rental-items:query')")
    public CommonResult<List<RentalItemsCountReqVO>> getRentalItemsRecord(@RequestParam("orderNo") String orderNo) {

        return success(rentalItemsRecordService.getRentalItemsCount(orderNo, false));
    }

    @GetMapping("/page")
    @Operation(summary = "获得租赁物品记录分页")
    @PreAuthorize("@ss.hasPermission('system:rental-items:query')")
    public CommonResult<PageResult<RentalItemsRecordRespVO>> getRentalItemsRecordPage(@Valid RentalItemsRecordPageReqVO pageReqVO) {
        PageResult<RentalItemsRecordDO> pageResult = rentalItemsRecordService.getRentalItemsRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RentalItemsRecordRespVO.class));
    }
}
