activity_appointment.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package cygx
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. )
  7. type CygxActivityAppointment struct {
  8. Id int `orm:"column(id);pk"`
  9. ActivityId int `description:"活动ID"`
  10. UserId int `description:"用户ID"`
  11. AdminId int `description:"管理员ID"`
  12. CreateTime time.Time `description:"创建时间"`
  13. Mobile string `description:"手机号"`
  14. Email string `description:"邮箱"`
  15. CompanyId int `description:"公司id"`
  16. CompanyName string `description:"公司名称"`
  17. RealName string `description:"用户实际名称"`
  18. SellerName string `description:"所属销售"`
  19. Source int `description:"来源,1小程序,2后台添加"`
  20. }
  21. // 取消纪要预约
  22. func CancelcygxActivityAppointment(item *CygxActivityAppointment) (lastId int64, err error) {
  23. o, err := orm.NewOrmUsingDB("hz_cygx").Begin()
  24. if err != nil {
  25. return
  26. }
  27. defer func() {
  28. fmt.Println(err)
  29. if err == nil {
  30. o.Commit()
  31. } else {
  32. o.Rollback()
  33. }
  34. }()
  35. //判断是否删除我的日程
  36. var countSingup int
  37. var countReminder int
  38. sql := `SELECT COUNT(1) AS count FROM cygx_activity_signup WHERE is_cancel = 0 AND user_id=? AND activity_id=? `
  39. err = o.Raw(sql, item.UserId, item.ActivityId).QueryRow(&countSingup)
  40. sql = `SELECT COUNT(1) AS count FROM cygx_activity_meeting_reminder WHERE user_id=? AND activity_id=? `
  41. err = o.Raw(sql, item.UserId, item.ActivityId).QueryRow(&countReminder)
  42. if err != nil {
  43. return
  44. }
  45. if countSingup == 0 && countReminder == 0 {
  46. sql = `DELETE FROM cygx_my_schedule WHERE user_id=? AND activity_id=? `
  47. _, err = o.Raw(sql, item.UserId, item.ActivityId).Exec()
  48. if err != nil {
  49. return
  50. }
  51. }
  52. //删除预约的纪要
  53. sql = `DELETE FROM cygx_activity_appointment WHERE user_id=? AND activity_id=? `
  54. _, err = o.Raw(sql, item.UserId, item.ActivityId).Exec()
  55. if err != nil {
  56. return
  57. }
  58. return
  59. }
  60. // 预约纪要的人数列表
  61. func GetCygxAppointmentSummaryList(activityId int, sqlStr string) (item []*CygxAppointmentList, err error) {
  62. o := orm.NewOrmUsingDB("hz_cygx")
  63. sql := `SELECT s.*,a.activity_time
  64. FROM
  65. cygx_activity_appointment AS s
  66. LEFT JOIN cygx_activity AS a ON a.activity_id = s.activity_id
  67. WHERE a.activity_id = ? ` + sqlStr + ` GROUP BY s.user_id ORDER BY s.create_time DESC `
  68. _, err = o.Raw(sql, activityId).QueryRows(&item)
  69. return
  70. }
  71. // 预约纪要的人数数量
  72. func GetCygxAppointmentSummaryCount(activityId int, sqlStr string) (count int, err error) {
  73. o := orm.NewOrmUsingDB("hz_cygx")
  74. sql := `SELECT COUNT(*) FROM
  75. cygx_activity_appointment AS s
  76. LEFT JOIN cygx_activity AS a ON a.activity_id = s.activity_id
  77. WHERE
  78. s.activity_id = ? ` + sqlStr + `
  79. GROUP BY s.user_id)`
  80. sql = `SELECT COUNT(*) as count FROM (` + sql + ` a`
  81. err = o.Raw(sql, activityId).QueryRow(&count)
  82. return
  83. }
  84. // 通过ID获取预约纪要详情
  85. func GetCygxAppointmentSummaryInfoById(id int) (item *CygxActivityAppointment, err error) {
  86. o := orm.NewOrmUsingDB("hz_cygx")
  87. sql := `SELECT * FROM cygx_activity_appointment WHERE id=?`
  88. err = o.Raw(sql, id).QueryRow(&item)
  89. return
  90. }
  91. // 获取某一用户的报名的数量
  92. func GetUserCygxActivityAppointmentCount(uid, activityId int) (count int, err error) {
  93. sqlCount := `SELECT COUNT(1) AS count FROM cygx_activity_appointment WHERE user_id=? AND activity_id =? `
  94. o := orm.NewOrmUsingDB("hz_cygx")
  95. err = o.Raw(sqlCount, uid, activityId).QueryRow(&count)
  96. return
  97. }
  98. // 添加预约纪要
  99. func AddCygxActivityAppointmentUser(items []*CygxActivityAppointment) (err error) {
  100. o := orm.NewOrmUsingDB("hz_cygx")
  101. to, err := o.Begin()
  102. if err != nil {
  103. return
  104. }
  105. defer func() {
  106. if err != nil {
  107. fmt.Println(err)
  108. _ = to.Rollback()
  109. } else {
  110. _ = to.Commit()
  111. }
  112. }()
  113. for _, item := range items {
  114. var count int
  115. sql := `SELECT COUNT(1) AS count FROM cygx_my_schedule WHERE user_id=? AND activity_id=? `
  116. err = to.Raw(sql, item.UserId, item.ActivityId).QueryRow(&count)
  117. if err != nil {
  118. return
  119. }
  120. if count == 0 {
  121. itemMy := new(CygxMySchedule)
  122. itemMy.UserId = item.UserId
  123. itemMy.ActivityId = item.ActivityId
  124. itemMy.CreateTime = time.Now()
  125. itemMy.Mobile = item.Mobile
  126. itemMy.Email = item.Email
  127. itemMy.CompanyId = item.CompanyId
  128. itemMy.CompanyName = item.CompanyName
  129. _, err = to.Insert(itemMy)
  130. if err != nil {
  131. return
  132. }
  133. }
  134. sql = `SELECT COUNT(1) AS count FROM cygx_activity_appointment WHERE user_id=? AND activity_id=? `
  135. err = to.Raw(sql, item.UserId, item.ActivityId).QueryRow(&count)
  136. if err != nil {
  137. return
  138. }
  139. if count == 0 {
  140. _, err = to.Insert(item)
  141. }
  142. }
  143. return
  144. }
  145. type CygxAppointmentMobileList struct {
  146. ActivityId int `description:"活动ID"`
  147. Mobile string `description:"手机号"`
  148. ActivityName string `description:"活动名称"`
  149. }
  150. // 通过活动ID获取预约纪要的人数列表
  151. func GetCygxAppointmentSummaryListBySubjectId(subjectIds string) (item []*CygxAppointmentMobileList, err error) {
  152. o := orm.NewOrmUsingDB("hz_cygx")
  153. sql := `SELECT
  154. ap.mobile,
  155. ap.activity_id,
  156. a.activity_name
  157. FROM
  158. cygx_activity_appointment AS ap
  159. INNER JOIN cygx_activity AS a ON a.activity_id = ap.activity_id
  160. INNER JOIN cygx_industrial_activity_group_subject AS sg ON sg.activity_id = a.activity_id
  161. WHERE
  162. a.active_state = 3
  163. AND a.chart_permission_id = 31
  164. AND DATE_SUB( CURDATE(), INTERVAL 14 DAY ) <= date( a.activity_time )
  165. AND a.activity_type_id = 1
  166. AND sg.industrial_subject_id IN (` + subjectIds + `)
  167. GROUP BY
  168. ap.mobile,
  169. ap.activity_id `
  170. _, err = o.Raw(sql).QueryRows(&item)
  171. return
  172. }
  173. // 获取预约活动纪要的的用户的openID
  174. func GetActivityAppointmentOpenIdList(activityId int) (items []*OpenIdList, err error) {
  175. o := orm.NewOrmUsingDB("hz_cygx")
  176. var sql string
  177. sql = `SELECT DISTINCT
  178. u.open_id,
  179. u.cygx_user_id AS user_id
  180. FROM
  181. cygx_activity_appointment AS m
  182. INNER JOIN cygx_user_record AS u ON u.cygx_bind_account = m.mobile
  183. AND u.cygx_user_id = m.user_id
  184. WHERE
  185. m.activity_id = ? `
  186. _, err = o.Raw(sql, activityId).QueryRows(&items)
  187. return
  188. }
  189. // 获取预约活动纪要的的用户的openID
  190. func GetMfyxActivityAppointmentOpenIdList(activityId int) (items []*OpenIdList, err error) {
  191. o := orm.NewOrmUsingDB("hz_cygx")
  192. var sql string
  193. sql = `SELECT DISTINCT
  194. u.open_id,
  195. u.cygx_user_id AS user_id
  196. FROM
  197. cygx_activity_appointment AS m
  198. INNER JOIN cygx_mfyx_gzh_user_record AS u ON u.cygx_bind_account = m.mobile
  199. AND u.cygx_user_id = m.user_id
  200. WHERE
  201. m.activity_id = ? `
  202. _, err = o.Raw(sql, activityId).QueryRows(&items)
  203. return
  204. }