// 模块模型操作接口-基站参数，包括基本参数和位置参数
// 秒寻科技
// zt 2024-02-15

package bparamapi

import (
	"errors"
	"ipsomc/core/dao/mysql/mysqlapi"
	"ipsomc/core/dao/mysql/mysqlmodel"
	"ipsomc/module/bparam/bparammodel"
	"ipsomc/module/project/projectmodel"
	"ipsomc/public"

	"github.com/jinzhu/gorm"
)

type BparamApi struct {
	MySqlApi mysqlapi.MySqlApi
}

// 增加基站基本参数
func (obj *BparamApi) AddBtsBaseParam(dataModel *bparammodel.BtsBaseParam) error {
	btsBaseModel := new(bparammodel.BtsBaseParam)
	projectModel := new(projectmodel.Project) //项目模型

	//基于项目ID，补充公司ID
	if dataModel.SourceID == 3 { //申请来源于GUI网管
		companyId, err := obj.GuiGetCompanyIdByProjectId(dataModel) //基于项目ID获得公司ID
		if err != nil {
			return err
		} else {
			dataModel.CompanyID = companyId //补充公司ID
		}
	}

	//查重
	if err := obj.MySqlApi.GetDb().Model(btsBaseModel).Where("company_id=? and project_id=? and bts_id=?", dataModel.CompanyID, dataModel.ProjectID, dataModel.BtsID).First(btsBaseModel).Error; err != nil {
		if err != gorm.ErrRecordNotFound {
			return err
		}
	}

	//该记录已经存在，则修改记录
	if btsBaseModel.ID > 0 {
		dataModel.ID = btsBaseModel.ID
		if err := obj.MySqlApi.GetDb().Model(btsBaseModel).Updates(map[string]interface{}{"carrier_id": dataModel.CarrierID, "timeslot": dataModel.Timeslot}).Where("id=?", dataModel.ID).Error; err != nil {
			return err
		}
	} else { //该记录还不存在，则心在记录
		////判断该记录是否合法，即项目ID是否存在，该项目下属基站是否已经超过最大规模数
		//判断项目ID是否存在
		if err := obj.MySqlApi.GetDb().Model(projectModel).Where("project_id=?", dataModel.ProjectID).First(projectModel).Error; err != nil {
			if err != gorm.ErrRecordNotFound {
				return err
			}
		}

		//项目ID不存在
		if projectModel.ID <= 0 {
			return errors.New("该项目编号不存在，请与系统管理员联系")
		} else { //项目ID存在
			//新增记录
			if err := obj.MySqlApi.AddItem(dataModel); err != nil {
				return err
			}
		}
	}

	return nil
}

// 修改基站基本参数
func (obj *BparamApi) UpdateBtsBaseParam(dataModel *bparammodel.BtsBaseParam, dataMap map[string]interface{}) error {
	var count int = 0

	//查重
	if err := obj.MySqlApi.GetDb().Model(dataModel).Where("id<>? and company_id=? and project_id=? and bts_id=?", dataModel.ID, dataModel.CompanyID, dataModel.ProjectID, dataModel.BtsID).Count(&count).Error; err != nil {
		return err
	}
	if count > 0 {
		return errors.New("该基站记录已经存在")
	}

	//修改
	delete(dataMap, "project_id")
	//delete(dataMap, "bts_id")
	if err := obj.MySqlApi.UpdateItem(dataModel, dataMap); err != nil {
		return errors.New("数据库操作失败")
	}

	return nil
}

// 修改设备状态
func (obj *BparamApi) UpdateBtsStatus(mapHeart map[string]public.HeartMapValue) error {
	modeBaseParam := new(bparammodel.BtsBaseParam)

	//开启事务
	tx := obj.MySqlApi.GetDb().Begin()

	//将所有项目的基站的状态修改为离线
	if err := tx.Model(modeBaseParam).Update("status", 1).Error; err != nil {
		tx.Rollback()
		return err
	}

	//map不为空
	if len(mapHeart) > 0 {
		//修改基站状态
		for _, v := range mapHeart {
			if err := tx.Model(modeBaseParam).Where("project_id=? and bts_id=?", v.ProjectID, v.BtsID).Update("status", 2).Error; err != nil {
				tx.Rollback()
				return err
			}
		}
	}

	//提交事务
	if err := tx.Commit().Error; err != nil {
		tx.Rollback()
		return err
	}

	//清空map
	public.PublicDeleteAllItem()

	return nil
}

// 删除基站基本参数
func (obj *BparamApi) DeleteBtsBaseParam(dataModel *bparammodel.BtsBaseParam) error {
	if err := obj.MySqlApi.DeleteItem(dataModel); err != nil {
		return errors.New("删除记录失败")
	}
	return nil
}

// 查询基站基本参数单条记录
func (obj *BparamApi) GetBtsBaseParamById(dataModel *bparammodel.BtsBaseParam) error {
	if err := obj.MySqlApi.GetItemByID(dataModel); err != nil {
		return errors.New("查询记录失败")
	}
	return nil
}

// 查询基站基本参数记录（满足条件的全部记录）
func (obj *BparamApi) GetBtsBaseParamList(dataModel *bparammodel.BtsBaseParam, listParam *mysqlmodel.ListParam, dataList interface{}) error {
	if err := obj.MySqlApi.GetItemList(dataModel, listParam, dataList); err != nil {
		return err
	}

	return nil
}

// 分页查询基站基本参数记录
func (obj *BparamApi) GetBtsBaseParamPageList(dataModel *bparammodel.BtsBaseParam, pageListParam *mysqlmodel.PageListParam, dataList interface{}) (mysqlmodel.PageListData, error) {
	pageListData, err := obj.MySqlApi.GetPageItemList(dataModel, pageListParam, dataList)
	if err != nil {
		return pageListData, err
	}
	return pageListData, nil
}

// 基于项目ID获得公司ID
func (obj *BparamApi) GuiGetCompanyIdByProjectId(dataModel *bparammodel.BtsBaseParam) (int, error) {
	modelProject := new(projectmodel.Project) //项目模型
	var companyId int = 0

	//获得公司信息
	if err := obj.MySqlApi.GetDb().Model(modelProject).Where("project_id=?", dataModel.ProjectID).First(modelProject).Error; err != nil {
		return companyId, err
	}

	if modelProject.CompanyID <= 0 {
		return companyId, errors.New("未找到与项目匹配的公司资料")
	}

	return modelProject.CompanyID, nil
}

// 基于项目ID和基站ID查询记录ID
func (obj *BparamApi) GetRecordIdByProjectIdAndBtsId(dataModel *bparammodel.BtsBaseParam) error {
	modelBtsBaseParam := new(bparammodel.BtsBaseParam) //项目模型

	//获得公司信息
	if err := obj.MySqlApi.GetDb().Model(modelBtsBaseParam).Where("project_id=? and bts_id=?", dataModel.ProjectID, dataModel.BtsID).First(modelBtsBaseParam).Error; err != nil {
		return err
	}
	dataModel.ID = modelBtsBaseParam.ID

	return nil
}

// 基于项目ID和公司ID检查基站参数记录是否已经存在
func (obj *BparamApi) GuiCheckBparamRecordIsExist(dataModel *bparammodel.BtsBaseParam) (int, error) {
	modelTmp := new(bparammodel.BtsBaseParam)

	//查询基站参数
	if err := obj.MySqlApi.GetDb().Model(modelTmp).Where("company_id=? and project_id=?", dataModel.CompanyID, dataModel.ProjectID).First(modelTmp).Error; err != nil {
		return modelTmp.ID, err
	}

	if modelTmp.ID <= 0 { //记录不存在
		return modelTmp.ID, nil
	}

	dataModel.ID = modelTmp.ID
	return modelTmp.ID, nil //记录存在
}

// 修改
func (obj *BparamApi) GuiUpdateBaseParam(dataModel *bparammodel.BtsBaseParam, dataMap map[string]interface{}) error {
	if err := obj.MySqlApi.UpdateItem(dataModel, dataMap); err != nil {
		return errors.New("数据库操作失败")
	}
	return nil
}

// 移动端查询基站参数
func (obj *BparamApi) GetMobileBtsParamList(dataModel *bparammodel.BtsBaseParam) ([]bparammodel.MobileBtsParam, error) {
	tempModel := new(bparammodel.BtsBaseParam)
	dataList := []bparammodel.MobileBtsParam{}

	db := obj.MySqlApi.GetDb()
	if err := db.Model(tempModel).Where("project_id=? and enable_flag=1", dataModel.ProjectID).Scan(&dataList).Error; err != nil {
		return dataList, err
	}
	return dataList, nil
}
