package com.zg.project.Inventory.AutoInventory.controller;

import com.zg.framework.web.controller.BaseController;
import com.zg.framework.web.domain.AjaxResult;
import com.zg.project.Inventory.AutoInventory.service.InventoryMatchScanService;
import com.zg.project.Inventory.domain.dto.QueryDTO;
import com.zg.project.Inventory.domain.vo.ChartDataVO;
import com.zg.project.Inventory.AutoInventory.service.IRfidService;
import com.zg.project.Inventory.AutoInventory.service.ISodService;
import com.zg.project.wisdom.service.IRkInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
@RequestMapping("/AutoInventory")
public class AutoInventoryController extends BaseController {

    @Autowired
    private ISodService sodService;

    @Autowired
    private IRkInfoService rkInfoService;

    @Autowired
    private IRfidService rfidService;

    @Autowired
    private InventoryMatchScanService inventoryMatchScanService;
    @GetMapping("/cs")
    @ApiOperation("测试")
    public AjaxResult cs() {
        return AjaxResult.success("测试成功");
    }
    @PostMapping("/sodQuery")
    @ApiOperation("条件查询库存数据")
    public AjaxResult query(@RequestBody QueryDTO dto) {
        return AjaxResult.success(sodService.selectByPcdeList(dto));
    }

    /**
     * 开始盘点
     */
    @GetMapping("/scan")
    @ApiOperation("开始标签盘点（调用 GetEPC）")
    public AjaxResult startScan(@RequestParam String deviceId) {
        boolean started = rfidService.startScan(deviceId);
        return started
                ? AjaxResult.success("设备[" + deviceId + "]正在盘点中...")
                : AjaxResult.error("盘点命令发送失败");
    }

    /**
     * 停止盘点
     */
    @GetMapping("/stop")
    @ApiOperation("停止标签盘点")
    public AjaxResult stopScan(@RequestParam String deviceId) {
        boolean stopped = rfidService.stopScan(deviceId);
        return stopped
                ? AjaxResult.success("已发送停止盘点命令")
                : AjaxResult.error("停止失败");
    }

    /**
     * 开始匹配
     */
    @PostMapping("/match")
    @ApiOperation("开始匹配")
    public AjaxResult match(@RequestBody QueryDTO dto) {
        // 停止盘点
        rfidService.stopScan(dto.getDeviceId());

        dto.setScanType(1);

        rkInfoService.matchWithStatus(dto);

        return AjaxResult.success();
    }

    /**
     * 匹配后图表统计
     */
    @PostMapping("/chart")
    @ApiOperation("匹配后图表统计")
    public AjaxResult chart(@RequestBody QueryDTO dto) {
        ChartDataVO vo = rkInfoService.matchWithAll(dto);
        return AjaxResult.success(vo);
    }


    /**
     * 断开连接
     */
    @GetMapping("/disconnect")
    @ApiOperation("断开设备连接")
    public AjaxResult disconnect(@RequestParam String deviceId) {
        rfidService.disconnect(deviceId);
        return AjaxResult.success("设备[" + deviceId + "]连接已断开");
    }

    /**
     * 查询设备连接状态
     */
    @GetMapping("/status")
    @ApiOperation("查看设备是否已连接")
    public AjaxResult isConnected(@RequestParam String deviceId) {
        boolean connected = rfidService.isConnected(deviceId);
        return AjaxResult.success("设备[" + deviceId + "]连接状态: " + (connected ? "已连接" : "未连接"));
    }

    @GetMapping("/getAll")
    @ApiOperation("获取所有盘点数据")
    public AjaxResult getAll() {
        Map<String, Object> resultMap = inventoryMatchScanService.getAll();
        return AjaxResult.success(resultMap);
    }

}
