privilege.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/admin/main/vip/model"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. "github.com/jinzhu/gorm"
  8. "github.com/pkg/errors"
  9. "golang.org/x/sync/errgroup"
  10. )
  11. // Privileges Privilege list.
  12. func (s *Service) Privileges(c context.Context, langType int8) (res []*model.PrivilegeResp, err error) {
  13. var (
  14. ps []*model.Privilege
  15. prs []*model.PrivilegeResources
  16. )
  17. if ps, err = s.dao.PrivilegeList(c, langType); err != nil {
  18. err = errors.WithStack(err)
  19. return
  20. }
  21. if prs, err = s.dao.PrivilegeResourcesList(c); err != nil {
  22. err = errors.WithStack(err)
  23. return
  24. }
  25. prmap := make(map[int64]map[int8]*model.PrivilegeResources, len(prs))
  26. for _, v := range prs {
  27. if prmap[v.PID] == nil {
  28. prmap[v.PID] = map[int8]*model.PrivilegeResources{}
  29. }
  30. prmap[v.PID][v.Type] = v
  31. }
  32. for i, v := range ps {
  33. r := &model.PrivilegeResp{
  34. ID: v.ID,
  35. Name: v.Name,
  36. Title: v.Title,
  37. Explain: v.Explain,
  38. Type: v.Type,
  39. Operator: v.Operator,
  40. State: v.State,
  41. IconURL: v.IconURL,
  42. IconGrayURL: v.IconGrayURL,
  43. LangType: v.LangType,
  44. Order: int64(i),
  45. }
  46. if prmap[v.ID] != nil && prmap[v.ID][model.WebResources] != nil {
  47. r.WebLink = prmap[v.ID][model.WebResources].Link
  48. r.WebImageURL = prmap[v.ID][model.WebResources].ImageURL
  49. }
  50. if prmap[v.ID] != nil && prmap[v.ID][model.AppResources] != nil {
  51. r.AppLink = prmap[v.ID][model.AppResources].Link
  52. r.AppImageURL = prmap[v.ID][model.AppResources].ImageURL
  53. }
  54. res = append(res, r)
  55. }
  56. return
  57. }
  58. // UpdatePrivilegeState update privilege state.
  59. func (s *Service) UpdatePrivilegeState(c context.Context, p *model.Privilege) (err error) {
  60. if _, err = s.dao.UpdateStatePrivilege(c, p); err != nil {
  61. err = errors.WithStack(err)
  62. }
  63. return
  64. }
  65. // DeletePrivilege delete privilege .
  66. func (s *Service) DeletePrivilege(c context.Context, id int64) (err error) {
  67. if _, err = s.dao.DeletePrivilege(c, id); err != nil {
  68. err = errors.WithStack(err)
  69. }
  70. return
  71. }
  72. // AddPrivilege add privilege.
  73. func (s *Service) AddPrivilege(c context.Context, req *model.ArgAddPrivilege, img *model.ArgImage) (err error) {
  74. var (
  75. iconURL string
  76. iconGrayURL string
  77. order int64
  78. webImageURL string
  79. appImageURL string
  80. id int64
  81. tx *gorm.DB
  82. )
  83. if iconURL, iconGrayURL, webImageURL, appImageURL, err = s.uploadImage(c, img); err != nil {
  84. err = errors.WithStack(err)
  85. return
  86. }
  87. if iconURL == "" || iconGrayURL == "" {
  88. err = ecode.VipFileUploadFaildErr
  89. return
  90. }
  91. if order, err = s.dao.MaxOrder(c); err != nil {
  92. err = errors.WithStack(err)
  93. return
  94. }
  95. order++
  96. tx = s.dao.BeginGormTran(c)
  97. defer func() {
  98. if err != nil {
  99. if err1 := tx.Rollback(); err1 != nil {
  100. err = errors.Wrapf(err, "tx.Rollback(%+v)", req)
  101. }
  102. return
  103. }
  104. if err = tx.Commit().Error; err != nil {
  105. err = errors.Wrapf(err, "tx.Commit(%+v)", req)
  106. }
  107. }()
  108. if id, err = s.dao.AddPrivilege(tx, &model.Privilege{
  109. Name: req.Name,
  110. Title: req.Title,
  111. Explain: req.Explain,
  112. Type: req.Type,
  113. Operator: req.Operator,
  114. State: model.NormalPrivilege,
  115. IconURL: iconURL,
  116. IconGrayURL: iconGrayURL,
  117. Order: order,
  118. LangType: req.LangType,
  119. }); err != nil {
  120. err = errors.WithStack(err)
  121. return
  122. }
  123. if _, err = s.dao.AddPrivilegeResources(tx, &model.PrivilegeResources{
  124. PID: id,
  125. Link: req.AppLink,
  126. ImageURL: appImageURL,
  127. Type: model.AppResources,
  128. }); err != nil {
  129. err = errors.WithStack(err)
  130. return
  131. }
  132. if _, err = s.dao.AddPrivilegeResources(tx, &model.PrivilegeResources{
  133. PID: id,
  134. Link: req.WebLink,
  135. ImageURL: webImageURL,
  136. Type: model.WebResources,
  137. }); err != nil {
  138. err = errors.WithStack(err)
  139. return
  140. }
  141. return
  142. }
  143. // UpdatePrivilege update privilege.
  144. func (s *Service) UpdatePrivilege(c context.Context, req *model.ArgUpdatePrivilege, img *model.ArgImage) (err error) {
  145. var (
  146. iconURL string
  147. iconGrayURL string
  148. webImageURL string
  149. appImageURL string
  150. tx *gorm.DB
  151. )
  152. if iconURL, iconGrayURL, webImageURL, appImageURL, err = s.uploadImage(c, img); err != nil {
  153. err = errors.WithStack(err)
  154. return
  155. }
  156. tx = s.dao.BeginGormTran(c)
  157. defer func() {
  158. if err != nil {
  159. if err1 := tx.Rollback().Error; err1 != nil {
  160. err = errors.Wrapf(err, "tx.Rollback(%+v)", req)
  161. }
  162. return
  163. }
  164. if err = tx.Commit().Error; err != nil {
  165. err = errors.Wrapf(err, "tx.Commit(%+v)", req)
  166. }
  167. }()
  168. if _, err = s.dao.UpdatePrivilege(tx, &model.Privilege{
  169. ID: req.ID,
  170. Name: req.Name,
  171. Title: req.Title,
  172. Explain: req.Explain,
  173. Type: req.Type,
  174. Operator: req.Operator,
  175. State: model.NormalPrivilege,
  176. IconURL: iconURL,
  177. IconGrayURL: iconGrayURL,
  178. }); err != nil {
  179. err = errors.WithStack(err)
  180. return
  181. }
  182. if _, err = s.dao.UpdatePrivilegeResources(tx, &model.PrivilegeResources{
  183. PID: req.ID,
  184. Link: req.AppLink,
  185. ImageURL: appImageURL,
  186. Type: model.AppResources,
  187. }); err != nil {
  188. err = errors.WithStack(err)
  189. return
  190. }
  191. if _, err = s.dao.UpdatePrivilegeResources(tx, &model.PrivilegeResources{
  192. PID: req.ID,
  193. Link: req.WebLink,
  194. ImageURL: webImageURL,
  195. Type: model.WebResources,
  196. }); err != nil {
  197. err = errors.WithStack(err)
  198. return
  199. }
  200. return
  201. }
  202. func (s *Service) uploadImage(c context.Context, req *model.ArgImage) (iconURL, iconGrayURL, webImageURL, appImageURL string, err error) {
  203. var g errgroup.Group
  204. if req.IconFileType == "" &&
  205. req.IconGrayFileType == "" &&
  206. req.WebImageFileType == "" &&
  207. req.AppImageFileType == "" {
  208. return
  209. }
  210. if req.IconFileType != "" {
  211. g.Go(func() (err error) {
  212. //update icon.
  213. if iconURL, err = s.dao.Upload(c, "", req.IconFileType, req.IconBody, s.c.Bfs); err != nil {
  214. log.Error("d.Upload iconURL(%+v) error(%v)", req, err)
  215. }
  216. return
  217. })
  218. }
  219. if req.IconGrayFileType != "" {
  220. g.Go(func() (err error) {
  221. //update gray icon.
  222. if iconGrayURL, err = s.dao.Upload(c, "", req.IconGrayFileType, req.IconGrayBody, s.c.Bfs); err != nil {
  223. log.Error("d.Upload iconGrayURL(%+v) error(%v)", req, err)
  224. }
  225. return
  226. })
  227. }
  228. if req.WebImageFileType != "" {
  229. g.Go(func() (err error) {
  230. if webImageURL, err = s.dao.Upload(c, "", req.WebImageFileType, req.WebImageBody, s.c.Bfs); err != nil {
  231. log.Error("d.Upload webImageURL(%+v) error(%v)", req, err)
  232. }
  233. return
  234. })
  235. }
  236. if req.AppImageFileType != "" {
  237. g.Go(func() (err error) {
  238. if appImageURL, err = s.dao.Upload(c, "", req.AppImageFileType, req.AppImageBody, s.c.Bfs); err != nil {
  239. log.Error("d.Upload appImageURL(%+v) error(%v)", req, err)
  240. }
  241. return
  242. })
  243. }
  244. if err = g.Wait(); err != nil {
  245. log.Error("g.Wait error(%v)", err)
  246. return
  247. }
  248. return
  249. }
  250. // UpdateOrder update order.
  251. func (s *Service) UpdateOrder(c context.Context, a *model.ArgOrder) (err error) {
  252. if _, err = s.dao.UpdateOrder(c, a.AID, a.BID); err != nil {
  253. err = errors.WithStack(err)
  254. }
  255. return
  256. }