business.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package service
  2. import (
  3. "context"
  4. "sort"
  5. "strings"
  6. "time"
  7. "go-common/app/admin/main/workflow/model"
  8. "go-common/app/admin/main/workflow/model/param"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. )
  12. const (
  13. _lenShortCut = 8
  14. )
  15. // 0: 有效 1: 无效 2: 流转 3: 众裁 4: 批量有效 5: 批量无效 6: 批量流转 7: 批量移交众裁
  16. var btnName = map[uint]string{
  17. 0: "有效",
  18. 1: "无效",
  19. 2: "流转",
  20. 3: "众裁",
  21. 4: "批量有效",
  22. 5: "批量无效",
  23. 6: "批量流转",
  24. 7: "批量移交众裁",
  25. }
  26. var btnInitShortCut = map[uint]string{
  27. 0: "A",
  28. 1: "S",
  29. 2: "D",
  30. 3: "F",
  31. 4: "J",
  32. 5: "K",
  33. 6: "H",
  34. 7: "L",
  35. }
  36. // ListMeta will list business meta inforamtion from DAO
  37. func (s *Service) ListMeta(c context.Context, itemType string) (metaList []*model.Meta, err error) {
  38. allMetas := s.dao.AllMetas(c)
  39. if err != nil {
  40. log.Error("Failed to fetch all metas from database: %v", err)
  41. return
  42. }
  43. metaList = make([]*model.Meta, 0, len(allMetas))
  44. for _, m := range allMetas {
  45. if itemType != "" && m.ItemType != itemType {
  46. continue
  47. }
  48. //sort rounds field asc by id
  49. sort.Sort(model.RoundSlice(m.Rounds))
  50. metaList = append(metaList, m)
  51. }
  52. //sort meta asc by business
  53. sort.Sort(model.MetaSlice(metaList))
  54. return
  55. }
  56. // ListBusAttr list business attr info
  57. func (s *Service) ListBusAttr(ctx context.Context) (busAttr []*model.BusinessAttr, err error) {
  58. busAttr = make([]*model.BusinessAttr, 0)
  59. if err = s.dao.ORM.Table("workflow_business_attr").Find(&busAttr).Error; err != nil {
  60. return
  61. }
  62. return
  63. }
  64. // ListBusAttrV3 .
  65. func (s *Service) ListBusAttrV3(ctx context.Context) (busAttr []*model.BusinessAttr, err error) {
  66. log.Info("start ListBusAttrV3")
  67. t := time.Now()
  68. busAttr = make([]*model.BusinessAttr, 0)
  69. if err = s.dao.ORM.Table("workflow_business_attr").Find(&busAttr).Error; err != nil {
  70. log.Info("query table workflow_business_attr error(%v)", err)
  71. return
  72. }
  73. for _, attr := range busAttr {
  74. btnShortCut := strings.Split(attr.ButtonKey, ",")
  75. if len(btnShortCut) != _lenShortCut {
  76. log.Warn("button short cut length not 8, load initial value")
  77. btnShortCut = []string{"A", "S", "D", "F", "J", "K", "H", "L"}
  78. }
  79. for i := uint(0); i < _lenShortCut; i++ {
  80. state := false
  81. mask := uint8(1 << i)
  82. if attr.Button&mask > 0 {
  83. state = true
  84. }
  85. if btnShortCut[i] == "" {
  86. btnShortCut[i] = btnInitShortCut[i]
  87. }
  88. attr.Buttons = append(attr.Buttons, &model.Button{
  89. Index: int(i),
  90. Name: btnName[i],
  91. State: state,
  92. Key: btnShortCut[i],
  93. })
  94. }
  95. }
  96. log.Info("end ListBusAttrV3 time(%v)", time.Since(t).String())
  97. return
  98. }
  99. // AddOrUpdateBusAttr add or update business attr info
  100. func (s *Service) AddOrUpdateBusAttr(ctx context.Context, abap *param.AddBusAttrParam) (err error) {
  101. busAttr := &model.BusinessAttr{
  102. ID: abap.ID,
  103. }
  104. attr := map[string]interface{}{
  105. "bid": abap.Bid,
  106. "name": abap.Name,
  107. "deal_type": abap.DealType,
  108. "expire_time": abap.ExpireTime,
  109. "assign_type": abap.AssignType,
  110. "assign_max": abap.AssignMax,
  111. "group_type": abap.GroupType,
  112. "business_name": abap.BusinessName,
  113. }
  114. if err = s.dao.ORM.Table("workflow_business_attr").
  115. Where("id=?", abap.ID).
  116. Assign(attr).FirstOrCreate(busAttr).Error; err != nil {
  117. log.Error("Failed to create business_attr(%+v): %v", busAttr, err)
  118. return
  119. }
  120. s.loadBusAttrs()
  121. return
  122. }
  123. // SetSwitch .
  124. func (s *Service) SetSwitch(ctx context.Context, bs *param.BusAttrButtonSwitch) (err error) {
  125. attr := new(model.BusinessAttr)
  126. if err = s.dao.ORM.Table("workflow_business_attr").Where("bid = ?", bs.Bid).Find(attr).Error; err != nil {
  127. log.Error("Failed to find business_attr where bid = %d : %v", bs.Bid, err)
  128. return
  129. }
  130. oldBut := attr.Button
  131. mask := uint8(^(1 << bs.Index))
  132. oldBut = oldBut & mask
  133. newBut := oldBut + (bs.Switch << bs.Index)
  134. if err = s.dao.ORM.Table("workflow_business_attr").Where("bid = ?", bs.Bid).Update("button", newBut).Error; err != nil {
  135. log.Error("Failed to update business_attr button field where bid = %d, button = %d : %v", bs.Bid, newBut, err)
  136. }
  137. return
  138. }
  139. // SetShortCut .
  140. func (s *Service) SetShortCut(ctx context.Context, sc *param.BusAttrButtonShortCut) (err error) {
  141. attr := new(model.BusinessAttr)
  142. if err = s.dao.ORM.Table("workflow_business_attr").Where("bid = ?", sc.Bid).Find(attr).Error; err != nil {
  143. log.Error("Failed to find business_attr where bid = %d : %v", sc.Bid, err)
  144. return
  145. }
  146. oldShortCut := strings.Split(attr.ButtonKey, ",")
  147. if len(oldShortCut) != 8 {
  148. oldShortCut = make([]string, 8)
  149. }
  150. oldShortCut[sc.Index] = sc.ShortCut
  151. newShortCut := strings.Join(oldShortCut, ",")
  152. if err = s.dao.ORM.Table("workflow_business_attr").Where("bid = ?", sc.Bid).Update("button_key", newShortCut).Error; err != nil {
  153. log.Error("Failed to update business_attr button_key field where bid = %d, button_key = %d : %v", sc.Bid, newShortCut, err)
  154. }
  155. return
  156. }
  157. // ManagerTag .
  158. func (s *Service) ManagerTag(ctx context.Context) (map[int8]map[int64]*model.TagMeta, error) {
  159. return s.tagListCache, nil
  160. }
  161. // UserBlockInfo .
  162. // http://info.bilibili.co/pages/viewpage.action?pageId=5417571
  163. // http://info.bilibili.co/pages/viewpage.action?pageId=7559616
  164. func (s *Service) UserBlockInfo(ctx context.Context, bi *param.BlockInfo) (resp model.BlockInfoResp, err error) {
  165. var sum int64
  166. if sum, err = s.dao.BlockNum(ctx, bi.Mid); err != nil {
  167. log.Error("s.dao.BlockNum(%d) error(%v)", bi.Mid, err)
  168. return
  169. }
  170. if resp, err = s.dao.BlockInfo(ctx, bi.Mid); err != nil {
  171. log.Error("s.dao.BlockInfo(%d) error(%v)", bi.Mid, err)
  172. return
  173. }
  174. resp.Data.BlockedSum = sum
  175. return
  176. }
  177. // SourceList .
  178. func (s *Service) SourceList(ctx context.Context, src *param.Source) (data map[string]interface{}, err error) {
  179. // check if has external uri
  180. if _, ok := s.callbackCache[src.Bid]; !ok {
  181. err = ecode.WkfBusinessCallbackConfigNotFound
  182. return
  183. }
  184. uri := ""
  185. if uri = s.callbackCache[src.Bid].SourceAPI; uri == "" {
  186. return
  187. }
  188. return s.dao.SourceInfo(ctx, uri)
  189. }