package cn.iocoder.yudao.module.order.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.order.entity.RepairOrderInfo;
import cn.iocoder.yudao.module.order.service.RepairOrderInfoService;
import cn.iocoder.yudao.module.order.vo.RepairOrderInfoPageReqVO;
import cn.iocoder.yudao.module.order.vo.RepairOrderInfoRespVO;
import cn.iocoder.yudao.module.order.vo.RepairOrderInfoSaveReqVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;

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

@Tag(name = "管理后台 - 维修模块 订单")
@RestController
@RequestMapping("/repair/order-info")
@Validated
public class RepairOrderInfoController {

    @Resource
    private RepairOrderInfoService repairOrderInfoService;


    @DeleteMapping("/delete")
    @Operation(summary = "删除维修模块 订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('repair:order-info:delete')")
    public CommonResult<Boolean> deleteOrderInfo(@RequestParam("id") String id) {
        repairOrderInfoService.deleteOrderInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得维修模块 订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('repair:order-info:query')")
    public CommonResult<RepairOrderInfoRespVO> getOrderInfo(@RequestParam("id") String id) {
        RepairOrderInfo orderInfo = repairOrderInfoService.getOrderInfo(id);
        return success(BeanUtils.toBean(orderInfo, RepairOrderInfoRespVO.class));
    }



    /**
     * 更新订单信息
     *
     * @param saveReqVO 订单信息
     */
    @PutMapping("/update")
    @Operation(summary = "更新车辆信息")
    public CommonResult<Boolean> updateCarMain(@RequestBody RepairOrderInfoSaveReqVO saveReqVO) {
        repairOrderInfoService.rechargeCallback(saveReqVO.getOrderNo());
        return success(true);
    }

    /**
     * 分页查询订单内容
     *
     * @param pageReqVO RepairOrderInfoPageReqVO实体
     * @param pageNo    分页参数
     * @param pageSize  分页参数
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<com.baomidou.mybatisplus.core.metadata.IPage < ?>>
     * @author PQZ
     * @date 14:30 2024/9/23
     **/
    @GetMapping("/page")
    @Operation(summary = "获得维修模块 订单分页")
    @PreAuthorize("@ss.hasPermission('repair:order-info:query')")
    public CommonResult<IPage<?>> getOrderInfoPage(@Valid RepairOrderInfoPageReqVO pageReqVO,
                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<RepairOrderInfoRespVO> page = new Page<>(pageNo, pageSize);
        return success(repairOrderInfoService.queryListPage(pageReqVO,page));
    }

    /**
     * 统计未入账和已入账(最简版)
     *6
     * @author 小李
     * @date 18:52 2024/9/25
    **/
    @GetMapping("/census")
    @Operation(summary = "统计未入账和已入账")
    @PreAuthorize("@ss.hasPermission('repair:order-info:query')")
    public CommonResult<?> census() {
        return success(repairOrderInfoService.census());
    }

    /**
     * PC支付按钮
     *
     * @author 小李
     * @date 17:26 2024/10/30
     * @param id id
    **/
    @GetMapping("/toPay")
    @Operation(summary = "PC支付按钮")
    public CommonResult<?> toPay(@RequestParam("id")String id) throws IOException, NoSuchAlgorithmException, SignatureException, InvalidKeyException {
        return success(repairOrderInfoService.payTransactionsCode(id));
    }
}