customer.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. package entity
  2. import (
  3. "database/sql"
  4. "errors"
  5. "fmt"
  6. "log"
  7. "strings"
  8. "time"
  9. "app.yhyue.com/moapp/jybase/api"
  10. qutil "app.yhyue.com/moapp/jybase/common"
  11. . "app.yhyue.com/moapp/jybase/date"
  12. "app.yhyue.com/moapp/jybase/encrypt"
  13. util "app.yhyue.com/moapp/jypkg/ent/util"
  14. )
  15. var VarCustomer = &Customer{}
  16. type Customer struct {
  17. Id int
  18. Name string //客户名字
  19. Industry string //行业
  20. Area string //地区
  21. Address string //详细地址
  22. Remarks int //备注
  23. }
  24. /*
  25. *获取企业已分配人员
  26. */
  27. func (this *Customer) GetDistributeUser(entId int, customerId string) []int {
  28. res := util.Mysql.Find(util.Entniche_user_customer, api.M{"customer_id": customerId}, "user_id", "", -1, -1)
  29. userids := []int{}
  30. if res != nil {
  31. for _, user := range *res {
  32. userids = append(userids, qutil.IntAll(user["user_id"]))
  33. }
  34. }
  35. return userids
  36. }
  37. /*
  38. 清除客户的分配人员
  39. customerId:客户id
  40. users:管辖下的客户列表
  41. excludeUserId:不清除的用户列表
  42. */
  43. func (this *Customer) ClearDistributeUser(tx *sql.Tx, customerId string, users []*User, excludeUserId []string) bool {
  44. uIds := []string{}
  45. for _, v := range users {
  46. has := false //排除本次分配的中仍分配的
  47. for _, vv := range excludeUserId {
  48. if fmt.Sprint(v.Id) == vv {
  49. has = true
  50. break
  51. }
  52. }
  53. if !has {
  54. uIds = append(uIds, fmt.Sprintf("%d", v.Id))
  55. }
  56. }
  57. if len(uIds) == 0 {
  58. return true
  59. }
  60. value, str := util.GetInForComma(strings.Join(uIds, ","))
  61. log.Println(`delete from entniche_user_customer where customer_id = ? and user_id in (` + str + `)`)
  62. if util.Mysql.UpdateOrDeleteBySqlByTx(tx, `delete from entniche_user_customer where customer_id = ? and user_id in (`+str+`)`, append([]interface{}{customerId}, value...)...) > -1 {
  63. return true
  64. }
  65. return false
  66. }
  67. /*
  68. 新增分配人员
  69. customerId:客户id
  70. userIds:分配的用户数组
  71. */
  72. var distribute_fields = []string{"customer_id", "user_id", "type", "timestamp", "source_type", "source"}
  73. func (this *Customer) AddDeptDistributeUsers(tx *sql.Tx, customerId string, userIds []string, userName string, personalNumb int64) (bool, error) {
  74. if len(userIds) == 0 {
  75. return true, nil
  76. }
  77. //删除新增分配中已分配的用户
  78. value, str := util.GetInForComma(strings.Join(userIds, ","))
  79. res := util.Mysql.SelectBySqlByTx(tx, `SELECT user_id FROM entniche_user_customer WHERE customer_id=? and user_id in(`+str+`)`, append([]interface{}{customerId}, value...)...)
  80. addUserid := []string{}
  81. if res != nil && len(*res) != 0 { //添加的用户中含有已分配的
  82. for _, id := range userIds {
  83. has := false
  84. for _, v := range *res {
  85. if id == fmt.Sprint(v["user_id"]) {
  86. has = true
  87. break
  88. }
  89. }
  90. if !has {
  91. addUserid = append(addUserid, id)
  92. }
  93. }
  94. if len(addUserid) == 0 {
  95. return true, nil
  96. }
  97. } else {
  98. addUserid = userIds
  99. }
  100. //插入新增的分配
  101. timestamp := time.Now().Format(Date_Full_Layout)
  102. values := []interface{}{}
  103. for i := 0; i < len(addUserid); i++ {
  104. //获取个人客户数
  105. count := util.Mysql.Count("entniche_user_customer", api.M{"user_id": addUserid[i]})
  106. //判断个人客户数
  107. if count >= personalNumb {
  108. //判断企业客户数
  109. return false, errors.New("客户数量超限")
  110. }
  111. values = append(values, customerId, addUserid[i], 2, timestamp, 3, 3)
  112. }
  113. count, _ := util.Mysql.InsertBatchByTx(tx, util.Entniche_user_customer, distribute_fields, values)
  114. if count == qutil.Int64All(len(addUserid)) {
  115. return true, nil
  116. }
  117. return false, errors.New(fmt.Sprintf("分配出错:目标%d人,完成%d人", len(addUserid), count))
  118. }
  119. /*
  120. 获取企业客户列表
  121. ent_id 企业id
  122. alloc 是否分配 1分配 2未分配
  123. area 地区
  124. startTme 查询开始时间
  125. endTime 查询结束时间
  126. pageIndex 页码
  127. pageSize 每页数据量
  128. */
  129. func (this *Customer) GetEntCustomerList(ent_id int, name, alloc, staff_names string, area map[string]interface{}, startTme, endTime int64, pageIndex, pageSize int, sourceType []int, industry string) (count int64, customerList *[]map[string]interface{}) {
  130. customerList = &[]map[string]interface{}{}
  131. searchSql := ` a.ent_id =? and a.state=1 `
  132. searchValues := []interface{}{}
  133. searchValues = append(searchValues, ent_id)
  134. if name != "" { //名字模糊查询
  135. searchSql += ` and a.name like ? `
  136. searchValues = append(searchValues, "%"+name+"%")
  137. }
  138. if len(area) > 0 {
  139. areaStr := ""
  140. cityStr := ""
  141. for k, v := range area {
  142. if len(qutil.ObjArrToStringArr(v.([]interface{}))) == 0 {
  143. areaStr += `,` + k
  144. } else {
  145. for _, v := range qutil.ObjArrToStringArr(v.([]interface{})) {
  146. cityStr += `,` + qutil.ObjToString(v)
  147. }
  148. }
  149. }
  150. areaValue, areastr := util.GetInForComma(areaStr)
  151. cityValue, citystr := util.GetInForComma(cityStr)
  152. if len(areastr) > 0 {
  153. if len(citystr) > 0 {
  154. searchSql += fmt.Sprintf(` and ( a.area in (%s) or a.city in (%s))`, areastr, citystr)
  155. searchValues = append(searchValues, areaValue...)
  156. searchValues = append(searchValues, cityValue...)
  157. } else {
  158. searchSql += fmt.Sprintf(` and ( a.area in (%s) )`, areastr)
  159. searchValues = append(searchValues, areaValue...)
  160. }
  161. } else {
  162. if len(citystr) > 0 {
  163. searchSql += fmt.Sprintf(` and ( a.city in (%s))`, citystr)
  164. searchValues = append(searchValues, cityValue...)
  165. }
  166. }
  167. }
  168. if startTme > 0 { //开始时间查询
  169. searchSql += ` and a.updatetime >= ? `
  170. searchValues = append(searchValues, time.Unix(startTme, 0).Format(Date_Full_Layout))
  171. }
  172. if endTime > 0 { //开始时间查询
  173. searchSql += ` and a.updatetime <= ? `
  174. searchValues = append(searchValues, time.Unix(endTime, 0).Format(Date_Full_Layout))
  175. }
  176. if industry != "" {
  177. /*searchSql += ` and a.industry = ? `
  178. searchValues = append(searchValues, industry)*/
  179. if industry != "" {
  180. industryValue, industrystr := util.GetInForComma(industry)
  181. searchSql += fmt.Sprintf(` and ( a.industry in (%s))`, industrystr)
  182. searchValues = append(searchValues, industryValue...)
  183. }
  184. }
  185. allocSql := ``
  186. customersql := ""
  187. countValue := []interface{}{}
  188. if alloc == "1" || alloc == "2" {
  189. if alloc == "2" { //是否分配查询
  190. allocSql += ` HAVING staff_names IS NULL `
  191. } else if alloc == "1" {
  192. if staff_names != "" {
  193. //_, _ := GetInForComma(staff_names)
  194. allocSql += fmt.Sprintf(` HAVING FIND_IN_SET (%s,staff_names) `, `"`+staff_names+`"`)
  195. } else {
  196. allocSql += `HAVING staff_names IS NOT NULL `
  197. }
  198. }
  199. searchValues = append(searchValues, (pageIndex-1)*pageSize, pageSize)
  200. log.Println(`SELECT GROUP_CONCAT(c.name) as staff_names,a.industry,a.createtime,a.name as customer_name,a.id as customer_id,(SELECT b.time FROM entniche_project_track b WHERE b.customer_id = a.id ORDER BY b.time desc LIMIT 0,1 ) AS updatetime
  201. from
  202. (select a.name,a.id,a.createtime,a.industry FROM entniche_customer a
  203. LEFT JOIN entniche_department_parent b on (a.dept_id=b.id)
  204. WHERE `+searchSql+` order by updatetime desc limit 100000) as a
  205. LEFT JOIN entniche_user_customer b on (a.id=b.customer_id)
  206. LEFT JOIN entniche_user c on (b.user_id=c.id)
  207. GROUP BY a.id `+allocSql+` order by updatetime desc, a.createtime desc limit ?,?`, searchValues)
  208. customersql = `SELECT GROUP_CONCAT(c.name) as staff_names,a.industry,a.createtime,a.name as customer_name,a.id as customer_id,(SELECT b.time FROM entniche_project_track b WHERE b.customer_id = a.id ORDER BY b.time desc LIMIT 0,1 ) AS updatetime
  209. from
  210. (select a.name,a.id,a.createtime,a.industry FROM entniche_customer a
  211. LEFT JOIN entniche_department_parent b on (a.dept_id=b.id)
  212. WHERE ` + searchSql + ` order by updatetime desc limit 100000) as a
  213. LEFT JOIN entniche_user_customer b on (a.id=b.customer_id)
  214. LEFT JOIN entniche_user c on (b.user_id=c.id)
  215. GROUP BY a.id ` + allocSql + ` order by updatetime desc, a.createtime desc`
  216. customerList = util.Mysql.SelectBySql(customersql+` limit ?,?`, searchValues...)
  217. countValue = searchValues[:len(searchValues)-2]
  218. } else {
  219. searchValues = append(searchValues, (pageIndex-1)*pageSize, pageSize)
  220. log.Println(`SELECT GROUP_CONCAT(c.name) as staff_names,a.industry,a.createtime,a.name as customer_name,a.id as customer_id,
  221. (SELECT b.time FROM entniche_project ep INNER JOIN entniche_project_track b ON ep.id = b.project_id WHERE ep.customer_id = a.id ORDER BY b.time desc LIMIT 0,1 ) AS updatetime from
  222. (select a.name,a.id,a.createtime,a.industry FROM entniche_customer a
  223. LEFT JOIN entniche_department_parent b on (a.dept_id=b.id)
  224. WHERE `+searchSql+` order by updatetime desc limit ?,?) as a
  225. LEFT JOIN entniche_user_customer b on (a.id=b.customer_id)
  226. LEFT JOIN entniche_user c on (b.user_id=c.id)
  227. GROUP BY a.id `+allocSql+` order by updatetime desc, a.createtime desc`, searchValues)
  228. customersql = `SELECT GROUP_CONCAT(c.name) as staff_names,a.industry,a.createtime,a.name as customer_name,a.id as customer_id,
  229. (SELECT b.time FROM entniche_project_track b WHERE b.customer_id = a.id ORDER BY b.time desc LIMIT 0,1 ) AS updatetime
  230. from
  231. (select a.name,a.id,a.createtime,a.industry FROM entniche_customer a
  232. LEFT JOIN entniche_department_parent b on (a.dept_id=b.id)
  233. WHERE ` + searchSql + ` order by updatetime desc limit ?,?) as a
  234. LEFT JOIN entniche_user_customer b on (a.id=b.customer_id)
  235. LEFT JOIN entniche_user c on (b.user_id=c.id)
  236. GROUP BY a.id ` + allocSql + ` order by updatetime desc, a.createtime desc`
  237. customerList = util.Mysql.SelectBySql(customersql, searchValues...)
  238. countValue = searchValues
  239. }
  240. countSql := `select count(*) from (` + customersql + `) d`
  241. count = util.Mysql.CountBySql(countSql, countValue...)
  242. if count > 500 {
  243. count = 500
  244. }
  245. return
  246. }
  247. /*
  248. 获取部门客户列表
  249. dept_id 部门id
  250. alloc 是否分配 1分配 2未分配
  251. area 地区
  252. startTme 查询开始时间
  253. endTime 查询结束时间
  254. pageIndex 页码
  255. pageSize 每页数据量
  256. */
  257. func (this *Customer) GetDeptCustomerList(dept_id, ent_id int, name, alloc, staff_names string, area map[string]interface{}, startTme, endTime int64, pageIndex, pageSize int, sourceType []int, industry string) (count int64, customerList *[]map[string]interface{}) {
  258. customerList = &[]map[string]interface{}{}
  259. searchSql := ` 1=1 `
  260. searchValues := []interface{}{}
  261. searchValues = append(searchValues, dept_id, dept_id, ent_id)
  262. if name != "" { //名字模糊查询
  263. searchSql += ` and b.name like ? `
  264. searchValues = append(searchValues, "%"+name+"%")
  265. }
  266. if len(area) > 0 {
  267. areaStr := ""
  268. cityStr := ""
  269. for k, v := range area {
  270. if len(qutil.ObjArrToStringArr(v.([]interface{}))) == 0 {
  271. areaStr += `,` + k
  272. } else {
  273. for _, v := range qutil.ObjArrToStringArr(v.([]interface{})) {
  274. cityStr += `,` + qutil.ObjToString(v)
  275. }
  276. }
  277. }
  278. areaValue, areastr := util.GetInForComma(areaStr)
  279. cityValue, citystr := util.GetInForComma(cityStr)
  280. if len(areastr) > 0 {
  281. if len(citystr) > 0 {
  282. searchSql += fmt.Sprintf(` and ( b.area in (%s) or b.city in (%s))`, areastr, citystr)
  283. searchValues = append(searchValues, areaValue...)
  284. searchValues = append(searchValues, cityValue...)
  285. } else {
  286. searchSql += fmt.Sprintf(` and ( b.area in (%s) )`, areastr)
  287. searchValues = append(searchValues, areaValue...)
  288. }
  289. } else {
  290. if len(citystr) > 0 {
  291. searchSql += fmt.Sprintf(` and ( b.city in (%s))`, citystr)
  292. searchValues = append(searchValues, cityValue...)
  293. }
  294. }
  295. }
  296. if startTme > 0 { //开始时间查询
  297. searchSql += ` and b.updatetime >= ? `
  298. searchValues = append(searchValues, time.Unix(startTme, 0).Format(Date_Full_Layout))
  299. }
  300. if endTime > 0 { //开始时间查询
  301. searchSql += ` and b.updatetime <= ? `
  302. searchValues = append(searchValues, time.Unix(endTime, 0).Format(Date_Full_Layout))
  303. }
  304. if industry != "" {
  305. industryValue, industrystr := util.GetInForComma(industry)
  306. searchSql += fmt.Sprintf(` and ( b.industry in (%s))`, industrystr)
  307. searchValues = append(searchValues, industryValue...)
  308. }
  309. allocSql := ``
  310. customersql := ""
  311. countValue := []interface{}{}
  312. if alloc == "1" || alloc == "2" {
  313. if alloc == "2" { //是否分配查询
  314. allocSql += ` HAVING staff_names IS NULL `
  315. } else if alloc == "1" {
  316. if staff_names != "" {
  317. allocSql += fmt.Sprintf(` HAVING FIND_IN_SET (%s,staff_names) `, `"`+staff_names+`"`)
  318. } else {
  319. allocSql += `HAVING staff_names IS NOT NULL `
  320. }
  321. }
  322. searchValues = append(searchValues, (pageIndex-1)*pageSize, pageSize)
  323. customersql = `SELECT
  324. GROUP_CONCAT( c.NAME ) AS staff_names,
  325. b.createtime,
  326. b.area,
  327. b.industry,
  328. b.NAME AS customer_name,
  329. b.id AS customer_id,
  330. ( SELECT b.time FROM entniche_project_track b WHERE b.customer_id = a.customer_id ORDER BY b.time DESC LIMIT 0, 1 ) AS updatetime
  331. FROM
  332. (
  333. SELECT
  334. a.customer_id,
  335. a.user_id
  336. FROM
  337. entniche_user_customer a
  338. INNER JOIN (
  339. SELECT DISTINCT
  340. c.id
  341. FROM
  342. entniche_department_parent a
  343. INNER JOIN entniche_department_user b ON (
  344. b.dept_id = ?
  345. OR ( a.pid = ? AND a.id = b.dept_id ))
  346. INNER JOIN entniche_user c ON ( c.ent_id = ? AND b.user_id = c.id )
  347. ) b ON ( a.user_id = b.id )
  348. limit 100000) as a
  349. LEFT JOIN entniche_customer b ON ( a.customer_id = b.id )
  350. AND b.ent_id = 14640
  351. AND b.state = 1
  352. LEFT JOIN entniche_user c ON ( a.user_id = c.id ) WHERE ` + searchSql + `
  353. GROUP BY a.customer_id ` + allocSql + ` order by updatetime desc, b.createtime desc `
  354. customerList = util.Mysql.SelectBySql(customersql+` limit ?,?`, searchValues...)
  355. countValue = searchValues[:len(searchValues)-2]
  356. } else {
  357. searchValues = append(searchValues, (pageIndex-1)*pageSize, pageSize)
  358. customersql = `SELECT
  359. GROUP_CONCAT( c.NAME ) AS staff_names,
  360. b.createtime,
  361. b.area,
  362. b.industry,
  363. b.NAME AS customer_name,
  364. b.id AS customer_id,
  365. ( SELECT b.time FROM entniche_project_track b WHERE b.customer_id = a.customer_id ORDER BY b.time DESC LIMIT 0, 1 ) AS updatetime
  366. FROM
  367. (
  368. SELECT
  369. a.customer_id,
  370. a.user_id
  371. FROM
  372. entniche_user_customer a
  373. INNER JOIN (
  374. SELECT DISTINCT
  375. c.id
  376. FROM
  377. entniche_department_parent a
  378. INNER JOIN entniche_department_user b ON (
  379. b.dept_id = ?
  380. OR ( a.pid = ? AND a.id = b.dept_id ))
  381. INNER JOIN entniche_user c ON ( c.ent_id = ? AND b.user_id = c.id )
  382. ) b ON ( a.user_id = b.id ) limit 100000
  383. ) as a
  384. LEFT JOIN entniche_customer b ON ( a.customer_id = b.id )
  385. AND b.ent_id = 14640
  386. AND b.state = 1
  387. LEFT JOIN entniche_user c ON ( a.user_id = c.id ) WHERE ` + searchSql + `
  388. GROUP BY a.customer_id ` + allocSql + ` order by updatetime desc, b.createtime desc `
  389. customerList = util.Mysql.SelectBySql(customersql+` limit ?,?`, searchValues...)
  390. countValue = searchValues
  391. }
  392. countSql := `select count(*) from (` + customersql + `) d`
  393. count = util.Mysql.CountBySql(countSql, countValue...)
  394. if count > 500 {
  395. count = 500
  396. }
  397. return
  398. }
  399. /*
  400. 获取员工客户列表
  401. user_id 员工id
  402. area 地区
  403. startTme 查询开始时间
  404. endTime 查询结束时间
  405. pageIndex 页码
  406. pageSize 每页数据量
  407. */
  408. func (this *Customer) GetStaffCustomerList(user_id, ent_id int, name string, area map[string]interface{}, startTme, endTime int64, pageIndex, pageSize int, sourceType []int, followStatus []int, industry, label, userId string) (count int64, customerList *[]map[string]interface{}) {
  409. customerList = &[]map[string]interface{}{}
  410. searchValues := []interface{}{}
  411. searchSql := ` and a.ent_id=? and b.user_id=? and a.state=1`
  412. searchValues = append(searchValues, ent_id, user_id)
  413. if len(area) > 0 {
  414. areaStr := ""
  415. cityStr := ""
  416. for k, v := range area {
  417. if len(qutil.ObjArrToStringArr(v.([]interface{}))) == 0 {
  418. areaStr += `,` + k
  419. } else {
  420. for _, v := range qutil.ObjArrToStringArr(v.([]interface{})) {
  421. cityStr += `,` + qutil.ObjToString(v)
  422. }
  423. }
  424. }
  425. areaValue, areastr := util.GetInForComma(areaStr)
  426. cityValue, citystr := util.GetInForComma(cityStr)
  427. if len(areastr) > 0 {
  428. if len(citystr) > 0 {
  429. searchSql += fmt.Sprintf(` and ( a.area in (%s) or a.city in (%s))`, areastr, citystr)
  430. searchValues = append(searchValues, areaValue...)
  431. searchValues = append(searchValues, cityValue...)
  432. } else {
  433. searchSql += fmt.Sprintf(` and a.area in (%s) `, areastr)
  434. searchValues = append(searchValues, areaValue...)
  435. }
  436. } else {
  437. if len(citystr) > 0 {
  438. searchSql += fmt.Sprintf(` and a.city in (%s)`, citystr)
  439. searchValues = append(searchValues, cityValue...)
  440. }
  441. }
  442. }
  443. if label != "" { //标签查询
  444. searchSql += " and("
  445. value, _ := util.GetInForComma(label)
  446. for key, v := range value {
  447. if key == 0 {
  448. searchSql += fmt.Sprintf(` FIND_IN_SET(%s,d.labelid) `, encrypt.SE.DecodeString(qutil.ObjToString(v)))
  449. } else {
  450. searchSql += fmt.Sprintf(` or FIND_IN_SET(%s,d.labelid) `, encrypt.SE.DecodeString(qutil.ObjToString(v)))
  451. }
  452. }
  453. searchSql += " ) "
  454. searchSql += ` and d.userid=? `
  455. searchValues = append(searchValues, userId)
  456. }
  457. if sourceType != nil && len(sourceType) > 0 && sourceType[0] != 999 { //来源类型:0订阅分配、1认领、2自主添加、3别人划转
  458. sourceStr := []string{}
  459. for i := 0; i < len(sourceType); i++ {
  460. sourceStr = append(sourceStr, "?")
  461. searchValues = append(searchValues, sourceType[i])
  462. }
  463. searchSql += fmt.Sprintf(` and b.source in (%s)`, strings.Join(sourceStr, ","))
  464. }
  465. if industry != "" {
  466. /*searchSql += ` and a.industry = ? `
  467. searchValues = append(searchValues, industry)*/
  468. if industry != "" {
  469. industryValue, industrystr := util.GetInForComma(industry)
  470. searchSql += fmt.Sprintf(` and ( a.industry in (%s))`, industrystr)
  471. searchValues = append(searchValues, industryValue...)
  472. }
  473. }
  474. if name != "" { //名字模糊查询
  475. searchSql += ` and a.name like ? `
  476. searchValues = append(searchValues, "%"+name+"%")
  477. }
  478. timeSql := ``
  479. if followStatus != nil && len(followStatus) > 0 && followStatus[0] != 999 { //跟踪状态;1-成单,2-继续跟踪,3-跟踪失败 4取消跟踪
  480. statusStr := []string{}
  481. for i := 0; i < len(followStatus); i++ {
  482. statusStr = append(statusStr, "?")
  483. searchValues = append(searchValues, followStatus[i])
  484. }
  485. timeSql += fmt.Sprintf(` having status IN (%s) `, strings.Join(statusStr, ","))
  486. }
  487. if startTme > 0 { //开始时间查询
  488. if timeSql == `` {
  489. timeSql += ` HAVING updatetime >= ? `
  490. } else {
  491. timeSql += ` and updatetime >= ? `
  492. }
  493. searchValues = append(searchValues, time.Unix(startTme, 0).Format(Date_Full_Layout))
  494. }
  495. if endTime > 0 { //截止时间查询
  496. if timeSql == `` {
  497. timeSql += ` HAVING updatetime <= ? `
  498. } else {
  499. timeSql += ` and updatetime <= ? `
  500. }
  501. searchValues = append(searchValues, time.Unix(endTime, 0).Format(Date_Full_Layout))
  502. }
  503. searchValues = append(searchValues, (pageIndex-1)*pageSize, pageSize)
  504. log.Println(`SELECT a.id as customer_id,a.name as customer_name,a.industry,c.name as staff_names,a.createtime,
  505. (SELECT e.STATUS FROM entniche_project_track e WHERE e.customer_id = a.id and e.user_id= b.user_id ORDER BY e.time desc LIMIT 0,1 ) AS status,
  506. (SELECT e.time FROM entniche_project_track e WHERE e.customer_id = a.id and e.user_id= b.user_id ORDER BY e.time desc LIMIT 0,1 ) AS updatetime
  507. FROM entniche_customer a
  508. LEFT JOIN bdcollection_entniche d on (d.customer_id = a.id and d.userid="`+userId+`")
  509. LEFT JOIN entniche_user_customer b on ( a.id=b.customer_id)
  510. LEFT JOIN entniche_user c on (c.id = b.user_id)
  511. Where 1=1 `+searchSql+timeSql+`
  512. order by updatetime desc , a.createtime desc limit ?,?`, searchValues)
  513. customersql := `SELECT a.id as customer_id,a.industry,a.name as customer_name,c.name as staff_names,a.createtime,
  514. (SELECT e.STATUS FROM entniche_project_track e WHERE e.customer_id = a.id and e.user_id= b.user_id ORDER BY e.time desc LIMIT 0,1 ) AS status,
  515. (SELECT e.time FROM entniche_project_track e WHERE e.customer_id = a.id and e.user_id= b.user_id ORDER BY e.time desc LIMIT 0,1 ) AS updatetime
  516. FROM entniche_customer a
  517. LEFT JOIN bdcollection_entniche d on (d.customer_id = a.id and d.userid="` + userId + `")
  518. LEFT JOIN entniche_user_customer b on ( a.id=b.customer_id)
  519. LEFT JOIN entniche_user c on (c.id = b.user_id)
  520. Where 1=1 ` + searchSql + timeSql + `
  521. order by updatetime desc ,a.createtime desc `
  522. countValue := searchValues[:len(searchValues)-2]
  523. customerList = util.Mysql.SelectBySql(customersql+` limit ?,?`, searchValues...)
  524. countSql := `select count(*) from (` + customersql + `) d`
  525. count = util.Mysql.CountBySql(countSql, countValue...)
  526. if count > 500 {
  527. count = 500
  528. }
  529. return
  530. }
  531. func (this *Customer) GetCustomerNumb(user_id int, personalNumb int64) (data map[string]interface{}) {
  532. customerNumbList := util.Mysql.SelectBySql(`SELECT
  533. count(if(source="1",true,null)) as claimed,
  534. count(if(source="2",true,null)) as autonomousAddition,
  535. count(if(source="3",true,null)) as distribution,
  536. count(if(source="4",true,null)) as subscribe
  537. FROM
  538. entniche_user_customer
  539. WHERE
  540. user_id = ?`, user_id)
  541. (*customerNumbList)[0]["count"] = personalNumb
  542. data = (*customerNumbList)[0]
  543. return
  544. }
  545. func (this *Customer) GetExist(cIds, userIds string) *[]map[string]interface{} {
  546. q := "select group_concat(c.name separator'、') as customer_name,b.name from entniche_user_customer a ,entniche_user b,entniche_customer c where a.user_id in ( %s ) and a.customer_id in (%s) and a.user_id = b.id and c.id=a.customer_id group by b.id"
  547. return util.Mysql.SelectBySql(fmt.Sprintf(q, userIds, cIds))
  548. }