// 模块模型操作接口-项目
// 秒寻科技
// zt 2024-02-03

package projectapi

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

type ProjectApi struct {
	MySqlApi mysqlapi.MySqlApi
}

// 生成项目编号
func (obj *ProjectApi) GenerateProjectID() (string, error) {
	dataModel := new(projectmodel.Project)
	var strProjectId string = ""
	var count int = 0

	for {
		ProjectID := util.GenRandomProjectID(7) //生成项目ID，7个字符
		ProjectID = "0" + ProjectID
		if err := obj.MySqlApi.GetDb().Model(dataModel).Where("project_id=?", ProjectID).Count(&count).Error; err != nil {
			return strProjectId, err
		}
		if count <= 0 {
			strProjectId = ProjectID
			break
		}
	}

	return strProjectId, nil
}

// 查重
func (obj *ProjectApi) AddProject(dataModel *projectmodel.Project) error {
	var count uint

	//项目名称查重
	if err := obj.MySqlApi.GetDb().Model(dataModel).Where("company_id=? and name=?", dataModel.CompanyID, dataModel.Name).Count(&count).Error; err != nil {
		return err
	}
	if count > 0 {
		return errors.New("该项目已经存在")
	}

	//项目编号查重
	count = 0
	if err := obj.MySqlApi.GetDb().Model(dataModel).Where("project_id=?", dataModel.ProjectID).Count(&count).Error; err != nil {
		return err
	}
	if count > 0 {
		return errors.New("该项目编号已经存在")
	}

	//查重二级域名
	count = 0
	if err := obj.MySqlApi.GetDb().Model(dataModel).Where("domain_name=?", dataModel.DomainName).Count(&count).Error; err != nil {
		return err
	}
	if count > 0 {
		return errors.New("该二级域名已经存在")
	}

	if err := obj.MySqlApi.AddItem(dataModel); err != nil {
		return errors.New("数据库操作失败")
	}

	return nil
}

// 修改
func (obj *ProjectApi) UpdateProject(dataModel *projectmodel.Project, dataMap map[string]interface{}) error {
	var count uint
	var tmpModel projectmodel.Project

	//查重
	if err := obj.MySqlApi.GetDb().Model(&tmpModel).Where("id<>? and company_id=? and name=?", dataModel.ID, dataModel.CompanyID, dataModel.Name).Count(&count).Error; err != nil {
		return err
	}
	if count > 0 {
		return errors.New("该项目已经存在")
	}

	//查重二级域名
	count = 0
	if err := obj.MySqlApi.GetDb().Model(dataModel).Where("id<>? and domain_name=?", dataModel.ID, dataModel.DomainName).Count(&count).Error; err != nil {
		return err
	}
	if count > 0 {
		return errors.New("访问域名已经存在")
	}

	if err := obj.MySqlApi.UpdateItem(dataModel, dataMap); err != nil {
		return err
	}

	return nil
}

// 删除，删除项目的同时还要删除项目下面的所有基站
func (obj *ProjectApi) DeleteProject(dataModel *projectmodel.Project) error {
	//开启一个事务
	tx := obj.MySqlApi.GetDb().Begin()

	//基于项目ID获得项目资料
	if err := tx.Model(dataModel).Where("id=?", dataModel.ID).First(dataModel).Error; err != nil {
		tx.Rollback()
		return err
	}

	//删除该项目的所有基站
	if err := tx.Delete(&bparammodel.BtsBaseParam{}, "project_id=?", dataModel.ProjectID).Error; err != nil {
		tx.Rollback()
		return err
	}

	//删除项目资料
	if err := tx.Delete(&projectmodel.Project{}, "id=?", dataModel.ID).Error; err != nil {
		tx.Rollback()
		return err
	}

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

	return nil
}

// 查询单条记录
func (obj *ProjectApi) GetProjectById(dataModel *projectmodel.Project) error {
	if err := obj.MySqlApi.GetItemByID(dataModel); err != nil {
		return errors.New("查询记录失败")
	}
	return nil
}

// 根据域名获得项目
func (obj *ProjectApi) GetProjectByDomainName(dataModel *projectmodel.Project) error {
	if err := obj.MySqlApi.GetDb().Model(dataModel).Where("domain_name=?", dataModel.DomainName).First(dataModel).Error; err != nil {
		return errors.New("查询记录失败")
	}
	return nil
}

// 查询记录
func (obj *ProjectApi) GetProjectList(dataModel *projectmodel.Project, listParam *mysqlmodel.ListParam, dataList interface{}) error {
	if err := obj.MySqlApi.GetItemList(dataModel, listParam, dataList); err != nil {
		return err
	}
	return nil
}

// 分页查询记录
func (obj *ProjectApi) GetProjectPageList(dataModel *projectmodel.Project, 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
}

// 获得公司项目数
func (obj *ProjectApi) GetCompanyProjectSum(companyId int) (int, error) {
	dataModel := new(projectmodel.Project)
	var count int = 0

	//获得公司项目数
	if err := obj.MySqlApi.GetDb().Model(dataModel).Where("company_id=?", companyId).Count(&count).Error; err != nil {
		return 0, err
	}

	return count, nil
}
