168 lines
4.5 KiB
Go
168 lines
4.5 KiB
Go
package dao
|
|
|
|
import (
|
|
"case-admin-api/api/model"
|
|
"case-admin-api/api/requests"
|
|
"case-admin-api/global"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
type CasePlatformDao struct {
|
|
}
|
|
|
|
// GetCasePlatformById 获取数据-id
|
|
func (r *CasePlatformDao) GetCasePlatformById(casePlatformId int64) (m *model.CasePlatform, err error) {
|
|
err = global.Db.First(&m, casePlatformId).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetCasePlatformPreloadById 获取数据-加载全部关联-id
|
|
func (r *CasePlatformDao) GetCasePlatformPreloadById(casePlatformId int64) (m *model.CasePlatform, err error) {
|
|
err = global.Db.Preload(clause.Associations).First(&m, casePlatformId).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// DeleteCasePlatform 删除
|
|
func (r *CasePlatformDao) DeleteCasePlatform(tx *gorm.DB, maps interface{}) error {
|
|
err := tx.Where(maps).Delete(&model.CasePlatform{}).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteCasePlatformById 删除-id
|
|
func (r *CasePlatformDao) DeleteCasePlatformById(tx *gorm.DB, casePlatformId int64) error {
|
|
if err := tx.Delete(&model.CasePlatform{}, casePlatformId).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditCasePlatform 修改
|
|
func (r *CasePlatformDao) EditCasePlatform(tx *gorm.DB, maps interface{}, data interface{}) error {
|
|
err := tx.Model(&model.CasePlatform{}).Where(maps).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditCasePlatformById 修改-id
|
|
func (r *CasePlatformDao) EditCasePlatformById(tx *gorm.DB, casePlatformId int64, data interface{}) error {
|
|
err := tx.Model(&model.CasePlatform{}).Where("case_platform_id = ?", casePlatformId).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetCasePlatformList 获取列表
|
|
func (r *CasePlatformDao) GetCasePlatformList(maps interface{}) (m []*model.CasePlatform, err error) {
|
|
err = global.Db.Where(maps).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetCasePlatformCount 获取数量
|
|
func (r *CasePlatformDao) GetCasePlatformCount(maps interface{}) (total int64, err error) {
|
|
err = global.Db.Model(&model.CasePlatform{}).Where(maps).Count(&total).Error
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return total, nil
|
|
}
|
|
|
|
// GetCasePlatformListRand 获取列表-随机
|
|
func (r *CasePlatformDao) GetCasePlatformListRand(maps interface{}, limit int) (m []*model.CasePlatform, err error) {
|
|
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// AddCasePlatform 新增
|
|
func (r *CasePlatformDao) AddCasePlatform(tx *gorm.DB, model *model.CasePlatform) (*model.CasePlatform, error) {
|
|
if err := tx.Create(model).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return model, nil
|
|
}
|
|
|
|
// GetCasePlatform 获取
|
|
func (r *CasePlatformDao) GetCasePlatform(maps interface{}) (m *model.CasePlatform, err error) {
|
|
err = global.Db.Where(maps).First(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// Inc 自增
|
|
func (r *CasePlatformDao) Inc(tx *gorm.DB, caseId int64, field string, numeral int) error {
|
|
err := tx.Model(&model.CasePlatform{}).Where("case_platform_id = ?", caseId).UpdateColumn(field, gorm.Expr(field+" + ?", numeral)).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Dec 自减
|
|
func (r *CasePlatformDao) Dec(tx *gorm.DB, caseId int64, field string, numeral int) error {
|
|
err := tx.Model(&model.CasePlatform{}).Where("case_platform_id = ?", caseId).UpdateColumn(field, gorm.Expr(field+" - ?", numeral)).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetCasePlatformPageSearch 获取列表-分页
|
|
func (r *CasePlatformDao) GetCasePlatformPageSearch(req requests.GetStatsCasePlatformPage, page, pageSize int) (m []*model.CasePlatform, total int64, err error) {
|
|
var totalRecords int64
|
|
|
|
// 构建查询条件
|
|
query := global.Db.Model(&model.CasePlatform{})
|
|
|
|
// 项目平台
|
|
query = query.Preload("Platform")
|
|
|
|
query = query.Where("case_id = ?", req.CaseId)
|
|
|
|
// 搜索关键字
|
|
if req.Keyword != "" {
|
|
keyword := "%" + req.Keyword + "%" //
|
|
|
|
// 姓名/手机号
|
|
subQuery := global.Db.Model(&model.Platform{}).
|
|
Select("platform_id").
|
|
Where("platform_name LIKE ?", keyword)
|
|
|
|
// 执行
|
|
query = query.Where(gorm.Expr("platform_id IN (?)", subQuery))
|
|
}
|
|
|
|
// 排序
|
|
query = query.Order("created_at desc")
|
|
|
|
// 查询总数量
|
|
if err = query.Count(&totalRecords).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
err = query.Scopes(model.Paginate(page, pageSize)).Find(&m).Error
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return m, totalRecords, nil
|
|
}
|