egg.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. package egg
  2. import (
  3. "time"
  4. "go-common/app/admin/main/feed/conf"
  5. "go-common/app/admin/main/feed/dao/egg"
  6. "go-common/app/admin/main/feed/model/common"
  7. eggModel "go-common/app/admin/main/feed/model/egg"
  8. Log "go-common/app/admin/main/feed/util"
  9. "go-common/library/log"
  10. libTime "go-common/library/time"
  11. )
  12. const (
  13. //_ActionAdd log action add
  14. _ActionAdd = "add"
  15. //_ActionUpdate log action update
  16. _ActionUpdate = "update"
  17. //_ActionDel log action delete
  18. _ActionDel = "delete"
  19. //_ActionPub log action publish
  20. _ActionPub = "publish"
  21. )
  22. // Service is egg service
  23. type Service struct {
  24. dao *egg.Dao
  25. }
  26. // New new a egg service
  27. func New(c *conf.Config) (s *Service) {
  28. s = &Service{
  29. dao: egg.New(c),
  30. }
  31. return
  32. }
  33. //EggWithID get egg with ID
  34. func (s *Service) EggWithID(id uint) (egg *eggModel.Egg, err error) {
  35. egg = &eggModel.Egg{}
  36. if err = s.dao.DB.Model(&eggModel.Egg{}).Where("id = ?", id).
  37. Where("`delete` != ?", eggModel.Delete).First(egg).
  38. Error; err != nil {
  39. log.Error("eggSrv.DelEgg Update error(%v)", err)
  40. return
  41. }
  42. return
  43. }
  44. //DelEgg update egg
  45. func (s *Service) DelEgg(id uint, person string, uid int64) (err error) {
  46. tx := s.dao.DB.Begin()
  47. if err = tx.Model(&eggModel.Egg{}).Where("id = ?", id).Update("delete", eggModel.Delete).Error; err != nil {
  48. log.Error("eggSrv.DelEgg Update error(%v)", err)
  49. return
  50. }
  51. if err = tx.Model(&eggModel.Query{}).Where("egg_id = ?", id).Update("deleted", eggModel.Delete).Error; err != nil {
  52. log.Error("eggSrv.DelEgg UpdateQuery error(%v)", err)
  53. return
  54. }
  55. tx.Commit()
  56. if err = Log.AddLog(eggModel.Business, person, uid, int64(id), _ActionDel, ""); err != nil {
  57. log.Error("eggSrv.AddEgg AddLog error(%v)", err)
  58. return
  59. }
  60. return
  61. }
  62. //PubEgg publish egg
  63. func (s *Service) PubEgg(id uint, publish uint8, person string, uid int64) (err error) {
  64. if err = s.dao.DB.Model(&eggModel.Egg{}).Where("id = ?", id).Update("publish", publish).Error; err != nil {
  65. log.Error("eggSrv.PubEgg Update error(%v)", err)
  66. return
  67. }
  68. if err = Log.AddLog(eggModel.Business, person, uid, int64(id), _ActionPub, ""); err != nil {
  69. log.Error("eggSrv.PubEgg AddLog error(%v)", err)
  70. return
  71. }
  72. return
  73. }
  74. //UpdateEgg update egg
  75. func (s *Service) UpdateEgg(e *eggModel.Egg, p []eggModel.Plat, w []string) (err error) {
  76. tx := s.dao.DB.Begin()
  77. db := tx.Model(&eggModel.Egg{}).Where("id = ?", e.ID).Update(e)
  78. if err = db.Error; err != nil {
  79. log.Error("eggSrv.UpdateEgg UpdateEgg egg error(%v)", err)
  80. return
  81. }
  82. db = tx.Model(&eggModel.Plat{}).Where("egg_id = ?", e.ID).Update("deleted", eggModel.Delete)
  83. if err = db.Error; err != nil {
  84. log.Error("eggSrv.UpdateEgg UpdatePlat error(%v)", err)
  85. return
  86. }
  87. db = tx.Model(&eggModel.Query{}).Where("egg_id = ?", e.ID).Update("deleted", eggModel.Delete)
  88. if err = db.Error; err != nil {
  89. log.Error("eggSrv.UpdateEgg UpdateQuery error(%v)", err)
  90. return
  91. }
  92. for _, v := range p {
  93. v.EggID = e.ID
  94. v.Deleted = eggModel.NotDelete
  95. db := tx.Model(&eggModel.Plat{}).Create(v)
  96. if err = db.Error; err != nil {
  97. log.Error("eggSrv.UpdateEgg create egg plat error(%v)", err)
  98. return
  99. }
  100. }
  101. for _, v := range w {
  102. q := eggModel.Query{
  103. Word: v,
  104. EggID: e.ID,
  105. STime: e.Stime,
  106. ETime: e.Etime,
  107. Deleted: eggModel.NotDelete,
  108. }
  109. db := tx.Model(&eggModel.Query{}).Create(q)
  110. if err = db.Error; err != nil {
  111. log.Error("eggSrv.UpdateEgg create egg query error(%v)", err)
  112. return
  113. }
  114. }
  115. tx.Commit()
  116. obj := map[string]interface{}{
  117. "egg": e,
  118. "plat": p,
  119. "words": w,
  120. }
  121. if err = Log.AddLog(eggModel.Business, e.Person, e.UID, int64(e.ID), _ActionUpdate, obj); err != nil {
  122. log.Error("eggSrv.AddEgg AddLog error(%v)", err)
  123. return
  124. }
  125. return
  126. }
  127. //AddEgg add egg
  128. func (s *Service) AddEgg(e *eggModel.Egg, p []eggModel.Plat, w []string) (err error) {
  129. tx := s.dao.DB.Begin()
  130. db := tx.Model(&eggModel.Egg{}).Create(e)
  131. if err = db.Error; err != nil {
  132. log.Error("eggSrv.AddEgg create egg error(%v)", err)
  133. return
  134. }
  135. for _, v := range p {
  136. v.EggID = e.ID
  137. v.Deleted = eggModel.NotDelete
  138. db := tx.Model(&eggModel.Plat{}).Create(v)
  139. if err = db.Error; err != nil {
  140. log.Error("eggSrv.AddEgg create egg plat error(%v)", err)
  141. return
  142. }
  143. }
  144. for _, v := range w {
  145. q := eggModel.Query{
  146. Word: v,
  147. EggID: e.ID,
  148. STime: e.Stime,
  149. ETime: e.Etime,
  150. Deleted: eggModel.NotDelete,
  151. }
  152. db := tx.Model(&eggModel.Query{}).Create(q)
  153. if err = db.Error; err != nil {
  154. log.Error("eggSrv.AddEgg create egg query error(%v)", err)
  155. return
  156. }
  157. }
  158. tx.Commit()
  159. obj := map[string]interface{}{
  160. "egg": e,
  161. "plat": p,
  162. "words": w,
  163. }
  164. if err = Log.AddLog(eggModel.Business, e.Person, e.UID, int64(e.ID), _ActionAdd, obj); err != nil {
  165. log.Error("eggSrv.AddEgg AddLog error(%v)", err)
  166. return
  167. }
  168. return
  169. }
  170. //IsWdExist the word will add is exist
  171. func (s *Service) IsWdExist(words []string, sTime, eTime libTime.Time, eggID uint) (exist bool, w string, err error) {
  172. var (
  173. c int
  174. )
  175. for _, v := range words {
  176. query := s.dao.DB.Model(&eggModel.Query{}).
  177. Where("deleted=?", eggModel.NotDelete).
  178. Where("word = ?", v).
  179. Where("s_time < ?", eTime).
  180. Where("e_time > ?", sTime)
  181. if eggID != 0 {
  182. query = query.Where("egg_id != ?", eggID)
  183. }
  184. if err = query.Count(&c).Error; err != nil {
  185. log.Error("eggSrv.IsWdExist Query error(%v)", err)
  186. return
  187. }
  188. if c > 0 {
  189. return true, v, nil
  190. }
  191. }
  192. return false, "", nil
  193. }
  194. func (s *Service) qWord(word string) (ids []uint, err error) {
  195. q := []eggModel.Query{}
  196. if err = s.dao.DB.Model(&eggModel.Query{}).Where("deleted=?", eggModel.NotDelete).
  197. Where("word like ?", "%"+word+"%").Find(&q).Error; err != nil {
  198. log.Error("eggSrv.IndexEgg Query error(%v)", err)
  199. return
  200. }
  201. for _, v := range q {
  202. ids = append(ids, v.EggID)
  203. }
  204. return
  205. }
  206. //IndexEgg egg list
  207. func (s *Service) IndexEgg(param *eggModel.IndexParam) (values *eggModel.IndexPager, err error) {
  208. values = &eggModel.IndexPager{
  209. Page: common.Page{
  210. Num: param.Pn,
  211. Size: param.Ps,
  212. },
  213. }
  214. w := map[string]interface{}{
  215. "delete": eggModel.NotDelete,
  216. }
  217. if param.ID != "" {
  218. w["id"] = param.ID
  219. }
  220. query := s.dao.DB.Model(&eggModel.Index{}).Where(w)
  221. if param.Stime != "" {
  222. query = query.Where("stime >= ?", param.Stime)
  223. }
  224. if param.Etime != "" {
  225. query = query.Where("etime <= ?", param.Etime)
  226. }
  227. if param.Person != "" {
  228. query = query.Where("person like ?", "%"+param.Person+"%")
  229. }
  230. if param.Word != "" {
  231. var ids = []uint{}
  232. if ids, err = s.qWord(param.Word); err != nil {
  233. return
  234. }
  235. if len(ids) != 0 {
  236. query = query.Where("id in (?)", ids)
  237. } else {
  238. query = query.Where("id in (?)", 0)
  239. }
  240. }
  241. if err = query.Order("`id` DESC").Offset((param.Pn - 1) * param.Ps).Limit(param.Ps).Find(&values.Item).Error; err != nil {
  242. log.Error("eggSrv.IndexEgg Index list error(%v)", err)
  243. return
  244. }
  245. if err = query.Count(&values.Page.Total).Error; err != nil {
  246. log.Error("eggSrv.IndexEgg Index count error(%v)", err)
  247. return
  248. }
  249. for k, v := range values.Item {
  250. q := []eggModel.Query{}
  251. p := []eggModel.Plat{}
  252. //select egg query words
  253. w := map[string]interface{}{
  254. "egg_id": v.ID,
  255. "deleted": eggModel.NotDelete,
  256. }
  257. if err = s.dao.DB.Model(&eggModel.Query{}).Where(w).Find(&q).Error; err != nil {
  258. log.Error("eggSrv.IndexEgg Query error(%v)", err)
  259. return
  260. }
  261. for _, qV := range q {
  262. if values.Item[k].Words == "" {
  263. values.Item[k].Words = qV.Word
  264. } else {
  265. values.Item[k].Words = values.Item[k].Words + "," + qV.Word
  266. }
  267. }
  268. //select egg plat
  269. w = map[string]interface{}{
  270. "egg_id": v.ID,
  271. "deleted": eggModel.NotDelete,
  272. }
  273. if err = s.dao.DB.Model(&eggModel.Plat{}).Where(w).Find(&p).Error; err != nil {
  274. log.Error("eggSrv.IndexEgg Plat error(%v)", err)
  275. return
  276. }
  277. values.Item[k].Plat = p
  278. }
  279. return
  280. }
  281. //SearchEgg search egg list
  282. func (s *Service) SearchEgg() (values []eggModel.SearchEgg, err error) {
  283. param := eggModel.IndexParam{}
  284. w := map[string]interface{}{
  285. "delete": eggModel.NotDelete,
  286. "publish": eggModel.Publish,
  287. }
  288. cTime := time.Now().Unix()
  289. cTime = cTime + 10*60
  290. tm := time.Unix(cTime, 0)
  291. param.Stime = tm.Format("2006-01-02 15:04:05")
  292. param.Etime = param.Stime
  293. query := s.dao.DB.Model(&eggModel.SearchEgg{}).Where(w)
  294. query = query.Where("stime <= ?", param.Stime).Where("etime >= ?", param.Etime)
  295. if err = query.Order("`id` DESC").Find(&values).Error; err != nil {
  296. log.Error("eggSrv.SearchEgg Index list error(%v)", err)
  297. return
  298. }
  299. for k, v := range values {
  300. q := []eggModel.Query{}
  301. p := []eggModel.Plat{}
  302. Words := []string{}
  303. //select egg query words
  304. w := map[string]interface{}{
  305. "egg_id": v.ID,
  306. "deleted": eggModel.NotDelete,
  307. }
  308. if err = s.dao.DB.Model(&eggModel.Query{}).Where(w).Find(&q).Error; err != nil {
  309. log.Error("eggSrv.IndexEgg Query error(%v)", err)
  310. return
  311. }
  312. for _, v := range q {
  313. Words = append(Words, v.Word)
  314. }
  315. values[k].Words = Words
  316. //select egg plat
  317. w = map[string]interface{}{
  318. "egg_id": v.ID,
  319. "deleted": eggModel.NotDelete,
  320. }
  321. if err = s.dao.DB.Model(&eggModel.Plat{}).Where(w).Find(&p).Error; err != nil {
  322. log.Error("eggSrv.IndexEgg Plat error(%v)", err)
  323. return
  324. }
  325. values[k].Plat = make(map[uint8]eggModel.Plat)
  326. for _, v := range p {
  327. values[k].Plat[v.Plat] = v
  328. }
  329. }
  330. return
  331. }
  332. //SearchEggWeb search egg list
  333. func (s *Service) SearchEggWeb() (values []eggModel.SearchEggWeb, err error) {
  334. param := eggModel.IndexParam{}
  335. w := map[string]interface{}{
  336. "delete": eggModel.NotDelete,
  337. "publish": eggModel.Publish,
  338. }
  339. cTime := time.Now().Unix()
  340. cTime = cTime + 10*60
  341. tm := time.Unix(cTime, 0)
  342. param.Stime = tm.Format("2006-01-02 15:04:05")
  343. param.Etime = param.Stime
  344. query := s.dao.DB.Model(&eggModel.SearchEgg{}).Where(w)
  345. query = query.Where("stime <= ?", param.Stime).Where("etime >= ?", param.Etime)
  346. if err = query.Order("`id` DESC").Find(&values).Error; err != nil {
  347. log.Error("eggSrv.SearchEgg Index list error(%v)", err)
  348. return
  349. }
  350. for k, v := range values {
  351. q := []eggModel.Query{}
  352. p := []eggModel.Plat{}
  353. Words := []string{}
  354. //select egg query words
  355. w := map[string]interface{}{
  356. "egg_id": v.ID,
  357. "deleted": eggModel.NotDelete,
  358. }
  359. if err = s.dao.DB.Model(&eggModel.Query{}).Where(w).Find(&q).Error; err != nil {
  360. log.Error("eggSrv.IndexEgg Query error(%v)", err)
  361. return
  362. }
  363. for _, v := range q {
  364. Words = append(Words, v.Word)
  365. }
  366. values[k].Words = Words
  367. //select egg plat
  368. w = map[string]interface{}{
  369. "egg_id": v.ID,
  370. "deleted": eggModel.NotDelete,
  371. }
  372. if err = s.dao.DB.Model(&eggModel.Plat{}).Where(w).Find(&p).Error; err != nil {
  373. log.Error("eggSrv.IndexEgg Plat error(%v)", err)
  374. return
  375. }
  376. values[k].Plat = make(map[uint8][]eggModel.Plat)
  377. for _, v := range p {
  378. values[k].Plat[v.Plat] = append(values[k].Plat[v.Plat], v)
  379. }
  380. }
  381. return
  382. }