resource.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package gorm
  2. import (
  3. "context"
  4. "database/sql"
  5. "encoding/json"
  6. "go-common/app/admin/main/aegis/model/resource"
  7. "go-common/library/log"
  8. "github.com/jinzhu/gorm"
  9. "go-common/app/admin/main/aegis/model"
  10. )
  11. const (
  12. _resourceResSQL = "SELECT r.id, r.business_id, r.oid, r.mid,r.content,r.extra1,r.extra2,r.extra3,r.extra4,r.extra1s,r.extra2s,r.metadata, rr.attribute, rr.note, rr.reject_reason, rr.reason_id, rr.state, rr.pubtime, rr.deltime " +
  13. "FROM resource r LEFT JOIN resource_result rr ON r.id = rr.rid WHERE r.id = ?"
  14. _resByOIDSQL = "SELECT r.id, r.business_id, r.oid, r.mid,r.content,r.extra1,r.extra2,r.extra3,r.extra4,r.extra1s,r.extra2s,r.metadata, rr.attribute, rr.note, rr.reject_reason, rr.reason_id, rr.state, rr.pubtime, rr.deltime " +
  15. "FROM resource r LEFT JOIN resource_result rr ON r.id = rr.rid WHERE r.business_id = ? AND r.oid = ?"
  16. )
  17. var _changeableFields = map[string]struct{}{
  18. "extra1": {},
  19. "extra2": {},
  20. "extra3": {},
  21. "extra4": {},
  22. "extra5": {},
  23. "extra6": {},
  24. "extra1s": {},
  25. "extra2s": {},
  26. "extra3s": {},
  27. "extra4s": {},
  28. "extratime1": {},
  29. }
  30. // ListHelperForTask 补充任务列表里面的oid和content
  31. func (d *Dao) ListHelperForTask(c context.Context, rids []int64) (res map[int64][]interface{}, err error) {
  32. var (
  33. rows *sql.Rows
  34. id int64
  35. oid, content, metadata string
  36. )
  37. if rows, err = d.orm.Table("resource").Select("id,oid,content,metadata").Where("id IN (?)", rids).Rows(); err != nil {
  38. log.Error("listHelperForTask err(%v)", err)
  39. return
  40. }
  41. res = make(map[int64][]interface{})
  42. defer rows.Close()
  43. for rows.Next() {
  44. if err = rows.Scan(&id, &oid, &content, &metadata); err != nil {
  45. log.Error("rows.Scan error(%v)", err)
  46. return
  47. }
  48. metas := make(map[string]interface{})
  49. if len(metadata) > 0 {
  50. if err = json.Unmarshal([]byte(metadata), &metas); err != nil {
  51. log.Error("ListHelperForTask err(%+v)\n", err)
  52. }
  53. err = nil
  54. }
  55. res[id] = []interface{}{oid, content, metas}
  56. }
  57. return
  58. }
  59. // TxAddResource .
  60. func (d *Dao) TxAddResource(tx *gorm.DB, r *resource.Resource, rr *resource.Result) (rid int64, err error) {
  61. err = tx.Table("resource").Where("business_id=? AND oid=?", r.BusinessID, r.OID).
  62. Assign(map[string]interface{}{
  63. "oid": r.OID,
  64. "content": r.Content,
  65. "extra1": r.Extra1,
  66. "extra2": r.Extra2,
  67. "extra3": r.Extra3,
  68. "extra4": r.Extra4,
  69. "extra1s": r.Extra1s,
  70. "extra2s": r.Extra2s,
  71. "metadata": r.MetaData,
  72. "extra5": r.Extra5,
  73. "extra6": r.Extra6,
  74. "extra3s": r.Extra3s,
  75. "extra4s": r.Extra4s,
  76. "extratime1": r.ExtraTime1,
  77. "octime": r.OCtime,
  78. "ptime": r.Ptime,
  79. }).FirstOrCreate(&r).Error
  80. if err != nil {
  81. return
  82. }
  83. rid = r.ID
  84. rr.RID = r.ID
  85. err = tx.Table("resource_result").Where("rid=?", rr.RID).Assign(map[string]interface{}{
  86. "attribute": rr.Attribute,
  87. "note": rr.Note,
  88. "reject_reason": rr.RejectReason,
  89. "reason_id": rr.ReasonID,
  90. "state": rr.State,
  91. "pubtime": rr.PubTime,
  92. "deltime": rr.DelTime,
  93. }).FirstOrCreate(&rr).Error
  94. return
  95. }
  96. // ResourceByOID .
  97. func (d *Dao) ResourceByOID(c context.Context, OID string, bizID int64) (res *resource.Resource, err error) {
  98. res = &resource.Resource{}
  99. if err = d.orm.Where("oid = ? AND business_id = ?", OID, bizID).First(res).Error; err == gorm.ErrRecordNotFound {
  100. res = nil
  101. err = nil
  102. }
  103. return
  104. }
  105. // RidsByOids .
  106. func (d *Dao) RidsByOids(c context.Context, bizID int64, oids []string) (rids string, err error) {
  107. err = d.orm.Table("resource").Select("GROUP_CONCAT(id)").Where("business_id=? AND oid IN (?)", bizID, oids).Row().Scan(&rids)
  108. return
  109. }
  110. // OidByRID 通过rid查到oid
  111. func (d *Dao) OidByRID(c context.Context, rid int64) (oid string, err error) {
  112. if err = d.orm.Table("resource").Select("oid").Where("id=?", rid).Row().Scan(&oid); err == sql.ErrNoRows {
  113. err = nil
  114. }
  115. return
  116. }
  117. // TxUpdateResult .
  118. func (d *Dao) TxUpdateResult(ormTx *gorm.DB, rid int64, res map[string]interface{}, rscRes *resource.Result) (err error) {
  119. params := make(map[string]interface{})
  120. for k, v := range res {
  121. if k != "state" && k != "attribute" {
  122. continue
  123. }
  124. params[k] = v
  125. }
  126. db := ormTx.Table("resource_result").
  127. Where("rid = ?", rid)
  128. //Update(params)
  129. if rscRes != nil {
  130. if rscRes.Attribute != -1 {
  131. params["attribute"] = rscRes.Attribute
  132. }
  133. if rscRes.Note != "" {
  134. params["note"] = rscRes.Note
  135. }
  136. if rscRes.RejectReason != "" {
  137. params["reject_reason"] = rscRes.RejectReason
  138. }
  139. if rscRes.ReasonID != 0 {
  140. params["reason_id"] = rscRes.ReasonID
  141. }
  142. }
  143. return db.Update(params).Error
  144. }
  145. // TxUpdateResource . TODO Resource表的字段要不要直接审核提交变更 还是等待业务方同步更新?(例如单话的上线下线)
  146. func (d *Dao) TxUpdateResource(ormTx *gorm.DB, rid int64, res map[string]interface{}) (err error) {
  147. params := make(map[string]interface{})
  148. for k, v := range res {
  149. if _, ok := _changeableFields[k]; !ok {
  150. continue
  151. }
  152. params[k] = v
  153. }
  154. return ormTx.Table("resource").Where("id = ?", rid).Update(params).Error
  155. }
  156. // TxUpdateState 更新状态
  157. func (d *Dao) TxUpdateState(tx *gorm.DB, rids []int64, state int) (err error) {
  158. return tx.Table("resource_result").Where("rid IN (?)", rids).Update("state", state).Error
  159. }
  160. // UpdateResource 更新资源
  161. func (d *Dao) UpdateResource(c context.Context, bizid int64, oid string, update map[string]interface{}) (rows int64, err error) {
  162. db := d.orm.Table("resource").Where("business_id=? AND oid=?", bizid, oid).Update(update)
  163. return db.RowsAffected, db.Error
  164. }
  165. // ResourceRes .
  166. func (d *Dao) ResourceRes(c context.Context, rid int64) (res *resource.Res, err error) {
  167. res = &resource.Res{}
  168. if err = d.orm.Raw(_resourceResSQL, rid).Scan(res).Error; err == gorm.ErrRecordNotFound {
  169. res = nil
  170. err = nil
  171. }
  172. return
  173. }
  174. // ResByOID .
  175. func (d *Dao) ResByOID(c context.Context, bizID int64, OID string) (res *resource.Res, err error) {
  176. res = &resource.Res{}
  177. if err = d.orm.Raw(_resByOIDSQL, bizID, OID).Scan(res).Error; err == gorm.ErrRecordNotFound {
  178. res = nil
  179. err = nil
  180. }
  181. return
  182. }
  183. //ResOIDByID 根据id获取资源oid
  184. func (d *Dao) ResOIDByID(c context.Context, rids []int64) (res map[int64]string, err error) {
  185. list := []struct {
  186. ID int64 `gorm:"column:id"`
  187. OID string `gorm:"column:oid"`
  188. }{}
  189. res = map[int64]string{}
  190. if err = d.orm.Table("resource").Select("id,oid").Where("id in (?)", rids).Find(&list).Error; err != nil {
  191. log.Error("ResOIDByID error(%v) rids(%v)", err, rids)
  192. return
  193. }
  194. for _, item := range list {
  195. res[item.ID] = item.OID
  196. }
  197. return
  198. }
  199. //ResIDByOID 根据oid获取rid
  200. func (d *Dao) ResIDByOID(C context.Context, bizID int64, oids []string) (res map[string]int64, err error) {
  201. list := []struct {
  202. ID int64 `gorm:"column:id"`
  203. OID string `gorm:"column:oid"`
  204. }{}
  205. res = map[string]int64{}
  206. if err = d.orm.Table("resource").Select("id,oid").Where("business_id=? and oid in (?)", bizID, oids).Find(&list).Error; err != nil {
  207. log.Error("ResIDByOID error(%v) oids(%v)", err, oids)
  208. return
  209. }
  210. for _, item := range list {
  211. res[item.OID] = item.ID
  212. }
  213. return
  214. }
  215. //ResourceHit 根据状态筛选资源
  216. func (d *Dao) ResourceHit(c context.Context, rids []int64) (hitids map[int64]int64, err error) {
  217. hitids = make(map[int64]int64)
  218. rows, err := d.orm.Table("resource_result").Select("rid,state").Where("rid IN (?)", rids).Rows()
  219. if err != nil {
  220. log.Error("ResourceHitState error(%v)", err)
  221. return
  222. }
  223. defer rows.Close()
  224. for rows.Next() {
  225. var rid, state int64
  226. if err = rows.Scan(&rid, &state); err != nil {
  227. log.Error("ResourceHitState error(%v)", err)
  228. }
  229. hitids[rid] = state
  230. }
  231. return
  232. }
  233. //ResultByRID result by rid
  234. func (d *Dao) ResultByRID(c context.Context, rid int64) (res *resource.Result, err error) {
  235. res = &resource.Result{}
  236. if err = d.orm.Where("rid=?", rid).First(&res).Error; err == gorm.ErrRecordNotFound {
  237. log.Error("ResultByRID rid(%d) error(%v)", rid, err)
  238. }
  239. return
  240. }
  241. //MetaByRID .
  242. func (d *Dao) MetaByRID(c context.Context, rids []int64) (metas map[int64]string, err error) {
  243. var rows *sql.Rows
  244. metas = make(map[int64]string)
  245. rows, err = d.orm.Table("resource").Select("id,metadata").Where("id IN (?)", rids).Rows()
  246. if err != nil {
  247. log.Error("MetaByRID Query error(%v)", err)
  248. return
  249. }
  250. defer rows.Close()
  251. for rows.Next() {
  252. var (
  253. id int64
  254. meta string
  255. )
  256. if err = rows.Scan(&id, &meta); err != nil {
  257. log.Error("MetaByRID rows.Scan error(%v)", err)
  258. return
  259. }
  260. metas[id] = meta
  261. }
  262. return
  263. }
  264. //UpsertByOIDs 获取需要更新到搜索部分
  265. func (d *Dao) UpsertByOIDs(c context.Context, businessID int64, oids []string) (res []*model.UpsertItem, err error) {
  266. res = []*model.UpsertItem{}
  267. sqlstr := `SELECT r.id,r.extra1,r.extra2,r.extra3,r.extra4,rr.state
  268. FROM resource r
  269. LEFT JOIN resource_result rr ON r.id=rr.rid
  270. WHERE r.business_id=? AND r.oid IN (?)`
  271. if err = d.orm.Raw(sqlstr, businessID, oids).Find(&res).Error; err != nil {
  272. log.Error("UpsertByOIDs error(%+v) businessid(%d) oids(%+v)", err, businessID, oids)
  273. }
  274. return
  275. }