package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.domain.BaseDevice;
import com.usky.fire.domain.BaseDeviceStatus;
import com.usky.fire.mapper.BaseDeviceStatusMapper;
import com.usky.fire.service.BaseDeviceService;
import com.usky.fire.service.BaseDeviceStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 设备状态表 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-09-16
 */
@Service
public class BaseDeviceStatusServiceImpl extends AbstractCrudService<BaseDeviceStatusMapper, BaseDeviceStatus> implements BaseDeviceStatusService {

    @Autowired
    private BaseDeviceService baseDeviceService;

    @Override
    public List<BaseDeviceStatus> deviceStatusData(String deviceCode) {
        LambdaQueryWrapper<BaseDeviceStatus> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BaseDeviceStatus::getDeviceCode, deviceCode);
        List<BaseDeviceStatus> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public int deviceStatusCount(String companyId, Integer deviceStatus) {
        LambdaQueryWrapper<BaseDeviceStatus> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotBlank(companyId), BaseDeviceStatus::getCompanyId, companyId)
                .eq(deviceStatus != null, BaseDeviceStatus::getDeviceStatus, deviceStatus);
        int count = this.count(queryWrapper);
        return count;
    }

    @Override
    public int deviceStatusByTypeCount(String companyId, Integer deviceStatus, Integer deviceType) {
        LambdaQueryWrapper<BaseDevice> queryWrapper2 = Wrappers.lambdaQuery();
        queryWrapper2.select(BaseDevice::getId, BaseDevice::getDeviceCode)
                .eq(StringUtils.isNotBlank(companyId),BaseDevice::getCompanyId, companyId)
                .eq(BaseDevice::getDeviceType, deviceType);
        List<BaseDevice> list2 = baseDeviceService.list(queryWrapper2);
        List<String> deviceCodeList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list2)) {
            for (int j = 0; j < list2.size(); j++) {
                deviceCodeList.add(list2.get(j).getDeviceCode());
            }
        }
        int count = 0;
        if (CollectionUtils.isNotEmpty(deviceCodeList)){
            LambdaQueryWrapper<BaseDeviceStatus> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(deviceStatus != null, BaseDeviceStatus::getDeviceStatus, deviceStatus)
                    .in(BaseDeviceStatus::getDeviceCode, deviceCodeList);
            count = this.count(queryWrapper);
        }
        return count;
    }

}
