package com.aiotagro.cattletrade.business.controller;

import com.aiotagro.cattletrade.business.service.IXqClientService;
import com.aiotagro.common.core.web.domain.AjaxResult;
import com.aiotagro.common.core.web.domain.PageResultResponse;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 项圈客户端控制器
 *
 * @author System
 * @date 2025-01-16
 */
@RestController
@RequestMapping("/xqClient")
public class XqClientController {

    @Resource
    private IXqClientService xqClientService;

    /**
     * 项圈列表（分页查询）
     */
    @PostMapping(value = "/list")
    public PageResultResponse list(@RequestBody Map<String, Object> params) {
        return xqClientService.xqList(params);
    }

    /**
     * 根据运送清单ID查询项圈列表
     */
    @PostMapping(value = "/pageXqListByDeliveryId")
    public AjaxResult pageXqListByDeliveryId(@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 xqClientService.getDevicesByDeliveryId(deliveryId, 3); // 3表示项圈
    }

    /**
     * 查询分页列表（通用）
     */
    @PostMapping(value = "/pageQuery")
    public PageResultResponse pageQuery(@RequestBody Map<String, Object> params) {
        return xqClientService.xqList(params);
    }
    
    /**
     * 项圈轨迹
     */
    @PostMapping(value = "/xqTrack")
    public AjaxResult xqTrack(@RequestBody Map<String, Object> params) {
        String deliveryId = params.get("deliveryId") == null ? null : String.valueOf(params.get("deliveryId"));
        String xqDeviceId = params.get("xqDeviceId") == null ? null : String.valueOf(params.get("xqDeviceId"));
        String trackTime = params.get("trackTime") == null ? null : String.valueOf(params.get("trackTime"));
        String trackEndTime = params.get("trackEndTime") == null ? null : String.valueOf(params.get("trackEndTime"));
        return xqClientService.getCollarTrack(deliveryId, xqDeviceId, trackTime, trackEndTime);
    }
    
    /**
     * 项圈位置
     */
    @PostMapping(value = "/xqLocation")
    public AjaxResult xqLocation(@RequestBody Map<String, Object> params) {
        String deliveryId = params.get("deliveryId") == null ? null : String.valueOf(params.get("deliveryId"));
        String xqDeviceId = params.get("xqDeviceId") == null ? null : String.valueOf(params.get("xqDeviceId"));
        return xqClientService.getCollarLocation(deliveryId, xqDeviceId);
    }
    
    /**
     * 测试项圈定位接口
     */
    @GetMapping("/testLocation/{deviceId}")
    public AjaxResult testLocation(@PathVariable("deviceId") String deviceId) {
        try {
            AjaxResult result = xqClientService.getCollarLocation(null, deviceId);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("测试失败: " + e.getMessage());
        }
    }
    
    /**
     * 设备轨迹
     */
    @PostMapping(value = "/xqDeviceTrack")
    public AjaxResult xqDeviceTrack(@RequestBody Map<String, Object> params) {
        String deliveryId = params.get("deliveryId") == null ? null : String.valueOf(params.get("deliveryId"));
        String xqDeviceId = params.get("xqDeviceId") == null ? null : String.valueOf(params.get("xqDeviceId"));
        String trackTime = params.get("trackTime") == null ? null : String.valueOf(params.get("trackTime"));
        String trackEndTime = params.get("trackEndTime") == null ? null : String.valueOf(params.get("trackEndTime"));
        return xqClientService.getCollarDeviceTrack(deliveryId, xqDeviceId, trackTime, trackEndTime);
    }
}


