package com.kym.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageHelper;
import com.kym.common.utils.CommUtil;
import com.kym.entity.WashDevice;
import com.kym.entity.common.PageBean;
import com.kym.entity.queryParams.DeviceQueryParams;
import com.kym.entity.vo.WashDeviceVo;
import com.kym.mapper.WashDeviceMapper;
import com.kym.service.WashDeviceService;
import com.kym.service.WashOrderService;
import com.kym.service.cache.KymCache;
import com.kym.service.mybatisplus.MyBaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
*
* 洗车设备表 服务实现类
*
*
* @author skyline
* @since 2024-10-09
*/
@Service
public class WashDeviceServiceImpl extends MyBaseServiceImpl implements WashDeviceService {
private WashOrderService washOrderService;
public WashDeviceServiceImpl(WashOrderService washOrderService) {
this.washOrderService = washOrderService;
}
//region 小程序
/**
* 获取设备列表
*
* @param params
* @return
*/
@Override
public List 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();
//TODO 根据订单查询占用中的设备
// var currentUserId = StpUtil.getLoginIdAsLong();
for (WashDevice washDevice : list) {
var vo = new WashDeviceVo();
// .setCurrentUserId(currentUserId);
BeanUtils.copyProperties(washDevice, vo);
vo.setShortId(KymCache.INSTANCE.getShortIdByProductKeyAndDeviceName(washDevice.getProductKey(), washDevice.getDeviceName()));
vo.setSeqName(KymCache.INSTANCE.getSeqNameByShortId(vo.getShortId()));
voList.add(vo);
}
return voList;
}
/**
* 根据设备短id获取设备信息
*
* @param shortId
* @return
*/
@Override
public WashDeviceVo getDevice(String shortId) {
var productKeyAndDeviceName = KymCache.INSTANCE.getProductKeyAndDeviceNameByWashShortId(shortId);
var washDevice = lambdaQuery()
.eq(WashDevice::getProductKey, productKeyAndDeviceName[0])
.eq(WashDevice::getDeviceName, productKeyAndDeviceName[1])
.one();
//TODO 根据订单查询占用中的设备
var vo = new WashDeviceVo().setCurrentUserId(StpUtil.getLoginIdAsLong());
BeanUtils.copyProperties(washDevice, vo.setShortId(shortId));
return vo;
}
@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]));
}
//endregion
//region 管理后台
@Override
public Object detail(long id) {
return getById(id);
}
@Override
public void remove(long id) {
}
@Override
public void modify(WashDevice device) {
CommUtil.assertsNonNulls(List.of(device.getDeviceName(), device.getStationId(), device.getProductKey(), device.getId()), "参数异常");
List list = lambdaQuery()
.eq(WashDevice::getStationId, device.getStationId())
.eq(WashDevice::getDeviceName, device.getDeviceName())
.eq(WashDevice::getProductKey, device.getProductKey())
.list();
CommUtil.asserts(list.size() <= 1, "设备信息异常");
if (list.size() == 1) {
CommUtil.asserts(list.get(0).getDeviceName().equalsIgnoreCase(device.getDeviceName()), "站点已存在");
}
updateById(device);
}
@Override
public void add(WashDevice device) {
CommUtil.assertsNonNulls(List.of(device.getDeviceName(), device.getStationId(), device.getProductKey()), "参数异常");
Long count = lambdaQuery()
.eq(WashDevice::getStationId, device.getStationId())
.eq(WashDevice::getProductKey, device.getProductKey()).count();
CommUtil.asserts(count == 0, "设备已存在");
save(device);
}
@Override
public PageBean list(DeviceQueryParams query) {
PageHelper.startPage(query.getPageNum(), query.getPageSize());
List list = lambdaQuery()
.eq(CommUtil.isNotEmptyAndNull(query.getStationId()), WashDevice::getStationId, query.getStationId())
.list();
var voList = new ArrayList();
list.forEach(item -> {
var vo = new WashDeviceVo();
BeanUtils.copyProperties(item, vo);
vo.setShortId(KymCache.INSTANCE.getShortIdByProductKeyAndDeviceName(item.getProductKey(), item.getDeviceName()));
vo.setStationName(KymCache.INSTANCE.getStationNameById(item.getStationId()));
voList.add(vo);
});
return new PageBean<>(voList);
}
//endregion
}