package com.kym.service.miniapp.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.kym.common.utils.CommUtil;
import com.kym.entity.miniapp.OrderRechargeRights;
import com.kym.entity.miniapp.UserRechargeRights;
import com.kym.entity.miniapp.WashDevice;
import com.kym.entity.miniapp.queryParams.DeviceQueryParams;
import com.kym.entity.miniapp.vo.UserOrderRechargeRightsVo;
import com.kym.entity.miniapp.vo.WashDeviceVo;
import com.kym.mapper.miniapp.WashDeviceMapper;
import com.kym.service.cache.KymCache;
import com.kym.service.miniapp.WashDeviceService;
import com.kym.service.miniapp.WashOrderService;
import com.kym.service.mybatisplus.MyBaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 洗车设备表 服务实现类
 * </p>
 *
 * @author skyline
 * @since 2024-10-09
 */
@Service
public class WashDeviceServiceImpl extends MyBaseServiceImpl<WashDeviceMapper, WashDevice> implements WashDeviceService {

    private WashOrderService washOrderService;

    public WashDeviceServiceImpl(WashOrderService washOrderService) {
        this.washOrderService = washOrderService;
    }


    /**
     * 获取设备列表
     *
     * @param params
     * @return
     */
    @Override
    public List<WashDeviceVo> listWashDevice(DeviceQueryParams params) {
        var list = lambdaQuery()
                .eq(CommUtil.isNotEmptyAndNull(params.getId()), WashDevice::getId, params.getId())
                .eq(CommUtil.isNotEmptyAndNull(params.getStationId()), WashDevice::getStationId, params.getStationId())
                .orderByAsc(WashDevice::getDeviceName)
                .list();
        var voList = new ArrayList<WashDeviceVo>();
        for (WashDevice washDevice : list) {
            var vo = new WashDeviceVo();
            BeanUtils.copyProperties(washDevice, vo);
            vo.setShortId(KymCache.INSTANCE.getShortIdByProductKeyAndDeviceName(washDevice.getProductKey(), washDevice.getDeviceName()));
            voList.add(vo);
        }
        return voList;
    }

    /**
     * 根据设备短id获取设备信息
     *
     * @param shortId
     * @return
     */
    @Override
    public WashDevice getDevice(String shortId) {
        var productKeyAndDeviceName = KymCache.INSTANCE.getProductKeyAndDeviceNameByWashShortId(shortId);
        return lambdaQuery()
                .eq(WashDevice::getProductKey, productKeyAndDeviceName[0])
                .eq(WashDevice::getDeviceName, productKeyAndDeviceName[1])
                .one();
    }


    @Override
    public String startDevice(String shortId) {
        var productKeyAndDeviceName = KymCache.INSTANCE.getProductKeyAndDeviceNameByWashShortId(shortId);
        return washOrderService.createOrder(new DeviceQueryParams(shortId, productKeyAndDeviceName[0], productKeyAndDeviceName[1]));
    }

    @Override
    public void stopDevice(String shortId) {
        var productKeyAndDeviceName = KymCache.INSTANCE.getProductKeyAndDeviceNameByWashShortId(shortId);
        washOrderService.closeOrder(new DeviceQueryParams(shortId, productKeyAndDeviceName[0], productKeyAndDeviceName[1]));
    }

}
