// 模块控制接口-基站参数
// 秒寻科技
// zt 2024-02-16

package bparamhandler

import (
	"fmt"
	"ipsomc/core/dao/mysql/mysqlmodel"
	"ipsomc/core/gin/bind"
	"ipsomc/core/gin/resp"
	"ipsomc/module/bparam/bparamapi"
	"ipsomc/module/bparam/bparamlogic"
	"ipsomc/module/bparam/bparammodel"
	"ipsomc/module/bparam/bparamoam"
	"ipsomc/public"
	"time"

	"github.com/gin-gonic/gin"
)

type BparamHan struct {
	bindApi      bind.BindApi
	bparamApi    bparamapi.BparamApi
	bparamLgc    bparamlogic.BparamLgc
	bparamOamApi bparamoam.BparamOam
}

// 新增基站基本参数
func (obj *BparamHan) AddBtsBaseParamHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//保存数据
	if err := obj.bparamApi.AddBtsBaseParam(dataModel); err != nil {
		resp.RespFail(c, err.Error())
		return
	}

	//返回数据
	resp.RespId(c, dataModel.ID)
}

// 修改
func (obj *BparamHan) UpdateBtsBaseParamHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)
	dataMap := make(map[string]interface{})

	//把前端数据绑定到模型
	if err := obj.bindApi.Bind2Map(c, dataModel, dataMap); err != nil {
		resp.RespFail(c, err.Error())
		return
	}

	//保存数据
	if err := obj.bparamApi.UpdateBtsBaseParam(dataModel, dataMap); err != nil {
		resp.RespFail(c, err.Error())
		return
	}

	//返回数据
	resp.RespOk(c)
}

// 删除
func (obj *BparamHan) DeleteBtsBaseParamHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据解析错误")
		return
	}

	if err := obj.bparamApi.DeleteBtsBaseParam(dataModel); err != nil {
		resp.RespFail(c, err.Error())
		return
	}

	//返回数据
	resp.RespOk(c)
}

// 查询单条记录
func (obj *BparamHan) GetBtsBaseParamByIdHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据解析错误")
		return
	}

	if err := obj.bparamApi.GetBtsBaseParamById(dataModel); err != nil {
		resp.RespFail(c, err.Error())
		return
	}

	//返回数据
	resp.RespData(c, dataModel)
}

// 查询满足条件的所有记录
func (obj *BparamHan) GetBtsBaseParamListHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)
	listParam := new(mysqlmodel.ListParam)
	dataList := []bparammodel.BtsBaseParam{}

	//把前端数据绑定到模型
	if err := obj.bindApi.Bind2Query(c, listParam); err != nil {
		resp.RespFail(c, "数据解析错误")
		return
	}

	//查询满足条件的记录
	err := obj.bparamApi.GetBtsBaseParamList(dataModel, listParam, &dataList)
	if err != nil {
		resp.RespFail(c, err.Error())
		return
	}

	//返回记录
	resp.RespList(c, dataList)
}

// 分页查询
func (obj *BparamHan) GetBtsBaseParamPageListHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)
	pageListParam := new(mysqlmodel.PageListParam)
	dataList := []bparammodel.BtsBaseParam{}

	//把前端数据绑定到模型
	if err := obj.bindApi.Bind2PageQuery(c, pageListParam); err != nil {
		resp.RespFail(c, "数据解析错误")
		return
	}

	pageListData, err := obj.bparamApi.GetBtsBaseParamPageList(dataModel, pageListParam, &dataList)
	if err != nil {
		resp.RespFail(c, err.Error())
		return
	}

	resp.RespList(c, pageListData)
}

// 移动端查询基站参数
func (obj *BparamHan) GetMobileBtsParamListHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据解析错误")
		return
	}

	//查询满足条件的
	dataList, err := obj.bparamApi.GetMobileBtsParamList(dataModel)
	if err != nil {
		resp.RespFail(c, "查询数据错误")
		return
	}

	println(dataList)

	//返回记录
	resp.RespList(c, dataList)
}

// 本地网管新增基站基本参数
func (obj *BparamHan) GuiUploadBtsBaseParamAddHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//保存数据
	if err := obj.bparamApi.AddBtsBaseParam(dataModel); err != nil {
		resp.RespFail(c, err.Error())
		return
	}

	//返回数据
	resp.RespId(c, dataModel.ID)
}

// 本地网管修改基站基本参数
func (obj *BparamHan) GuiUploadBtsBaseParamUpdateHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)
	dataMap := make(map[string]interface{})

	//把前端数据绑定到模型
	if err := obj.bindApi.Bind2Map(c, dataModel, dataMap); err != nil {
		resp.RespFail(c, err.Error())
		return
	}

	//保存数据
	if err := obj.bparamLgc.UploadBparamLgc(dataModel, dataMap); err != nil {
		resp.RespFail(c, err.Error())
		return
	}

	//返回数据
	resp.RespOk(c)
}

//***********************************以上是http命令**********************************************
//**********************************************************************************************
//***********************************下面是socket命令********************************************

// 保存http请求通道
func (obj *BparamHan) SaveRequestChan(httpReqChan chan interface{}, ProjectID int, BtsID int, mcpCmdType uint8) {
	requestIdStr := fmt.Sprintf("%08X", ProjectID) + "-" + fmt.Sprintf("%08X", BtsID) + "-" + fmt.Sprintf("%d", mcpCmdType)
	public.Gpub_mutex.Lock()
	public.Gpub_mapChan[requestIdStr] = httpReqChan //保存通道
	public.Gpub_mutex.Unlock()
}

// 返回数据并删除http请求通道
func (obj *BparamHan) RespDataWithChan(c *gin.Context, httpReqChan chan interface{}, ProjectID int, BtsID int, mcpCmdType uint8) {
	requestIdStr := fmt.Sprintf("%08X", ProjectID) + "-" + fmt.Sprintf("%08X", BtsID) + "-" + fmt.Sprintf("%d", mcpCmdType)

	//把数据返回给客户端
	select {
	case v := <-httpReqChan:
		resp.RespData(c, v) //数据返回前端
	case <-time.After(10 * time.Second): // 10秒超时
		resp.RespFail(c, "查询超时")
	}

	public.Gpub_mutex.Lock()
	delete(public.Gpub_mapChan, requestIdStr)
	public.Gpub_mutex.Unlock()
}

// 查询基站设备参数
func (obj *BparamHan) GetBtsOamParamDeviceHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//生成并保存http请求通道
	httpReqChan := make(chan interface{})
	obj.SaveRequestChan(httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_QUERY)

	//查询基站参数
	if err := obj.bparamOamApi.OamQueryDeviceParam(dataModel); err != nil {
		resp.RespFail(c, "查询数据错误")
	}

	//返回数据并删除http请求通道
	obj.RespDataWithChan(c, httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_QUERY)
}

// 查询基站网管参数
func (obj *BparamHan) GetBtsOamParamNetworkHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//生成并保存http请求通道
	httpReqChan := make(chan interface{})
	obj.SaveRequestChan(httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_QUERY)

	//查询基站参数
	if err := obj.bparamOamApi.OamQueryNetworkParam(dataModel); err != nil {
		resp.RespFail(c, "查询数据错误")
	}

	//返回数据并删除http请求通道
	obj.RespDataWithChan(c, httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_QUERY)
}

// 设置基站网管参数
func (obj *BparamHan) SetBtsOamParamNetworkHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//生成并保存http请求通道
	httpReqChan := make(chan interface{})
	obj.SaveRequestChan(httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_SET)

	//设置基站参数
	if err := obj.bparamOamApi.OamSetNetworkParam(dataModel); err != nil {
		resp.RespFail(c, "设置数据错误")
	}

	//返回数据并删除http请求通道
	obj.RespDataWithChan(c, httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_SET)
}

// 查询基站同步参数
func (obj *BparamHan) GetBtsOamParamSyncHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//生成并保存http请求通道
	httpReqChan := make(chan interface{})
	obj.SaveRequestChan(httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_QUERY)

	//查询基站参数
	if err := obj.bparamOamApi.OamQuerySyncParam(dataModel); err != nil {
		resp.RespFail(c, "查询数据错误")
	}

	//返回数据并删除http请求通道
	obj.RespDataWithChan(c, httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_QUERY)
}

// 设置基站同步参数
func (obj *BparamHan) SetBtsOamParamSyncHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//生成并保存http请求通道
	httpReqChan := make(chan interface{})
	obj.SaveRequestChan(httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_SET)

	//设置基站参数
	if err := obj.bparamOamApi.OamSetSyncParam(dataModel); err != nil {
		resp.RespFail(c, "设置数据错误")
	}

	//返回数据并删除http请求通道
	obj.RespDataWithChan(c, httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_SET)
}

// 查询基站增益参数
func (obj *BparamHan) GetBtsOamParamGainHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsDeviceParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//生成并保存http请求通道
	httpReqChan := make(chan interface{})
	obj.SaveRequestChan(httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_QUERY)

	//查询基站参数
	if err := obj.bparamOamApi.OamQueryGainParam(dataModel); err != nil {
		resp.RespFail(c, "查询数据错误")
	}

	//返回数据并删除http请求通道
	obj.RespDataWithChan(c, httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_QUERY)
}

// 设置基站增益参数
func (obj *BparamHan) SetBtsOamParamGainHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//生成并保存http请求通道
	httpReqChan := make(chan interface{})
	obj.SaveRequestChan(httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_SET)

	//设置基站参数
	if err := obj.bparamOamApi.OamSetGainParam(dataModel); err != nil {
		resp.RespFail(c, "设置数据错误")
	}

	//返回数据并删除http请求通道
	obj.RespDataWithChan(c, httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_SET)
}

// 查询安装参数
func (obj *BparamHan) GetBtsOamParamInstallHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsDeviceParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//生成并保存http请求通道
	httpReqChan := make(chan interface{})
	obj.SaveRequestChan(httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_QUERY)

	//查询基站参数
	if err := obj.bparamOamApi.OamQueryInstallParam(dataModel); err != nil {
		resp.RespFail(c, "查询数据错误")
	}

	//返回数据并删除http请求通道
	obj.RespDataWithChan(c, httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_QUERY)
}

// 配置基站参数
func (obj *BparamHan) SetBtsOamParamInstallHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//生成并保存http请求通道
	httpReqChan := make(chan interface{})
	obj.SaveRequestChan(httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_SET)

	//设置基站参数
	if err := obj.bparamOamApi.OamSetInstallParam(dataModel); err != nil {
		resp.RespFail(c, "设置数据错误")
	}

	//返回数据并删除http请求通道
	obj.RespDataWithChan(c, httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_SET)
}

// 查询基站参数
func (obj *BparamHan) GetBtsOamParamBtsHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsDeviceParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//生成并保存http请求通道
	httpReqChan := make(chan interface{})
	obj.SaveRequestChan(httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_QUERY)

	//查询基站参数
	if err := obj.bparamOamApi.OamQueryBtsParam(dataModel); err != nil {
		resp.RespFail(c, "查询数据错误")
	}

	//返回数据并删除http请求通道
	obj.RespDataWithChan(c, httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_QUERY)
}

// 配置基站参数
func (obj *BparamHan) SetBtsOamParamBtsHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//生成并保存http请求通道
	httpReqChan := make(chan interface{})
	obj.SaveRequestChan(httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_SET)

	//设置基站参数
	if err := obj.bparamOamApi.OamSetBtsParam(dataModel); err != nil {
		resp.RespFail(c, "设置数据错误")
	}

	//返回数据并删除http请求通道
	obj.RespDataWithChan(c, httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_SET)
}

// 查询定位类型参数
func (obj *BparamHan) GetBtsOamParamLocationTypeHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsDeviceParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//生成并保存http请求通道
	httpReqChan := make(chan interface{})
	obj.SaveRequestChan(httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_QUERY)

	//查询基站参数
	if err := obj.bparamOamApi.OamQueryLocationTypeParam(dataModel); err != nil {
		resp.RespFail(c, "查询数据错误")
	}

	//返回数据并删除http请求通道
	obj.RespDataWithChan(c, httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_QUERY)
}

// 配置定位类型参数
func (obj *BparamHan) SetBtsOamParamLocationTypeHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//生成并保存http请求通道
	httpReqChan := make(chan interface{})
	obj.SaveRequestChan(httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_SET)

	//设置基站参数
	if err := obj.bparamOamApi.OamSetLocationTypeParam(dataModel); err != nil {
		resp.RespFail(c, "设置数据错误")
	}

	//返回数据并删除http请求通道
	obj.RespDataWithChan(c, httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_SET)
}

// 查询rtc参数
func (obj *BparamHan) GetBtsOamParamRtcHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//生成并保存http请求通道
	httpReqChan := make(chan interface{})
	obj.SaveRequestChan(httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_QUERY)

	//查询基站参数
	if err := obj.bparamOamApi.OamQueryRtcParam(dataModel); err != nil {
		resp.RespFail(c, "查询数据错误")
	}

	//返回数据并删除http请求通道
	obj.RespDataWithChan(c, httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_QUERY)
}

// 配置rtc参数
func (obj *BparamHan) SetBtsOamParamRtcHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//生成并保存http请求通道
	httpReqChan := make(chan interface{})
	obj.SaveRequestChan(httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_SET)

	//设置基站参数
	if err := obj.bparamOamApi.OamSetRtcParam(dataModel); err != nil {
		resp.RespFail(c, "设置数据错误")
	}

	//返回数据并删除http请求通道
	obj.RespDataWithChan(c, httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_SET)
}

// 保存参数
func (obj *BparamHan) SaveOamParamHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//生成并保存http请求通道
	httpReqChan := make(chan interface{})
	obj.SaveRequestChan(httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_SET)

	//设置基站参数
	if err := obj.bparamOamApi.OamSaveParam(dataModel); err != nil {
		resp.RespFail(c, "设置数据错误")
	}

	//返回数据并删除http请求通道
	obj.RespDataWithChan(c, httpReqChan, dataModel.ProjectID, dataModel.BtsID, public.BTS_MCP_SET)
}

// 复位基站
func (obj *BparamHan) ResetBtsHan(c *gin.Context) {
	dataModel := new(bparammodel.BtsBaseParam)

	//把前端数据绑定到模型上
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//设置基站参数
	if err := obj.bparamOamApi.OamResetBts(dataModel); err != nil {
		resp.RespFail(c, "设置数据错误")
	}

	resp.RespOk(c)
}
