package com.sptg.device.biz.controller;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.entity.PublishTask;
import com.sptg.device.api.constant.DeviceConstants;
import com.sptg.device.api.entity.Device;
import com.sptg.device.api.entity.InstructPublishLog;
import com.sptg.device.api.feigns.MsgPublishFeign;
import com.sptg.device.biz.service.DeviceServiceInterface;
import com.sptg.device.biz.service.MsgPublishService;
import lombok.RequiredArgsConstructor;
import org.apache.http.util.Asserts;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 *
 *
 * description:
 */
@RestController
@RequiredArgsConstructor
public class MsgPublishController implements MsgPublishFeign {

    private final MsgPublishService msgPublishService;
    private final DeviceServiceInterface deviceServiceInterface;

    @Override
    @PostMapping(value = "/device/msg/publishTask")
    public void publishTask(@RequestBody PublishTask task , @RequestParam Map<String,String> map) {
        this.msgPublishService.publishTask(task);
    }

    @Override
    @PostMapping(value = "/device/instruct/resend")
    public ResultBean<Boolean> resendInstruct(@RequestBody InstructPublishLog instructPublishLog) {
        String serialNumber = instructPublishLog.getSerialNumber();
        Asserts.notBlank(serialNumber, "设备序列号不能为空");
        Device device = this.deviceServiceInterface.selectBySerialNumber(serialNumber);
        Asserts.notNull(device, "设备不存在，不能下发指令");
        if(DeviceConstants.DEVICE_FACTORY_DAHAO.equals(device.getDeviceFactory())){
            this.msgPublishService.asyncResendInstructDH(instructPublishLog.getPublishData(), serialNumber, instructPublishLog);
            return new ResultBean<>(ResultEnum.SUCCESS, true);
        }
        this.msgPublishService.asyncResendInstruct(instructPublishLog.getPublishData(), serialNumber, instructPublishLog);
        return new ResultBean<>(ResultEnum.SUCCESS, true);
    }
}
