package cn.ysk.cashier.mybatis.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.ysk.cashier.cons.TableConstant;
import cn.ysk.cashier.mybatis.mapper.TbCashierCartMapper;
import cn.ysk.cashier.mybatis.mapper.TbOrderInfoMapper;
import cn.ysk.cashier.mybatis.service.MpCashierCartService;
import cn.ysk.cashier.mybatis.service.MpOrderInfoService;
import cn.ysk.cashier.pojo.order.TbCashierCart;
import cn.ysk.cashier.pojo.order.TbOrderInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * (TbShopPermission)表服务实现类
 *
 * @author makejava
 * @since 2024-09-14 17:08:49
 */
@Service
public class MpOrderInfoServiceImpl extends ServiceImpl<TbOrderInfoMapper, TbOrderInfo> implements MpOrderInfoService {
    @Override
    public TbOrderInfo selectReturnOrderByOrderId(Integer orderId) {
        return getOne(new LambdaQueryWrapper<TbOrderInfo>()
                .eq(TbOrderInfo::getSource, orderId));
    }

    @Override
    public boolean updateTableIdAndUseTypeById(Integer orderId, String useType, String tableId) {
        return update(new LambdaUpdateWrapper<TbOrderInfo>()
                .eq(TbOrderInfo::getId, orderId)
                .set(TbOrderInfo::getUseType, useType)
                .set(TbOrderInfo::getTableId, tableId));
    }

    @Override
    public TbOrderInfo selectOrderByIdAndState(Integer orderId, TableConstant.OrderInfo.Status status) {
        return getOne(new LambdaQueryWrapper<TbOrderInfo>()
                .eq(TbOrderInfo::getId, orderId)
                .eq(TbOrderInfo::getStatus, status.getValue()));
    }

    @Override
    public boolean incrAmount(Integer orderId, BigDecimal subtract) {
        return update(new LambdaUpdateWrapper<TbOrderInfo>()
                .eq(TbOrderInfo::getId, orderId)
                .eq(TbOrderInfo::getStatus, TableConstant.OrderInfo.Status.UNPAID.getValue())
                .apply(StrUtil.format("settlement_amount + {} >= 0", subtract))
                .setSql(StrUtil.format("settlement_amount=settlement_amount+{}", subtract))
                .setSql(StrUtil.format("order_amount=order_amount+{}", subtract))
                .setSql(StrUtil.format("amount=amount+{}", subtract))
                .setSql(StrUtil.format("origin_amount=origin_amount+{}", subtract))
        );
    }
}

