medal.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package service
  2. import (
  3. "context"
  4. "encoding/csv"
  5. "fmt"
  6. "io"
  7. "mime/multipart"
  8. "net/url"
  9. "strconv"
  10. "strings"
  11. "github.com/pkg/errors"
  12. "go-common/app/admin/main/usersuit/model"
  13. accmdl "go-common/app/service/main/account/model"
  14. "go-common/library/ecode"
  15. "go-common/library/log"
  16. "go-common/library/xstr"
  17. )
  18. // Medal medal .
  19. func (s *Service) Medal(c context.Context) (res []*model.MedalInfo, err error) {
  20. nps, err := s.d.Medal(c)
  21. if err != nil {
  22. log.Error("s.d.Medal error(%v)", err)
  23. }
  24. mgs, err := s.d.MedalGroup(c)
  25. if err != nil {
  26. log.Error("s.d.MedalGroup error(%v)", err)
  27. }
  28. res = make([]*model.MedalInfo, 0)
  29. for _, np := range nps {
  30. re := &model.MedalInfo{}
  31. re.Medal = np
  32. if _, ok := mgs[np.GID]; ok {
  33. re.GroupName = mgs[np.GID].Name
  34. if mgs[np.GID].PID != 0 {
  35. re.ParentGroupName = mgs[mgs[np.GID].PID].Name
  36. }
  37. }
  38. res = append(res, re)
  39. }
  40. return
  41. }
  42. // MedalView .
  43. func (s *Service) MedalView(c context.Context, id int64) (res *model.MedalInfo, err error) {
  44. res = &model.MedalInfo{}
  45. res.Medal, err = s.d.MedalByID(c, id)
  46. if err != nil {
  47. log.Error("s.d.MedalByID(%d) error(%v)", id, err)
  48. return
  49. }
  50. mgs, err := s.d.MedalGroup(c)
  51. if err != nil {
  52. log.Error("s.d.MedalGroup error(%v)", err)
  53. return
  54. }
  55. if _, ok := mgs[res.Medal.GID]; ok {
  56. res.GroupName = mgs[res.Medal.GID].Name
  57. if _, ok1 := mgs[mgs[res.Medal.GID].PID]; ok && ok1 {
  58. res.ParentGroupName = mgs[mgs[res.Medal.GID].PID].Name
  59. }
  60. }
  61. return
  62. }
  63. func getImagePath(raw string) string {
  64. uri, err := url.Parse(raw)
  65. if err != nil {
  66. return raw
  67. }
  68. return uri.Path
  69. }
  70. // AddMedal add medal .
  71. func (s *Service) AddMedal(c context.Context, np *model.Medal) (err error) {
  72. np.Image = getImagePath(np.Image)
  73. np.ImageSmall = getImagePath(np.ImageSmall)
  74. if _, err := s.d.AddMedal(c, np); err != nil {
  75. log.Error("s.d.AddMedal error(%v)", err)
  76. }
  77. return
  78. }
  79. // UpMedal update medal .
  80. func (s *Service) UpMedal(c context.Context, id int64, np *model.Medal) (err error) {
  81. np.Image = getImagePath(np.Image)
  82. np.ImageSmall = getImagePath(np.ImageSmall)
  83. if _, err := s.d.UpMedal(c, id, np); err != nil {
  84. log.Error("s.d.UpMedal error(%v)", err)
  85. }
  86. return
  87. }
  88. // MedalGroup medal group .
  89. func (s *Service) MedalGroup(c context.Context) (res map[int64]*model.MedalGroup, err error) {
  90. res, err = s.d.MedalGroup(c)
  91. if err != nil {
  92. log.Error("s.MedalGroup error(%v)", err)
  93. }
  94. return
  95. }
  96. // MedalGroupInfo medal group all info include parent group name .
  97. func (s *Service) MedalGroupInfo(c context.Context) (res []*model.MedalGroup, err error) {
  98. res, err = s.d.MedalGroupInfo(c)
  99. if err != nil {
  100. log.Error("s.MedalGroupInfo error(%v)", err)
  101. }
  102. return
  103. }
  104. // MedalGroupParent medal group all info include parent group name .
  105. func (s *Service) MedalGroupParent(c context.Context) (res []*model.MedalGroup, err error) {
  106. res, err = s.d.MedalGroupParent(c)
  107. if err != nil {
  108. log.Error("s.MedalGroupParent error(%v)", err)
  109. }
  110. return
  111. }
  112. // MedalGroupByGid nameplate by gid .
  113. func (s *Service) MedalGroupByGid(c context.Context, id int64) (ng *model.MedalGroup, err error) {
  114. if ng, err = s.d.MedalGroupByID(c, id); err != nil {
  115. log.Error("s.MedalGroupByID error(%v)", err)
  116. }
  117. return
  118. }
  119. // MedalGroupAdd add medal group.
  120. func (s *Service) MedalGroupAdd(c context.Context, ng *model.MedalGroup) (err error) {
  121. if _, err := s.d.MedalGroupAdd(c, ng); err != nil {
  122. log.Error("s.MedalGroupAdd error(%v)", err)
  123. }
  124. return
  125. }
  126. // MedalGroupUp update medal group.
  127. func (s *Service) MedalGroupUp(c context.Context, id int64, ng *model.MedalGroup) (err error) {
  128. if _, err := s.d.MedalGroupUp(c, id, ng); err != nil {
  129. log.Error("s.MedalGroupUp error(%v)", err)
  130. }
  131. return
  132. }
  133. // MedalOwner medal onwer .
  134. func (s *Service) MedalOwner(c context.Context, mid int64) (no []*model.MedalMemberMID, err error) {
  135. if no, err = s.d.MedalOwner(c, mid); err != nil {
  136. log.Error("s.d.MedalOwner error(%+v)", err)
  137. }
  138. return
  139. }
  140. // MedalOwnerAdd medal owner add .
  141. func (s *Service) MedalOwnerAdd(c context.Context, mid, nid int64, title, msg string, oid int64) (err error) {
  142. count, err := s.d.CountOwnerBYNidMid(c, mid, nid)
  143. if count > 0 || err != nil {
  144. err = ecode.MedalHasGet
  145. return
  146. }
  147. if _, err = s.d.MedalOwnerAdd(c, mid, nid); err != nil {
  148. log.Error("s.MedalOwnerAdd(mid:%d nid:%d) error(%v)", mid, nid, err)
  149. return
  150. }
  151. if err = s.d.DelMedalOwnersCache(c, mid); err != nil {
  152. log.Error("s.DelMedalOwnersCache(mid:%d) error(%v)", mid, err)
  153. err = nil
  154. }
  155. var (
  156. mids []int64
  157. ismsg bool
  158. action string
  159. )
  160. mids = append(mids, mid)
  161. log.Error("MedalOwnerAdd title(%+v) msg(%+v)", title, msg)
  162. if title != "" && msg != "" {
  163. ismsg = true
  164. if err = s.d.SendSysMsg(c, mids, title, msg, ""); err != nil {
  165. log.Error("MedalOwnerAdd(mid:%d nid:%d title:%s msg:%s) SendSysMsg error(%+v)", mid, nid, title, msg, err)
  166. err = nil
  167. }
  168. }
  169. if oid > 0 {
  170. mi, err := s.d.MedalByID(c, nid)
  171. if err != nil {
  172. log.Error("MedalByID(id:%d) error(%+v)", nid, err)
  173. err = nil
  174. }
  175. action = fmt.Sprintf("激活勋章:%s,", mi.Name)
  176. log.Error("MedalOwnerAdd ismsg(id:%d %+v) error(%+v)", nid, ismsg, err)
  177. if ismsg {
  178. action += fmt.Sprintf("并发送消息title:%s msg:%s", title, msg)
  179. } else {
  180. action += fmt.Sprintf("并没有发送消息")
  181. }
  182. s.d.AddMedalOperLog(c, oid, mid, nid, action)
  183. }
  184. return
  185. }
  186. // MedalOwnerAddList .
  187. func (s *Service) MedalOwnerAddList(c context.Context, mid int64) (res []*model.MedalMemberAddList, err error) {
  188. if res, err = s.d.MedalAddList(c, mid); err != nil {
  189. log.Error("s.d.MedalOwnerAddList(%d) error(%v)", mid, err)
  190. }
  191. return
  192. }
  193. // MedalOwnerUpActivated update medal owner is_activated.
  194. func (s *Service) MedalOwnerUpActivated(c context.Context, mid, nid int64) (err error) {
  195. if _, err = s.d.MedalOwnerUpActivated(c, mid, nid); err != nil {
  196. log.Error("s.d.UpMedalOwnerActivated(mid:%d nid:%d) error(%v)", mid, nid, err)
  197. }
  198. if _, err = s.d.MedalOwnerUpNotActivated(c, mid, nid); err != nil {
  199. log.Error("s.d.UpMedalOwnerNotActivated(mid:%d nid:%d) error(%v)", mid, nid, err)
  200. }
  201. if err = s.d.SetMedalActivatedCache(c, mid, nid); err != nil {
  202. log.Error("s.d.DelMedalActivatedCache(mid:%d nid:%d) error(%v)", mid, nid, err)
  203. err = nil
  204. }
  205. s.addAsyn(func() {
  206. if err = s.accNotify(context.Background(), mid, model.AccountNotifyUpdateMedal); err != nil {
  207. log.Error("s.accNotify(%d) error(%+v)", mid, err)
  208. return
  209. }
  210. })
  211. return
  212. }
  213. // MedalOwnerDel update medal owner is_del .
  214. func (s *Service) MedalOwnerDel(c context.Context, mid, nid int64, isDel int8, title, msg string) (err error) {
  215. if _, err = s.d.MedalOwnerDel(c, mid, nid, isDel); err != nil {
  216. log.Error("s.d.MedalOwnerDel error(%v)", err)
  217. }
  218. if err = s.d.DelMedalOwnersCache(c, mid); err != nil {
  219. log.Error("s.d.DelMedalOwnersCache(%d) error(%v)", mid, err)
  220. err = nil
  221. }
  222. var mids []int64
  223. mids = append(mids, mid)
  224. if title != "" && msg != "" {
  225. if err = s.d.SendSysMsg(c, mids, title, msg, ""); err != nil {
  226. log.Error("MedalOwnerDel(mid:%d nid:%d title:%s msg:%s) SendSysMsg error(%+v)", mid, nid, title, msg, err)
  227. err = nil
  228. }
  229. }
  230. return
  231. }
  232. // ReadCsv read csv file
  233. func (s *Service) ReadCsv(f multipart.File, h *multipart.FileHeader) (rs [][]string, err error) {
  234. r := csv.NewReader(f)
  235. for {
  236. record, err := r.Read()
  237. if err == io.EOF {
  238. break
  239. }
  240. if err != nil {
  241. fmt.Println(err)
  242. log.Error("upload question ReadCsv error(%v)", err)
  243. }
  244. if len(record) == 1 {
  245. rs = append(rs, record)
  246. }
  247. }
  248. return
  249. }
  250. // BatchAdd medal bacth add.
  251. func (s *Service) BatchAdd(c context.Context, nid int64, f multipart.File, h *multipart.FileHeader) (msg string, err error) {
  252. if h != nil && !strings.HasSuffix(h.Filename, ".csv") {
  253. msg = "not csv file."
  254. return
  255. }
  256. rs, err := s.ReadCsv(f, h)
  257. if len(rs) == 0 || len(rs) > model.MaxCount {
  258. msg = "file size count is 0 or more than " + strconv.FormatInt(model.MaxCount, 10)
  259. return
  260. }
  261. for _, r := range rs {
  262. mid, err := strconv.ParseInt(r[0], 10, 64)
  263. if err == nil {
  264. if err = s.MedalOwnerAdd(c, mid, nid, "", "", 0); err != nil {
  265. log.Error("s.d.MedalOwnerAdd(mid:%d nid:%d) error(%v)", mid, nid, err)
  266. }
  267. }
  268. }
  269. return
  270. }
  271. // MedalOperlog medal operactlog .
  272. func (s *Service) MedalOperlog(c context.Context, mid int64, pn, ps int) (opers []*model.MedalOperLog, pager *model.Pager, err error) {
  273. var total int64
  274. pager = &model.Pager{
  275. PN: pn,
  276. PS: ps,
  277. }
  278. if total, err = s.d.MedalOperationLogTotal(c, mid); err != nil {
  279. err = errors.Wrap(err, "s.d.MedalOperationLogTotal()")
  280. return
  281. }
  282. if total <= 0 {
  283. return
  284. }
  285. pager.Total = total
  286. var uids []int64
  287. if opers, uids, err = s.d.MedalOperLog(c, mid, pn, ps); err != nil {
  288. err = errors.Wrapf(err, "s.d.MedalOperLog(%d,%d,%d)", mid, pn, ps)
  289. return
  290. }
  291. var accInfoMap map[int64]*accmdl.Info
  292. if accInfoMap, err = s.fetchInfos(c, uids, _fetchInfoTimeout); err != nil {
  293. log.Error("service.fetchInfos(%v, %v) error(%v)", xstr.JoinInts(uids), _fetchInfoTimeout, err)
  294. err = nil
  295. }
  296. for _, v := range opers {
  297. if accInfo, ok := accInfoMap[v.MID]; ok {
  298. v.Action = fmt.Sprintf("给用户(%s) %s", accInfo.Name, v.Action)
  299. }
  300. if operName, ok := s.Managers[v.OID]; ok {
  301. v.OperName = operName
  302. }
  303. }
  304. return
  305. }