network.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "math"
  7. "sort"
  8. "strings"
  9. "time"
  10. . "app.yhyue.com/moapp/jybase/common"
  11. . "app.yhyue.com/moapp/jybase/date"
  12. "app.yhyue.com/moapp/jybase/encrypt"
  13. . "app.yhyue.com/moapp/jybase/es"
  14. . "app.yhyue.com/moapp/jybase/sort"
  15. . "bp.jydev.jianyu360.cn/CRM/application/api/common"
  16. "bp.jydev.jianyu360.cn/CRM/application/api/internal/types"
  17. "github.com/shopspring/decimal"
  18. "github.com/zeromicro/go-zero/core/logx"
  19. )
  20. var Network = &network{}
  21. type network struct {
  22. }
  23. type networkTree struct {
  24. Count int64 `json:"count"`
  25. Name string `json:"name"`
  26. Children []*networkTreeChild `json:"children"`
  27. }
  28. type networkTreeChild struct {
  29. Count int64 `json:"count"`
  30. Id int64 `json:"id"`
  31. CompanyId string `json:"companyId"`
  32. CompanyName string `json:"companyName"`
  33. Type string `json:"type"`
  34. CreateTime string `json:"createTime"`
  35. }
  36. func (s *networkTree) Len() int {
  37. return len(s.Children)
  38. }
  39. func (s *networkTree) Less(i, j int) bool {
  40. if s.Name == "甲方" {
  41. return s.Children[i].CompanyName < s.Children[j].CompanyName
  42. }
  43. it, err1 := time.ParseInLocation(Date_Full_Layout, s.Children[i].CreateTime, time.Local)
  44. if err1 != nil {
  45. return true
  46. }
  47. jt, err2 := time.ParseInLocation(Date_Full_Layout, s.Children[j].CreateTime, time.Local)
  48. if err2 != nil {
  49. return true
  50. }
  51. return it.Unix() < jt.Unix()
  52. }
  53. func (s *networkTree) Swap(i, j int) {
  54. s.Children[i], s.Children[j] = s.Children[j], s.Children[i]
  55. }
  56. type projectInfo struct {
  57. BuyerCount int64
  58. ProjectCount int64
  59. ProjectAmount float64
  60. MonitorCount int64
  61. ExportId []string
  62. }
  63. type firstpartyNetwork struct {
  64. CompanyId string
  65. CompanyName string
  66. Name string
  67. }
  68. type idName struct {
  69. Id string
  70. Name string
  71. }
  72. type introduceOwnerProject struct {
  73. Networks []*myNetwork
  74. FirstpartyNetwork map[string][]*firstpartyNetwork
  75. Firstparty map[string]*projectInfo
  76. Supplier map[string]*projectInfo
  77. Adiffb map[string]*projectInfo
  78. Agency map[string]*projectInfo
  79. Middleman map[string]*projectInfo
  80. }
  81. type myNetwork struct {
  82. Id int64
  83. Company_id string
  84. Company_name string
  85. Qyxy_id string
  86. Itype int64
  87. Person string
  88. Phone string
  89. Buyer_count int64
  90. Project_count int64
  91. Relate_buyer_id string
  92. Relate_buyer_name string
  93. Relate_project_id string
  94. Create_time string
  95. }
  96. type nodeTree struct {
  97. NAME string
  98. ID string
  99. SZ_PID0 string
  100. SZ_PID1 string
  101. SZ_PID2 string
  102. SZ_PID3 string
  103. CODE string
  104. PCODE string
  105. DATACOUNT int64
  106. SZ_LEVEL int
  107. SZ_LEAF int
  108. TYPE string
  109. MYID string
  110. MYTYPE string
  111. }
  112. //人脉库-添加/修改人脉
  113. func (n *network) AddOrUpdate(in *types.AddOrUpdateReq) *types.Reply {
  114. reply := &types.Reply{Data: map[string]interface{}{
  115. "status": 0,
  116. }}
  117. if in.Type != "middleman" && in.Company_id == "" {
  118. return reply
  119. }
  120. if in.Company_name == "" {
  121. in.Company_name = "未填写"
  122. }
  123. nowFormat := NowFormat(Date_Full_Layout)
  124. var saveIntroduce = func(tx *sql.Tx, cid int64, isUpdate bool) bool {
  125. if in.Type != "middleman" {
  126. return true
  127. } else if in.Introduce_owner_id == "" || in.Introduce_project_id == "" {
  128. return false
  129. }
  130. values := []interface{}{}
  131. ioi := strings.Split(in.Introduce_owner_id, ",")
  132. ioqi := strings.Split(in.Introduce_owner_qyxy_id, ",")
  133. ion := strings.Split(in.Introduce_owner_name, ",")
  134. if len(ioi) != len(ion) || len(ioi) != len(ioqi) {
  135. return false
  136. }
  137. for k, v := range ioi {
  138. values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, ioqi[k], v, ion[k], 1, nowFormat)
  139. }
  140. ipi := strings.Split(in.Introduce_project_id, ",")
  141. ipn := strings.Split(in.Introduce_project_name, ",")
  142. if len(ipi) != len(ipn) {
  143. return false
  144. }
  145. for k, v := range ipi {
  146. values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, nil, v, ipn[k], 2, nowFormat)
  147. }
  148. var r2 int64
  149. if isUpdate {
  150. r2 = CrmMysql.UpdateOrDeleteBySqlByTx(tx, `delete from crm.connection_introduce where connection_id=? and position_id=?`, in.Id, in.PositionId)
  151. }
  152. r3, _ := CrmMysql.InsertBatchByTx(tx, "crm.connection_introduce", []string{"position_id", "ent_id", "ent_dept_id", "ent_user_id", "connection_id", "qyxy_id", "relate_id", "relate_name", "itype", "create_time"}, values)
  153. return r2 >= 0 && r3 > 0
  154. }
  155. itype := n.TypeStrConvert(in.Type)
  156. if in.Id > 0 {
  157. if CrmMysql.ExecTx("更新人脉", func(tx *sql.Tx) bool {
  158. if in.Company_id != "" {
  159. count := CrmMysql.CountBySql(`select count(1) as count from crm.connection where position_id=? and company_id=? and itype=? and id<>? and status=1`, in.PositionId, in.Company_id, itype, in.Id)
  160. if count == -1 {
  161. return false
  162. } else if count > 0 {
  163. reply.Data = map[string]interface{}{
  164. "status": -1,
  165. }
  166. return false
  167. }
  168. }
  169. r1 := CrmMysql.UpdateOrDeleteBySqlByTx(tx, `update crm.connection set company_name=?,company_id=?,contact_person=?,contact_phone=?,update_time=? where id=? and position_id=?`, in.Company_name, in.Company_id, in.Contact_person, in.Contact_phone, nowFormat, in.Id, in.PositionId)
  170. return r1 >= 0 && saveIntroduce(tx, in.Id, true)
  171. }) {
  172. reply.Data = map[string]interface{}{
  173. "status": 1,
  174. }
  175. }
  176. } else {
  177. if in.Company_id != "" {
  178. count := CrmMysql.CountBySql(`select count(1) as count from crm.connection where position_id=? and company_id=? and itype=? and status=1`, in.PositionId, in.Company_id, itype)
  179. if count == -1 {
  180. return reply
  181. } else if count > 0 {
  182. reply.Data = map[string]interface{}{
  183. "status": -1,
  184. }
  185. return reply
  186. }
  187. }
  188. var r1 int64
  189. if CrmMysql.ExecTx("新增人脉", func(tx *sql.Tx) bool {
  190. _, r1 = CrmMysql.InsertBatchByTx(tx, "crm.connection", []string{"position_id", "ent_id", "ent_dept_id", "ent_user_id", "itype", "company_name", "company_id", "qyxy_id", "contact_person", "contact_phone", "status", "create_time", "update_time"}, []interface{}{in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, itype, in.Company_name, in.Company_id, in.Qyxy_id, in.Contact_person, in.Contact_phone, 1, nowFormat, nowFormat})
  191. return r1 > 0 && saveIntroduce(tx, r1, false)
  192. }) {
  193. reply.Data = map[string]interface{}{
  194. "status": 1,
  195. "id": r1,
  196. }
  197. }
  198. }
  199. return reply
  200. }
  201. //人脉库-业主名称联想
  202. func (n *network) Associate(in *types.AssociateReq) (reply *types.Reply) {
  203. //类型;firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 middleman_owner:中间人-业主 middleman_project:中间人-项目 agency:招标代理机构
  204. res := []map[string]string{}
  205. reply = &types.Reply{Data: res}
  206. in.Name = strings.TrimSpace(in.Name)
  207. if in.Name == "" {
  208. return
  209. }
  210. pageSize := 10
  211. if in.Type == "adiffb" {
  212. probusfors := NetworkCom.GetMyProbusfor(in.EntAccountId)
  213. if len(probusfors) > 0 {
  214. args := []interface{}{in.EntName}
  215. wh, newArgs := NetworkCom.WhArgs(probusfors)
  216. args = append(args, newArgs...)
  217. q := `select DISTINCT winner_id,winner from information.transaction_info where buyer_id in (select buyer_id from information.transaction_info where has(winner, ?) and buyer_id<>'') and hasAny(property_form,[` + wh + `])=0 ORDER BY project_id`
  218. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  219. if err != nil {
  220. logx.Error(err)
  221. } else {
  222. repeat := map[string]bool{}
  223. ids := []string{}
  224. for rows.Next() {
  225. var (
  226. winner_id []string
  227. winner []string
  228. )
  229. if err := rows.Scan(&winner_id, &winner); err != nil {
  230. logx.Error(err)
  231. continue
  232. }
  233. for k, v := range winner {
  234. if repeat[v] || !strings.Contains(v, in.Name) {
  235. continue
  236. }
  237. repeat[v] = true
  238. if k >= len(winner_id) {
  239. continue
  240. }
  241. ids = append(ids, winner_id[k])
  242. res = append(res, map[string]string{
  243. "company_name": v,
  244. "company_id": winner_id[k],
  245. })
  246. if len(res) == pageSize {
  247. break
  248. }
  249. }
  250. }
  251. rows.Close()
  252. if err := rows.Err(); err != nil {
  253. logx.Error(err)
  254. }
  255. qyxyId := n.GetQyxyId(ids)
  256. for _, v := range res {
  257. v["qyxy_id"] = qyxyId[v["company_id"]]
  258. }
  259. }
  260. }
  261. } else {
  262. must := []string{fmt.Sprintf(`{"multi_match":{"query":"%s","type":"phrase","fields":["company_name"]}}`, in.Name)}
  263. switch in.Type {
  264. case "firstparty":
  265. must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
  266. must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
  267. case "supplier":
  268. must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
  269. must = append(must, `{"term":{"company_unit_type":3}}`)
  270. case "middleman_owner":
  271. must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
  272. case "agency":
  273. must = append(must, `{"term":{"company_unit_type":4}}`)
  274. }
  275. q := fmt.Sprintf(`{"query":{"bool":{"must":[%s]}},"size":%d,"_source":["id","company_id","company_name"]}`, strings.Join(must, ","), pageSize)
  276. logx.Info("人脉库-业主名称联想", q)
  277. datas := VarEs.Get("ent_info", "ent_info", q)
  278. if datas != nil {
  279. for _, v := range *datas {
  280. res = append(res, map[string]string{
  281. "company_name": ObjToString(v["company_name"]),
  282. "company_id": ObjToString(v["id"]),
  283. "qyxy_id": ObjToString(v["company_id"]),
  284. })
  285. }
  286. }
  287. }
  288. reply.Data = res
  289. return
  290. }
  291. //人脉库-全部人脉项目
  292. func (n *network) AllProject(in *types.AllprojectReq) (reply *types.Reply) {
  293. reply = &types.Reply{}
  294. probusfors := NetworkCom.GetMyProbusfor(in.EntAccountId)
  295. var count int64
  296. var list []*networkTree
  297. firstpartyChild := map[string][]*firstpartyNetwork{}
  298. if in.Id != "" {
  299. if in.Type == "firstparty" {
  300. fpn := n.FirstpartyNetwork(in.Name, []string{in.Id})
  301. if fpn[in.Id] != nil {
  302. firstparty := n.Introduce_Firstparty(fpn, map[string]bool{}, probusfors)
  303. nameIndex := map[string]int{}
  304. for _, v := range fpn[in.Id] {
  305. if _, ok := nameIndex[v.Name]; !ok {
  306. nameIndex[v.Name] = len(list)
  307. list = append(list, &networkTree{
  308. Name: v.Name,
  309. })
  310. }
  311. ntc := &networkTreeChild{
  312. CompanyName: v.CompanyName,
  313. CompanyId: v.CompanyId,
  314. Type: "firstparty",
  315. }
  316. if firstparty[v.CompanyId] != nil {
  317. ntc.Count = firstparty[v.CompanyId].ProjectCount
  318. }
  319. count++
  320. list[nameIndex[v.Name]].Count += ntc.Count
  321. list[nameIndex[v.Name]].Children = append(list[nameIndex[v.Name]].Children, ntc)
  322. }
  323. }
  324. }
  325. } else {
  326. args := []interface{}{in.PositionId, in.PositionId, in.PositionId}
  327. sqlAppend1 := ""
  328. if in.Name != "" {
  329. sqlAppend1 += ` and a.company_name like ?`
  330. args = append(args, "%"+in.Name+"%")
  331. }
  332. aio := n.AllIntroduceOwner(sqlAppend1, "", args, true, probusfors, map[string]bool{})
  333. firstpartyChild = aio.FirstpartyNetwork
  334. list = []*networkTree{
  335. &networkTree{
  336. Name: "甲方",
  337. Children: []*networkTreeChild{},
  338. },
  339. &networkTree{
  340. Name: "供应商",
  341. Children: []*networkTreeChild{},
  342. },
  343. &networkTree{
  344. Name: "同甲异业渠道",
  345. Children: []*networkTreeChild{},
  346. },
  347. &networkTree{
  348. Name: "中间人",
  349. Children: []*networkTreeChild{},
  350. },
  351. &networkTree{
  352. Name: "招标代理",
  353. Children: []*networkTreeChild{},
  354. },
  355. }
  356. //
  357. for _, v := range aio.Networks {
  358. if v.Itype <= 0 || v.Itype > int64(len(list)) {
  359. return
  360. }
  361. ntc := &networkTreeChild{
  362. Id: v.Id,
  363. CompanyName: v.Company_name,
  364. CompanyId: v.Company_id,
  365. Type: n.TypeIntConvert(v.Itype),
  366. CreateTime: v.Create_time,
  367. }
  368. switch v.Itype {
  369. case 1:
  370. if aio.Firstparty[v.Company_id] != nil {
  371. ntc.Count = aio.Firstparty[v.Company_id].ProjectCount
  372. }
  373. case 2:
  374. if aio.Supplier[v.Company_id] != nil {
  375. ntc.Count = aio.Supplier[v.Company_id].ProjectCount
  376. }
  377. case 3:
  378. if aio.Adiffb[v.Company_id] != nil {
  379. ntc.Count = aio.Adiffb[v.Company_id].ProjectCount
  380. }
  381. case 4:
  382. if v.Relate_project_id != "" {
  383. for _, v := range strings.Split(v.Relate_project_id, ",") {
  384. if aio.Middleman[v] != nil {
  385. ntc.Count++
  386. }
  387. }
  388. }
  389. case 5:
  390. if aio.Agency[v.Company_id] != nil {
  391. ntc.Count = aio.Agency[v.Company_id].ProjectCount
  392. }
  393. }
  394. count += ntc.Count
  395. list[v.Itype-1].Count += ntc.Count
  396. list[v.Itype-1].Children = append(list[v.Itype-1].Children, ntc)
  397. }
  398. }
  399. convList := []*nodeTree{}
  400. for _, v := range list {
  401. pm := &nodeTree{
  402. CODE: v.Name,
  403. NAME: v.Name,
  404. SZ_PID0: v.Name,
  405. SZ_LEVEL: 0,
  406. DATACOUNT: 0,
  407. }
  408. if len(v.Children) > 0 {
  409. pm.SZ_LEAF = 0
  410. } else {
  411. pm.SZ_LEAF = 1
  412. }
  413. sort.Sort(v)
  414. id, pType, pMyType, pMyId := "", "", "", ""
  415. for _, vv := range v.Children {
  416. myId := vv.CompanyId
  417. if vv.Type == "middleman" {
  418. myId = fmt.Sprint(vv.Id)
  419. }
  420. if pMyId != "" {
  421. pMyId += ","
  422. }
  423. pMyId += myId
  424. if pMyType != "" {
  425. pMyType += ","
  426. }
  427. pMyType += vv.Type
  428. myChildIds, myChildTypes := "", ""
  429. if in.Id == "" && v.Name == "甲方" {
  430. cccNodes := []*nodeTree{}
  431. cccIndexMap := map[string]int{}
  432. for _, vvv := range firstpartyChild[vv.CompanyId] {
  433. cccIndex, cccOk := cccIndexMap[vvv.Name]
  434. if cccOk {
  435. cccNodes[cccIndex].ID += "," + vvv.CompanyId
  436. cccNodes[cccIndex].MYID += "," + vvv.CompanyId
  437. cccNodes[cccIndex].TYPE += ",firstparty"
  438. cccNodes[cccIndex].MYTYPE += ",firstparty"
  439. } else {
  440. cccNodes = append(cccNodes, &nodeTree{
  441. NAME: vvv.Name,
  442. ID: vvv.CompanyId,
  443. SZ_PID0: v.Name,
  444. SZ_PID1: v.Name + ":" + myId,
  445. SZ_PID2: v.Name + ":" + myId + ":" + vvv.Name,
  446. CODE: v.Name + ":" + myId + ":" + vvv.Name,
  447. PCODE: v.Name + ":" + myId,
  448. SZ_LEVEL: 2,
  449. SZ_LEAF: 0,
  450. TYPE: "firstparty",
  451. MYID: vvv.CompanyId,
  452. MYTYPE: "firstparty",
  453. })
  454. cccIndexMap[vvv.Name] = len(cccNodes) - 1
  455. }
  456. if id != "" {
  457. id += ","
  458. }
  459. id += vvv.CompanyId
  460. if pType != "" {
  461. pType += ","
  462. }
  463. pType += vv.Type
  464. if myChildIds != "" {
  465. myChildIds += ","
  466. }
  467. myChildIds += vvv.CompanyId
  468. if myChildTypes != "" {
  469. myChildTypes += ","
  470. }
  471. myChildTypes += vv.Type
  472. convList = append(convList, &nodeTree{
  473. NAME: vvv.CompanyName,
  474. ID: vvv.CompanyId,
  475. SZ_PID0: v.Name,
  476. SZ_PID1: v.Name + ":" + myId,
  477. SZ_PID2: cccNodes[cccIndex].SZ_PID2,
  478. SZ_PID3: v.Name + ":" + myId + ":" + vvv.Name + ":" + vvv.CompanyId,
  479. CODE: v.Name + ":" + myId + ":" + vvv.Name + ":" + vvv.CompanyId,
  480. PCODE: cccNodes[cccIndex].PCODE,
  481. SZ_LEVEL: 3,
  482. SZ_LEAF: 1,
  483. TYPE: "firstparty",
  484. MYID: vvv.CompanyId,
  485. MYTYPE: "firstparty",
  486. })
  487. }
  488. for _, cccNode := range cccNodes {
  489. convList = append(convList, cccNode)
  490. }
  491. } else {
  492. if id != "" {
  493. id += ","
  494. }
  495. id += myId
  496. if pType != "" {
  497. pType += ","
  498. }
  499. pType += vv.Type
  500. myChildIds = myId
  501. myChildTypes = vv.Type
  502. }
  503. cm := &nodeTree{
  504. NAME: vv.CompanyName,
  505. ID: myChildIds,
  506. SZ_PID0: v.Name,
  507. SZ_PID1: v.Name + ":" + myId,
  508. CODE: v.Name + ":" + myId,
  509. PCODE: v.Name,
  510. DATACOUNT: vv.Count,
  511. SZ_LEVEL: If(myChildIds == "", 1, 0).(int),
  512. SZ_LEAF: 1,
  513. TYPE: myChildTypes,
  514. MYID: myId,
  515. MYTYPE: vv.Type,
  516. }
  517. convList = append(convList, cm)
  518. }
  519. pm.ID = id
  520. pm.TYPE = pType
  521. pm.MYTYPE = pMyType
  522. pm.MYID = pMyId
  523. convList = append(convList, pm)
  524. }
  525. reply = &types.Reply{
  526. Data: map[string]interface{}{
  527. "count": count,
  528. "list": convList,
  529. },
  530. }
  531. return reply
  532. }
  533. //人脉库-列表
  534. func (n *network) List(in *types.NetWorkListReq) *types.Reply {
  535. if in.Page_size <= 0 {
  536. in.Page_size = 10
  537. }
  538. if in.Current_page <= 0 {
  539. in.Current_page = 1
  540. }
  541. dbPaging := in.Buyercount_start == 0 && in.Buyercount_end == 0 && in.Monitor == 0 && in.Monitorcount_start == 0 && in.Monitorcount_end == 0 && in.Project_matchme == 0 && in.Order_amount == 0
  542. sqlAppendArgs := []interface{}{}
  543. sqlAppend1 := ""
  544. if dbPaging && in.Type != "" {
  545. sqlAppend1 += ` and a.itype=?`
  546. sqlAppendArgs = append(sqlAppendArgs, n.TypeStrConvert(in.Type))
  547. }
  548. comSqlAppend := ""
  549. comSqlArgs := []interface{}{}
  550. if in.Starttime != "" {
  551. comSqlAppend += ` and a.create_time>=?`
  552. comSqlArgs = append(comSqlArgs, in.Starttime+" 00:00:00")
  553. }
  554. if in.Endtime != "" {
  555. comSqlAppend += ` and a.create_time<=?`
  556. if in.Starttime == in.Endtime {
  557. in.Endtime += " 23:59:59"
  558. } else {
  559. in.Endtime += " 00:00:00"
  560. }
  561. comSqlArgs = append(comSqlArgs, in.Endtime)
  562. }
  563. if in.Name != "" {
  564. comSqlAppend += ` and a.company_name like ?`
  565. comSqlArgs = append(comSqlArgs, "%"+in.Name+"%")
  566. }
  567. sqlAppend1 += comSqlAppend
  568. sqlAppendArgs = append(sqlAppendArgs, comSqlArgs...)
  569. var count int64
  570. start := (in.Current_page - 1) * in.Page_size
  571. args := []interface{}{in.PositionId, in.PositionId, in.PositionId}
  572. args = append(args, sqlAppendArgs...)
  573. sqlAppend2 := ""
  574. firstparty_count, supplier_count, adiffb_count, middleman_count, agency_count := 0, 0, 0, 0, 0
  575. if dbPaging {
  576. newArgs := []interface{}{in.PositionId}
  577. newArgs = append(newArgs, sqlAppendArgs...)
  578. count = CrmMysql.CountBySql(`select count(1) as count from crm.connection a where a.position_id=?`+sqlAppend1, newArgs...)
  579. sqlAppend2 = ` limit ?,?`
  580. args = append(args, start, in.Page_size)
  581. itemArgs := []interface{}{in.PositionId}
  582. itemArgs = append(itemArgs, comSqlArgs...)
  583. items := CrmMysql.SelectBySql(`SELECT itype,SUM(1) AS sum FROM crm.connection a WHERE a.position_id=?`+comSqlAppend+` GROUP BY a.itype`, itemArgs...)
  584. if items != nil {
  585. for _, v := range *items {
  586. switch Int64All(v["itype"]) {
  587. case 1:
  588. firstparty_count = IntAll(v["sum"])
  589. case 2:
  590. supplier_count = IntAll(v["sum"])
  591. case 3:
  592. adiffb_count = IntAll(v["sum"])
  593. case 4:
  594. middleman_count = IntAll(v["sum"])
  595. case 5:
  596. agency_count = IntAll(v["sum"])
  597. }
  598. }
  599. }
  600. }
  601. //
  602. list := []*map[string]interface{}{}
  603. isTjProject := true
  604. probusfors := []string{}
  605. if in.Project_matchme == 1 {
  606. probusfors = NetworkCom.GetMyProbusfor(in.EntAccountId)
  607. if len(probusfors) == 0 {
  608. isTjProject = false
  609. }
  610. }
  611. if isTjProject {
  612. entMonitor := NetworkCom.EntMonitor(in.UserId)
  613. aio := n.AllIntroduceOwner(sqlAppend1, sqlAppend2, args, isTjProject, probusfors, entMonitor)
  614. for _, v := range aio.Networks {
  615. itype := ""
  616. buyer_count, project_count, expect_amount, monitor_count := int64(0), int64(0), float64(0), int64(0)
  617. export_id := []string{}
  618. jump_type, jump_id := "", ""
  619. switch v.Itype {
  620. case 1:
  621. itype = "甲方"
  622. jump_type = "firstparty"
  623. for _, vv := range aio.FirstpartyNetwork[v.Company_id] {
  624. if jump_id != "" {
  625. jump_id += ","
  626. }
  627. jump_id += vv.CompanyId
  628. }
  629. if aio.Firstparty[v.Company_id] != nil {
  630. buyer_count = aio.Firstparty[v.Company_id].BuyerCount
  631. project_count = aio.Firstparty[v.Company_id].ProjectCount
  632. expect_amount = aio.Firstparty[v.Company_id].ProjectAmount
  633. monitor_count = aio.Firstparty[v.Company_id].MonitorCount
  634. export_id = aio.Firstparty[v.Company_id].ExportId
  635. }
  636. case 2:
  637. itype = "供应商"
  638. jump_type = "supplier"
  639. jump_id = v.Company_id
  640. if aio.Supplier[v.Company_id] != nil {
  641. buyer_count = aio.Supplier[v.Company_id].BuyerCount
  642. project_count = aio.Supplier[v.Company_id].ProjectCount
  643. expect_amount = aio.Supplier[v.Company_id].ProjectAmount
  644. monitor_count = aio.Supplier[v.Company_id].MonitorCount
  645. export_id = aio.Supplier[v.Company_id].ExportId
  646. }
  647. case 3:
  648. itype = "同甲异业渠道"
  649. jump_type = "adiffb"
  650. jump_id = v.Company_id
  651. if aio.Adiffb[v.Company_id] != nil {
  652. buyer_count = aio.Adiffb[v.Company_id].BuyerCount
  653. project_count = aio.Adiffb[v.Company_id].ProjectCount
  654. expect_amount = aio.Adiffb[v.Company_id].ProjectAmount
  655. monitor_count = aio.Adiffb[v.Company_id].MonitorCount
  656. export_id = aio.Adiffb[v.Company_id].ExportId
  657. }
  658. case 4:
  659. itype = "中间人"
  660. jump_type = "middleman"
  661. jump_id = fmt.Sprint(v.Id)
  662. buyer_count = v.Buyer_count
  663. if v.Relate_buyer_name != "" {
  664. for _, v := range strings.Split(v.Relate_buyer_name, ",") {
  665. if v == "" {
  666. continue
  667. }
  668. if entMonitor[v] {
  669. monitor_count++
  670. }
  671. }
  672. }
  673. if v.Relate_project_id != "" {
  674. export_id = strings.Split(v.Relate_project_id, ",")
  675. for _, v := range export_id {
  676. if aio.Middleman[v] != nil {
  677. project_count++
  678. expect_amount += aio.Middleman[v].ProjectAmount
  679. }
  680. }
  681. }
  682. case 5:
  683. itype = "招标代理机构"
  684. jump_type = "agency"
  685. jump_id = v.Company_id
  686. if aio.Agency[v.Company_id] != nil {
  687. buyer_count = aio.Agency[v.Company_id].BuyerCount
  688. project_count = aio.Agency[v.Company_id].ProjectCount
  689. expect_amount = aio.Agency[v.Company_id].ProjectAmount
  690. monitor_count = aio.Agency[v.Company_id].MonitorCount
  691. export_id = aio.Agency[v.Company_id].ExportId
  692. }
  693. }
  694. if buyer_count < in.Buyercount_start {
  695. continue
  696. } else if in.Buyercount_end > 0 && buyer_count > in.Buyercount_end {
  697. continue
  698. } else if in.Monitor == 1 && monitor_count <= 0 {
  699. continue
  700. } else if in.Monitor == -1 && monitor_count > 0 {
  701. continue
  702. } else if monitor_count < in.Monitorcount_start {
  703. continue
  704. } else if in.Monitorcount_end > 0 && monitor_count > in.Monitorcount_end {
  705. continue
  706. } else if in.Project_matchme == 1 && project_count == 0 {
  707. continue
  708. }
  709. if !dbPaging {
  710. switch v.Itype {
  711. case 1:
  712. firstparty_count++
  713. case 2:
  714. supplier_count++
  715. case 3:
  716. adiffb_count++
  717. case 4:
  718. middleman_count++
  719. case 5:
  720. agency_count++
  721. }
  722. if in.Type != "" && n.TypeStrConvert(in.Type) != v.Itype {
  723. continue
  724. }
  725. }
  726. export_url := ""
  727. if len(export_id) > 0 {
  728. exportIdRepeat := map[string]bool{}
  729. exportId := ""
  730. for _, v := range export_id {
  731. if exportIdRepeat[v] {
  732. continue
  733. }
  734. exportIdRepeat[v] = true
  735. if exportId != "" {
  736. exportId += ","
  737. }
  738. exportId += v
  739. }
  740. export_url = "/subscribepay/network/projectExport?export_id=" + encrypt.SE.EncodeStringByCheck(exportId)
  741. }
  742. url := ""
  743. if v.Qyxy_id != "" && (jump_type == "supplier" || jump_type == "adiffb") {
  744. url = "/swordfish/page_big_pc/ent_portrait/" + encrypt.EncodeArticleId2ByCheck(v.Qyxy_id)
  745. } else if jump_type == "firstparty" && v.Company_name != "" {
  746. url = "/swordfish/page_big_pc/unit_portrayal/" + v.Company_name
  747. }
  748. list = append(list, &map[string]interface{}{
  749. "company_id": v.Company_id,
  750. "company_name": v.Company_name,
  751. "type": itype,
  752. "jump_type": jump_type,
  753. "jump_id": jump_id,
  754. "person": v.Person,
  755. "phone": v.Phone,
  756. "buyer_count": buyer_count,
  757. "monitor_count": monitor_count,
  758. "expect_amount": RetainDecimal(expect_amount/10000, 2),
  759. "project_count": project_count,
  760. "create_time": v.Create_time,
  761. "export_url": export_url,
  762. "url": url,
  763. "id": v.Id,
  764. })
  765. }
  766. }
  767. csList := &ComSortList{
  768. SortKeys: []*ComSortKey{
  769. &ComSortKey{
  770. Keys: []string{"expect_amount"},
  771. Order: 1,
  772. Type: "float",
  773. },
  774. },
  775. List: list,
  776. }
  777. if in.Order_amount == -1 {
  778. csList.SortKeys[0].Order = -1
  779. }
  780. if in.Order_amount != 0 {
  781. sort.Sort(csList)
  782. }
  783. finalList := []*map[string]interface{}{}
  784. if !dbPaging {
  785. count = int64(len(csList.List))
  786. if count > 0 && start < count {
  787. end := start + in.Page_size
  788. if end > count {
  789. end = count
  790. }
  791. finalList = csList.List[start:end]
  792. }
  793. } else {
  794. finalList = csList.List
  795. }
  796. total_page := int64(math.Ceil(float64(count) / float64(in.Page_size)))
  797. return &types.Reply{
  798. Data: map[string]interface{}{
  799. "count": count,
  800. "total_page": total_page,
  801. "firstparty_count": firstparty_count,
  802. "supplier_count": supplier_count,
  803. "adiffb_count": adiffb_count,
  804. "middleman_count": middleman_count,
  805. "agency_count": agency_count,
  806. "list": finalList,
  807. },
  808. }
  809. }
  810. //
  811. func (n *network) FirstpartyNetwork(name string, values []string) map[string][]*firstpartyNetwork {
  812. result := map[string][]*firstpartyNetwork{}
  813. if len(values) == 0 {
  814. return result
  815. }
  816. wh, args := NetworkCom.WhArgs(values)
  817. q := `select DISTINCT a.a_id,a.b_id as company_id,a.b_name as company_name,b.name from information.ent_map_code a
  818. inner join information.ent_code b on (a.a_id in (` + wh + `) and b.pcode in ('0100','0200') and a.code=b.code)`
  819. if name != "" {
  820. q += ` where c.company_name like ?`
  821. args = append(args, "%"+name+"%")
  822. }
  823. q += ` order by b.name,a.a_id,a.b_name`
  824. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  825. if err != nil {
  826. logx.Error(err)
  827. return result
  828. }
  829. for rows.Next() {
  830. var (
  831. a_id string
  832. company_id string
  833. company_name string
  834. name string
  835. )
  836. if err := rows.Scan(&a_id, &company_id, &company_name, &name); err != nil {
  837. logx.Error(err)
  838. continue
  839. }
  840. if company_id == "" || company_name == "" {
  841. continue
  842. }
  843. result[a_id] = append(result[a_id], &firstpartyNetwork{
  844. CompanyId: company_id,
  845. CompanyName: company_name,
  846. Name: name,
  847. })
  848. }
  849. return result
  850. }
  851. //
  852. func (n *network) Introduce_Firstparty(fpn map[string][]*firstpartyNetwork, entMonitor map[string]bool, probusfors []string) map[string]*projectInfo {
  853. values := []string{}
  854. vm := map[string]*projectInfo{}
  855. for _, v := range fpn {
  856. for _, vv := range v {
  857. vm[vv.CompanyId] = &projectInfo{}
  858. values = append(values, vv.CompanyId)
  859. }
  860. }
  861. result := map[string]*projectInfo{}
  862. if len(values) == 0 {
  863. return result
  864. }
  865. wh, args := NetworkCom.WhArgs(values)
  866. q := `select buyer_id,count(project_id) AS project_count,sum(project_money) AS project_amount,groupUniqArray(project_id) from information.transaction_info where buyer_id in (` + wh + `)`
  867. if len(probusfors) > 0 {
  868. newWh, newArgs := NetworkCom.WhArgs(probusfors)
  869. q += ` and hasAny(property_form,[` + newWh + `])`
  870. args = append(args, newArgs...)
  871. }
  872. q += ` group by buyer_id`
  873. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  874. if err != nil {
  875. logx.Error(err)
  876. return nil
  877. }
  878. for rows.Next() {
  879. var (
  880. buyer_id string
  881. project_count uint64
  882. project_amount decimal.Decimal
  883. project_id []string
  884. )
  885. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  886. logx.Error(err)
  887. continue
  888. }
  889. if vm[buyer_id] == nil {
  890. continue
  891. }
  892. vm[buyer_id].ProjectCount += int64(project_count)
  893. pf, _ := project_amount.Float64()
  894. vm[buyer_id].ProjectAmount += pf
  895. vm[buyer_id].ExportId = project_id
  896. }
  897. rows.Close()
  898. if err := rows.Err(); err != nil {
  899. logx.Error(err)
  900. }
  901. for k, v := range fpn {
  902. if result[k] == nil {
  903. result[k] = &projectInfo{}
  904. }
  905. result[k].BuyerCount = int64(len(v))
  906. for _, vv := range v {
  907. if entMonitor[vv.CompanyName] {
  908. result[k].MonitorCount++
  909. }
  910. if vm[vv.CompanyId] == nil {
  911. continue
  912. }
  913. result[k].ProjectCount += vm[vv.CompanyId].ProjectCount
  914. result[k].ProjectAmount += vm[vv.CompanyId].ProjectAmount
  915. result[k].ExportId = append(result[k].ExportId, vm[vv.CompanyId].ExportId...)
  916. }
  917. }
  918. return result
  919. }
  920. //
  921. func (n *network) Introduce_Supplier(values []string, entMonitor map[string]bool, probusfors []string) map[string]*projectInfo {
  922. if len(values) == 0 {
  923. return map[string]*projectInfo{}
  924. }
  925. wh, args := NetworkCom.WhArgs(values)
  926. vbs := map[string][]*idName{}
  927. repeat := map[string]bool{}
  928. whRepeat := map[string]map[string]bool{}
  929. buyers := []string{}
  930. q := `select winner_id,buyer_id,buyer from information.transaction_info where hasAny(winner_id,[` + wh + `])`
  931. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  932. if err != nil {
  933. logx.Error(err)
  934. } else {
  935. for rows.Next() {
  936. var (
  937. winner_id []string
  938. buyer_id string
  939. buyer string
  940. )
  941. if err := rows.Scan(&winner_id, &buyer_id, &buyer); err != nil {
  942. logx.Error(err)
  943. continue
  944. }
  945. if buyer_id == "" || buyer == "" {
  946. continue
  947. }
  948. if !repeat[buyer_id] {
  949. repeat[buyer_id] = true
  950. buyers = append(buyers, buyer_id)
  951. }
  952. for _, v := range winner_id {
  953. if whRepeat[v] != nil && whRepeat[v][buyer_id] {
  954. continue
  955. }
  956. if whRepeat[v] == nil {
  957. whRepeat[v] = map[string]bool{}
  958. }
  959. whRepeat[v][buyer_id] = true
  960. vbs[v] = append(vbs[v], &idName{
  961. Id: buyer_id,
  962. Name: buyer,
  963. })
  964. }
  965. }
  966. rows.Close()
  967. if err := rows.Err(); err != nil {
  968. logx.Error(err)
  969. }
  970. }
  971. return n.MakeProjectInfo(buyers, vbs, probusfors, entMonitor)
  972. }
  973. //
  974. func (n *network) Introduce_Agency(values []string, entMonitor map[string]bool, probusfors []string) map[string]*projectInfo {
  975. if len(values) == 0 {
  976. return map[string]*projectInfo{}
  977. }
  978. wh, args := NetworkCom.WhArgs(values)
  979. q := `select DISTINCT agency_id,buyer_id,buyer from information.transaction_info where agency_id in (` + wh + `)`
  980. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  981. if err != nil {
  982. logx.Error(err)
  983. return nil
  984. }
  985. vbs := map[string][]*idName{}
  986. buyers := []string{}
  987. repeat := map[string]bool{}
  988. for rows.Next() {
  989. var (
  990. agency_id string
  991. buyer_id string
  992. buyer string
  993. )
  994. if err := rows.Scan(&agency_id, &buyer_id, &buyer); err != nil {
  995. logx.Error(err)
  996. continue
  997. }
  998. if buyer_id == "" || buyer == "" {
  999. continue
  1000. }
  1001. vbs[agency_id] = append(vbs[agency_id], &idName{
  1002. Id: buyer_id,
  1003. Name: buyer,
  1004. })
  1005. if !repeat[buyer_id] {
  1006. repeat[buyer_id] = true
  1007. buyers = append(buyers, buyer_id)
  1008. }
  1009. }
  1010. rows.Close()
  1011. if err := rows.Err(); err != nil {
  1012. logx.Error(err)
  1013. }
  1014. return n.MakeProjectInfo(buyers, vbs, probusfors, entMonitor)
  1015. }
  1016. //
  1017. func (n *network) Introduce_Middleman(values []string, entMonitor map[string]bool, probusfors []string) map[string]*projectInfo {
  1018. result := map[string]*projectInfo{}
  1019. if len(values) == 0 {
  1020. return result
  1021. }
  1022. wh, args := NetworkCom.WhArgs(values)
  1023. q := `select DISTINCT project_id,project_money from information.transaction_info where project_id in (` + wh + `)`
  1024. if len(probusfors) > 0 {
  1025. newWh, newArgs := NetworkCom.WhArgs(probusfors)
  1026. q += ` and hasAny(property_form,[` + newWh + `])`
  1027. args = append(args, newArgs...)
  1028. }
  1029. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1030. if err != nil {
  1031. logx.Error(err)
  1032. return nil
  1033. }
  1034. for rows.Next() {
  1035. var (
  1036. project_id string
  1037. project_money decimal.Decimal
  1038. )
  1039. if err := rows.Scan(&project_id, &project_money); err != nil {
  1040. logx.Error(err)
  1041. continue
  1042. }
  1043. pf, _ := project_money.Float64()
  1044. result[project_id] = &projectInfo{
  1045. ProjectAmount: pf,
  1046. }
  1047. }
  1048. rows.Close()
  1049. if err := rows.Err(); err != nil {
  1050. logx.Error(err)
  1051. }
  1052. return result
  1053. }
  1054. //
  1055. func (n *network) TypeStrConvert(itype string) int64 {
  1056. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  1057. switch itype {
  1058. case "firstparty":
  1059. return 1
  1060. case "supplier":
  1061. return 2
  1062. case "adiffb":
  1063. return 3
  1064. case "middleman":
  1065. return 4
  1066. case "agency":
  1067. return 5
  1068. }
  1069. return 0
  1070. }
  1071. //
  1072. func (n *network) TypeIntConvert(itype int64) string {
  1073. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  1074. switch itype {
  1075. case 1:
  1076. return "firstparty"
  1077. case 2:
  1078. return "supplier"
  1079. case 3:
  1080. return "adiffb"
  1081. case 4:
  1082. return "middleman"
  1083. case 5:
  1084. return "agency"
  1085. }
  1086. return ""
  1087. }
  1088. //
  1089. func (n *network) GetQyxyId(ids []string) map[string]string {
  1090. m := map[string]string{}
  1091. if len(ids) == 0 {
  1092. return m
  1093. }
  1094. wh, args := NetworkCom.WhArgs(ids)
  1095. q := `select id,company_id from information.ent_info where id in (` + wh + `)`
  1096. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1097. if err != nil {
  1098. logx.Error(err)
  1099. } else {
  1100. for rows.Next() {
  1101. var (
  1102. id string
  1103. company_id string
  1104. )
  1105. if err := rows.Scan(&id, &company_id); err != nil {
  1106. logx.Error(err)
  1107. continue
  1108. }
  1109. m[id] = company_id
  1110. }
  1111. rows.Close()
  1112. if err := rows.Err(); err != nil {
  1113. logx.Error(err)
  1114. }
  1115. }
  1116. return m
  1117. }
  1118. //
  1119. func (n *network) BuyerProjectInfo(ids []string, probusfors []string) map[string]*projectInfo {
  1120. vm := map[string]*projectInfo{}
  1121. if len(ids) == 0 {
  1122. return vm
  1123. }
  1124. wh, args := NetworkCom.WhArgs(ids)
  1125. q := `select buyer_id,count(DISTINCT project_id) AS project_count,sum(project_money) AS project_amount,groupUniqArray(project_id) from information.transaction_info where buyer_id in (` + wh + `)`
  1126. if len(probusfors) > 0 {
  1127. newWh, newArgs := NetworkCom.WhArgs(probusfors)
  1128. q += ` and hasAny(property_form,[` + newWh + `])`
  1129. args = append(args, newArgs...)
  1130. }
  1131. q += ` group by buyer_id`
  1132. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1133. if err != nil {
  1134. logx.Error(err)
  1135. return nil
  1136. }
  1137. for rows.Next() {
  1138. var (
  1139. buyer_id string
  1140. project_count uint64
  1141. project_amount decimal.Decimal
  1142. project_id []string
  1143. )
  1144. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  1145. logx.Error(err)
  1146. continue
  1147. }
  1148. pf, _ := project_amount.Float64()
  1149. vm[buyer_id] = &projectInfo{
  1150. ProjectCount: int64(project_count),
  1151. ExportId: project_id,
  1152. ProjectAmount: pf,
  1153. }
  1154. }
  1155. rows.Close()
  1156. if err := rows.Err(); err != nil {
  1157. logx.Error(err)
  1158. }
  1159. return vm
  1160. }
  1161. //
  1162. func (n *network) MakeProjectInfo(buyers []string, vbs map[string][]*idName, probusfors []string, entMonitor map[string]bool) map[string]*projectInfo {
  1163. pis := n.BuyerProjectInfo(buyers, probusfors)
  1164. vm := map[string]*projectInfo{}
  1165. for k, v := range vbs {
  1166. pi := &projectInfo{
  1167. BuyerCount: int64(len(v)),
  1168. }
  1169. re := map[string]bool{}
  1170. for _, vv := range v {
  1171. if pis[vv.Id] != nil {
  1172. pi.ProjectCount += pis[vv.Id].ProjectCount
  1173. pi.ProjectAmount += pis[vv.Id].ProjectAmount
  1174. for _, vvv := range pis[vv.Id].ExportId {
  1175. if re[vvv] {
  1176. continue
  1177. }
  1178. pi.ExportId = append(pi.ExportId, vvv)
  1179. re[vvv] = true
  1180. }
  1181. }
  1182. if entMonitor[vv.Name] {
  1183. pi.MonitorCount++
  1184. }
  1185. }
  1186. vm[k] = pi
  1187. }
  1188. return vm
  1189. }
  1190. //
  1191. func (n *network) AllIntroduceOwner(sqlAppend1, sqlAppend2 string, args []interface{}, isTjProject bool, probusfors []string, entMonitor map[string]bool) *introduceOwnerProject {
  1192. q := `select a.id,a.company_id,a.company_name,a.qyxy_id,a.itype,a.contact_person as person,a.contact_phone as phone,count(DISTINCT if(b.itype=1,b.relate_id,null)) as buyer_count,count(DISTINCT if(b.itype=2,b.relate_id,null)) as project_count,GROUP_CONCAT(IF(b.itype=1,b.relate_id,NULL)) AS relate_buyer_id,GROUP_CONCAT(IF(b.itype=1,b.relate_name,NULL)) AS relate_buyer_name,GROUP_CONCAT(IF(b.itype=2,b.relate_id,NULL)) AS relate_project_id,a.create_time from crm.connection a
  1193. left join crm.connection_introduce b on (a.position_id=? and b.position_id=? and a.id=b.connection_id) where a.position_id=?` + sqlAppend1 + ` GROUP BY a.id order by a.create_time desc` + sqlAppend2
  1194. listTemp := CrmMysql.SelectBySql(q, args...)
  1195. firstparty_array, supplier_array, adiffb_array, agency_array, middleman_project_array := []string{}, []string{}, []string{}, []string{}, []string{}
  1196. myNetworks := []*myNetwork{}
  1197. for _, v := range *listTemp {
  1198. myNetwork := &myNetwork{
  1199. Id: Int64All(v["id"]),
  1200. Company_id: ObjToString(v["company_id"]),
  1201. Company_name: ObjToString(v["company_name"]),
  1202. Qyxy_id: ObjToString(v["qyxy_id"]),
  1203. Itype: Int64All(v["itype"]),
  1204. Person: ObjToString(v["person"]),
  1205. Phone: ObjToString(v["phone"]),
  1206. Buyer_count: Int64All(v["buyer_count"]),
  1207. Project_count: Int64All(v["project_count"]),
  1208. Relate_buyer_id: ObjToString(v["relate_buyer_id"]),
  1209. Relate_buyer_name: ObjToString(v["relate_buyer_name"]),
  1210. Relate_project_id: ObjToString(v["relate_project_id"]),
  1211. Create_time: ObjToString(v["create_time"]),
  1212. }
  1213. switch myNetwork.Itype {
  1214. case 1:
  1215. firstparty_array = append(firstparty_array, myNetwork.Company_id)
  1216. case 2:
  1217. supplier_array = append(supplier_array, myNetwork.Company_id)
  1218. case 3:
  1219. adiffb_array = append(adiffb_array, myNetwork.Company_id)
  1220. case 4:
  1221. if myNetwork.Relate_project_id != "" {
  1222. middleman_project_array = append(middleman_project_array, strings.Split(myNetwork.Relate_project_id, ",")...)
  1223. }
  1224. case 5:
  1225. agency_array = append(agency_array, myNetwork.Company_id)
  1226. }
  1227. myNetworks = append(myNetworks, myNetwork)
  1228. }
  1229. iop := &introduceOwnerProject{
  1230. Networks: myNetworks,
  1231. FirstpartyNetwork: map[string][]*firstpartyNetwork{},
  1232. Firstparty: map[string]*projectInfo{},
  1233. Supplier: map[string]*projectInfo{},
  1234. Adiffb: map[string]*projectInfo{},
  1235. Agency: map[string]*projectInfo{},
  1236. Middleman: map[string]*projectInfo{},
  1237. }
  1238. if isTjProject {
  1239. iop.FirstpartyNetwork = n.FirstpartyNetwork("", firstparty_array)
  1240. iop.Firstparty = n.Introduce_Firstparty(iop.FirstpartyNetwork, entMonitor, probusfors)
  1241. iop.Supplier = n.Introduce_Supplier(supplier_array, entMonitor, probusfors)
  1242. iop.Adiffb = n.Introduce_Supplier(adiffb_array, entMonitor, probusfors)
  1243. iop.Agency = n.Introduce_Agency(agency_array, entMonitor, probusfors)
  1244. iop.Middleman = n.Introduce_Middleman(middleman_project_array, entMonitor, probusfors)
  1245. }
  1246. return iop
  1247. }