package com.sptg.device.biz.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.enums.Instruct;
import com.sptg.device.api.dto.DeviceOptionsDto;
import com.sptg.device.api.dto.DeviceTemplateDto;
import com.sptg.device.api.entity.Device;
import com.sptg.device.api.entity.DeviceOptions;
import com.sptg.device.api.entity.DeviceTemplate;
import com.sptg.device.api.feigns.DeviceOptionsFeign;
import com.sptg.device.api.feigns.DeviceTemplateFeign;
import com.sptg.device.biz.service.DeviceOptionsServiceInterface;
import com.sptg.device.biz.service.DeviceServiceInterface;
import com.sptg.device.biz.service.DeviceTemplateServiceInterface;
import com.sptg.device.biz.service.MsgPublishService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequiredArgsConstructor
public class DeviceOptionsController implements DeviceOptionsFeign {
    private final DeviceOptionsServiceInterface deviceOptionsServiceInterface;
    private final MsgPublishService msgPublishService;
    private final DeviceServiceInterface deviceServiceInterface;

    @Override
    @PostMapping(value = "/device/options/add")
    public Boolean add(@RequestBody DeviceOptionsDto deviceOptionsDto) {
        Boolean add = this.deviceOptionsServiceInterface.add(deviceOptionsDto);
        if(add){
            //  下发远程更新设备配置信息指令
            //  通知设备信息被修改
            DeviceOptions deviceOptions = this.deviceOptionsServiceInterface.getBySerialNumber(deviceOptionsDto.getSerialNumber());
            Device device = this.deviceServiceInterface.selectBySerialNumber(deviceOptions.getSerialNumber());
            if(deviceOptions != null && device != null){
                Map<String,String> map = new HashMap<>();
                //小区
                map.put("xqName", device.getXqName());
                //单元
                map.put("unitName", device.getDyName());
                this.msgPublishService.asyncPublish(deviceOptions, device, Instruct.remote_update_device_setting, map);
            }
        }
        return add;
    }

    @Override
    @PostMapping(value = "/device/options/edit")
    public Boolean edit(@RequestBody DeviceOptionsDto deviceOptionsDto) {
        Boolean edit = this.deviceOptionsServiceInterface.edit(deviceOptionsDto);
        if(edit){
            //  下发远程更新设备配置信息指令
            //  通知设备信息被修改
            Device device = this.deviceServiceInterface.selectBySerialNumber(deviceOptionsDto.getSerialNumber());
            DeviceOptions deviceOptions = this.deviceOptionsServiceInterface.getBySerialNumber(deviceOptionsDto.getSerialNumber());
            if(deviceOptions != null && device != null){
                Map<String,String> map = new HashMap<>();
                //小区
                map.put("xqName", device.getXqName());
                //单元
                map.put("unitName", device.getDyName());
                this.msgPublishService.asyncPublish(deviceOptions, device, Instruct.remote_update_device_setting, map);
            }
        }
        return edit;
    }

    @Override
    @PostMapping(value = "/device/options/deleteOptions")
    public Boolean deleteOptions(@RequestBody DeviceOptionsDto deviceOptionsDto) {
        return this.deviceOptionsServiceInterface.deleteOptions(deviceOptionsDto);
    }

    @Override
    @PostMapping(value = "/device/options/deleteByOptionsId")
    public Boolean deleteByOptionsId(@RequestBody Long id) {
        return this.deviceOptionsServiceInterface.deleteByOptionsId(id);
    }

    @Override
    @GetMapping(value = "/device/options/getBySerialNumber")
    public DeviceOptions getBySerialNumber(@RequestBody String serialNumber) {
        return this.deviceOptionsServiceInterface.getBySerialNumber(serialNumber);
    }

    @Override
    @GetMapping(value = "/device/options/getByDeviceId/{deviceId}")
    public DeviceOptions getByDeviceId(@PathVariable(value = "deviceId") Long deviceId) {
        return this.deviceOptionsServiceInterface.getByDeviceId(deviceId);
    }

    @Override
    @GetMapping(value = "/device/options/getOne")
    public DeviceOptions getOne(@RequestBody DeviceOptionsDto deviceOptionsDto) {
        return this.deviceOptionsServiceInterface.getOne(deviceOptionsDto);
    }
}
