activity_meet_detail_log.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package cygx
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "strings"
  6. "time"
  7. )
  8. // 报名
  9. type CygxActivityMeetDetailLog struct {
  10. AttendanceId int `orm:"column(attendance_id);pk;"description:"主键ID"`
  11. ActivityId int `description:"活动ID"`
  12. RealName string `description:"姓名"`
  13. Mobile string `description:"手机号"`
  14. CompanyName string `description:"公司名称"`
  15. CompanyId int `description:"公司id 不在数据库的用户为0"`
  16. CreateTime time.Time `description:"创建时间"`
  17. }
  18. func GetOfflineMeetingDetailListCompanyName(companyName string) (item []*CygxActivityMeetDetailLog, err error) {
  19. o := orm.NewOrmUsingDB("hz_cygx")
  20. sql := `SELECT * FROM cygx_activity_meet_detail_log WHERE company_name IN (` + companyName + `)`
  21. _, err = o.Raw(sql).QueryRows(&item)
  22. return
  23. }
  24. // 添加
  25. func AddCygxActivityMeetDetailLog(item *CygxActivityMeetDetailLog) (lastId int64, err error) {
  26. o := orm.NewOrmUsingDB("hz_cygx")
  27. lastId, err = o.Insert(item)
  28. return
  29. }
  30. func GetActivityMeetDetailLog(mobileStr string) (item []*CygxActivityMeetDetailLog, err error) {
  31. o := orm.NewOrmUsingDB("hz_cygx")
  32. sql := `SELECT * FROM cygx_activity_meet_detail_log WHERE mobile IN (` + mobileStr + `)`
  33. _, err = o.Raw(sql).QueryRows(&item)
  34. return
  35. }
  36. func GetActivityMeetDetailLogByMobile() (item []*CygxActivityMeetDetailLog, err error) {
  37. o := orm.NewOrmUsingDB("hz_cygx")
  38. sql := `SELECT * FROM cygx_activity_meet_detail_log WHERE mobile !='' GROUP BY mobile `
  39. _, err = o.Raw(sql).QueryRows(&item)
  40. return
  41. }
  42. // 获取数量
  43. func GetActivityMeetDetailLogCount(condition string) (count int, err error) {
  44. o := orm.NewOrmUsingDB("hz_cygx")
  45. sqlCount := ` SELECT COUNT(1) AS count FROM cygx_activity_meet_detail_log WHERE 1=1 `
  46. if condition != "" {
  47. sqlCount += condition
  48. }
  49. err = o.Raw(sqlCount).QueryRow(&count)
  50. return
  51. }
  52. // 修改公司参会数量
  53. func UpdateActivityMeetDetailLog(companyName string, num int) (err error) {
  54. sql := ` UPDATE cygx_activity_meet_detail_log SET company_meet_num= ? WHERE company_name = ?`
  55. o := orm.NewOrmUsingDB("hz_cygx")
  56. _, err = o.Raw(sql, num, companyName).Exec()
  57. return
  58. }
  59. // 修改个人参会数量
  60. func UpdateActivityMeetDetailLogByUser(mobile string, num int) (err error) {
  61. sql := ` UPDATE cygx_activity_meet_detail_log SET user_meet_num= ? WHERE mobile = ?`
  62. o := orm.NewOrmUsingDB("hz_cygx")
  63. _, err = o.Raw(sql, num, mobile).Exec()
  64. return
  65. }
  66. type MeetDetailLoggGroupCount struct {
  67. CompanyId int `description:"公司id 不在数据库的用户为0"`
  68. Mobile string `description:"手机号"`
  69. Count int `description:"数量"`
  70. }
  71. // 根据分组获取数量
  72. func GetMeetDetailLogGroupCount(condition string, pars []interface{}) (items []*MeetDetailLoggGroupCount, err error) {
  73. if condition == "" {
  74. return
  75. }
  76. o := orm.NewOrmUsingDB("hz_cygx")
  77. sql := `SELECT COUNT(1) AS count,company_id,mobile FROM cygx_activity_meet_detail_log WHERE 1=1 `
  78. if condition != "" {
  79. sql += condition
  80. }
  81. _, err = o.Raw(sql, pars).QueryRows(&items)
  82. return
  83. }
  84. // 添加线下到会记录
  85. func UpdatecygxActivityMeetDetailLogOffline(items []*CygxActivityMeetDetailLog, itemsUpdateCompany []*MeetDetailLoggGroupCount, itemsUpdateMobile []*MeetDetailLoggGroupCount, activityId int) (err error) {
  86. o := orm.NewOrmUsingDB("hz_cygx")
  87. to, err := o.Begin()
  88. if err != nil {
  89. return
  90. }
  91. defer func() {
  92. if err != nil {
  93. fmt.Println(err)
  94. _ = to.Rollback()
  95. } else {
  96. _ = to.Commit()
  97. }
  98. }()
  99. //删除原有数据
  100. sql := ` DELETE FROM cygx_activity_meet_detail_log WHERE activity_id = ?`
  101. _, err = to.Raw(sql, activityId).Exec()
  102. if err != nil {
  103. return
  104. }
  105. if len(items) > 0 {
  106. //批量添加记录
  107. _, err = to.InsertMulti(len(items), items)
  108. if err != nil {
  109. return
  110. }
  111. }
  112. return
  113. }
  114. // 添加线上到会记录
  115. func AddCygxActivityMeetDetailLogOnline(list []*CygxActivityAttendanceDetail, activityId int) (err error) {
  116. o := orm.NewOrmUsingDB("hz_cygx")
  117. to, err := o.Begin()
  118. if err != nil {
  119. return
  120. }
  121. defer func() {
  122. if err != nil {
  123. fmt.Println(err)
  124. _ = to.Rollback()
  125. } else {
  126. _ = to.Commit()
  127. }
  128. }()
  129. var CompanyName string
  130. var MobileStr string
  131. var items []*CygxActivityMeetDetailLog
  132. //删除原有数据
  133. sql := ` DELETE FROM cygx_activity_meet_detail_log WHERE activity_id = ?`
  134. _, err = to.Raw(sql, activityId).Exec()
  135. if err != nil {
  136. return
  137. }
  138. //插入新的数据
  139. for _, v := range list {
  140. item := new(CygxActivityMeetDetailLog)
  141. item.ActivityId = v.ActivityId
  142. item.Mobile = v.Mobile
  143. item.CompanyName = v.CompanyName
  144. item.CompanyId = v.CompanyId
  145. item.CreateTime = v.CreateTime
  146. CompanyName += "'" + v.CompanyName + "',"
  147. MobileStr += v.Mobile + ","
  148. items = append(items, item)
  149. }
  150. CompanyName = strings.TrimRight(CompanyName, ",")
  151. MobileStr = strings.TrimRight(MobileStr, ",")
  152. for _, v := range items {
  153. _, err = to.Insert(v)
  154. if err != nil {
  155. return
  156. }
  157. }
  158. listCompany, err := GetOfflineMeetingDetailListCompanyName(CompanyName)
  159. if err != nil {
  160. return
  161. }
  162. //修改公司对应的数量
  163. var condition string
  164. for _, v := range listCompany {
  165. var total int
  166. condition = ` AND company_name = '` + v.CompanyName + `' `
  167. total, err = GetActivityMeetDetailLogCount(condition)
  168. if err != nil {
  169. return
  170. }
  171. sql := ` UPDATE cygx_activity_meet_detail_log SET company_meet_num= ? WHERE company_name = ?`
  172. _, err = to.Raw(sql, total, v.CompanyName).Exec()
  173. if err != nil {
  174. return
  175. }
  176. }
  177. listMobile, err := GetActivityMeetDetailLog(MobileStr)
  178. if err != nil {
  179. return
  180. }
  181. //修改个人对应的数量
  182. for _, v := range listMobile {
  183. var total int
  184. condition = ` AND mobile = '` + v.Mobile + `' `
  185. total, err = GetActivityMeetDetailLogCount(condition)
  186. if err != nil {
  187. return
  188. }
  189. sql := ` UPDATE cygx_activity_meet_detail_log SET user_meet_num= ? WHERE mobile = ?`
  190. _, err = to.Raw(sql, total, v.Mobile).Exec()
  191. if err != nil {
  192. return
  193. }
  194. }
  195. return
  196. }