package org.jeecg.modules.device.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.device.entity.BuDevice;
import org.jeecg.modules.device.entity.BuDeviceType;
import org.jeecg.modules.device.entity.DeviceLocation;
import org.jeecg.modules.device.entity.Part;
import org.jeecg.modules.device.mapper.BuDeviceMapper;
import org.jeecg.modules.device.mapper.BuDeviceTypeMapper;
import org.jeecg.modules.device.mapper.DeviceLocationMapper;
import org.jeecg.modules.device.mapper.PartMapper;
import org.jeecg.modules.device.model.DeviceSensorStatus;
import org.jeecg.modules.device.service.IBuDeviceService;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description: 设备信息表
 * @Author: jeecg-boot
 * @Date:   2020-12-11
 * @Version: V1.0
 */
@Service
public class BuDeviceServiceImpl extends ServiceImpl<BuDeviceMapper, BuDevice> implements IBuDeviceService {

    @Autowired
    private BuDeviceMapper buDeviceMapper;
    @Autowired
    private BuDeviceTypeMapper buDeviceTypeMapper;
    @Autowired
    private PartMapper partMapper;
    @Autowired
    private ISysDictItemService dictItemService;

    @Autowired
    private DeviceLocationMapper locationMapper;
    @Override
    public List<BuDevice> getDevice(){
        return buDeviceMapper.getDevice();
    }

    @Override
    public BuDevice getDeviceById(String id){
        return buDeviceMapper.getDeviceById(id);
    }

    @Override
    public BuDevice getDeviceByName(String deviceName){
        return buDeviceMapper.getDeviceByName(deviceName);
    }

    @Override
    public BuDevice getDeviceByCode(String deviceCode){
        return buDeviceMapper.getDeviceByCode(deviceCode);
    }

    @Override
    public BuDevice getDevicesensorid(String sensorid){
        return buDeviceMapper.getDevicesensorid(sensorid);
    }

    @Override
    public List<BuDevice> getDeviceUse(){
        return buDeviceMapper.getDeviceUse();
    }

    @Override
    public List<BuDevice> getDeviceUses(){
        return buDeviceMapper.getDeviceUses();
    }

    @Override
    public List<DeviceSensorStatus> queryDeviceStatus() {
        return buDeviceMapper.queryDeviceStatus();
    }

    @Override
    public List<DeviceSensorStatus> queryDevice() {
        return buDeviceMapper.queryDevice();
    }

    @Override
    public List<DeviceSensorStatus> querySensorStatus() {
        return buDeviceMapper.querySensorStatus();
    }

    @Override
    public List<DeviceSensorStatus> getDeviceTypeCount() {
        return buDeviceMapper.getDeviceTypeCount();
    }

    @Override
    public IPage<BuDevice> queryPageList(BuDevice buDevice, Integer pageNo, Integer pageSize, HttpServletRequest req){
//        QueryWrapper<BuDevice> queryWrapper = QueryGenerator.initQueryWrapper(buDevice, req.getParameterMap());
        LambdaQueryWrapper<BuDevice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(buDevice.getDeviceCode()),BuDevice::getDeviceCode, buDevice.getDeviceCode());
        queryWrapper.like(StringUtils.isNotBlank(buDevice.getDeviceName()),BuDevice::getDeviceName, buDevice.getDeviceName());
        queryWrapper.eq(StringUtils.isNotBlank(buDevice.getDeviceTypeId()),BuDevice::getDeviceTypeId, buDevice.getDeviceTypeId());
        Page<BuDevice> page = new Page<BuDevice>(pageNo, pageSize);
        // 查询设备列表
        IPage<BuDevice> pageList = page(page, queryWrapper);
        pageList.getRecords().forEach(item -> {
            if(StringUtils.isNotBlank(item.getLocation())){
                DeviceLocation location = locationMapper.selectById(item.getLocation());
                if(location!=null){
                    item.setLatlng(location.getLatitude() + "," + location.getLongitude());
                }
            }
        });
        return pageList;
    }

}
