apply_record.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package yb
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type ApplyRecord struct {
  7. ApplyRecordId int `orm:"column(apply_record_id);pk" description:"申请记录ID"`
  8. UserId int `description:"微信用户ID"`
  9. BusinessCardUrl string `description:"名片"`
  10. RealName string `description:"用户姓名"`
  11. CompanyName string `description:"公司名称"`
  12. Mobile string `description:"手机号"`
  13. Email string `description:"邮箱"`
  14. Permission string `description:"关注品种/申请权限,多个权限用英文,隔开"`
  15. FiccSellerId int `description:"ficc销售id,没有开通ficc权限的话,那么所属销售id为0"`
  16. SellerName string `description:"销售姓名,多个产品销售用/隔开"`
  17. Status string `description:"用户状态"`
  18. CompanyIdPay int `description:"已付费客户公司id"`
  19. CompanyNamePay string `description:"已付费客户公司名称"`
  20. OpStatus int `description:"操作状态,0:未处理,1:已处理"`
  21. DealTime time.Time `description:"处理时间"`
  22. SysUserId int `description:"处理人id"`
  23. CreateTime time.Time `description:"创建时间"`
  24. IsMove int `description:"是否已移动"`
  25. Source int `description:"申请来源:1-我的 2-活动 3-图库"`
  26. MarkGroup string `description:"标记分组"`
  27. }
  28. func (applyRecord *ApplyRecord) TableName() string {
  29. return "yb_apply_record"
  30. }
  31. type ApplyList struct {
  32. ApplyRecordId int `orm:"column(apply_record_id);pk" description:"申请记录ID"`
  33. UserId int `description:"微信用户ID"`
  34. BusinessCardUrl string `description:"名片"`
  35. RealName string `description:"用户姓名"`
  36. CompanyId int `description:"现有用户公司ID/潜在用户此ID=1"`
  37. CompanyName string `description:"现有用户公司名称"`
  38. UserCompanyName string `description:"潜在客户自填公司名称"`
  39. Mobile string `description:"手机号"`
  40. Email string `description:"邮箱"`
  41. Permission string `description:"关注品种/申请权限,多个权限用英文,隔开"`
  42. SellerName string `description:"销售姓名"`
  43. Status string `description:"用户状态"`
  44. OpStatus int `description:"操作状态"`
  45. IsMove int `description:"是否已移动"`
  46. Source int `description:"申请来源 1-我的 2-活动 3-图库"`
  47. SourceAgent int `description:"申请入口来源,1:小程序,2:pc"`
  48. FromPage string `description:"申请来源具体页面"`
  49. CreateTime time.Time `description:"申请时间"`
  50. }
  51. type ApplyListV2 struct {
  52. UserId int `description:"微信用户ID"`
  53. BusinessCardUrl string `description:"名片"`
  54. RealName string `description:"用户姓名"`
  55. CompanyId int `description:"现有用户公司ID/潜在用户此ID=1"`
  56. CompanyName string `description:"现有用户公司名称"`
  57. Mobile string `description:"手机号"`
  58. Email string `description:"邮箱"`
  59. Permission string `description:"关注品种/申请权限,多个权限用英文,隔开"`
  60. SellerName string `description:"现销售姓名"`
  61. SellerId int `description:"现销售ID"`
  62. Status string `description:"用户状态"`
  63. OpStatus int `description:"操作状态"`
  64. IsMove int `description:"是否已移动"`
  65. Source int `description:"申请来源 1-我的 2-活动 3-图库"`
  66. SourceAgent int `description:"申请入口来源,1:小程序,2:pc"`
  67. FromPage string `description:"申请来源具体页面`
  68. ApplyStatus string `description: 申请状态:已申请,未申请`
  69. LastTime time.Time `description:"最新提交时间"`
  70. LastTimeStr string `description:"最新提交时间"`
  71. ViewTotal int `description:"累计阅读次数"`
  72. ApplyTotal int `description:"累计提交次数"`
  73. LastViewTimeStr string `description:"最近一次阅读时间"`
  74. LastViewTime time.Time `description:"最近一次阅读时间"`
  75. ApplyRecordId int `description:"申请记录ID"`
  76. OriginSellerName string `description:"原销售姓名"`
  77. RegisterSource int `description:"注册来源"`
  78. SourceStr string `description:"来源"`
  79. DelBtn bool `description:"是否展示删除按钮,true展示,false隐藏"`
  80. MarkGroup string `description:"标记分组"`
  81. }
  82. // GetApplyRecordList 获取申请记录列表
  83. func GetApplyRecordList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*ApplyList, err error) {
  84. o := orm.NewOrm()
  85. fields := "a.apply_record_id, a.user_id, a.real_name, a.business_card_url, a.company_name AS user_company_name," +
  86. " a.mobile, a.email, a.permission, a.seller_name, a.op_status, a.create_time, a.status, a.is_move, a.source, a.source_agent, a.from_page," +
  87. " b.company_id, b.company_name"
  88. sql := "SELECT " + fields + " FROM yb_apply_record AS a LEFT JOIN company_product AS b ON a.company_id_pay = b.company_id AND b.product_id = 1 WHERE 1=1"
  89. sql += condition
  90. sql += ` ORDER BY a.create_time desc`
  91. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z `
  92. err = o.Raw(totalSql, pars).QueryRow(&total)
  93. sql += ` LIMIT ?,?`
  94. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  95. return
  96. }
  97. // Update 更新申请记录
  98. func (applyRecord *ApplyRecord) Update(cols []string) (err error) {
  99. o := orm.NewOrm()
  100. _, err = o.Update(applyRecord, cols...)
  101. return
  102. }
  103. // GetApplyRecordById 获取申请记录
  104. func GetApplyRecordById(applyRecordId int) (item *ApplyRecord, err error) {
  105. o := orm.NewOrm()
  106. sql := "select * from yb_apply_record where apply_record_id = ?"
  107. err = o.Raw(sql, applyRecordId).QueryRow(&item)
  108. return
  109. }
  110. // DealUnDealFiccApplyByIds 批量处理用户申请标记状态
  111. func DealUnDealFiccApplyByIds(userId, sysUserId int, dealTime time.Time) (err error) {
  112. sql := `update yb_apply_record set op_status=1, is_move=1, sys_user_id=?, deal_time=? where user_id=? and (op_status = 0 or is_move=0)`
  113. _, err = orm.NewOrm().Raw(sql, sysUserId, dealTime, userId).Exec()
  114. return
  115. }
  116. // DealUserApplyRecords 标记处理用户所有待处理申请记录
  117. func DealUserApplyRecords(userId, sysUserId int, dealTime time.Time) (err error) {
  118. o := orm.NewOrm()
  119. sql := `UPDATE yb_apply_record SET op_status=1, sys_user_id=?, deal_time=? WHERE op_status=0 AND user_id=?`
  120. _, err = o.Raw(sql, sysUserId, dealTime, userId).Exec()
  121. return
  122. }
  123. // GetApplyRecordListV2 获取用户申请记录列表
  124. func GetApplyRecordListV2(condition string, pars []interface{}, startTime string, startSize, pageSize int) (list []*ApplyListV2, err error) {
  125. o := orm.NewOrm()
  126. sql := `
  127. SELECT
  128. a.user_id,
  129. a.real_name,
  130. a.mobile,
  131. a.email,
  132. a.company_id,
  133. IF(a.company_id > 1,b.company_name,a.note) AS company_name,
  134. #(SELECT count(1) FROM yb_apply_record AS ya WHERE ya.user_id=a.user_id GROUP BY a.user_id) AS apply_total,
  135. a.report_last_view_time AS last_view_time,
  136. a.source as register_source,
  137. #a.created_time as register_time,
  138. y.apply_record_id,
  139. IF(y.apply_record_id > 0,y.create_time, a.created_time) as last_time,
  140. y.seller_name as origin_seller_name,
  141. IF(y.apply_record_id > 0,y.status, "潜在用户") as status,
  142. IF(y.apply_record_id > 0,"已申请", "未申请") as apply_status,
  143. y.source_agent,
  144. IF(y.apply_record_id > 0,y.op_status, a.is_deal) as op_status,
  145. y.permission,
  146. y.is_move,
  147. y.source,
  148. y.from_page,
  149. IF(y.mark_group = '' OR y.mark_group IS NULL OR y.apply_record_id = 0 OR y.apply_record_id IS NULL , a.mark_group, y.mark_group) AS mark_group,
  150. y.company_name as user_company_name
  151. #bp.seller_id,
  152. #bp.seller_name
  153. FROM
  154. wx_user AS a
  155. LEFT JOIN company AS b ON a.company_id = b.company_id
  156. LEFT JOIN (SELECT * from company_product where product_id = 1) AS bp ON a.company_id = bp.company_id
  157. LEFT JOIN (SELECT user_record_id, create_platform, user_id from user_record) AS c ON a.user_id = c.user_id
  158. LEFT JOIN (SELECT * from yb_apply_record where apply_record_id in (SELECT max(apply_record_id) from yb_apply_record GROUP BY user_id)) as y on a.user_id = y.user_id
  159. WHERE
  160. b.enabled = 1
  161. AND ( y.apply_record_id > 0 or a.company_id=1 )
  162. AND ( a.mobile IS NOT NULL || a.email IS NOT NULL )
  163. AND ( a.mobile <> '' OR a.email <> '' )
  164. AND ( c.create_platform in (1,3,6,9) OR (c.create_platform IS NULL and a.register_platform!=7) )
  165. AND ((y.apply_record_id > 0 and y.create_time > ?) OR (y.apply_record_id is null AND a.created_time > ?) )
  166. AND ( y.source > 0 OR y.source IS NULL)
  167. `
  168. sql += condition
  169. sql += ` GROUP BY a.user_id ORDER BY last_time desc`
  170. sql += ` LIMIT ?,?`
  171. //pars = append(pars, startTime)
  172. //pars = append(pars, startTime)
  173. _, err = o.Raw(sql, startTime, startTime, pars, startSize, pageSize).QueryRows(&list)
  174. return
  175. }
  176. func GetApplyRecordTotal(condition string, pars []interface{}, startTime string) (total int, err error) {
  177. o := orm.NewOrm()
  178. sql := `
  179. SELECT
  180. a.user_id
  181. FROM
  182. wx_user AS a
  183. LEFT JOIN company AS b ON a.company_id = b.company_id
  184. LEFT JOIN (SELECT * from company_product where product_id = 1) AS bp ON a.company_id = bp.company_id
  185. LEFT JOIN (SELECT user_record_id, create_platform, user_id from user_record) AS c ON a.user_id = c.user_id
  186. LEFT JOIN (SELECT * from yb_apply_record where apply_record_id in (SELECT max(apply_record_id) from yb_apply_record GROUP BY user_id)) as y on a.user_id = y.user_id
  187. WHERE
  188. b.enabled = 1
  189. AND ( y.apply_record_id > 0 or a.company_id=1)
  190. AND ( a.mobile IS NOT NULL || a.email IS NOT NULL )
  191. AND ( a.mobile <> '' OR a.email <> '' )
  192. AND ( c.create_platform in (1,3,6,9) OR (c.create_platform IS NULL and a.register_platform!=7) )
  193. AND ((y.apply_record_id > 0 and y.create_time > ?) OR (y.apply_record_id is null AND a.created_time > ?) )
  194. AND ( y.source > 0 OR y.source IS NULL)
  195. `
  196. pars = append(pars, startTime)
  197. pars = append(pars, startTime)
  198. sql += condition
  199. sql += ` GROUP BY a.user_id`
  200. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z `
  201. err = o.Raw(totalSql, pars).QueryRow(&total)
  202. return
  203. }
  204. // GetApplyRecordListV2Export 用户申请记录列表导出
  205. func GetApplyRecordListV2Export(condition string, pars []interface{}, startTime string) (list []*ApplyListV2, err error) {
  206. o := orm.NewOrm()
  207. sql := `
  208. SELECT
  209. a.user_id,
  210. a.real_name,
  211. a.mobile,
  212. a.email,
  213. a.company_id,
  214. IF(a.company_id > 1,b.company_name,a.note) AS company_name,
  215. # IF( b.type IN ( 1, 2 ), 1, 0 ) AS is_fee_customer,
  216. # (SELECT count(1) FROM yb_apply_record AS ya WHERE ya.user_id=a.user_id GROUP BY a.user_id) AS apply_total,
  217. a.report_last_view_time AS last_view_time,
  218. a.source as register_source,
  219. #a.created_time as register_time,
  220. y.apply_record_id,
  221. IF(y.apply_record_id > 0,y.create_time, a.created_time) as last_time,
  222. y.seller_name as origin_seller_name,
  223. IF(y.apply_record_id > 0,y.status, "潜在用户") as status,
  224. IF(y.apply_record_id > 0,"已申请", "未申请") as apply_status,
  225. y.source_agent,
  226. y.op_status,
  227. y.permission,
  228. y.is_move,
  229. y.source,
  230. y.from_page,
  231. y.company_name as user_company_name
  232. #bp.seller_id,
  233. #bp.seller_name
  234. FROM
  235. wx_user AS a
  236. LEFT JOIN company AS b ON a.company_id = b.company_id
  237. LEFT JOIN (SELECT * from company_product where product_id = 1) AS bp ON a.company_id = bp.company_id
  238. LEFT JOIN (SELECT user_record_id, create_platform, user_id from user_record) AS c ON a.user_id = c.user_id
  239. LEFT JOIN (SELECT * from yb_apply_record where apply_record_id in (SELECT max(apply_record_id) from yb_apply_record GROUP BY user_id)) as y on a.user_id = y.user_id
  240. WHERE
  241. b.enabled = 1
  242. AND ( y.apply_record_id > 0 or a.company_id=1)
  243. AND ( a.mobile IS NOT NULL || a.email IS NOT NULL )
  244. AND ( a.mobile <> '' OR a.email <> '' )
  245. AND ( c.create_platform in (1,3,6,9) OR (c.create_platform IS NULL and a.register_platform!=7) )
  246. AND ((y.apply_record_id > 0 and y.create_time > ?) OR (y.apply_record_id is null AND a.created_time > ?) )
  247. AND ( y.source > 0 OR y.source IS NULL)
  248. `
  249. sql += condition
  250. sql += ` GROUP BY a.user_id ORDER BY last_time desc`
  251. //pars = append(pars, startTime)
  252. //pars = append(pars, startTime)
  253. _, err = o.Raw(sql, startTime, startTime, pars).QueryRows(&list)
  254. return
  255. }
  256. type UserApplyTotal struct {
  257. UserId int
  258. Total int
  259. }
  260. // GetTotalByUserIds 获取用户的申请数
  261. func GetTotalByUserIds(userIdsStr string) (list []*UserApplyTotal, err error) {
  262. o := orm.NewOrm()
  263. sql := "SELECT count(*) as total, user_id FROM yb_apply_record WHERE user_id in (" + userIdsStr + ") GROUP BY user_id"
  264. _, err = o.Raw(sql).QueryRows(&list)
  265. return
  266. }
  267. // DelApplyRecordByUserId 根据用户id删除该用户的所有申请
  268. func DelApplyRecordByUserId(userId int64) (err error) {
  269. o := orm.NewOrm()
  270. sql := ` DELETE FROM yb_apply_record WHERE user_id=? `
  271. _, err = o.Raw(sql, userId).Exec()
  272. return
  273. }