package com.mx.cneeds.server.device.service.impl;

import com.mx.cneeds.common.pager.PageUtils;
import com.mx.cneeds.common.pager.Query;
import com.mx.cneeds.server.dao.DeviceInfoDao;
import com.mx.cneeds.server.device.service.DeviceInfoService;
import com.mx.cneeds.server.device.service.ProductSeriesService;
import com.mx.cneeds.server.entity.DeviceInfoEntity;
import com.mx.cneeds.server.entity.ProductSeriesEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


@Service("deviceInfoService")
public class DeviceInfoServiceImpl extends ServiceImpl<DeviceInfoDao, DeviceInfoEntity> implements DeviceInfoService {

    @Autowired
    private ProductSeriesService productSeriesService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DeviceInfoEntity> page = this.page(
                new Query<DeviceInfoEntity>().getPage(params),
                new QueryWrapper<DeviceInfoEntity>()
        );

        Set<Long> set  = new HashSet<>();

        page.getRecords().forEach(deviceInfoEntity -> {
            set.add(deviceInfoEntity.getSeriesId());
        });

        if (set.size() != 0){
            List<ProductSeriesEntity>  seriesEntityList = productSeriesService.queryListById(set);
            page.getRecords().forEach(deviceInfoEntity -> {
                seriesEntityList.forEach(seriesEntity -> {
                    if (deviceInfoEntity.getSeriesId().equals(seriesEntity.getSeriesId())){
                        deviceInfoEntity.setSeriesNum(seriesEntity.getSeriesNum());
                    }
                });
            });
        }

        return new PageUtils(page);
    }

    @Override
    public void updateBatchByImei(List<DeviceInfoEntity> deviceInfoEntityList) {
        deviceInfoEntityList.forEach(deviceInfoEntity -> {
            getBaseMapper().update(deviceInfoEntity,new QueryWrapper<DeviceInfoEntity>().eq("imei",deviceInfoEntity.getImei()));
        });
    }

    @Override
    public DeviceInfoEntity queryDeviceByImei(String imei){
        DeviceInfoEntity deviceInfoEntity = getBaseMapper().selectOne(new QueryWrapper<DeviceInfoEntity>()
                .eq("imei", imei));
        return deviceInfoEntity;
    }

    @Override
    public List<DeviceInfoEntity> queryDeviceByImeis(List<String> imeis){
        List<DeviceInfoEntity> entities = getBaseMapper().selectList(new QueryWrapper<DeviceInfoEntity>()
                .in("imei", imeis));
        return entities;
    }


    @Override
    public List<DeviceInfoEntity> queryDeviceByIds(Set<Long> ids){
        List<DeviceInfoEntity> deviceInfoEntities = getBaseMapper().queryDeviceByIds(ids);
        return deviceInfoEntities;
    }


}
