custom.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package overseas_custom
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "hongze/hz_crm_api/utils"
  7. )
  8. type Custom struct {
  9. CompanyId int `description:"客户id"`
  10. CompanyName string `description:"客户名称"`
  11. Nation string `description:"国家"`
  12. SellerId int `description:"销售id"`
  13. SellerName string `description:"销售名称"`
  14. CompanyStatus string `description:"状态"`
  15. ViewTotal int `description:"累计点击量"`
  16. RoadShowTotal int `description:"路演数量"`
  17. LastViewTime string `description:"最近阅读时间"`
  18. CreateTime string `description:"创建时间"`
  19. IsHide int `description:"是否隐藏:0:不隐藏,1:隐藏"`
  20. OverseasStatus string `description:"海外客户状态:'正式','试用','关闭'"`
  21. Source int `description:"来源:1,英文客户,2:客户列表"`
  22. OverseasLabel int `description:"海外客户试用子标签:1未分类、2 推进、3 跟踪、4 预备、"`
  23. ResetBtn int `description:"转正式和重置按钮:同步过来默认为0:显示转正式为1:显示重置为2"`
  24. Status string `description:"客户状态"`
  25. }
  26. type CustomTotal struct {
  27. CompanyStatus string `description:"状态"`
  28. Total int `description:"总数"`
  29. }
  30. func (obj *Custom) GetCustomTotal(condition string, pars []interface{}) (list []*CustomTotal, err error) {
  31. o := orm.NewOrm()
  32. sql := ``
  33. var databaseName string
  34. if utils.RunMode == "debug" {
  35. databaseName = "test_v2_hongze_rddp"
  36. } else {
  37. databaseName = "hongze_rddp"
  38. }
  39. sql = `SELECT overseas_status AS company_status,COUNT(1) AS total,is_hide FROM (
  40. SELECT a.company_id,a.company_name,a.nation,b.seller_id,b.seller_name,
  41. b.view_total,b.road_show_total,a.created_time AS create_time,b.last_view_time,a.is_hide,a.overseas_status,b.overseas_label
  42. FROM company AS a
  43. INNER JOIN company_product AS b ON a.company_id=b.company_id
  44. INNER JOIN overseas_custom_seller AS c ON b.seller_id=c.seller_id
  45. WHERE b.is_overseas = 0
  46. UNION ALL
  47. SELECT t.company_id+10000000,t.company_name,t.nation,t.seller_id,t.seller_name,
  48. t.view_total,0 AS road_show_total,t.create_time,'' AS last_view_time,t.is_hide,t.overseas_status,t.overseas_label
  49. FROM %s.english_company AS t
  50. INNER JOIN overseas_custom_seller AS n ON t.seller_id=n.seller_id
  51. WHERE 1=1 AND t.is_deleted=0
  52. )AS m
  53. WHERE 1=1
  54. `
  55. if condition != "" {
  56. sql += condition
  57. }
  58. sql += ` GROUP BY m.overseas_status `
  59. sql = fmt.Sprintf(sql, databaseName)
  60. _, err = o.Raw(sql, pars).QueryRows(&list)
  61. return
  62. }
  63. //
  64. //func (obj *Custom) GetCustomOverseasStatusTotal(condition string, pars []interface{}) (total int, err error) {
  65. // o := orm.NewOrm()
  66. // sql := ``
  67. // var databaseName string
  68. // if utils.RunMode == "debug" {
  69. // databaseName = "test_v2_hongze_rddp"
  70. // } else {
  71. // databaseName = "hongze_rddp"
  72. // }
  73. //
  74. // sql = `SELECT COUNT(1) AS total FROM (
  75. // SELECT a.company_id,a.company_name,a.nation,b.seller_id,b.seller_name,
  76. // b.view_total,b.road_show_total,a.created_time AS create_time,b.last_view_time,a.is_hide,a.overseas_status,b.overseas_label
  77. // FROM company AS a
  78. // INNER JOIN company_product AS b ON a.company_id=b.company_id
  79. // INNER JOIN overseas_custom_seller AS c ON b.seller_id=c.seller_id
  80. // WHERE b.is_overseas = 0
  81. // UNION ALL
  82. // SELECT t.company_id+10000000,t.company_name,t.nation,t.seller_id,t.seller_name,
  83. // t.view_total,0 AS road_show_total,t.create_time,'' AS last_view_time,t.is_hide,t.overseas_status,t.overseas_label
  84. // FROM %s.english_company AS t
  85. // INNER JOIN overseas_custom_seller AS n ON t.seller_id=n.seller_id
  86. // WHERE 1=1 AND t.is_deleted=0
  87. // )AS m
  88. // WHERE 1=1
  89. // AND m.is_hide=0
  90. //AND m. overseas_status='正式' `
  91. //
  92. // if condition != "" {
  93. // sql += condition
  94. // }
  95. //
  96. // sql += ` GROUP BY m.overseas_status `
  97. //
  98. // sql = fmt.Sprintf(sql, databaseName)
  99. //
  100. // err = o.Raw(sql, pars).QueryRow(&total)
  101. // return
  102. //}
  103. // GetCompanyIdsByKeyword 关键词获取客户IDs
  104. func (obj *Custom) GetCompanyIdsByKeyword(keyword string) (companyIds []int, err error) {
  105. o := orm.NewOrm()
  106. sql := ` SELECT DISTINCT b.company_id FROM wx_user AS a
  107. INNER JOIN company AS b ON a.company_id=b.company_id
  108. WHERE 1=1 AND (a.email LIKE ? OR a.mobile LIKE ? OR b.company_name LIKE ? OR b.credit_code LIKE ? ) `
  109. _, err = o.Raw(sql, keyword, keyword, keyword, keyword).QueryRows(&companyIds)
  110. return
  111. }
  112. func (obj *Custom) GetCustomListCount(condition string, pars []interface{}) (count int, err error) {
  113. o := orm.NewOrm()
  114. sql := ``
  115. var databaseName string
  116. if utils.RunMode == "debug" {
  117. databaseName = "test_v2_hongze_rddp"
  118. } else {
  119. databaseName = "hongze_rddp"
  120. }
  121. sql = `SELECT COUNT(1) AS count FROM (
  122. SELECT a.company_id,a.company_name,a.nation,b.seller_id,b.seller_name,
  123. b.view_total,b.road_show_total,a.created_time AS create_time,b.last_view_time,a.is_hide,a.overseas_status,b.overseas_label
  124. FROM company AS a
  125. INNER JOIN company_product AS b ON a.company_id=b.company_id
  126. INNER JOIN overseas_custom_seller AS c ON b.seller_id=c.seller_id
  127. WHERE b.is_overseas = 0`
  128. sql += `
  129. UNION ALL
  130. SELECT t.company_id+10000000,t.company_name,t.nation,t.seller_id,t.seller_name,
  131. t.view_total,0 AS road_show_total,t.create_time,'' AS last_view_time,t.is_hide,t.overseas_status,t.overseas_label
  132. FROM %s.english_company AS t
  133. INNER JOIN overseas_custom_seller AS n ON t.seller_id=n.seller_id
  134. WHERE 1=1 AND t.is_deleted=0 `
  135. sql += ` )AS m
  136. WHERE 1=1 `
  137. if condition != "" {
  138. sql += condition
  139. }
  140. sql = fmt.Sprintf(sql, databaseName)
  141. err = o.Raw(sql, pars).QueryRow(&count)
  142. return
  143. }
  144. func (obj *Custom) GetCustomList(condition string, pars []interface{}, sortField string, startSize, pageSize, sortDesc int) (list []*Custom, err error) {
  145. o := orm.NewOrm()
  146. sql := ``
  147. var databaseName string
  148. if utils.RunMode == "debug" {
  149. databaseName = "test_v2_hongze_rddp"
  150. } else {
  151. databaseName = "hongze_rddp"
  152. }
  153. sql = `SELECT * FROM (
  154. SELECT a.company_id,a.company_name,a.nation,b.seller_id,b.seller_name,
  155. b.view_total,b.road_show_total,a.created_time AS create_time,b.last_view_time,a.is_hide,a.overseas_status,
  156. 2 AS source,b.overseas_label,a.reset_btn,b.status
  157. FROM company AS a
  158. INNER JOIN company_product AS b ON a.company_id=b.company_id
  159. INNER JOIN overseas_custom_seller AS c ON b.seller_id=c.seller_id
  160. WHERE b.product_id=1 AND b.is_overseas = 0`
  161. sql += `
  162. UNION ALL
  163. SELECT t.company_id+10000000,t.company_name,t.nation,t.seller_id,t.seller_name,
  164. t.view_total,t.road_show_total,t.create_time,t.last_view_time,t.is_hide,t.overseas_status,
  165. 1 AS source,t.overseas_label,t.reset_btn,CASE t.enabled WHEN 0 THEN '关闭' WHEN 1 THEN '试用' WHEN 2 THEN '试用' END status
  166. FROM %s.english_company AS t
  167. INNER JOIN overseas_custom_seller AS n ON t.seller_id=n.seller_id
  168. WHERE 1=1 AND t.is_deleted=0 `
  169. sql += ` )AS m WHERE 1=1 `
  170. if condition != "" {
  171. sql += condition
  172. }
  173. var sortFieldStr, sortDescStr string
  174. if sortField != "" {
  175. if sortField == "ViewTotal" {
  176. sortFieldStr = "view_total"
  177. } else if sortField == "RoadShowTotal" {
  178. sortFieldStr = "road_show_total"
  179. } else if sortField == "CreateTime" {
  180. sortFieldStr = "create_time"
  181. } else if sortField == "LastViewTime" {
  182. sortFieldStr = "last_view_time"
  183. }
  184. }
  185. if sortDesc == 1 {
  186. sortDescStr = "DESC"
  187. } else {
  188. sortDescStr = "ASC"
  189. }
  190. sql += ` ORDER BY %s %s,m.create_time DESC`
  191. sql += ` LIMIT ?,? `
  192. sql = fmt.Sprintf(sql, databaseName, sortFieldStr, sortDescStr)
  193. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  194. return
  195. }
  196. type CustomListResp struct {
  197. Paging *paging.PagingItem
  198. List []*Custom
  199. }
  200. type CustomHideReq struct {
  201. CompanyId int `description:"客户ID"`
  202. }
  203. func (obj *Custom) GetCustomByCompanyId(companyId int) (item *Custom, err error) {
  204. o := orm.NewOrm()
  205. sql := ``
  206. var databaseName string
  207. if utils.RunMode == "debug" {
  208. databaseName = "test_v2_hongze_rddp"
  209. } else {
  210. databaseName = "hongze_rddp"
  211. }
  212. //if companyId > utils.EnCompanyIdStep {
  213. // companyId = companyId - utils.EnCompanyIdStep
  214. //}
  215. sql = `SELECT * FROM (
  216. SELECT a.company_id,a.company_name,a.nation,b.seller_id,b.seller_name,
  217. b.view_total,b.road_show_total,a.created_time AS create_time,b.last_view_time,a.is_hide,a.overseas_status,a.reset_btn,
  218. CASE b.status
  219. WHEN '正式' THEN '正式'
  220. WHEN '永续' THEN '正式'
  221. WHEN '试用' THEN '试用'
  222. ELSE '关闭' END AS company_status
  223. FROM company AS a
  224. INNER JOIN company_product AS b ON a.company_id=b.company_id
  225. INNER JOIN overseas_custom_seller AS c ON b.seller_id=c.seller_id
  226. WHERE b.is_overseas = 0`
  227. sql += ` UNION ALL
  228. SELECT t.company_id+10000000,t.company_name,t.nation,t.seller_id,t.seller_name,
  229. t.view_total,0 AS road_show_total,t.create_time,'' AS last_view_time,t.is_hide,t.overseas_status,t.reset_btn,
  230. CASE t.enabled
  231. WHEN 0 THEN '关闭'
  232. ELSE '试用' END AS company_status
  233. FROM %s.english_company AS t
  234. INNER JOIN overseas_custom_seller AS n ON t.seller_id=n.seller_id
  235. WHERE 1=1 `
  236. sql += ` )AS m WHERE 1=1 `
  237. sql += ` AND m.company_id=? `
  238. sql = fmt.Sprintf(sql, databaseName)
  239. err = o.Raw(sql, companyId).QueryRow(&item)
  240. return
  241. }
  242. type OverseasLabelTotal struct {
  243. OverseasLabel int `description:"海外客户试用子标签:1未分类、2 推进、3 跟踪、4 预备、"`
  244. Total int `description:"总数"`
  245. }
  246. func (obj *Custom) GetCustomOverseasLabelTotal(condition string, pars []interface{}) (list []*OverseasLabelTotal, err error) {
  247. o := orm.NewOrm()
  248. sql := ``
  249. var databaseName string
  250. if utils.RunMode == "debug" {
  251. databaseName = "test_v2_hongze_rddp"
  252. } else {
  253. databaseName = "hongze_rddp"
  254. }
  255. sql = `SELECT overseas_label,COUNT(1) AS total FROM (
  256. SELECT a.company_id,a.company_name,a.nation,b.seller_id,b.seller_name,
  257. b.view_total,b.road_show_total,a.created_time AS create_time,b.last_view_time,a.is_hide,a.overseas_status,b.overseas_label
  258. FROM company AS a
  259. INNER JOIN company_product AS b ON a.company_id=b.company_id
  260. INNER JOIN overseas_custom_seller AS c ON b.seller_id=c.seller_id
  261. WHERE b.is_overseas = 0
  262. UNION ALL
  263. SELECT t.company_id+10000000,t.company_name,t.nation,t.seller_id,t.seller_name,
  264. t.view_total,0 AS road_show_total,t.create_time,'' AS last_view_time,t.is_hide,t.overseas_status,t.overseas_label
  265. FROM %s.english_company AS t
  266. INNER JOIN overseas_custom_seller AS n ON t.seller_id=n.seller_id
  267. WHERE 1=1 AND t.is_deleted=0
  268. )AS m
  269. WHERE 1=1
  270. `
  271. if condition != "" {
  272. sql += condition
  273. }
  274. sql += ` GROUP BY m.overseas_label `
  275. sql = fmt.Sprintf(sql, databaseName)
  276. _, err = o.Raw(sql, pars).QueryRows(&list)
  277. return
  278. }
  279. type CustomOverseasLabelSetReq struct {
  280. CompanyId int `description:"客户ID"`
  281. OverseasLabel int `description:"海外客户试用子标签:1未分类、2 推进、3 跟踪、4 预备、"`
  282. }
  283. // 更新客户产品信息
  284. func (obj *Custom) UpdateCompanyProduct(updateParams, whereParam map[string]interface{}) (err error) {
  285. to := orm.NewOrm()
  286. ptrStructOrTableName := "company_product"
  287. qs := to.QueryTable(ptrStructOrTableName)
  288. for expr, exprV := range whereParam {
  289. qs = qs.Filter(expr, exprV)
  290. }
  291. _, err = qs.Update(updateParams)
  292. return
  293. }
  294. // 更新客户产品信息
  295. func (obj *Custom) UpdateCompany(updateParams, whereParam map[string]interface{}) (err error) {
  296. to := orm.NewOrm()
  297. ptrStructOrTableName := "company"
  298. qs := to.QueryTable(ptrStructOrTableName)
  299. for expr, exprV := range whereParam {
  300. qs = qs.Filter(expr, exprV)
  301. }
  302. _, err = qs.Update(updateParams)
  303. return
  304. }
  305. // GetEnCompanyIdsByKeyword 关键词获取英文客户IDs
  306. func GetEnCompanyIdsByKeyword(keyword string) (companyIds []int, err error) {
  307. o := orm.NewOrmUsingDB("rddp")
  308. sql := ` SELECT (a.company_id+10000000) AS company_id
  309. FROM english_company AS a
  310. LEFT JOIN english_report_email AS b ON a.company_id=b.company_id
  311. WHERE a.is_deleted=0
  312. AND ( a.company_name LIKE ? OR a.country_code LIKE ? OR b.email LIKE ? OR b.mobile LIKE ?)
  313. GROUP BY a.company_id `
  314. _, err = o.Raw(sql, keyword, keyword, keyword, keyword).QueryRows(&companyIds)
  315. return
  316. }
  317. func GetOverseasCustomList(condition string, pars []interface{}) (list []*Custom, err error) {
  318. o := orm.NewOrm()
  319. sql := ``
  320. var databaseName string
  321. if utils.RunMode == "debug" {
  322. databaseName = "test_v2_hongze_rddp"
  323. } else {
  324. databaseName = "hongze_rddp"
  325. }
  326. sql = `SELECT * FROM (
  327. SELECT a.company_id,a.company_name,a.nation,b.seller_id,b.seller_name,
  328. b.view_total,b.road_show_total,a.created_time AS create_time,b.last_view_time,a.is_hide,a.overseas_status,
  329. 2 AS source,b.overseas_label,a.reset_btn
  330. FROM company AS a
  331. INNER JOIN company_product AS b ON a.company_id=b.company_id
  332. INNER JOIN overseas_custom_seller AS c ON b.seller_id=c.seller_id
  333. WHERE b.product_id=1 AND b.is_overseas = 0`
  334. sql += `
  335. UNION ALL
  336. SELECT t.company_id+10000000,t.company_name,t.nation,t.seller_id,t.seller_name,
  337. t.view_total,t.road_show_total,t.create_time,t.last_view_time,t.is_hide,t.overseas_status,
  338. 1 AS source,t.overseas_label,t.reset_btn
  339. FROM %s.english_company AS t
  340. INNER JOIN overseas_custom_seller AS n ON t.seller_id=n.seller_id
  341. WHERE 1=1 AND t.is_deleted=0 `
  342. sql += ` )AS m WHERE 1=1 `
  343. if condition != "" {
  344. sql += condition
  345. }
  346. sql = fmt.Sprintf(sql, databaseName)
  347. _, err = o.Raw(sql, pars).QueryRows(&list)
  348. return
  349. }