business.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/app/admin/main/workflow/model"
  6. "go-common/app/admin/main/workflow/model/manager"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "github.com/pkg/errors"
  10. )
  11. const _businessRoleURI = "http://manager.bilibili.co/x/admin/manager/internal/business/role"
  12. var metas map[int8]*model.Meta
  13. func init() {
  14. metas = make(map[int8]*model.Meta)
  15. data := `[
  16. {
  17. "business": 1,
  18. "name": "稿件投诉",
  19. "item_type": "group",
  20. "rounds": [
  21. {
  22. "id": 1,
  23. "name": "一审"
  24. },
  25. {
  26. "id": 2,
  27. "name": "回查"
  28. }
  29. ]
  30. },
  31. {
  32. "business": 2,
  33. "name": "稿件申诉",
  34. "item_type": "challenge",
  35. "rounds": [
  36. {
  37. "id": 1,
  38. "name": "一审"
  39. },
  40. {
  41. "id": 2,
  42. "name": "回查"
  43. },
  44. {
  45. "id": 3,
  46. "name": "三查"
  47. },
  48. {
  49. "id": 11,
  50. "name": "客服"
  51. }
  52. ]
  53. },
  54. {
  55. "business": 3,
  56. "name": "短点评投诉",
  57. "item_type": "group",
  58. "rounds": [
  59. {
  60. "id": 1,
  61. "name": "一审"
  62. },
  63. {
  64. "id": 2,
  65. "name": "回查"
  66. }
  67. ]
  68. },
  69. {
  70. "business": 4,
  71. "name": "长点评投诉",
  72. "item_type": "group",
  73. "rounds": [
  74. {
  75. "id": 1,
  76. "name": "一审"
  77. },
  78. {
  79. "id": 2,
  80. "name": "回查"
  81. }
  82. ]
  83. },
  84. {
  85. "business": 5,
  86. "name": "小黑屋",
  87. "item_type": "challenge",
  88. "rounds": [
  89. {
  90. "id": 1,
  91. "name": "评论"
  92. },
  93. {
  94. "id": 2,
  95. "name": "弹幕"
  96. },
  97. {
  98. "id": 3,
  99. "name": "私信"
  100. },
  101. {
  102. "id": 4,
  103. "name": "标签"
  104. },
  105. {
  106. "id": 5,
  107. "name": "个人资料"
  108. },
  109. {
  110. "id": 6,
  111. "name": "投稿"
  112. },
  113. {
  114. "id": 7,
  115. "name": "音频"
  116. },
  117. {
  118. "id": 8,
  119. "name": "专栏"
  120. },
  121. {
  122. "id": 9,
  123. "name": "空间头图"
  124. }
  125. ]
  126. },
  127. {
  128. "business": 6,
  129. "name": "稿件审核",
  130. "item_type": "challenge",
  131. "rounds": [
  132. {
  133. "id": 1,
  134. "name": "一审"
  135. }
  136. ]
  137. },
  138. {
  139. "business": 7,
  140. "name": "任务质检",
  141. "item_type": "challenge",
  142. "rounds": [
  143. {
  144. "id": 1,
  145. "name": "一审"
  146. }
  147. ]
  148. },
  149. {
  150. "business": 8,
  151. "name": "频道举报",
  152. "item_type": "group",
  153. "rounds": [
  154. {
  155. "id": 1,
  156. "name": "一审"
  157. }
  158. ]
  159. }
  160. ]`
  161. ml := make([]*model.Meta, 0)
  162. err := json.Unmarshal([]byte(data), &ml)
  163. if err != nil {
  164. panic(err)
  165. }
  166. for _, m := range ml {
  167. metas[m.Business] = m
  168. }
  169. }
  170. // BatchLastBusRecIDs will retrive the last business record ids by serveral conditions
  171. func (d *Dao) BatchLastBusRecIDs(c context.Context, oids []int64, business int8) (bids []int64, err error) {
  172. bids = make([]int64, 0, len(oids))
  173. if len(oids) <= 0 {
  174. return
  175. }
  176. rows, err := d.ReadORM.Table("workflow_business").Select("max(id)").
  177. Where("oid IN (?) AND business=?", oids, business).
  178. Group("oid,business").Rows()
  179. if err != nil {
  180. err = errors.Wrapf(err, "Query(%v, %d)", oids, business)
  181. return
  182. }
  183. defer rows.Close()
  184. for rows.Next() {
  185. var bid int64
  186. if err = rows.Scan(&bid); err != nil {
  187. err = errors.WithStack(err)
  188. return
  189. }
  190. bids = append(bids, bid)
  191. }
  192. return
  193. }
  194. // BusinessRecs will retrive the business record by ids
  195. func (d *Dao) BusinessRecs(c context.Context, bids []int64) (bs map[int32]*model.Business, err error) {
  196. bs = make(map[int32]*model.Business, len(bids))
  197. if len(bids) <= 0 {
  198. return
  199. }
  200. blist := make([]*model.Business, 0, len(bids))
  201. err = d.ReadORM.Table("workflow_business").Where("id IN (?)", bids).Find(&blist).Error
  202. if err != nil {
  203. err = errors.Wrapf(err, "Query(%v)", bids)
  204. return
  205. }
  206. for _, b := range blist {
  207. bs[b.Bid] = b
  208. }
  209. return
  210. }
  211. // LastBusRec will retrive last business record by business oid
  212. func (d *Dao) LastBusRec(c context.Context, business int8, oid int64) (bs *model.Business, err error) {
  213. bs = new(model.Business)
  214. err = d.ReadORM.Table("workflow_business").Where("oid=? AND business=?", oid, business).Last(bs).Error
  215. if err != nil || bs.Bid == 0 {
  216. err = errors.Wrapf(err, "Query(%d, %d)", business, oid)
  217. bs = nil
  218. return
  219. }
  220. return
  221. }
  222. // BatchBusRecByCids will retrive businesses by cids
  223. func (d *Dao) BatchBusRecByCids(c context.Context, cids []int64) (cidToBus map[int64]*model.Business, err error) {
  224. cidToBus = make(map[int64]*model.Business)
  225. if len(cids) <= 0 {
  226. return
  227. }
  228. blist := make([]*model.Business, 0, len(cids))
  229. err = d.ReadORM.Table("workflow_business").Where("cid IN (?)", cids).Find(&blist).Error
  230. if err != nil {
  231. err = errors.Wrapf(err, "Query(%v)", cids)
  232. return
  233. }
  234. for _, b := range blist {
  235. cidToBus[b.Cid] = b
  236. }
  237. return
  238. }
  239. // BusObjectByGids will retrive businesses by gids
  240. func (d *Dao) BusObjectByGids(c context.Context, gids []int64) (gidToBus map[int64]*model.Business, err error) {
  241. gidToBus = make(map[int64]*model.Business, len(gids))
  242. if len(gids) <= 0 {
  243. return
  244. }
  245. blist := make([]*model.Business, 0, len(gids))
  246. if err = d.ReadORM.Table("workflow_business").Where("gid IN (?)", gids).Find(&blist).Error; err != nil {
  247. err = errors.Wrapf(err, "Query(%v)", gids)
  248. return
  249. }
  250. for _, b := range blist {
  251. gidToBus[b.Gid] = b
  252. }
  253. return
  254. }
  255. // AllMetas will retrive business meta infomation from pre-configured
  256. func (d *Dao) AllMetas(c context.Context) map[int8]*model.Meta {
  257. return metas
  258. }
  259. // LoadRole .
  260. func (d *Dao) LoadRole(c context.Context) (role map[int8]map[int8]string, err error) {
  261. var (
  262. resp *manager.RoleResponse
  263. ok bool
  264. uri = _businessRoleURI
  265. )
  266. role = make(map[int8]map[int8]string)
  267. if err = d.httpRead.Get(c, uri, "", nil, &resp); err != nil {
  268. log.Error("failed call %s error(%v)", uri, err)
  269. return
  270. }
  271. if resp.Code != ecode.OK.Code() {
  272. err = ecode.Int(resp.Code)
  273. log.Error("call %s error response code(%d) message(%s)", uri, resp.Code, resp.Message)
  274. return
  275. }
  276. for _, r := range resp.Data {
  277. if _, ok = role[r.Bid]; !ok {
  278. role[r.Bid] = make(map[int8]string)
  279. }
  280. role[r.Bid][r.Rid] = r.Name
  281. }
  282. return
  283. }