package com.kym.service.impl;

import com.github.pagehelper.PageHelper;
import com.kym.common.utils.CommUtil;
import com.kym.entity.DeviceConfig;
import com.kym.entity.WashDevice;
import com.kym.entity.common.PageBean;
import com.kym.entity.queryParams.DeviceConfigParams;
import com.kym.entity.queryParams.DeviceConfigQueryParams;
import com.kym.mapper.DeviceConfigMapper;
import com.kym.service.DeviceConfigService;
import com.kym.service.WashDeviceService;
import com.kym.service.awoara.AwoaraService;
import com.kym.service.cache.KymCache;
import com.kym.service.mybatisplus.MyBaseServiceImpl;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 洗车设备参数配置表 服务实现类
 * </p>
 *
 * @author skyline
 * @since 2025-01-19
 */
@Service
public class DeviceConfigServiceImpl extends MyBaseServiceImpl<DeviceConfigMapper, DeviceConfig> implements DeviceConfigService {

    private final AwoaraService awoaraService;

    private final WashDeviceService washDeviceService;

    public DeviceConfigServiceImpl(AwoaraService awoaraService, @Lazy WashDeviceService washDeviceService) {
        this.awoaraService = awoaraService;
        this.washDeviceService = washDeviceService;
    }

    @Override
    public void add(DeviceConfig deviceConfig) {
        save(deviceConfig);
    }

    @Override
    public void modify(DeviceConfig deviceConfig) {
        updateById(deviceConfig);
    }

    @Override
    public void batchModify(DeviceConfigParams params) {
        var deviceList = washDeviceService.lambdaQuery()
                .in(CommUtil.isNotEmptyAndNull(params.getDeviceIds()), WashDevice::getId, params.getDeviceIds())
                .list();
        CommUtil.asserts(CommUtil.isNotEmptyAndNull(deviceList), "设备不存在");
        var deviceConfig = getById(params.getDeviceConfigId());
        CommUtil.asserts(CommUtil.isNotEmptyAndNull(deviceConfig), "设备参数不存在");
        deviceList.forEach(device -> awoaraService.writeConfig(device.getProductKey(), device.getDeviceName(), deviceConfig));
        // 更新设备对应的配置ID
        washDeviceService.lambdaUpdate().set(WashDevice::getDeviceConfigId, params.getDeviceConfigId()).in(WashDevice::getId, params.getDeviceIds()).update();
    }

    @Override
    public Object readDeviceConfig(String shortId) {
        return awoaraService.readConfig(KymCache.INSTANCE.getProductKeyAndDeviceNameByWashShortId(shortId)[0], KymCache.INSTANCE.getProductKeyAndDeviceNameByWashShortId(shortId)[1]);
    }

    @Override
    public PageBean<DeviceConfig> list(DeviceConfigQueryParams params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        var res = lambdaQuery()
                .orderByDesc(DeviceConfig::getId)
                .list();
        return new PageBean<>(res);
    }

    @Override
    public void pushNewDeviceConfig(Long deviceConfigId){
        var deviceConfig = getById(deviceConfigId);
        CommUtil.asserts(CommUtil.isNotEmptyAndNull(deviceConfig), "设备参数不存在");
        // 查询该配置下绑定的设备
        var deviceList = washDeviceService.lambdaQuery().eq(WashDevice::getDeviceConfigId,deviceConfigId).list();
        deviceList.forEach(device -> awoaraService.writeConfig(device.getProductKey(), device.getDeviceName(), deviceConfig));
    }


}
