package com.fuyuanshen.app.controller.device;

import com.fuyuanshen.app.domain.bo.AppPersonnelInfoBo;
import com.fuyuanshen.app.domain.dto.AppDeviceLogoUploadDto;
import com.fuyuanshen.app.domain.dto.DeviceInstructDto;
import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.common.core.validate.AddGroup;
import com.fuyuanshen.common.log.annotation.Log;
import com.fuyuanshen.common.ratelimiter.annotation.FunctionAccessAnnotation;
import com.fuyuanshen.common.ratelimiter.annotation.FunctionAccessBatcAnnotation;
import com.fuyuanshen.common.web.core.BaseController;
import com.fuyuanshen.customer.mapper.CustomerMapper;
import com.fuyuanshen.equipment.domain.DeviceType;
import com.fuyuanshen.equipment.domain.dto.AppDeviceSendMsgBo;
import com.fuyuanshen.equipment.domain.form.DeviceForm;
import com.fuyuanshen.equipment.mapper.DeviceMapper;
import com.fuyuanshen.equipment.service.DeviceService;
import com.fuyuanshen.equipment.service.DeviceTypeService;
import com.fuyuanshen.web.domain.Dto.DeviceXinghanInstructDto;
import com.fuyuanshen.web.service.device.DeviceBJQBizService;
import com.fuyuanshen.web.service.device.DeviceXinghanBizService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * HBY670设备控制类
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/xinghan/device")
public class AppDeviceXinghanController extends BaseController {

    private final DeviceXinghanBizService appDeviceService;
    private final DeviceService deviceService;


    /**
     * 人员信息登记
     */
    @Log(title = "xinghan指令-人员信息登记")
    @PostMapping(value = "/registerPersonInfo")
//    @FunctionAccessAnnotation("registerPersonInfo")
    public R<Void> registerPersonInfo(@Validated(AddGroup.class) @RequestBody AppPersonnelInfoBo bo) {
        return toAjax(appDeviceService.registerPersonInfo(bo));
    }

    /**
     * 发送紧急通知
     */
    @Log(title = "xinghan指令-发送紧急通知")
    @PostMapping(value = "/sendAlarmMessage")
    @FunctionAccessBatcAnnotation(value = "sendAlarmMessage", timeOut = 5, batchMaxTimeOut = 10)
    public R<Void> sendAlarmMessage(@RequestBody AppDeviceSendMsgBo bo) {
        return toAjax(appDeviceService.sendAlarmMessage(bo));
    }

    /**
     * 上传设备logo图片
     */
    @Log(title = "xinghan指令-上传设备logo图片")
    @PostMapping("/uploadLogo")
    @FunctionAccessAnnotation("uploadLogo")
    public R<Void> upload(@Validated @ModelAttribute AppDeviceLogoUploadDto bo) {

        MultipartFile file = bo.getFile();
        if (file.getSize() > 1024 * 1024 * 2) {
            return R.warn("图片不能大于2M");
        }
        appDeviceService.uploadDeviceLogo(bo);

        return R.ok();
    }

    /**
     * 静电预警档位
     * 3,2,1,0,分别表示高档/中档/低挡/关闭
     */
    @Log(title = "xinghan指令-静电预警档位")
    @PostMapping("/DetectGradeSettings")
    public R<Void> DetectGradeSettings(@RequestBody DeviceXinghanInstructDto params) {
        // params 转 JSONObject
        appDeviceService.upDetectGradeSettings(params);
        return R.ok();
    }

    /**
     * 照明档位
     * 照明档位，2,1,0,分别表示弱光/强光/关闭
     */
    @Log(title = "xinghan指令-照明档位")
    @PostMapping("/LightGradeSettings")
    public R<Void> LightGradeSettings(@RequestBody DeviceXinghanInstructDto params) {
        // params 转 JSONObject
        appDeviceService.upLightGradeSettings(params);
        return R.ok();
    }

    /**
     * SOS档位s
     * SOS档位，2,1,0, 分别表示红蓝模式/爆闪模式/关闭
     */
    @Log(title = "xinghan指令-SOS档位s")
    @PostMapping("/SOSGradeSettings")
    public R<Void> SOSGradeSettings(@RequestBody DeviceXinghanInstructDto params) {
        // params 转 JSONObject
        appDeviceService.upSOSGradeSettings(params);
        return R.ok();
    }

    /**
     * 静止报警状态
     * 静止报警状态，0-未静止报警，1-正在静止报警。
     */
    @Log(title = "xinghan指令-静止报警状态")
    @PostMapping("/ShakeBitSettings")
    public R<Void> ShakeBitSettings(@RequestBody DeviceXinghanInstructDto params) {
        // params 转 JSONObject
        appDeviceService.upShakeBitSettings(params);
        return R.ok();
    }

    @GetMapping(value = "/typeAll")
    @Operation(summary = "查询所有设备类型")
    public R<List<DeviceType>> queryDeviceTypes() {
        List<DeviceType> deviceTypes = appDeviceService.queryDeviceTypes();
        return R.ok(deviceTypes);
    }

    // @Log("新增设备")
    @Log(title = "新增设备")
    @PostMapping(value = "/add")
    public R<Void> addDevice(@RequestBody DeviceForm deviceForm) {
        try {
            appDeviceService.addDevice(deviceForm);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok();
    }

    @PostMapping(value = "/GetDeviceByName")
    @Operation(summary = "通过蓝牙名/设备名称查询设备")
    public R<Object> GetDeviceByName(@RequestBody DeviceForm deviceForm) {
        Object device = appDeviceService.GetDeviceByName(deviceForm);
        return R.ok(device);
    }


    @PostMapping(value = "/getEquipCountByType")
    @Operation(summary = "查询某个类型下的设备总数量")
    public R<Object> getEquipCountByType(@RequestBody DeviceForm deviceForm) {
        Object device = appDeviceService.getEquipCountByType(deviceForm);
        return R.ok(device);
    }

    @PostMapping(value = "/getEquipAllByType")
    @Operation(summary = "查询某个类型下的设备")
    public R<List<Map<String,Object>>> getEquipAllByType(@RequestBody DeviceForm deviceForm){
        List<Map<String,Object>> list=appDeviceService.getEquipAllByType(deviceForm);
        return R.ok(list);
    }

}
