package com.njzscloud.supervisory.device.controller;

import com.alibaba.fastjson2.JSONObject;
import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.device.pojo.entity.DeviceInfoEntity;
import com.njzscloud.supervisory.device.service.DeviceInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

/**
 * 设备信息表
 */
@Slf4j
@RestController
@RequestMapping("/device_info")
@RequiredArgsConstructor
public class DeviceInfoController {

    private final DeviceInfoService deviceInfoService;

    /**
     * 新增
     *
     * @param deviceInfoEntity 数据
     */
    @PostMapping("/add")
    public R<?> add(@RequestBody DeviceInfoEntity deviceInfoEntity) {
        deviceInfoService.add(deviceInfoEntity);
        return R.success();
    }

    /**
     * 修改
     *
     * @param deviceInfoEntity 数据
     */
    @PostMapping("/modify")
    public R<?> modify(@RequestBody DeviceInfoEntity deviceInfoEntity) {
        deviceInfoService.modify(deviceInfoEntity);
        return R.success();
    }

    /**
     * 删除
     *
     * @param ids Ids
     */
    @PostMapping("/del")
    public R<?> del(@RequestBody List<Long> ids) {
        deviceInfoService.del(ids);
        return R.success();
    }

    /**
     * 详情
     *
     * @param id Id
     * @return DeviceInfoEntity 结果
     */
    @GetMapping("/detail")
    public R<DeviceInfoEntity> detail(@RequestParam Long id) {
        return R.success(deviceInfoService.detail(id));
    }

    /**
     * 分页查询
     *
     * @param deviceInfoEntity 筛选条件
     * @param pageParam        分页参数
     * @return PageResult&lt;DeviceInfoEntity&gt; 分页结果
     */
    @GetMapping("/paging")
    public R<PageResult<DeviceInfoEntity>> paging(PageParam pageParam, DeviceInfoEntity deviceInfoEntity) {
        return R.success(deviceInfoService.paging(pageParam, deviceInfoEntity));
    }

    /**
     * 识别
     */
    @PostMapping("/recognition")
    public R<?> recognition(@RequestBody JSONObject param) {
        /* JSONObject alarmInfoPlate = param.getJSONObject("AlarmInfoPlate");
        if (alarmInfoPlate == null) {
            alarmInfoPlate = param.getJSONObject("alarmInfoPlate");
        }
        if (alarmInfoPlate == null) {
            return R.failed();
        }
        String serialno = alarmInfoPlate.getString("serialno");
        JSONObject result = alarmInfoPlate.getJSONObject("result");
        if (result == null) {
            result = alarmInfoPlate.getJSONObject("Result");
        }
        if (result == null) {
            return R.failed();
        }
        JSONObject plateResult = result.getJSONObject("PlateResult");
        if (plateResult == null) {
            plateResult = result.getJSONObject("plateResult");
        }
        if (plateResult == null) {
            return R.failed();
        }
        String imageFile = plateResult.getString("imageFile");
        String license = plateResult.getString("license");

        if (StrUtil.isBlank(serialno) || StrUtil.isBlank(imageFile) || StrUtil.isBlank(license) || license.contains("无")) {
            return R.failed();
        }

        deviceInfoService.recognition(new DeviceRecognitionParam().setSn(serialno).setImg(imageFile).setLicensePlate(license)); */

        return R.success();
    }

    /**
     * 上报1,识别时调用
     */
    @PostMapping("/report1")
    public Map<String, Object> report1(@RequestBody JSONObject data) {
        return deviceInfoService.report1(data);
    }

    /**
     * 上报2，称重完调用
     */
    @PostMapping("/report2")
    public Map<String, Object> report2(@RequestBody JSONObject data) {
        return deviceInfoService.report2(data);
    }

}
