package material import ( "eta/eta_api/models/system" "eta/eta_api/utils" "fmt" "github.com/beego/beego/v2/client/orm" "github.com/rdlucklib/rdluck_tools/paging" "time" ) type Material struct { MaterialId int `orm:"column(material_id);pk" description:"素材id"` MaterialName string `description:"素材名称"` MaterialNameEn string `description:"英文素材名称"` ImgUrl string `description:"素材图片地址"` SysUserId int `description:"作者id"` SysUserRealName string `description:"作者名称"` ModifyTime time.Time `description:"修改时间"` CreateTime time.Time `description:"创建时间"` ClassifyId int `description:"分类id"` Sort int `description:"排序"` } // Update 素材字段变更 func (material *Material) Update(cols []string) (err error) { o := orm.NewOrmUsingDB("rddp") _, err = o.Update(material, cols...) return } // GetMaterialById 根据素材id获取素材详情 func GetMaterialById(MaterialId int) (materialInfo *Material, err error) { o := orm.NewOrmUsingDB("rddp") sql := `select * FROM material where material_id = ?` err = o.Raw(sql, MaterialId).QueryRow(&materialInfo) return } func DeleteByMaterialIds(ids []int) (err error) { o := orm.NewOrmUsingDB("rddp") sql := `DELETE FROM material WHERE material_id in (` + utils.GetOrmInReplace(len(ids)) + `)` _, err = o.Raw(sql, ids).Exec() return } func UpdateClassifyByMaterialIds(ids []int, classifyId int, now time.Time) (err error) { o := orm.NewOrmUsingDB("rddp") sql := `UPDATE material SET classify_id=?, modify_time=? WHERE material_id in (` + utils.GetOrmInReplace(len(ids)) + `) and classify_id != ?` _, err = o.Raw(sql, classifyId, now, ids, classifyId).Exec() return } // AddMultiMaterial 批量添加素材 func AddMultiMaterial(materialInfoList []*Material) (err error) { o := orm.NewOrmUsingDB("rddp") _, err = o.InsertMulti(utils.MultiAddNum, materialInfoList) if err != nil { return } return } // UpdateMaterial 更新素材 func UpdateMaterial(materialInfo *Material, updateMaterialColumn []string) (err error) { o := orm.NewOrmUsingDB("rddp") to, err := o.Begin() if err != nil { return } defer func() { if err != nil { _ = to.Rollback() } else { _ = to.Commit() } }() _, err = to.Update(materialInfo, updateMaterialColumn...) if err != nil { return } return } // MaterialListItem 素材推演列表数据 type MaterialListItem struct { MaterialId int `description:"素材id"` MaterialName string `description:"素材名称"` MaterialNameEn string `description:"英文素材名称"` ImgUrl string `description:"素材图片地址"` ModifyTime string `description:"修改时间"` CreateTime string `description:"创建时间"` SysUserId int `description:"作者id"` SysUserRealName string `description:"作者名称"` } // MaterialSaveResp 保存素材响应体 type MaterialSaveResp struct { *Material } func GetMaterialAll() (list []*MaterialClassifyItems, err error) { o := orm.NewOrmUsingDB("rddp") sql := `SELECT material_id,classify_id,material_name AS classify_name, sort FROM material ` _, err = o.Raw(sql).QueryRows(&list) return } // CheckOpMaterialPermission 判断素材操作权限 func CheckOpMaterialPermission(sysUser *system.Admin, createUserId int) (ok bool) { if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN { ok = true } // 如果图表创建人与当前操作人相同的话,那么就是允许操作 if ok == false && createUserId == sysUser.AdminId { ok = true } return } // GetMaterialInfoByAdminId 获取所有我创建的素材,用于分类展示 func GetMaterialInfoByAdminId(adminId int) (items []*MaterialClassifyItems, err error) { o := orm.NewOrmUsingDB("rddp") sql := ` SELECT material_id,classify_id,material_name AS classify_name,code, sys_user_id,sys_user_real_name FROM material where sys_user_id = ? ORDER BY sort asc,create_time ASC ` _, err = o.Raw(sql, adminId).QueryRows(&items) return } func GetMaterialClassify(classifyId int) (classify_id string, err error) { o := orm.NewOrmUsingDB("rddp") sql := `SELECT GROUP_CONCAT(t.classify_id) AS classify_id FROM ( SELECT a.classify_id FROM material_classify AS a WHERE a.classify_id=? UNION ALL SELECT a.classify_id FROM material_classify AS a WHERE a.parent_id=? UNION ALL SELECT classify_id FROM material_classify WHERE parent_id IN ( SELECT classify_id FROM material_classify WHERE parent_id = ? ) )AS t` err = o.Raw(sql, classifyId, classifyId, classifyId).QueryRow(&classify_id) return } type MaterialListItems struct { Material ModifyTime string `description:"修改时间"` CreateTime string `description:"创建时间"` ParentIds string } func GetMaterialListPageByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*MaterialListItems, err error) { o := orm.NewOrmUsingDB("rddp") sql := ` SELECT * FROM material WHERE 1=1 ` if condition != "" { sql += condition } sql += " ORDER BY create_time DESC, material_id DESC LIMIT ?,? " _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item) return } func GetMaterialListByCondition(condition string, pars []interface{}) (item []*MaterialListItems, err error) { o := orm.NewOrmUsingDB("rddp") sql := ` SELECT * FROM material WHERE 1=1 ` if condition != "" { sql += condition } sql += " ORDER BY create_time DESC, material_id DESC" _, err = o.Raw(sql, pars).QueryRows(&item) return } func GetMaterialListCountByCondition(condition string, pars []interface{}) (count int, err error) { o := orm.NewOrmUsingDB("rddp") sql := ` SELECT COUNT(1) AS count FROM material WHERE 1=1 ` if condition != "" { sql += condition } err = o.Raw(sql, pars).QueryRow(&count) return } type MaterialListResp struct { Paging *paging.PagingItem List []*MaterialListItems } func AddMaterial(item *Material) (lastId int64, err error) { o := orm.NewOrmUsingDB("rddp") lastId, err = o.Insert(item) return } type MoveMaterialReq struct { MaterialId int `description:"素材ID"` PrevMaterialId int `description:"上一个素材ID"` NextMaterialId int `description:"下一个素材ID"` ClassifyId int `description:"分类id"` } func GetMaterialClassifyCountById(classifyId int) (count int, err error) { o := orm.NewOrmUsingDB("rddp") sql := `SELECT count(1) AS count FROM material_classify WHERE classify_id=? ` err = o.Raw(sql, classifyId).QueryRow(&count) return } // GetMaterialByClassifyIdAndName 根据分类id和素材名获取图表信息 func GetMaterialByClassifyIdAndName(classifyId int, name string) (item *Material, err error) { o := orm.NewOrmUsingDB("rddp") sql := ` SELECT * FROM material WHERE classify_id = ? and material_name=? ` err = o.Raw(sql, classifyId, name).QueryRow(&item) return } // GetMaterialByIds 根据素材id获取素材信息 func GetMaterialByIds(ids []int) (items []*MaterialListItems, err error) { o := orm.NewOrmUsingDB("rddp") sql := ` SELECT * FROM material WHERE material_id in (` + utils.GetOrmInReplace(len(ids)) + `) ` _, err = o.Raw(sql, ids).QueryRows(&items) return } // UpdateMaterialSortByClassifyId 根据素材id更新排序 func UpdateMaterialSortByClassifyId(classifyId, nowSort, prevMaterialId int, updateSort string) (err error) { o := orm.NewOrmUsingDB("rddp") sql := ` UPDATE material set sort = ` + updateSort + ` WHERE classify_id=? AND ` if prevMaterialId > 0 { sql += ` (sort > ? or (material_id > ` + fmt.Sprint(prevMaterialId) + ` and sort = ` + fmt.Sprint(nowSort) + `))` } _, err = o.Raw(sql, classifyId, nowSort).Exec() return } // GetFirstMaterialByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据 func GetFirstMaterialByClassifyId(classifyId int) (item *Material, err error) { o := orm.NewOrmUsingDB("rddp") sql := ` SELECT * FROM material WHERE classify_id=? order by sort asc,material_id asc limit 1` err = o.Raw(sql, classifyId).QueryRow(&item) return } type DetailParams struct { Code string `json:"code"` Id int `json:"id"` ClassifyId int `json:"classifyId"` } // AddAndEditMaterial 添加/编辑素材的请求数据 type AddAndEditMaterial struct { MaterialId int `description:"素材id"` MaterialName string `description:"素材名称"` ImgUrl string `description:"素材图片地址"` ClassifyId int `description:"分类id"` } // BatchAddMaterialReq 批量添加素材的请求数据 type BatchAddMaterialReq struct { MaterialList []BatchAddMaterialItem ClassifyId int `description:"分类id"` } type BatchAddMaterialItem struct { MaterialName string `description:"素材名称"` ImgUrl string `description:"素材图片地址"` } // DeleteMaterial 删除素材的请求数据 type DeleteMaterial struct { MaterialId int `description:"素材id"` } // BatchDeleteMaterialReq 删除素材的请求数据 type BatchDeleteMaterialReq struct { MaterialIds []int `description:"素材id"` ClassifyId int `description:"分类id"` IsShowMe bool `description:"操作人id,支持多选,用英文,隔开"` Keyword string `description:"关键字"` IsSelectAll bool `description:"是否选择所有素材"` } type BatchChangeClassifyMaterialReq struct { BatchDeleteMaterialReq NewClassifyId int `description:"新分类ID"` } type ChangeClassifyMaterialReq struct { MaterialId int `description:"素材id"` NewClassifyId int `description:"新分类ID"` } // RenameMaterialReq 添加/编辑素材的请求数据 type RenameMaterialReq struct { MaterialId int `description:"素材id"` MaterialName string `description:"素材名称"` } func GetMaterialInfoCountByClassifyIds(classifyIds []string) (count int, err error) { o := orm.NewOrmUsingDB("rddp") sql := ` SELECT COUNT(1) AS count FROM material WHERE classify_id in(` + utils.GetOrmInReplace(len(classifyIds)) + `)` err = o.Raw(sql, classifyIds).QueryRow(&count) return } func GetMaterialByNames(materialNames []string) (items []*Material, err error) { o := orm.NewOrmUsingDB("rddp") sql := `SELECT * FROM material WHERE material_name in (` + utils.GetOrmInReplace(len(materialNames)) + `) limit 1` _, err = o.Raw(sql, materialNames).QueryRows(&items) return } func GetMaterialByNameEns(materialNames []string) (items []*Material, err error) { o := orm.NewOrmUsingDB("rddp") sql := `SELECT * FROM material WHERE material_name_en in (` + utils.GetOrmInReplace(len(materialNames)) + `) limit 1` _, err = o.Raw(sql, materialNames).QueryRows(&items) return } func GetMaterialByName(materialName string) (item *Material, err error) { o := orm.NewOrmUsingDB("rddp") sql := `SELECT * FROM material WHERE material_name = ?` err = o.Raw(sql, materialName).QueryRow(&item) return } func GetMaterialByNameEn(materialName string) (item *Material, err error) { o := orm.NewOrmUsingDB("rddp") sql := `SELECT * FROM material WHERE material_name_en = ?` err = o.Raw(sql, materialName).QueryRow(&item) return } func GetMaterialCountByName(materialName string) (count int, err error) { o := orm.NewOrmUsingDB("rddp") sql := `SELECT COUNT(1) AS count FROM material WHERE material_name=? ` err = o.Raw(sql, materialName).QueryRow(&count) return } func GetMaterialCountByNameEn(materialName string) (count int, err error) { o := orm.NewOrmUsingDB("rddp") sql := `SELECT COUNT(1) AS count FROM material WHERE material_name_en=? ` err = o.Raw(sql, materialName).QueryRow(&count) return } // GetMaterialMaxSort 获取最大的排序数 func GetMaterialMaxSort() (sort int, err error) { o := orm.NewOrmUsingDB("rddp") sql := `SELECT MAX(sort) AS sort FROM material ` err = o.Raw(sql).QueryRow(&sort) return }