custom.go 13 KB

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