network.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "math"
  7. "strings"
  8. "sync"
  9. . "app.yhyue.com/moapp/jybase/common"
  10. . "app.yhyue.com/moapp/jybase/date"
  11. . "app.yhyue.com/moapp/jybase/es"
  12. . "bp.jydev.jianyu360.cn/CRM/application/api/common"
  13. "bp.jydev.jianyu360.cn/CRM/application/api/internal/types"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. var Network = &network{}
  17. type network struct {
  18. }
  19. type networkTree struct {
  20. Count int64
  21. Name string
  22. Children []*networkTreeChild
  23. }
  24. type networkTreeChild struct {
  25. Count int64
  26. Name string
  27. Id string
  28. Type int
  29. }
  30. //人脉库-添加/修改人脉
  31. func (n *network) AddOrUpdate(in *types.AddOrUpdateReq) *types.Reply {
  32. reply := &types.Reply{}
  33. nowFormat := NowFormat(Date_Full_Layout)
  34. var saveIntroduce = func(tx *sql.Tx, cid int64, isUpdate bool) bool {
  35. if in.Type != "middleman" {
  36. return true
  37. }
  38. values := []interface{}{}
  39. if in.Introduce_owner_id != "" {
  40. for k, v := range strings.Split(in.Introduce_owner_id, ",") {
  41. values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, v, strings.Split(in.Introduce_owner_name, ",")[k], 1, nowFormat)
  42. }
  43. }
  44. if in.Introduce_project_id != "" {
  45. for k, v := range strings.Split(in.Introduce_project_id, ",") {
  46. values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, v, strings.Split(in.Introduce_project_name, ",")[k], 2, nowFormat)
  47. }
  48. }
  49. if len(values) == 0 {
  50. return false
  51. }
  52. var r2 int64
  53. if isUpdate {
  54. r2 = CrmMysql.UpdateOrDeleteBySqlByTx(tx, `delete from crm.connection_introduce where connection_id=? and position_id=?`, in.Id, in.PositionId)
  55. }
  56. r3, _ := CrmMysql.InsertBatchByTx(tx, "crm.connection_introduce", []string{"position_id", "ent_id", "ent_dept_id", "ent_user_id", "connection_id", "relate_id", "relate_name", "itype", "create_time"}, values)
  57. return r2 >= 0 && r3 > 0
  58. }
  59. if in.Id > 0 {
  60. if CrmMysql.ExecTx("更新人脉", func(tx *sql.Tx) bool {
  61. 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)
  62. return r1 >= 0 && saveIntroduce(tx, in.Id, true)
  63. }) {
  64. reply.Data = map[string]interface{}{
  65. "status": 1,
  66. }
  67. } else {
  68. reply.Data = map[string]interface{}{
  69. "status": 0,
  70. }
  71. }
  72. } else {
  73. itype := n.TypeConvert(in.Type)
  74. var r1 int64
  75. if CrmMysql.ExecTx("新增人脉", func(tx *sql.Tx) bool {
  76. _, r1 = CrmMysql.InsertBatchByTx(tx, "crm.connection", []string{"position_id", "ent_id", "ent_dept_id", "ent_user_id", "itype", "company_name", "company_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.Contact_person, in.Contact_phone, 1, nowFormat, nowFormat})
  77. return r1 > 0 && saveIntroduce(tx, r1, false)
  78. }) {
  79. reply.Data = map[string]interface{}{
  80. "status": 1,
  81. "id": r1,
  82. }
  83. } else {
  84. reply.Data = map[string]interface{}{
  85. "status": 0,
  86. }
  87. }
  88. }
  89. return reply
  90. }
  91. //人脉库-业主名称联想
  92. func (n *network) Associate(in *types.AssociateReq) *types.Reply {
  93. //类型;firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 middleman_owner:中间人-业主 middleman_project:中间人-项目 agency:招标代理机构
  94. res := []map[string]interface{}{}
  95. if in.Type == "adiffb" {
  96. probusfors := NetworkCom.GetMyProbusfor(in.EntAccountId)
  97. if len(probusfors) > 0 {
  98. args := []interface{}{in.EntName}
  99. wh, newArgs := NetworkCom.WhArgs(probusfors)
  100. args = append(args, newArgs...)
  101. q := `select b.winner,b.winner_id from information.transaction_info a
  102. inner join information.transaction_info b on (has(a.winner, ?) and a.buyer_id=b.buyer_id and hasAny(b.property_form,[` + wh + `])`
  103. if in.Name != "" {
  104. q += ` and b.winner like ?`
  105. args = append(args, "%"+in.Name+"%")
  106. }
  107. q += `) order by b.winner`
  108. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  109. if err != nil {
  110. logx.Error(err)
  111. } else {
  112. for rows.Next() {
  113. var (
  114. company_name string
  115. company_id string
  116. )
  117. if err := rows.Scan(&company_name, &company_id); err != nil {
  118. logx.Error(err)
  119. continue
  120. }
  121. res = append(res, map[string]interface{}{
  122. "company_name": company_name,
  123. "company_id": company_id,
  124. })
  125. }
  126. rows.Close()
  127. if err := rows.Err(); err != nil {
  128. logx.Error(err)
  129. }
  130. }
  131. }
  132. } else {
  133. must := []string{fmt.Sprintf(`{"multi_match":{"query":"%s","type":"phrase","fields":["company_name"]}}`, in.Name)}
  134. switch in.Type {
  135. case "firstparty":
  136. must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
  137. must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
  138. case "supplier":
  139. must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
  140. must = append(must, `{"term":{"company_unit_type":3}}`)
  141. case "middleman_owner":
  142. must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
  143. case "agency":
  144. must = append(must, `{"term":{"company_unit_type":4}}`)
  145. }
  146. datas := VarEs.Get("ent_info", "ent_info", fmt.Sprintf(`{"query":{"bool":{"must":[%s]}},"size":10,"_source":["id","company_name"]}`, strings.Join(must, ",")))
  147. if datas != nil {
  148. for _, v := range *datas {
  149. res = append(res, map[string]interface{}{
  150. "company_name": ObjToString(v["company_name"]),
  151. "company_id": ObjToString(v["id"]),
  152. })
  153. }
  154. }
  155. }
  156. return &types.Reply{
  157. Data: res,
  158. }
  159. }
  160. //人脉库-全部人脉项目
  161. func (n *network) Allproject(in *types.AllprojectReq) (reply *types.Reply) {
  162. pool := make(chan bool, 5)
  163. wait := &sync.WaitGroup{}
  164. lock := &sync.Mutex{}
  165. reply = &types.Reply{}
  166. wh, newArgs := NetworkCom.WhArgs(NetworkCom.GetMyProbusfor(in.EntAccountId))
  167. if in.Id != "" {
  168. if in.Type == 1 {
  169. q := `select c.id as company_id,c.company_name,b.name from information.ent_map_code a
  170. inner join information.ent_code b on (a.a_id=? and b.pcode in ('0100','0200') and a.code=b.code)
  171. inner join information.ent_info c on (a.b_id=c.id)`
  172. args := []interface{}{in.Id}
  173. if in.Name != "" {
  174. q += ` where c.company_name like ?`
  175. args = append(args, "%"+in.Name+"%")
  176. }
  177. q += ` order by b.name,c.company_name`
  178. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  179. if err != nil {
  180. logx.Error(err)
  181. return
  182. }
  183. nameIndex := map[string]int{}
  184. list := []*networkTree{}
  185. for rows.Next() {
  186. var (
  187. company_id string
  188. company_name string
  189. name string
  190. )
  191. if err := rows.Scan(&company_id, &company_name, &name); err != nil {
  192. logx.Error(err)
  193. continue
  194. }
  195. if _, ok := nameIndex[name]; !ok {
  196. nameIndex[name] = len(list)
  197. list = append(list, &networkTree{
  198. Name: name,
  199. })
  200. }
  201. pool <- true
  202. wait.Add(1)
  203. go func(cIndex int, cId, cName string) {
  204. defer func() {
  205. <-pool
  206. wait.Done()
  207. }()
  208. ntc := &networkTreeChild{
  209. Name: cName,
  210. Id: cId,
  211. Type: 1,
  212. }
  213. if wh != "" {
  214. thisArgs := []interface{}{ntc.Id}
  215. thisArgs = append(thisArgs, newArgs...)
  216. ntc.Count = NetworkCom.Count(`select count(1) from information.transaction_info where buyer_id=? and hasAny(property_form,[`+wh+`])`, thisArgs...)
  217. }
  218. lock.Lock()
  219. list[cIndex].Count += ntc.Count
  220. list[cIndex].Children = append(list[cIndex].Children, ntc)
  221. lock.Unlock()
  222. }(nameIndex[name], company_id, company_name)
  223. }
  224. wait.Wait()
  225. rows.Close()
  226. if err := rows.Err(); err != nil {
  227. logx.Error(err)
  228. }
  229. }
  230. } else {
  231. q := `SELECT a.company_id,a.company_name,b.itype AS TYPE,a.contact_person AS person,a.contact_phone AS phone,COUNT(b.id) AS ipc FROM crm.connection a
  232. LEFT JOIN crm.connection_introduce b ON (b.position_id=? AND b.itype=2 AND a.id=b.connection_id) WHERE a.position_id=?`
  233. args := []interface{}{in.PositionId, in.PositionId}
  234. if in.Name != "" {
  235. q += ` and company_name like ?`
  236. args = append(args, "%"+in.Name+"%")
  237. }
  238. q += ` GROUP BY a.id ORDER BY a.create_time DESC`
  239. datas := CrmMysql.SelectBySql(q, args...)
  240. var count int64
  241. list := []*networkTree{
  242. &networkTree{
  243. Name: "甲方",
  244. },
  245. &networkTree{
  246. Name: "供应商",
  247. },
  248. &networkTree{
  249. Name: "同甲异业渠道",
  250. },
  251. &networkTree{
  252. Name: "中间人",
  253. },
  254. &networkTree{
  255. Name: "招标代理",
  256. },
  257. }
  258. //
  259. for _, vt := range *datas {
  260. pool <- true
  261. wait.Add(1)
  262. go func(v map[string]interface{}) {
  263. defer func() {
  264. <-pool
  265. wait.Done()
  266. }()
  267. itype := IntAll(v["itype"])
  268. if itype <= 0 || itype >= len(list) {
  269. return
  270. }
  271. ntc := &networkTreeChild{
  272. Name: ObjToString(v["company_name"]),
  273. Id: ObjToString(v["company_id"]),
  274. Type: IntAll(v["itype"]),
  275. }
  276. if wh != "" {
  277. thisArgs := []interface{}{ntc.Id}
  278. thisArgs = append(thisArgs, newArgs...)
  279. if itype == 1 {
  280. ntc.Count = NetworkCom.Count(`select count(1) from information.transaction_info where buyer_id=? and hasAny(b.property_form,[`+wh+`])`, thisArgs)
  281. } else if itype == 2 || itype == 3 {
  282. ntc.Count = NetworkCom.Count(`select count(1) from information.transaction_info where winner_id=? and hasAny(b.property_form,[`+wh+`])`, thisArgs)
  283. } else if itype == 4 {
  284. ntc.Count = Int64All(v["ipc"])
  285. } else if itype == 5 {
  286. ntc.Count = NetworkCom.Count(`select count(1) from information.transaction_info where agency_id=? and hasAny(b.property_form,[`+wh+`])`, thisArgs)
  287. }
  288. }
  289. lock.Lock()
  290. count += ntc.Count
  291. list[itype].Count += ntc.Count
  292. list[itype].Children = append(list[itype].Children, ntc)
  293. lock.Unlock()
  294. }(vt)
  295. }
  296. wait.Wait()
  297. reply = &types.Reply{
  298. Data: map[string]interface{}{
  299. "count": count,
  300. "list": list,
  301. },
  302. }
  303. }
  304. return reply
  305. }
  306. //人脉库-列表
  307. func (n *network) List(in *types.NetWorkListReq) *types.Reply {
  308. q := `select company_id,company_name,itype as type,contact_person as person,contact_phone as phone from crm.connection where position_id=?`
  309. args := []interface{}{in.PositionId}
  310. if in.Type != "" {
  311. q += ` and itype=?`
  312. args = append(args, n.TypeConvert(in.Type))
  313. }
  314. if in.Starttime != "" {
  315. q += ` and create_time>=?`
  316. args = append(args, in.Starttime)
  317. }
  318. if in.Endtime != "" {
  319. q += ` and create_time<=?`
  320. args = append(args, in.Endtime)
  321. }
  322. if in.Name != "" {
  323. q += ` and company_name like ?`
  324. args = append(args, "%"+in.Name+"%")
  325. }
  326. q += ` order by create_time desc`
  327. list := CrmMysql.SelectBySql(q, args...)
  328. finalList := []map[string]interface{}{}
  329. length := int64(len(*list))
  330. var pageSize int64 = 10
  331. total_page := int64(math.Ceil(float64(length) / float64(pageSize)))
  332. firstparty_count, supplier_count, adiffb_count, middleman_count, agency_count := 0, 0, 0, 0, 0
  333. if length > 0 {
  334. for _, v := range *list {
  335. switch Int64All(v["itype"]) {
  336. case 1:
  337. firstparty_count++
  338. case 2:
  339. supplier_count++
  340. case 3:
  341. adiffb_count++
  342. case 4:
  343. middleman_count++
  344. case 5:
  345. agency_count++
  346. }
  347. }
  348. if in.Current_page <= 0 {
  349. in.Current_page = 1
  350. }
  351. if in.Current_page > total_page {
  352. in.Current_page = total_page
  353. }
  354. start := (in.Current_page - 1) * pageSize
  355. end := start + pageSize
  356. if end > length {
  357. end = length
  358. }
  359. finalList = (*list)[start:end]
  360. }
  361. return &types.Reply{
  362. Data: map[string]interface{}{
  363. "total_page": total_page,
  364. "firstparty_count": firstparty_count,
  365. "supplier_count": supplier_count,
  366. "adiffb_count": adiffb_count,
  367. "middleman_count": middleman_count,
  368. "agency_count": agency_count,
  369. "list": finalList,
  370. },
  371. }
  372. }
  373. //
  374. func (n *network) TypeConvert(itype string) int {
  375. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  376. switch itype {
  377. case "firstparty":
  378. return 1
  379. case "supplier":
  380. return 2
  381. case "adiffb":
  382. return 3
  383. case "middleman":
  384. return 4
  385. case "agency":
  386. return 5
  387. }
  388. return 0
  389. }