search_web.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package show
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "go-common/app/admin/main/feed/model/common"
  6. "go-common/app/admin/main/feed/model/show"
  7. "go-common/library/log"
  8. "github.com/jinzhu/gorm"
  9. )
  10. // SearchWebAdd add search web
  11. func (d *Dao) SearchWebAdd(param *show.SearchWebAP) (err error) {
  12. var (
  13. querys []*show.SearchWebQuery
  14. )
  15. if param.Query != "" {
  16. if err = json.Unmarshal([]byte(param.Query), &querys); err != nil {
  17. return
  18. }
  19. }
  20. tx := d.DB.Begin()
  21. if err = tx.Model(&show.SearchWeb{}).Create(param).Error; err != nil {
  22. log.Error("SearchWebAdd tx.Model Create(%+v) error(%v)", param, err)
  23. err = tx.Rollback().Error
  24. return
  25. }
  26. if len(querys) > 0 {
  27. if err = tx.Model(&show.SearchWeb{}).Exec(show.BatchAddQuerySQL(param.ID, querys)).Error; err != nil {
  28. log.Error("SearchWebAdd tx.Model Exec(%+v) error(%v)", param, err)
  29. err = tx.Rollback().Error
  30. return
  31. }
  32. }
  33. if err = tx.Commit().Error; err != nil {
  34. return
  35. }
  36. return
  37. }
  38. // SearchWebUpdate update
  39. func (d *Dao) SearchWebUpdate(param *show.SearchWebUP) (err error) {
  40. var (
  41. newQuerys []*show.SearchWebQuery
  42. )
  43. if param.Query != "" {
  44. if err = json.Unmarshal([]byte(param.Query), &newQuerys); err != nil {
  45. return
  46. }
  47. }
  48. tx := d.DB.Begin()
  49. if err = tx.Error; err != nil {
  50. log.Error("dao.SearchWebUpdate.DB.Begin error(%v)", err)
  51. return
  52. }
  53. if err = tx.Model(&show.SearchWeb{}).Update(param).Error; err != nil {
  54. log.Error("dao.SearchWebUpdate (%+v) error(%v)", param, err)
  55. err = tx.Rollback().Error
  56. return
  57. }
  58. var (
  59. mapOldCData, mapNewQData map[int64]*show.SearchWebQuery
  60. upQData, addQData, oldQData []*show.SearchWebQuery
  61. delQData []int64
  62. )
  63. if len(newQuerys) > 0 {
  64. if err = d.DB.Model(&show.SearchWeb{}).Where("sid=?", param.ID).Where("deleted=?", common.NotDeleted).Find(&oldQData).Error; err != nil {
  65. log.Error("dao.SearchWebUpdate Find Old data (%+v) error(%v)", param.ID, err)
  66. return
  67. }
  68. mapOldCData = make(map[int64]*show.SearchWebQuery, len(oldQData))
  69. for _, v := range oldQData {
  70. mapOldCData[v.ID] = v
  71. }
  72. //新数据在老数据中 更新老数据。新的数据不在老数据 添加新数据
  73. for _, qData := range newQuerys {
  74. if _, ok := mapOldCData[qData.ID]; ok {
  75. upQData = append(upQData, qData)
  76. } else {
  77. addQData = append(addQData, qData)
  78. }
  79. }
  80. mapNewQData = make(map[int64]*show.SearchWebQuery, len(newQuerys))
  81. for _, v := range newQuerys {
  82. mapNewQData[v.ID] = v
  83. }
  84. //老数据在新数据中 上面已经处理。老数据不在新数据中 删除老数据
  85. for _, qData := range oldQData {
  86. if _, ok := mapNewQData[qData.ID]; !ok {
  87. delQData = append(delQData, qData.ID)
  88. }
  89. }
  90. if len(upQData) > 0 {
  91. if err = tx.Model(&show.SearchWebQuery{}).Exec(show.BatchEditQuerySQL(upQData)).Error; err != nil {
  92. log.Error("dao.SearchWebUpdate tx.Model Exec(%+v) error(%v)", upQData, err)
  93. err = tx.Rollback().Error
  94. return
  95. }
  96. }
  97. if len(delQData) > 0 {
  98. if err = tx.Model(&show.SearchWebQuery{}).Where("id IN (?)", delQData).Updates(map[string]interface{}{"deleted": common.Deleted}).Error; err != nil {
  99. log.Error("dao.SearchWebUpdate Updates(%+v) error(%v)", delQData, err)
  100. err = tx.Rollback().Error
  101. return
  102. }
  103. }
  104. if len(addQData) > 0 {
  105. if err = tx.Model(&show.SearchWebQuery{}).Exec(show.BatchAddQuerySQL(param.ID, addQData)).Error; err != nil {
  106. log.Error("EditContest s.dao.DB.Model Create(%+v) error(%v)", addQData, err)
  107. err = tx.Rollback().Error
  108. return
  109. }
  110. }
  111. } else {
  112. if err = tx.Model(&show.SearchWebQuery{}).Where("sid IN (?)", param.ID).Updates(map[string]interface{}{"deleted": common.Deleted}).Error; err != nil {
  113. log.Error("dao.SearchWebUpdate Updates(%+v) error(%v)", param.ID, err)
  114. err = tx.Rollback().Error
  115. return
  116. }
  117. }
  118. err = tx.Commit().Error
  119. return
  120. }
  121. // SearchWebDelete delete search web
  122. func (d *Dao) SearchWebDelete(id int64) (err error) {
  123. up := map[string]interface{}{
  124. "deleted": common.Deleted,
  125. }
  126. tx := d.DB.Begin()
  127. if err = tx.Error; err != nil {
  128. log.Error("dao.SearchWebDelete.DB.Begin error(%v)", err)
  129. return
  130. }
  131. if err = tx.Model(&show.SearchWeb{}).Where("id = ?", id).Update(up).Error; err != nil {
  132. log.Error("dao.show.SearchWebDelete(%+v) error(%v)", id, err)
  133. err = tx.Rollback().Error
  134. return
  135. }
  136. if err = tx.Model(&show.SearchWebQuery{}).Where("sid = (?)", id).Updates(map[string]interface{}{"deleted": common.Deleted}).Error; err != nil {
  137. log.Error("dao.SearchWebDelete Updates(%+v) error(%v)", id, err)
  138. err = tx.Rollback().Error
  139. return
  140. }
  141. err = tx.Commit().Error
  142. return
  143. }
  144. // SearchWebOption option search web
  145. func (d *Dao) SearchWebOption(up *show.SearchWebOption) (err error) {
  146. if err = d.DB.Model(&show.SearchWebOption{}).Update(up).Error; err != nil {
  147. log.Error("dao.SearchWebOption Updates(%+v) error(%v)", up, err)
  148. }
  149. return
  150. }
  151. // SWTimeValid search web time validate
  152. func (d *Dao) SWTimeValid(param *show.SWTimeValid) (count int, err error) {
  153. query := d.DB.Table("search_web_query").
  154. Select("search_web_query.id").
  155. Joins("left join search_web ON search_web.id = search_web_query.sid").
  156. Where("value = ?", param.Query).
  157. Where("priority = ?", param.Priority).
  158. Where("`check` in (?)", []int{common.Verify, common.Pass, common.Valid}).
  159. Where("stime <= ?", param.ETime).
  160. Where("etime >= ?", param.STime).
  161. Where("search_web_query.deleted = 0").
  162. Where("search_web.deleted = 0")
  163. if param.ID != 0 {
  164. query = query.Where("search_web.id != ?", param.ID)
  165. }
  166. if err = query.Count(&count).Error; err != nil {
  167. log.Error("dao.SWTimeValid Count error(%v)", err)
  168. }
  169. return
  170. }
  171. //SWFindByID search web table value find by id
  172. func (d *Dao) SWFindByID(id int64) (value *show.SearchWeb, err error) {
  173. value = &show.SearchWeb{}
  174. w := map[string]interface{}{
  175. "deleted": common.NotDeleted,
  176. "id": id,
  177. }
  178. if err = d.DB.Model(&show.SearchWeb{}).Where(w).Find(value).Error; err != nil {
  179. if err == gorm.ErrRecordNotFound {
  180. err = fmt.Errorf("ID为%d的数据不存在", id)
  181. return
  182. }
  183. return
  184. }
  185. return
  186. }