package com.kym.service.miniapp.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.kym.common.utils.OrderUtils;
import com.kym.entity.miniapp.WashOrder;
import com.kym.entity.miniapp.queryParams.DeviceQueryParams;
import com.kym.entity.miniapp.queryParams.WashOrderQueryParams;
import com.kym.mapper.miniapp.WashOrderMapper;
import com.kym.service.awoara.AwoaraService;
import com.kym.service.miniapp.AccountService;
import com.kym.service.miniapp.WashOrderService;
import com.kym.service.mybatisplus.MyBaseServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 洗车订单表 服务实现类
 * </p>
 *
 * @author skyline
 * @since 2024-09-11
 */
@Service
public class WashOrderServiceImpl extends MyBaseServiceImpl<WashOrderMapper, WashOrder> implements WashOrderService {

    private final AwoaraService awoaraService;
    private final AccountService accountService;

    public WashOrderServiceImpl(AwoaraService awoaraService, AccountService accountService) {
        this.awoaraService = awoaraService;
        this.accountService = accountService;
    }

    /**
     * 创建订单（启动洗车机）
     *
     * @param params
     * @return
     */
    @Override
    public String createOrder(DeviceQueryParams params) {
        var account = accountService.getAccountByUserId(StpUtil.getLoginIdAsLong());
        var memberName = StpUtil.getSession().getString("mobilePhone");
        var orderId = OrderUtils.getOrderNo();
        var createOrder = awoaraService.createOrder(params.getProductKey(), params.getDeviceName(),
                orderId,
                memberName,
                account.getBalance(),
                100, // 优惠折扣 todo 后期会员折扣可用
                account.getBalance() - 50); // 本次开机最大消费金额，减去0.5元，防止超消费
        var washOrder = new WashOrder()
                .setUserId(StpUtil.getLoginIdAsLong())
                .setStationId(params.getStationId())
                .setProductKey(params.getProductKey())
                .setDeviceName(params.getDeviceName())
                .setOpenType("network")
                .setOrderId(orderId)
                .setOrderIdLocal(createOrder.getOrder_id_local())
                .setMemberDiscount(100)
                .setPrepayMoney(account.getBalance() - 50)
                .setStartTime(LocalDateTime.now())
                .setOrderStatus(WashOrder.ORDER_STATUS_开机)
                .setPayStatus(WashOrder.PAY_STATUS_未支付);
        save(washOrder);
        return orderId;
    }

    @Override
    public void closeOrder(DeviceQueryParams params) {
        var order = lambdaQuery()
                .eq(WashOrder::getProductKey, params.getProductKey())
                .eq(WashOrder::getDeviceName, params.getDeviceName())
                .eq(WashOrder::getOrderStatus, WashOrder.ORDER_STATUS_开机)
                .eq(WashOrder::getPayStatus, WashOrder.PAY_STATUS_未支付)
                .one();
        if (order != null) {
            awoaraService.closeOrder(params.getProductKey(), params.getDeviceName(), order.getOrderId());
        }
    }

    /**
     * 查询订单详情
     *
     * @param params
     * @return
     */
    @Override
    public WashOrder queryOrder(WashOrderQueryParams params) {
        // 非实时数据
        return lambdaQuery()
                .eq(WashOrder::getOrderId, params.getOrderId())
                .one();
    }
}
