question.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package service
  2. import (
  3. "context"
  4. "encoding/csv"
  5. "io"
  6. "mime/multipart"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "go-common/app/admin/main/answer/model"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. )
  14. // QuestionList .
  15. func (s *Service) QuestionList(c context.Context, arg *model.ArgQue) (res *model.QuestionPage, err error) {
  16. res = &model.QuestionPage{}
  17. if res.Total, err = s.dao.QuestionCount(c, arg); err != nil {
  18. return
  19. }
  20. res.Items = []*model.QuestionDB{}
  21. if res.Total > 0 {
  22. if res.Items, err = s.dao.QuestionList(c, arg); err != nil {
  23. return
  24. }
  25. }
  26. return
  27. }
  28. // UpdateStatus update question state
  29. func (s *Service) UpdateStatus(c context.Context, qid int64, state int8, operator string) (err error) {
  30. var (
  31. r int64
  32. q *model.Question
  33. )
  34. if q, err = s.dao.QueByID(c, qid); err != nil {
  35. log.Error("dao QueByID(%d) error(%v)", qid, err)
  36. return
  37. }
  38. if q == nil || q.State == state {
  39. return
  40. }
  41. if r, err = s.dao.UpdateStatus(c, state, qid, operator); err != nil || r != 1 {
  42. return
  43. }
  44. return
  45. }
  46. // BatchUpdateState bacth update question state.
  47. func (s *Service) BatchUpdateState(c context.Context, qids []int64, state int8, operator string) (err error) {
  48. for _, id := range qids {
  49. s.UpdateStatus(c, id, state, operator)
  50. }
  51. return
  52. }
  53. // Types question type
  54. func (s *Service) Types(c context.Context) (res []*model.TypeInfo, err error) {
  55. return s.dao.Types(c)
  56. }
  57. // ReadCsv read csv file
  58. func (s *Service) ReadCsv(f multipart.File, h *multipart.FileHeader) (rs [][]string, err error) {
  59. r := csv.NewReader(f)
  60. for {
  61. record, err := r.Read()
  62. if err == io.EOF {
  63. break
  64. }
  65. if err != nil {
  66. log.Error("upload question ReadCsv error(%v)", err)
  67. break
  68. }
  69. if len(record) == model.ArgsCount {
  70. rs = append(rs, record)
  71. }
  72. }
  73. return
  74. }
  75. // UploadQsts upload questions
  76. func (s *Service) UploadQsts(c context.Context, f multipart.File, h *multipart.FileHeader, operator string) (msg string, err error) {
  77. defer f.Close()
  78. if h != nil && !strings.HasSuffix(h.Filename, ".csv") {
  79. msg = "not csv file."
  80. return
  81. }
  82. sz, ok := f.(model.Sizer)
  83. if !ok {
  84. msg = "get file size faild."
  85. return
  86. }
  87. size := sz.Size()
  88. if size > model.FileMaxSize {
  89. msg = "file size more than 2M."
  90. return
  91. }
  92. rs, err := s.ReadCsv(f, h)
  93. log.Info("file %s, len(%d)", h.Filename, len(rs))
  94. if len(rs) == 0 || len(rs) > model.MaxCount {
  95. msg = "file size count is 0 or more than " + strconv.FormatInt(model.MaxCount, 10)
  96. return
  97. }
  98. for _, r := range rs {
  99. typeID, err := strconv.ParseInt(r[0], 10, 8)
  100. if err != nil {
  101. log.Error("strconv.ParseInt(%+v) err(%v)", r[0], err)
  102. }
  103. if err == nil {
  104. q := &model.QuestionDB{
  105. TypeID: int8(typeID),
  106. Question: r[1],
  107. Ans1: r[2],
  108. Ans2: r[3],
  109. Ans3: r[4],
  110. Ans4: r[5],
  111. Operator: operator,
  112. }
  113. if err = s.QuestionAdd(c, q); err != nil {
  114. log.Error("s.QuestionAdd(%+v) error(%v)", q, err)
  115. }
  116. }
  117. }
  118. return
  119. }
  120. // QuestionAdd add register question
  121. func (s *Service) QuestionAdd(c context.Context, q *model.QuestionDB) (err error) {
  122. if len(q.Question) < model.MinQuestion || len(q.Question) > model.MaxQuestion {
  123. err = ecode.QuestionStrNotAllow
  124. return
  125. }
  126. if len(q.Ans1) < model.MinAns || len(q.Ans1) > model.MaxAns ||
  127. len(q.Ans2) < model.MinAns || len(q.Ans2) > model.MaxAns ||
  128. len(q.Ans3) < model.MinAns || len(q.Ans3) > model.MaxAns ||
  129. len(q.Ans4) < model.MinAns || len(q.Ans4) > model.MaxAns {
  130. err = ecode.QuestionAnsNotAllow
  131. return
  132. }
  133. if q.Tips != "" && (len(q.Tips) < model.MinTips || len(q.Tips) > model.MaxTips) {
  134. err = ecode.QuestionTipsNotAllow
  135. return
  136. }
  137. if q.TypeID <= 0 {
  138. err = ecode.QuestionTypeNotAllow
  139. return
  140. }
  141. // only sourport text question
  142. q.MediaType = model.TextMediaType
  143. q.State = model.PassCheck
  144. q.Ctime = time.Now()
  145. if _, err = s.dao.QuestionAdd(c, q); err != nil {
  146. return
  147. }
  148. qid := q.ID
  149. s.eventChan.Save(func() {
  150. s.CreateBFSImg(context.Background(), []int64{qid})
  151. })
  152. return
  153. }
  154. func (s *Service) loadtypes(c context.Context) (t map[int64]*model.TypeInfo, err error) {
  155. var tys []*model.TypeInfo
  156. tys, err = s.dao.Types(c)
  157. if err != nil {
  158. log.Error("s.dao.Types error(%v)", err)
  159. return
  160. }
  161. t = make(map[int64]*model.TypeInfo)
  162. for _, v := range tys {
  163. if v.Parentid == 0 && t[v.ID] == nil {
  164. t[v.ID] = &model.TypeInfo{ID: v.ID, Name: v.Name, Subs: []*model.SubType{}}
  165. } else if t[v.Parentid] != nil {
  166. t[v.Parentid].Subs = append(t[v.Parentid].Subs, &model.SubType{ID: v.ID, Name: v.Name, LabelName: v.LabelName})
  167. }
  168. }
  169. return
  170. }
  171. // QuestionEdit .
  172. func (s *Service) QuestionEdit(c context.Context, arg *model.QuestionDB) (aff int64, err error) {
  173. if aff, err = s.dao.QuestionEdit(c, arg); err != nil {
  174. return
  175. }
  176. s.eventChan.Save(func() {
  177. s.CreateBFSImg(context.Background(), []int64{arg.ID})
  178. })
  179. return
  180. }
  181. // LoadTypes .
  182. func (s *Service) LoadTypes(c context.Context) (err error) {
  183. var allType = []*model.TypeInfo{
  184. {ID: 1, Parentid: 0, Name: "游戏"},
  185. {ID: 2, Parentid: 0, Name: "影视"},
  186. {ID: 3, Parentid: 0, Name: "科技"},
  187. {ID: 4, Parentid: 0, Name: "动画"},
  188. {ID: 5, Parentid: 0, Name: "艺术"},
  189. {ID: 6, Parentid: 0, Name: "流行前线"},
  190. {ID: 7, Parentid: 0, Name: "鬼畜"},
  191. {ID: 8, Parentid: 1, Name: "动作射击", LabelName: "游戏"},
  192. {ID: 9, Parentid: 1, Name: "冒险格斗", LabelName: "游戏"},
  193. {ID: 12, Parentid: 1, Name: "策略模拟 ", LabelName: "游戏"},
  194. {ID: 13, Parentid: 1, Name: "角色扮演 ", LabelName: "游戏"},
  195. {ID: 14, Parentid: 1, Name: "音乐体育 ", LabelName: "游戏"},
  196. {ID: 15, Parentid: 2, Name: "纪录片 ", LabelName: "影视"},
  197. {ID: 16, Parentid: 2, Name: "电影 ", LabelName: "影视"},
  198. {ID: 17, Parentid: 2, Name: "电视剧 ", LabelName: "影视"},
  199. {ID: 18, Parentid: 3, Name: "军事 ", LabelName: "科技"},
  200. {ID: 19, Parentid: 3, Name: "地理 ", LabelName: "科技"},
  201. {ID: 20, Parentid: 3, Name: "历史 ", LabelName: "科技"},
  202. {ID: 21, Parentid: 3, Name: "文学 ", LabelName: "科技"},
  203. {ID: 22, Parentid: 3, Name: "数学 ", LabelName: "科技"},
  204. {ID: 23, Parentid: 3, Name: "物理 ", LabelName: "科技"},
  205. {ID: 24, Parentid: 3, Name: "化学 ", LabelName: "科技"},
  206. {ID: 25, Parentid: 3, Name: "生物 ", LabelName: "科技"},
  207. {ID: 26, Parentid: 3, Name: "数码科技 ", LabelName: "科技"},
  208. {ID: 27, Parentid: 4, Name: "动画声优 ", LabelName: "动画"},
  209. {ID: 28, Parentid: 4, Name: "动漫内容 ", LabelName: "动画"},
  210. {ID: 29, Parentid: 5, Name: "ACG音乐 ", LabelName: "艺术"},
  211. {ID: 30, Parentid: 5, Name: "三次元音乐 ", LabelName: "艺术"},
  212. {ID: 31, Parentid: 5, Name: "绘画 ", LabelName: "艺术"},
  213. {ID: 32, Parentid: 6, Name: "娱乐 ", LabelName: "流行前线"},
  214. {ID: 33, Parentid: 6, Name: "时尚 ", LabelName: "流行前线"},
  215. {ID: 34, Parentid: 6, Name: "运动 ", LabelName: "流行前线"},
  216. {ID: 35, Parentid: 7, Name: "鬼畜 ", LabelName: "鬼畜"},
  217. {ID: 36, Parentid: 0, Name: "基础题", LabelName: "基础题"},
  218. }
  219. for _, v := range allType {
  220. if _, err := s.dao.TypeSave(context.Background(), v); err != nil {
  221. log.Error("s.dao.TypeSave(%+v) err(%v)", v, err)
  222. }
  223. }
  224. return
  225. }
  226. // LoadImg .
  227. func (s *Service) LoadImg(c context.Context) (err error) {
  228. qss, err := s.dao.AllQS(c)
  229. if err != nil {
  230. log.Error("s.dao.AllQS() err(%v)", err)
  231. }
  232. for _, qs := range qss {
  233. lastID := qs.ID
  234. if err = s.eventChan.Save(func() {
  235. s.CreateBFSImg(context.Background(), []int64{lastID})
  236. }); err != nil {
  237. log.Error("s.CreateBFSImg(%d) err(%v)", lastID, err)
  238. }
  239. }
  240. return
  241. }
  242. // QueHistory .
  243. func (s *Service) QueHistory(c context.Context, arg *model.ArgHistory) (res *model.HistoryPage, err error) {
  244. res = &model.HistoryPage{}
  245. if res.Total, err = s.dao.HistoryCount(c, arg); err != nil {
  246. return
  247. }
  248. res.Items = []*model.AnswerHistoryDB{}
  249. if res.Total > 0 {
  250. if res.Items, err = s.dao.QueHistory(c, arg); err != nil {
  251. return
  252. }
  253. }
  254. return
  255. }
  256. // History .
  257. func (s *Service) History(c context.Context, arg *model.ArgHistory) (res *model.HistoryPage, err error) {
  258. if arg.Pn <= 0 || arg.Ps <= 0 {
  259. arg.Pn, arg.Ps = 1, 1000
  260. }
  261. res = &model.HistoryPage{}
  262. if res.Items, err = s.dao.HistoryES(c, arg); err != nil {
  263. return
  264. }
  265. res.Total = int64(len(res.Items))
  266. return
  267. }