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

package projectapi

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

type ProjectApi struct {
	MySqlApi mysqlapi.MySqlApi
}

// 查重
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("domain_name=?", 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 {
	if err := obj.MySqlApi.DeleteItem(dataModel); err != nil {
		return errors.New("删除记录失败")
	}
	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
}
