package org.wcs.serve.service.pub.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.wcs.factory.PubServeResponseFactory;
import org.wcs.mapper.intf.AppStackerStandService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.po.app.AppStackerStand;
import org.wcs.model.vo.pub.PubServeDataResponse;
import org.wcs.model.vo.pub.deveice.ConveyStatusVo;
import org.wcs.plugin.plc.PlcCommunicationFactory;
import org.wcs.plugin.plc.model.TaskConveyStatus;
import org.wcs.serve.service.pub.intf.IDeviceService;
import org.wcs.utils.AppBeanUtils;
import org.wcs.utils.AppStringUtils;

import java.util.List;

@Service
@RequiredArgsConstructor
public class DeviceService implements IDeviceService {



    private final AppStackerStandService stackerStandDao; // 堆垛机站台表操作类

    /**
     * 查询输送设备状态
     * @param conveyNo 输送设备编号
     * @return 响应
     */
    @Override
    public PubServeDataResponse<ConveyStatusVo> queryConveyStatus(String conveyNo) {
        List<AppStackerStand> stackerStands = stackerStandDao.queryStandInfo(conveyNo);
        if(stackerStands == null) {
            return PubServeResponseFactory.fail("数据服务异常，请稍后再试", null);
        }
        if(stackerStands.isEmpty()) {
            return PubServeResponseFactory.fail("站台不存在", null);
        }
        AppStackerStand standInfo = stackerStands.getFirst();
        // 这里需要修改
        Tuple2<String, List<TaskConveyStatus>> multiConveyStatusResult = PlcCommunicationFactory.getPlcCommunicationByPlcId(standInfo.getPlcId()).readTaskMultiConveyStatus(conveyNo);
        if(AppStringUtils.isNotEmpty(multiConveyStatusResult.item1) || multiConveyStatusResult.item2 == null || multiConveyStatusResult.item2.isEmpty()) {
            return PubServeResponseFactory.fail("获取失败，信息：" + multiConveyStatusResult.getItem1(), null);
        }
        TaskConveyStatus first = multiConveyStatusResult.item2.getFirst();
        ConveyStatusVo conveyStatusVo = new ConveyStatusVo();
        AppBeanUtils.copyProperties(first, conveyStatusVo);
        return PubServeResponseFactory.success("SUCCESS", conveyStatusVo);
    }


}
