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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.inspection.entity.InspectionInfo;
import cn.iocoder.yudao.module.inspection.service.IInspectionInfoService;
import cn.iocoder.yudao.module.payment.entity.OrderInfo;
import cn.iocoder.yudao.module.payment.service.OrderInfoService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/inspection/orderInfo")
@RequiredArgsConstructor
public class InspectionOrderInfoController {

    private final OrderInfoService orderInfoService;

    private final IInspectionInfoService inspectionInfoService;

    /**
     * 修改订单信息
     *
     * @param orderInfo 修改的订单信息
     * @return 修改结果
     */
    @PutMapping("/cashier")
    public CommonResult<?> cashier(@RequestBody OrderInfo orderInfo) {
        if ("accounting".equals(orderInfo.getType())) {
            orderInfo.setAccountingConfirmUser(SecurityFrameworkUtils.getLoginUserId());
        } else {
            orderInfo.setCashierConfirmUser(SecurityFrameworkUtils.getLoginUserId());
        }
        return CommonResult.success(orderInfoService.updateById(orderInfo));
    }

    /**
     * 获取订单信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult<?> get(@PathVariable Long id) {
        OrderInfo byId = orderInfoService.getById(id);
        InspectionInfo one = inspectionInfoService.getOne(Wrappers.<InspectionInfo>lambdaQuery()
                .eq(InspectionInfo::getInspectionOrderId, id));
        if (one != null) {
            byId.setCarNo(one.getCarNum());
        }
        return CommonResult.success(byId);
    }
}
