package com.aiotagro.cattletrade.business.controller;


import com.aiotagro.cattletrade.business.dto.DeviceDto;
import com.aiotagro.cattletrade.business.entity.JbqClient;
import com.aiotagro.cattletrade.business.entity.DeliveryDevice;
import com.aiotagro.cattletrade.business.mapper.DeliveryDeviceMapper;
import com.aiotagro.cattletrade.business.service.IJbqClientService;
import com.aiotagro.cattletrade.business.service.IDeliveryDeviceService;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 智能耳标表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-12-26
 */
@RestController
@RequestMapping("/jbqClient")
public class JbqClientController {
    @Autowired
    private IJbqClientService jbqClientService;
    
    @Autowired
    private IDeliveryDeviceService deliveryDeviceService;
    
    @Autowired
    private DeliveryDeviceMapper deliveryDeviceMapper;
    
    /**
     * 查询分页列表
     */
    @PostMapping(value = "/list")
    public PageResultResponse jbqList(@RequestBody DeviceDto dto) {
        return jbqClientService.jbqList(dto);
    }

    /**
     * 根据运送清单ID查询耳标列表
     */
    @PostMapping(value = "/pageJbqListByDeliveryId")
    public AjaxResult pageJbqListByDeliveryId(@RequestBody Map<String, Object> params) {
        Object deliveryIdObj = params.get("deliveryId");
        Integer deliveryId = null;
        if (deliveryIdObj != null) {
            if (deliveryIdObj instanceof Integer) {
                deliveryId = (Integer) deliveryIdObj;
            } else if (deliveryIdObj instanceof String) {
                deliveryId = Integer.valueOf((String) deliveryIdObj);
            }
        }
        return jbqClientService.getDevicesByDeliveryId(deliveryId, 2); // 2表示耳标类型
    }

    /**
     * 测试查询运单设备关联
     */
    @PostMapping(value = "/testDeliveryDevices")
    public AjaxResult testDeliveryDevices(@RequestBody Map<String, Object> params) {
        Object deliveryIdObj = params.get("deliveryId");
        Integer deliveryId = null;
        if (deliveryIdObj != null) {
            if (deliveryIdObj instanceof Integer) {
                deliveryId = (Integer) deliveryIdObj;
            } else if (deliveryIdObj instanceof String) {
                deliveryId = Integer.valueOf((String) deliveryIdObj);
            }
        }
        
        if (deliveryId == null) {
            return AjaxResult.error("运送清单ID不能为空");
        }
        
        // 查询所有设备类型
        LambdaQueryWrapper<DeliveryDevice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DeliveryDevice::getDeliveryId, deliveryId);
        List<DeliveryDevice> allDevices = deliveryDeviceMapper.selectList(wrapper);
        
        for (DeliveryDevice device : allDevices) {
        }
        
        // 按设备类型分组
        Map<Integer, List<DeliveryDevice>> devicesByType = allDevices.stream()
                .collect(Collectors.groupingBy(DeliveryDevice::getDeviceType));
        
        Map<String, Object> result = new HashMap<>();
        result.put("totalDevices", allDevices.size());
        result.put("devicesByType", devicesByType);
        result.put("allDevices", allDevices);
        
        return AjaxResult.success(result);
    }

    /**
     * 测试查询未分配设备
     */
    @PostMapping(value = "/testUnassigned")
    public AjaxResult testUnassigned(@RequestBody Map<String, Object> params) {
        try {
            
            // 直接查询jbq_client表
            LambdaQueryWrapper<JbqClient> wrapper = new LambdaQueryWrapper<>();
            List<JbqClient> devices = jbqClientService.list(wrapper);
            
            
            // 查询已分配的设备ID
            LambdaQueryWrapper<DeliveryDevice> deliveryWrapper = new LambdaQueryWrapper<>();
            deliveryWrapper.eq(DeliveryDevice::getDeviceType, 2);
            List<DeliveryDevice> assignedDevices = deliveryDeviceService.list(deliveryWrapper);
            
            
            // 筛选未分配的设备
            List<String> assignedDeviceIds = assignedDevices.stream()
                .map(DeliveryDevice::getDeviceId)
                .collect(Collectors.toList());
            
            List<JbqClient> unassignedDevices = devices.stream()
                .filter(device -> !assignedDeviceIds.contains(device.getDeviceId()))
                .collect(Collectors.toList());
            
            
            return AjaxResult.success("测试成功", unassignedDevices);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("测试失败: " + e.getMessage());
        }
    }

}

