package com.aiotagro.cattletrade.business.service.impl;

import com.aiotagro.cattletrade.business.dto.DeviceDto;
import com.aiotagro.cattletrade.business.entity.DeliveryDevice;
import com.aiotagro.cattletrade.business.entity.JbqClient;
import com.aiotagro.cattletrade.business.mapper.DeliveryDeviceMapper;
import com.aiotagro.cattletrade.business.mapper.JbqClientMapper;
import com.aiotagro.cattletrade.business.service.IJbqClientService;
import com.aiotagro.cattletrade.business.vo.JbqClientVo;
import com.aiotagro.common.core.web.domain.AjaxResult;
import com.aiotagro.common.core.web.domain.PageResultResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 智能耳标表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-12-26
 */
@Service
public class JbqClientServiceImpl extends ServiceImpl<JbqClientMapper, JbqClient> implements IJbqClientService {

    @Autowired
    private JbqClientMapper jbqClientMapper;
    
    @Autowired
    private DeliveryDeviceMapper deliveryDeviceMapper;
    
    @Override
    public PageResultResponse jbqList(DeviceDto dto) {
        Page<JbqClient> result = PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        
        try {
            // 如果指定了只查询未分配的设备
            if (dto.getUnassignedOnly() != null && dto.getUnassignedOnly()) {
                List<JbqClientVo> list = jbqClientMapper.selectUnassignedJbqList(dto);
                return new PageResultResponse(result.getTotal(), list);
            } else {
                List<JbqClientVo> list = jbqClientMapper.JbqList(dto);
                return new PageResultResponse(result.getTotal(), list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResultResponse(0L, new ArrayList<>());
        }
    }
    
    @Override
    public AjaxResult getDevicesByDeliveryId(Integer deliveryId, Integer deviceType) {
        
        if (deliveryId == null) {
            return AjaxResult.error("运送清单ID不能为空");
        }
        
        // 查询运送清单关联的设备
        LambdaQueryWrapper<DeliveryDevice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DeliveryDevice::getDeliveryId, deliveryId);
        wrapper.eq(DeliveryDevice::getDeviceType, deviceType);
        List<DeliveryDevice> deliveryDevices = deliveryDeviceMapper.selectList(wrapper);
        
        if (!deliveryDevices.isEmpty()) {
        }
        
        if (deliveryDevices.isEmpty()) {
            return AjaxResult.success(new ArrayList<>());
        }
        
        // 获取设备ID列表
        List<String> deviceIds = deliveryDevices.stream()
                .map(DeliveryDevice::getDeviceId)
                .collect(Collectors.toList());
        
        // 查询设备详细信息 - 只查询基础字段，避免查询不存在的扩展字段
        LambdaQueryWrapper<JbqClient> deviceWrapper = new LambdaQueryWrapper<>();
        deviceWrapper.in(JbqClient::getDeviceId, deviceIds)
                     .select(JbqClient::getId,
                             JbqClient::getDeviceId,
                             JbqClient::getDeviceVoltage,
                             JbqClient::getDeviceTemp,
                             JbqClient::getServerDeviceId,
                             JbqClient::getLatitude,
                             JbqClient::getLongitude,
                             JbqClient::getWalkSteps,
                             JbqClient::getYWalkSteps,
                             JbqClient::getCreateTime,
                             JbqClient::getCreateBy,
                             JbqClient::getUpdateTime,
                             JbqClient::getUpdateBy,
                             JbqClient::getSourceId);
        List<JbqClient> devices = jbqClientMapper.selectList(deviceWrapper);
        
        for (JbqClient device : devices) {
        }
        
        // 返回分页格式数据
        Map<String, Object> result = new HashMap<>();
        result.put("rows", devices);
        result.put("total", devices.size());
        
        return AjaxResult.success(result);
    }
}

