activity_appointment.go 6.9 KB

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