employService.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/date"
  5. elastic "app.yhyue.com/moapp/jybase/es"
  6. "app.yhyue.com/moapp/jypkg/ent/util"
  7. "app.yhyue.com/moapp/jypkg/public"
  8. MC "bp.jydev.jianyu360.cn/CRM/application/api/common"
  9. "bp.jydev.jianyu360.cn/CRM/application/api/internal/types"
  10. "bp.jydev.jianyu360.cn/CRM/application/entity"
  11. "database/sql"
  12. "fmt"
  13. "github.com/RoaringBitmap/roaring"
  14. "go.mongodb.org/mongo-driver/bson"
  15. "log"
  16. "regexp"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. const (
  22. BuyerIndex = "buyer" // 采购单位index
  23. BuyerType = "buyer"
  24. )
  25. type EmPloyService struct{}
  26. // InfoEmployinfo 收录情况查询
  27. func (e *EmPloyService) InfoEmployinfo(in *types.InfoEmployinfoReq) []map[string]interface{} {
  28. var data []map[string]interface{}
  29. for _, v := range strings.Split(in.IdArr, ",") {
  30. table, findKey, employKey, source := EmployKeyFormat(in.EmployType)
  31. id := ""
  32. id = IdFormat(v, in.EmployType)
  33. if id == "" {
  34. log.Println(v, in.EmployType, "该信息查询不到数据")
  35. break
  36. }
  37. valueMap := map[string]interface{}{
  38. "id": v,
  39. }
  40. //有收录情况
  41. //summaryMap := SummaryFormat(in.PositionId)
  42. //vint = mongodb.StringTOBsonId(id).Timestamp().Unix()
  43. //vint = extractNumbers(id)
  44. //valueMap["isEmploy"] = summaryMap[in.EmployType].Contains(uint32(vint))
  45. employData := MC.CrmMysql.FindOne(table, map[string]interface{}{
  46. employKey: id,
  47. "position_id": in.PositionId,
  48. "source": source,
  49. }, "", "")
  50. if employData != nil && len(*employData) > 0 {
  51. valueMap["isIgnore"] = common.If(common.Int64All((*employData)["is_ignore"]) == 1, true, false)
  52. valueMap["isEmploy"] = common.If(common.Int64All((*employData)["status"]) == 1, true, false)
  53. } else {
  54. valueMap["isIgnore"] = false
  55. valueMap["isEmploy"] = false
  56. }
  57. if len(strings.Split(in.IdArr, ",")) == 1 {
  58. //列表查询
  59. //是否忽略处理
  60. //客户数量
  61. customCount := MC.CrmMysql.Count(entity.CUSTOM, map[string]interface{}{
  62. findKey: id,
  63. "position_id": in.PositionId,
  64. })
  65. valueMap["customCount"] = customCount
  66. if in.EmployType == 1 || in.EmployType == 4 {
  67. //销售线索数量
  68. clueCount := MC.CrmMysql.Count(entity.SALE_CLUE, map[string]interface{}{
  69. "employ_info_id": id,
  70. "position_id": in.PositionId,
  71. })
  72. valueMap["clueCount"] = clueCount
  73. //销售机会数量
  74. chanceCount := MC.CrmMysql.Count(entity.SALE_CHANCE, map[string]interface{}{
  75. "employ_info_id": id,
  76. "position_id": in.PositionId,
  77. })
  78. valueMap["chanceCount"] = chanceCount
  79. }
  80. } else {
  81. valueMap["chanceCount"] = 0
  82. valueMap["clueCount"] = 0
  83. valueMap["isIgnore"] = false
  84. valueMap["customCount"] = 0
  85. }
  86. data = append(data, valueMap)
  87. }
  88. return data
  89. }
  90. // EmployOperate 收录操作
  91. func (e *EmPloyService) EmployOperate(in *types.EmployOperateReq) bool {
  92. //收录汇总表
  93. table, _, employKey, source := EmployKeyFormat(in.EmployType)
  94. //summaryMap := SummaryFormat(in.PositionId)
  95. return MC.CrmMysql.ExecTx("收录操作", func(tx *sql.Tx) bool {
  96. for _, v1 := range strings.Split(in.IdArr, ",") {
  97. //id转中文
  98. id := ""
  99. findMap := map[string]interface{}{}
  100. findMap["position_id"] = in.PositionId
  101. id = IdFormat(v1, in.EmployType)
  102. findMap[employKey] = id
  103. if id == "" {
  104. log.Println(v1, in.EmployType, "该信息查询不到数据")
  105. break
  106. }
  107. if in.IsEmploy {
  108. if MC.CrmMysql.Count(table, findMap) > 0 {
  109. MC.CrmMysql.UpdateByTx(tx, table, findMap, map[string]interface{}{
  110. "status": 1,
  111. })
  112. } else {
  113. //收录新增
  114. data := map[string]interface{}{}
  115. if in.EmployType == 1 || in.EmployType == 4 {
  116. data = InfoFind(id, in.EmployType)
  117. data["employ_way"] = 1
  118. data["jybx_url"] = "/article/content/" + v1 + ".html"
  119. } else if in.EmployType == 2 {
  120. data = CustomFind(id, in.EmployType)
  121. data["jybx_url"] = "swordfish/page_big_pc/svip/ent_ser_portrait/" + v1
  122. } else if in.EmployType == 3 {
  123. data = CustomFind(id, in.EmployType)
  124. data["jybx_url"] = "/swordfish/page_big_pc/unit_portrayal/" + v1
  125. }
  126. data["position_id"] = in.PositionId
  127. data["ent_id"] = in.EntId
  128. data["source"] = source
  129. ok := MC.CrmMysql.InsertByTx(tx, table, data)
  130. if ok <= 0 {
  131. log.Println(v1, id, in.EmployType, "收录失败")
  132. break
  133. }
  134. }
  135. } else {
  136. //取消收录
  137. ok := MC.CrmMysql.UpdateByTx(tx, table, findMap, map[string]interface{}{
  138. "status": -1,
  139. })
  140. if !ok {
  141. log.Println(v1, id, in.EmployType, "取消收录失败")
  142. }
  143. }
  144. /*//收录汇总表处理
  145. vint := extractNumbers(id)
  146. if in.IsEmploy {
  147. summaryMap[in.EmployType].Add(uint32(vint))
  148. } else {
  149. summaryMap[in.EmployType].Remove(uint32(vint))
  150. }*/
  151. //fool = true
  152. }
  153. /*if fool {
  154. return SummarySave(tx, in.PositionId, summaryMap)
  155. } else {
  156. return false
  157. }*/
  158. return true
  159. })
  160. }
  161. // CustomFind 企业信息查询
  162. func CustomFind(id string, employType int64) map[string]interface{} {
  163. data := map[string]interface{}{}
  164. //company_id 企业id name户名称 address 地址
  165. if employType == 2 {
  166. //企业详情
  167. entinfo, _ := MC.MgoEnt.FindOneByField("qyxy_std", map[string]interface{}{"_id": id}, map[string]interface{}{
  168. "company_address": 1, //注册地
  169. "company_name": 1,
  170. })
  171. if entinfo != nil && len(*entinfo) > 0 {
  172. data["name"] = (*entinfo)["company_name"]
  173. data["address"] = (*entinfo)["company_address"]
  174. data["company_id"] = id
  175. }
  176. } else {
  177. //采购单位详情
  178. rs := elastic.Get(BuyerIndex, BuyerType, fmt.Sprintf(`{"query":{"bool":{"must":[{"terms":{"_id":["%s"]}}]}},"size":1,"_source":["buyer_name","city"]}`, id))
  179. if rs != nil && len(*rs) > 0 {
  180. data["name"] = (*rs)[0]["buyer_name"]
  181. data["address"] = (*rs)[0]["city"]
  182. data["company_id"] = id
  183. }
  184. }
  185. data["status"] = 1
  186. data["create_time"] = time.Now().Format(date.Date_Full_Layout)
  187. return data
  188. }
  189. // InfoFind 标讯信息查询
  190. func InfoFind(id string, employType int64) map[string]interface{} {
  191. data := map[string]interface{}{}
  192. //source_id 信息id、项目id- title 标题-area 省 -city 市 -subtype 信息类型二级分类
  193. //buyerclass 采购单位行业 -budget 预算 -bidamount 中标金额 buyer采购单位 annex有无附件 publishtime发布时间 projectname 项目时间
  194. //ownerclass 业主类型(拟在建搜索) expurasing_time 预计采购时间 jybx_url 标讯详情页
  195. if employType == 1 {
  196. //标讯信息
  197. obj := map[string]interface{}{}
  198. brobj, ok := MC.Mgo.Find("bidding_rec", bson.M{"s_id": id}, `{"l_recoverydate":-1}`, public.MgoBiddingFields, false, 0, 1)
  199. if ok && (*brobj) != nil && len(*brobj) == 1 && (*brobj)[0] != nil {
  200. obj = (*brobj)[0]
  201. } else {
  202. aobj, ok := MC.MgoBidding.FindById("bidding", id, public.MgoBiddingFields)
  203. if ok && (aobj == nil || *aobj == nil || len(*aobj) == 0) {
  204. aobj, ok = MC.MgoBidding.FindById("bidding_back", id, public.MgoBiddingFields)
  205. }
  206. obj = *aobj
  207. }
  208. if ok && obj != nil && len(obj) > 0 {
  209. data["title"] = common.ObjToString(obj["title"])
  210. data["city"] = obj["city"]
  211. data["area"] = obj["area"]
  212. data["subtype"] = obj["subtype"]
  213. data["buyerclass"] = obj["buyerclass"]
  214. data["budget"] = obj["budget"]
  215. data["bidamount"] = obj["bidamount"]
  216. data["annex"] = 0
  217. //类型处理
  218. toptype := common.InterfaceToStr(obj["toptype"])
  219. switch toptype {
  220. case "采购意向", "预告", "招标":
  221. data["type"] = 2
  222. case "其他", "结果":
  223. data["type"] = 3
  224. default:
  225. data["type"] = 1
  226. }
  227. //data["type"] = in.SourceType
  228. if obj["projectinfo"] != nil {
  229. projectinfo := common.ObjToMap(obj["projectinfo"])
  230. if (*projectinfo)["attachments"] != nil {
  231. data["annex"] = 1
  232. }
  233. }
  234. data["publishtime"] = time.Unix(common.Int64All(obj["publishtime"]), 0).Format(date.Date_Full_Layout)
  235. data["show_time"] = time.Unix(common.Int64All(obj["publishtime"]), 0).Format(date.Date_Full_Layout)
  236. data["projectname"] = obj["projectname"]
  237. procurementlist, _ := obj["procurementlist"].([]interface{})
  238. for _, vs := range procurementlist { //1.采购意向清单数据集打码处理
  239. vsMap, _ := vs.(map[string]interface{})
  240. if vsMap["expurasingtime"] != nil {
  241. data["expurasing_time"] = vsMap["expurasingtime"]
  242. break
  243. }
  244. }
  245. }
  246. }
  247. data["status"] = 1
  248. data["employ_way"] = 1
  249. data["source_id"] = id
  250. data["create_time"] = time.Now().Format(date.Date_Full_Layout)
  251. return data
  252. }
  253. // GetBuyerIdByName 通过采购单位名称获取id
  254. func GetBuyerIdByName(buyerName string) (buyerId string) {
  255. r := elastic.Get(BuyerIndex, BuyerType, fmt.Sprintf(`{"query":{"bool":{"must":[{"term":{"buyer_name":"%s"}}]}},"size":1,"_source":["city","id"]}`, buyerName))
  256. if r == nil || len(*r) == 0 {
  257. return
  258. }
  259. buyerId, _ = (*r)[0]["_id"].(string)
  260. return
  261. }
  262. // IdFormat id解密
  263. func IdFormat(encryptionId string, employType int64) string {
  264. decryptId := ""
  265. switch employType {
  266. case 1, 2:
  267. decryptId = util.DecodeId(encryptionId)
  268. case 3:
  269. if len(encryptionId) > 0 && len([]rune(encryptionId)) == len(encryptionId) {
  270. //此数据是id
  271. //获取中文名字
  272. decryptId = util.DecodeId(encryptionId)
  273. } else {
  274. //次数据传的名字
  275. decryptId = GetBuyerIdByName(encryptionId)
  276. }
  277. }
  278. return decryptId
  279. }
  280. // SummaryFormat 汇总表处理
  281. func SummaryFormat(positionId int64) map[int64]*roaring.Bitmap {
  282. var byte1 []byte
  283. var byte2 []byte
  284. var byte3 []byte
  285. var byte4 []byte
  286. err := MC.CrmMysql.DB.QueryRow("select search_tencent, search_buyer, search_ent, search_nzj from employ_summary where position_id=?", positionId).Scan(&byte1, &byte3, &byte2, &byte4)
  287. if err != nil {
  288. return nil
  289. }
  290. rb1 := roaring.NewBitmap()
  291. rb2 := roaring.NewBitmap()
  292. rb3 := roaring.NewBitmap()
  293. rb4 := roaring.NewBitmap()
  294. data := map[int64]*roaring.Bitmap{}
  295. if byte1 != nil && len(byte1) > 0 {
  296. rb1.UnmarshalBinary(byte1)
  297. }
  298. if byte2 != nil && len(byte2) > 0 {
  299. rb2.UnmarshalBinary(byte2)
  300. }
  301. if byte3 != nil && len(byte3) > 0 {
  302. rb3.UnmarshalBinary(byte3)
  303. }
  304. if byte4 != nil && len(byte4) > 0 {
  305. rb4.UnmarshalBinary(byte4)
  306. }
  307. data[1] = rb1
  308. data[2] = rb2
  309. data[3] = rb3
  310. data[4] = rb4
  311. return data
  312. }
  313. // SummarySave 汇总表保存
  314. func SummarySave(tx *sql.Tx, positionId int64, data map[int64]*roaring.Bitmap) bool {
  315. employSummaryData := MC.CrmMysql.FindOne(entity.EMPLOY_SUMMARY, map[string]interface{}{
  316. "position_id": positionId,
  317. }, "", "")
  318. if employSummaryData != nil && len(*employSummaryData) > 0 {
  319. //修改
  320. updateData := map[string]interface{}{}
  321. updateData["search_tencent"], _ = data[1].ToBytes()
  322. updateData["search_buyer"], _ = data[3].ToBytes()
  323. updateData["search_ent"], _ = data[2].ToBytes()
  324. updateData["search_nzj"], _ = data[4].ToBytes()
  325. ok := MC.CrmMysql.UpdateByTx(tx, entity.EMPLOY_SUMMARY, map[string]interface{}{
  326. "position_id": positionId,
  327. }, updateData)
  328. return ok
  329. } else {
  330. //新增
  331. insertData := map[string]interface{}{}
  332. insertData["position_id"] = positionId
  333. insertData["search_tencent"], _ = data[1].ToBytes()
  334. insertData["search_buyer"], _ = data[3].ToBytes()
  335. insertData["search_ent"], _ = data[2].ToBytes()
  336. insertData["search_nzj"], _ = data[4].ToBytes()
  337. ok := MC.CrmMysql.InsertByTx(tx, entity.EMPLOY_SUMMARY, insertData)
  338. return ok > 0
  339. }
  340. }
  341. // EmployKeyFormat 根据收录类型 字段处理 table,findKey,employKey,source
  342. func EmployKeyFormat(employType int64) (string, string, string, int64) {
  343. table := ""
  344. employKey := "company_id"
  345. findKey := "employ_custom_id"
  346. source := int64(0)
  347. switch employType {
  348. case 1:
  349. table = entity.EMPLOY_INFO
  350. employKey = "source_id"
  351. findKey = "employ_info_id"
  352. source = 1
  353. case 2:
  354. table = entity.EMPLOY_CUSTOM
  355. source = 1
  356. case 3:
  357. table = entity.EMPLOY_CUSTOM
  358. source = 2
  359. case 4:
  360. table = entity.EMPLOY_INFO
  361. source = 2
  362. employKey = "source_id"
  363. findKey = "employ_info_id"
  364. }
  365. return table, findKey, employKey, source
  366. }
  367. // IgnoreOperate 忽略操作
  368. func (e *EmPloyService) IgnoreOperate(in *types.IgnoreOperateReq) {
  369. //
  370. table := ""
  371. switch in.EmployType {
  372. case 1, 4:
  373. table = entity.EMPLOY_INFO
  374. case 2, 3:
  375. table = entity.EMPLOY_CUSTOM
  376. }
  377. for _, v := range strings.Split(in.IdArr, ",") {
  378. id := IdFormat(v, in.EmployType)
  379. if id == "" {
  380. log.Println(v, id, in.EmployType, "忽略处理,id解析失败")
  381. break
  382. }
  383. findMap := map[string]interface{}{
  384. "position_id": in.PositionId,
  385. }
  386. if in.EmployType == 3 || in.EmployType == 2 {
  387. findMap["company_id"] = id
  388. } else {
  389. findMap["source_id"] = id
  390. }
  391. if MC.CrmMysql.Count(table, findMap) == 0 {
  392. log.Println(v, id, in.EmployType, "忽略处理,id所对应数据不存在")
  393. break
  394. }
  395. if in.IsIgnore {
  396. MC.CrmMysql.Update(table, findMap, map[string]interface{}{
  397. "is_ignore": 1,
  398. })
  399. } else {
  400. MC.CrmMysql.Update(table, findMap, map[string]interface{}{
  401. "is_ignore": 0,
  402. })
  403. }
  404. }
  405. }
  406. type PersonSmmary struct {
  407. EntUserId int64
  408. Summary map[int64]*roaring.Bitmap
  409. }
  410. // DistributePerson 分发操作
  411. func (e *EmPloyService) DistributePerson(in *types.EmployDistributeReq) bool {
  412. return MC.CrmMysql.ExecTx("收录操作", func(tx *sql.Tx) bool {
  413. personMap := map[int64]PersonSmmary{}
  414. //汇总表查询
  415. for _, person := range in.Person {
  416. personMap[person.PositionId] = PersonSmmary{
  417. EntUserId: person.EntUserId,
  418. //Summary: SummaryFormat(person.PositionId),
  419. }
  420. }
  421. //分发数据处理
  422. infoMap := map[string]map[string]interface{}{}
  423. for positionId, personSmmary := range personMap {
  424. for _, v := range strings.Split(in.EmployIdArr, ",") {
  425. VMap := map[string]interface{}{}
  426. if infoMap[v] == nil {
  427. //新增记录
  428. infoData := MC.CrmMysql.FindOne(entity.EMPLOY_INFO, map[string]interface{}{
  429. "id": v,
  430. }, "", "")
  431. if infoData != nil && len(*infoData) > 0 {
  432. (*infoData)["dis_id"] = v
  433. (*infoData)["is_handle"] = 0
  434. (*infoData)["is_ignore"] = 0
  435. (*infoData)["is_dis"] = 0
  436. (*infoData)["is_create_clue"] = 0
  437. (*infoData)["is_create_chance"] = 0
  438. (*infoData)["is_create_custom"] = 0
  439. (*infoData)["create_time"] = time.Now().Format(date.Date_Full_Layout)
  440. delete(*infoData, "id")
  441. } else {
  442. log.Println(v, "查询不到改收录信息")
  443. break
  444. }
  445. //更改已分发状态
  446. if common.IntAll((*infoData)["is_dis"]) != 1 {
  447. MC.CrmMysql.UpdateByTx(tx, entity.EMPLOY_INFO, map[string]interface{}{
  448. "id": v,
  449. }, map[string]interface{}{
  450. "is_dis": 1,
  451. })
  452. }
  453. infoMap[v] = *infoData
  454. VMap = *infoData
  455. } else {
  456. VMap = infoMap[v]
  457. }
  458. //查询时候收录过
  459. findMap := map[string]interface{}{
  460. "position_id": positionId,
  461. "source_id": VMap["source_id"],
  462. }
  463. if MC.CrmMysql.Count(entity.EMPLOY_INFO, findMap) == 0 {
  464. //新增
  465. //
  466. VMap["ent_user_id"] = personSmmary.EntUserId
  467. VMap["position_id"] = positionId
  468. ok := MC.CrmMysql.InsertByTx(tx, entity.EMPLOY_INFO, VMap)
  469. return ok > 0
  470. /*if ok > 0 {
  471. employType := common.If(common.Int64All(VMap["source"]) == 1, 1, 4)
  472. personSmmary.Summary[common.Int64All(employType)].Add(uint32(mongodb.StringTOBsonId(common.InterfaceToStr(VMap["source_id"])).Timestamp().Unix()))
  473. fool = true
  474. }*/
  475. }
  476. return true
  477. /*if fool {
  478. if !SummarySave(tx, positionId, personSmmary.Summary) {
  479. return false
  480. }
  481. }*/
  482. }
  483. }
  484. return true
  485. })
  486. }
  487. // 提取字符串中的数字
  488. func ExtractNumbers(str string) int64 {
  489. re := regexp.MustCompile(`\d+`)
  490. matches := re.FindAllString(str, -1)
  491. numbers := make([]int, len(matches))
  492. for i, match := range matches {
  493. number, err := strconv.Atoi(match)
  494. if err == nil {
  495. numbers[i] = number
  496. }
  497. }
  498. result := 0
  499. for _, number := range numbers {
  500. result = result*10 + number
  501. }
  502. return int64(result)
  503. }