package controller

import (
	"github.com/gin-gonic/gin"
	"iot_manager_service/app/operation/model"
	"iot_manager_service/app/operation/service"
	"iot_manager_service/util/common"
	"iot_manager_service/util/logger"
	"net/http"
)

var AepCallback = new(aepCallbackCtl)

type aepCallbackCtl struct{}

type aepCallbackCtlReq struct {
	ProductID string                   `json:"productId"`
	IMEI      string                   `json:"IMEI"`
	Payload   model.AepCallbackPayload `json:"payload"`
	Timestamp int64                    `json:"timestamp"`
}

func (c aepCallbackCtl) Save(ctx *gin.Context) {
	var req *aepCallbackCtlReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		logger.Logger.Errorf("aepCallbackCtl err = %s \n", err.Error())
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	//井盖设备
	if req.ProductID == "16682136" {
		err := service.AepCallbackRecordService.Save(req.Payload, req.Timestamp)
		if err != nil {
			logger.Logger.Errorf("16682136 aepCallbackCtl err = %s \n", err.Error())
			ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
			return
		}
		//TODO: 垃圾桶
		//} else if() {
	} else {
		logger.Logger.Errorf("不支持设备类型 aepCallbackCtl err = %s \n", req.ProductID)
		ctx.JSON(http.StatusOK, nil)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
}
