appeal.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. package appeal
  2. import (
  3. "context"
  4. xtime "go-common/library/time"
  5. "hash/crc32"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "go-common/app/interface/main/creative/model/appeal"
  10. "go-common/app/interface/main/creative/model/archive"
  11. "go-common/app/service/main/account/model"
  12. "go-common/app/service/main/archive/api"
  13. "go-common/library/ecode"
  14. "go-common/library/log"
  15. )
  16. // List get appeal List.
  17. func (s *Service) List(c context.Context, mid int64, pn, ps int, tp, ip string) (all, open, closed int, res []*appeal.Appeal, err error) {
  18. list, err := s.ap.AppealList(c, mid, appeal.Business, ip)
  19. if err != nil {
  20. log.Error("s.ap.Appeals error(%v)", err)
  21. return
  22. }
  23. if len(list) == 0 {
  24. return
  25. }
  26. var (
  27. start, end int
  28. oaps []*appeal.Appeal // open appeal
  29. caps []*appeal.Appeal // close appeal
  30. apTmp []*appeal.Appeal // tmp appeal .
  31. )
  32. aps := make([]*appeal.Appeal, 0, len(list))
  33. for _, v := range list {
  34. ap := &appeal.Appeal{}
  35. ap.ID = v.ID
  36. ap.Oid = v.Oid
  37. ap.Cid = v.ID
  38. ap.Mid = v.Mid
  39. ap.State = v.BusinessState
  40. ap.Content = v.Desc
  41. ap.Description = v.Desc
  42. ap.CTime = v.CTime
  43. ap.MTime = v.MTime
  44. aps = append(aps, ap)
  45. }
  46. for _, v := range aps {
  47. if appeal.IsOpen(v.State) {
  48. oaps = append(oaps, v)
  49. } else {
  50. caps = append(caps, v)
  51. }
  52. }
  53. all = len(aps)
  54. open = len(oaps)
  55. closed = len(caps)
  56. if pn > 1 {
  57. start = (pn - 1) * ps
  58. } else {
  59. start = 0
  60. }
  61. end = pn * ps
  62. if tp == "open" {
  63. apTmp = oaps
  64. } else if tp == "closed" {
  65. apTmp = caps
  66. } else {
  67. apTmp = aps
  68. }
  69. total := len(apTmp)
  70. if total == 0 {
  71. return
  72. }
  73. if total <= start {
  74. res = make([]*appeal.Appeal, 0)
  75. } else if total <= end {
  76. res = apTmp[start:total]
  77. } else {
  78. res = apTmp[start:end]
  79. }
  80. if len(res) > 0 {
  81. var (
  82. aids []int64
  83. aMap map[int64]*archive.ArcVideo
  84. )
  85. for _, v := range res {
  86. aids = append(aids, v.Oid)
  87. }
  88. if len(aids) > 0 {
  89. if aMap, err = s.arc.Views(c, mid, aids, ip); err != nil {
  90. log.Error("s.arc.Archives aids(%v), ip(%s) err(%v)", aids, ip, err)
  91. return
  92. }
  93. }
  94. for _, v := range res {
  95. if _, ok := aMap[v.Oid]; !ok {
  96. continue
  97. }
  98. arc := &api.Arc{
  99. Aid: aMap[v.Oid].Archive.Aid,
  100. Pic: coverURL(aMap[v.Oid].Archive.Cover),
  101. Title: aMap[v.Oid].Archive.Title,
  102. State: int32(aMap[v.Oid].Archive.State),
  103. }
  104. v.ID = v.Cid
  105. v.Title = arc.Title
  106. v.Pics = arc.Pic
  107. v.Archive = arc
  108. }
  109. }
  110. return
  111. }
  112. // Detail get a appeal and events.
  113. func (s *Service) Detail(c context.Context, mid, cid int64, ip string) (ap *appeal.Appeal, err error) {
  114. var apmeta *appeal.AppealMeta
  115. if apmeta, err = s.ap.AppealDetail(c, mid, cid, appeal.Business, ip); err != nil {
  116. log.Error("s.ap.AppealDetail error(%v)", err)
  117. return
  118. }
  119. if apmeta == nil {
  120. err = ecode.AppealNotExist
  121. return
  122. }
  123. var (
  124. av *archive.ArcVideo
  125. arc *api.Arc
  126. starStr, etimeStr string
  127. star, etime int64
  128. )
  129. ap = &appeal.Appeal{}
  130. ap.ID = apmeta.ID
  131. ap.Oid = apmeta.Oid
  132. ap.Cid = apmeta.ID
  133. ap.Mid = apmeta.Mid
  134. ap.State = apmeta.BusinessState
  135. ap.Content = apmeta.Desc
  136. ap.Description = apmeta.Desc
  137. ap.CTime = apmeta.CTime
  138. ap.MTime = apmeta.MTime
  139. ap.Attachments = apmeta.Attachments
  140. if starStr, etimeStr, err = s.ap.AppealStarInfo(c, mid, cid, appeal.Business, ip); err != nil {
  141. log.Error("s.ap.AppealStarInfo(%d,%d) error(%v)", mid, cid, err)
  142. err = nil
  143. }
  144. if starStr != "" {
  145. star, err = strconv.ParseInt(starStr, 10, 64)
  146. if err != nil {
  147. log.Error("strconv.Atoi(%s) applealid(%d) error(%v)", starStr, cid, err)
  148. err = nil
  149. }
  150. ap.Star = int8(star)
  151. }
  152. if etimeStr != "" {
  153. etime, err = strconv.ParseInt(etimeStr, 10, 64)
  154. if err != nil {
  155. log.Error("strconv.Atoi(%s) applealid(%d) error(%v)", etimeStr, cid, err)
  156. err = nil
  157. }
  158. ap.MTime = xtime.Time(etime)
  159. }
  160. if av, err = s.arc.View(c, mid, ap.Oid, ip, 0, 0); err != nil {
  161. log.Error("s.arc.View(%d,%d) error(%v)", mid, ap.Oid, err)
  162. err = ecode.ArchiveNotExist
  163. return
  164. }
  165. if av != nil && av.Archive != nil {
  166. arc = &api.Arc{
  167. Aid: av.Archive.Aid,
  168. Pic: coverURL(av.Archive.Cover),
  169. Title: av.Archive.Title,
  170. State: int32(av.Archive.State),
  171. }
  172. }
  173. if arc == nil {
  174. arc = &api.Arc{}
  175. }
  176. ap.Title = arc.Title
  177. ap.Aid = arc.Aid
  178. ap.Archive = arc
  179. eventTmp := make([]*appeal.Event, 0, len(apmeta.Events))
  180. for _, v := range apmeta.Events { // Attachments 过滤管理员备注.
  181. if v.Event == 2 {
  182. continue
  183. }
  184. apev := &appeal.Event{}
  185. apev.ID = v.ID
  186. apev.AdminID = v.Adminid
  187. apev.Content = v.Content
  188. apev.ApID = v.Cid
  189. apev.Pics = v.Attachments
  190. apev.Event = v.Event
  191. apev.Attachments = v.Attachments
  192. apev.CTime = v.CTime
  193. apev.MTime = v.MTime
  194. eventTmp = append(eventTmp, apev)
  195. }
  196. ap.Events = eventTmp
  197. var strTmp []string
  198. for _, v := range ap.Attachments {
  199. strTmp = append(strTmp, v.Path)
  200. }
  201. ap.Pics = strings.Join(strTmp, ";")
  202. if ap.State == appeal.StateNoRead {
  203. if err = s.ap.AppealState(c, mid, cid, appeal.Business, appeal.StateReply, ip); err != nil {
  204. log.Error("s.ap.AppealState error(%v)", err)
  205. err = nil
  206. }
  207. }
  208. var (
  209. pf *model.Profile
  210. )
  211. if pf, err = s.acc.Profile(c, mid, ip); err != nil {
  212. log.Error("s.acc.Profile(%d) mid(%d)|ip(%s)|error(%v)", mid, ip, err)
  213. return
  214. }
  215. if pf != nil {
  216. ap.UserInfo = &appeal.UserInfo{
  217. MID: pf.Mid,
  218. Name: pf.Name,
  219. Sex: pf.Sex,
  220. Face: pf.Face,
  221. Rank: pf.Rank,
  222. Level: pf.Level,
  223. }
  224. }
  225. return
  226. }
  227. // State shutdown an appeal.
  228. func (s *Service) State(c context.Context, mid, cid, state int64, ip string) (err error) {
  229. if err = s.ap.AppealState(c, mid, cid, appeal.Business, state, ip); err != nil {
  230. log.Error("s.ap.AppealState error(%v)", err)
  231. }
  232. return
  233. }
  234. // Add create an appeal.
  235. func (s *Service) Add(c context.Context, mid, aid int64, qq, phone, email, desc, attachments, ip string, ap *appeal.BusinessAppeal) (apID int64, err error) {
  236. arc, err := s.arc.View(c, mid, aid, ip, 0, 0)
  237. if err != nil {
  238. log.Error("s.arc.Archive error(%v)", err)
  239. err = ecode.CreativeArcServiceErr
  240. return
  241. }
  242. if arc == nil {
  243. log.Error("archive not exist")
  244. err = ecode.ArchiveNotExist
  245. return
  246. }
  247. if arc.Archive.Mid != mid {
  248. log.Error("login mid(%d) and archive mid(%d) are different ", mid, arc.Archive.Mid)
  249. err = ecode.AppealOwner
  250. return
  251. }
  252. if !appeal.Allow(arc.Archive.State) {
  253. log.Error("archive aid(%d) mid(%d) state(%d) ", aid, mid, arc.Archive.State)
  254. err = ecode.AppealLimit
  255. return
  256. }
  257. appeals, err := s.ap.AppealList(c, mid, appeal.Business, ip)
  258. if err != nil {
  259. log.Error("s.ap.AppealList error (%v)", err)
  260. return
  261. }
  262. for _, k := range appeals {
  263. if aid == k.Oid && appeal.IsOpen(k.BusinessState) {
  264. err = ecode.AppealOpen
  265. return
  266. }
  267. }
  268. var tid int64
  269. tid, err = s.tag.AppealTag(c, aid, ip)
  270. if err != nil {
  271. log.Error("s.tag.AppealTag error(%v)", err)
  272. return
  273. }
  274. if tid == 0 && s.appealTag != 0 {
  275. tid = s.appealTag
  276. }
  277. if tid == 0 {
  278. log.Error("s.tag.AppealTag tid(%d)", tid)
  279. return
  280. }
  281. if apID, err = s.ap.AddAppeal(c, tid, aid, mid, appeal.Business, qq, phone, email, desc, attachments, ip, ap); err != nil {
  282. log.Error("s.ap.AddAppeal error(%v)", err)
  283. }
  284. return
  285. }
  286. // Reply add reply an appeal.
  287. func (s *Service) Reply(c context.Context, mid, cid, event int64, content, attachments, ip string) (err error) {
  288. if err = s.ap.AddReply(c, cid, event, content, attachments, ip); err != nil {
  289. log.Error("s.ap.AddReply error(%v)", err)
  290. return
  291. }
  292. if err = s.ap.AppealState(c, mid, cid, appeal.Business, appeal.StateCreate, ip); err != nil {
  293. log.Error("user add reply s.ap.AppealState error(%v)", err)
  294. err = nil
  295. }
  296. return
  297. }
  298. // PhoneEmail get user phone & email
  299. func (s *Service) PhoneEmail(c context.Context, ck, ip string) (ct *appeal.Contact, err error) {
  300. if ct, err = s.acc.PhoneEmail(c, ck, ip); err != nil {
  301. log.Error("s.acc.PhoneEmail error(%v)", err)
  302. }
  303. if ct == nil {
  304. err = ecode.NothingFound
  305. }
  306. return
  307. }
  308. // Star give star to appeal.
  309. func (s *Service) Star(c context.Context, mid, cid, star int64, ip string) (err error) {
  310. if err = s.ap.AppealExtra(c, mid, cid, appeal.Business, star, "star", ip); err != nil {
  311. log.Error("s.ap.AppealExtra error(%v)", err)
  312. return
  313. }
  314. if err = s.ap.AppealExtra(c, mid, cid, appeal.Business, time.Now().Unix(), "etime", ip); err != nil {
  315. log.Error("s.ap.AppealExtra error(%v)", err)
  316. return
  317. }
  318. if s.ap.AppealState(c, mid, cid, appeal.Business, appeal.StateUserFinished, ip); err != nil {
  319. log.Error("star change stats s.ap.AppealState error(%v)", err)
  320. err = nil
  321. }
  322. return
  323. }
  324. // coverURL convert cover url to full url.
  325. func coverURL(uri string) (cover string) {
  326. if uri == "" {
  327. return
  328. }
  329. cover = uri
  330. if strings.Index(uri, "http://") == 0 {
  331. return
  332. }
  333. if len(uri) >= 10 && uri[:10] == "/templets/" {
  334. return
  335. }
  336. if strings.HasPrefix(uri, "group1") {
  337. cover = "http://i0.hdslb.com/" + uri
  338. return
  339. }
  340. if pos := strings.Index(uri, "/uploads/"); pos != -1 && (pos == 0 || pos == 3) {
  341. cover = uri[pos+8:]
  342. }
  343. cover = strings.Replace(cover, "{IMG}", "", -1)
  344. cover = "http://i" + strconv.FormatInt(int64(crc32.ChecksumIEEE([]byte(cover)))%3, 10) + ".hdslb.com" + cover
  345. return
  346. }