apply_record.go 12 KB

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