employService.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. subtype := common.InterfaceToStr(obj["subtype"])
  220. if toptype == "采购意向" || toptype == "预告" || toptype == "招标" {
  221. data["type"] = 2
  222. } else {
  223. if subtype == "废标" || subtype == "流标" || subtype == "其它" {
  224. data["type"] = 2
  225. } else if subtype == "成交" || subtype == "中标" || subtype == "合同" || subtype == "验收" || subtype == "违规" {
  226. data["type"] = 3
  227. } else {
  228. data["type"] = 1
  229. }
  230. }
  231. //data["type"] = in.SourceType
  232. if obj["projectinfo"] != nil {
  233. projectinfo := common.ObjToMap(obj["projectinfo"])
  234. if (*projectinfo)["attachments"] != nil {
  235. data["annex"] = 1
  236. }
  237. }
  238. data["publishtime"] = time.Unix(common.Int64All(obj["publishtime"]), 0).Format(date.Date_Full_Layout)
  239. data["show_time"] = time.Unix(common.Int64All(obj["publishtime"]), 0).Format(date.Date_Full_Layout)
  240. data["projectname"] = obj["projectname"]
  241. procurementlist, _ := obj["procurementlist"].([]interface{})
  242. for _, vs := range procurementlist { //1.采购意向清单数据集打码处理
  243. vsMap, _ := vs.(map[string]interface{})
  244. if vsMap["expurasingtime"] != nil {
  245. data["expurasing_time"] = vsMap["expurasingtime"]
  246. break
  247. }
  248. }
  249. }
  250. }
  251. data["status"] = 1
  252. data["employ_way"] = 1
  253. data["source_id"] = id
  254. data["create_time"] = time.Now().Format(date.Date_Full_Layout)
  255. return data
  256. }
  257. // GetBuyerIdByName 通过采购单位名称获取id
  258. func GetBuyerIdByName(buyerName string) (buyerId string) {
  259. r := elastic.Get(BuyerIndex, BuyerType, fmt.Sprintf(`{"query":{"bool":{"must":[{"term":{"buyer_name":"%s"}}]}},"size":1,"_source":["city","id"]}`, buyerName))
  260. if r == nil || len(*r) == 0 {
  261. return
  262. }
  263. buyerId, _ = (*r)[0]["_id"].(string)
  264. return
  265. }
  266. // IdFormat id解密
  267. func IdFormat(encryptionId string, employType int64) string {
  268. decryptId := ""
  269. switch employType {
  270. case 1, 2:
  271. decryptId = util.DecodeId(encryptionId)
  272. case 3:
  273. if len(encryptionId) > 0 && len([]rune(encryptionId)) == len(encryptionId) {
  274. //此数据是id
  275. //获取中文名字
  276. decryptId = util.DecodeId(encryptionId)
  277. } else {
  278. //次数据传的名字
  279. decryptId = GetBuyerIdByName(encryptionId)
  280. }
  281. }
  282. return decryptId
  283. }
  284. // SummaryFormat 汇总表处理
  285. func SummaryFormat(positionId int64) map[int64]*roaring.Bitmap {
  286. var byte1 []byte
  287. var byte2 []byte
  288. var byte3 []byte
  289. var byte4 []byte
  290. 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)
  291. if err != nil {
  292. return nil
  293. }
  294. rb1 := roaring.NewBitmap()
  295. rb2 := roaring.NewBitmap()
  296. rb3 := roaring.NewBitmap()
  297. rb4 := roaring.NewBitmap()
  298. data := map[int64]*roaring.Bitmap{}
  299. if byte1 != nil && len(byte1) > 0 {
  300. rb1.UnmarshalBinary(byte1)
  301. }
  302. if byte2 != nil && len(byte2) > 0 {
  303. rb2.UnmarshalBinary(byte2)
  304. }
  305. if byte3 != nil && len(byte3) > 0 {
  306. rb3.UnmarshalBinary(byte3)
  307. }
  308. if byte4 != nil && len(byte4) > 0 {
  309. rb4.UnmarshalBinary(byte4)
  310. }
  311. data[1] = rb1
  312. data[2] = rb2
  313. data[3] = rb3
  314. data[4] = rb4
  315. return data
  316. }
  317. // SummarySave 汇总表保存
  318. func SummarySave(tx *sql.Tx, positionId int64, data map[int64]*roaring.Bitmap) bool {
  319. employSummaryData := MC.CrmMysql.FindOne(entity.EMPLOY_SUMMARY, map[string]interface{}{
  320. "position_id": positionId,
  321. }, "", "")
  322. if employSummaryData != nil && len(*employSummaryData) > 0 {
  323. //修改
  324. updateData := map[string]interface{}{}
  325. updateData["search_tencent"], _ = data[1].ToBytes()
  326. updateData["search_buyer"], _ = data[3].ToBytes()
  327. updateData["search_ent"], _ = data[2].ToBytes()
  328. updateData["search_nzj"], _ = data[4].ToBytes()
  329. ok := MC.CrmMysql.UpdateByTx(tx, entity.EMPLOY_SUMMARY, map[string]interface{}{
  330. "position_id": positionId,
  331. }, updateData)
  332. return ok
  333. } else {
  334. //新增
  335. insertData := map[string]interface{}{}
  336. insertData["position_id"] = positionId
  337. insertData["search_tencent"], _ = data[1].ToBytes()
  338. insertData["search_buyer"], _ = data[3].ToBytes()
  339. insertData["search_ent"], _ = data[2].ToBytes()
  340. insertData["search_nzj"], _ = data[4].ToBytes()
  341. ok := MC.CrmMysql.InsertByTx(tx, entity.EMPLOY_SUMMARY, insertData)
  342. return ok > 0
  343. }
  344. }
  345. // EmployKeyFormat 根据收录类型 字段处理 table,findKey,employKey,source
  346. func EmployKeyFormat(employType int64) (string, string, string, int64) {
  347. table := ""
  348. employKey := "company_id"
  349. findKey := "employ_custom_id"
  350. source := int64(0)
  351. switch employType {
  352. case 1:
  353. table = entity.EMPLOY_INFO
  354. employKey = "source_id"
  355. findKey = "employ_info_id"
  356. source = 1
  357. case 2:
  358. table = entity.EMPLOY_CUSTOM
  359. source = 1
  360. case 3:
  361. table = entity.EMPLOY_CUSTOM
  362. source = 2
  363. case 4:
  364. table = entity.EMPLOY_INFO
  365. source = 2
  366. employKey = "source_id"
  367. findKey = "employ_info_id"
  368. }
  369. return table, findKey, employKey, source
  370. }
  371. // IgnoreOperate 忽略操作
  372. func (e *EmPloyService) IgnoreOperate(in *types.IgnoreOperateReq) {
  373. //
  374. table := ""
  375. switch in.EmployType {
  376. case 1, 4:
  377. table = entity.EMPLOY_INFO
  378. case 2, 3:
  379. table = entity.EMPLOY_CUSTOM
  380. }
  381. for _, v := range strings.Split(in.IdArr, ",") {
  382. id := IdFormat(v, in.EmployType)
  383. if id == "" {
  384. log.Println(v, id, in.EmployType, "忽略处理,id解析失败")
  385. break
  386. }
  387. findMap := map[string]interface{}{
  388. "position_id": in.PositionId,
  389. }
  390. if in.EmployType == 3 || in.EmployType == 2 {
  391. findMap["company_id"] = id
  392. } else {
  393. findMap["source_id"] = id
  394. }
  395. if MC.CrmMysql.Count(table, findMap) == 0 {
  396. log.Println(v, id, in.EmployType, "忽略处理,id所对应数据不存在")
  397. break
  398. }
  399. if in.IsIgnore {
  400. MC.CrmMysql.Update(table, findMap, map[string]interface{}{
  401. "is_ignore": 1,
  402. })
  403. } else {
  404. MC.CrmMysql.Update(table, findMap, map[string]interface{}{
  405. "is_ignore": 0,
  406. })
  407. }
  408. }
  409. }
  410. type PersonSmmary struct {
  411. EntUserId int64
  412. Summary map[int64]*roaring.Bitmap
  413. }
  414. // DistributePerson 分发操作
  415. func (e *EmPloyService) DistributePerson(in *types.EmployDistributeReq) bool {
  416. return MC.CrmMysql.ExecTx("收录操作", func(tx *sql.Tx) bool {
  417. personMap := map[int64]PersonSmmary{}
  418. //汇总表查询
  419. for _, person := range in.Person {
  420. personMap[person.PositionId] = PersonSmmary{
  421. EntUserId: person.EntUserId,
  422. //Summary: SummaryFormat(person.PositionId),
  423. }
  424. }
  425. //分发数据处理
  426. infoMap := map[string]map[string]interface{}{}
  427. for positionId, personSmmary := range personMap {
  428. for _, v := range strings.Split(in.EmployIdArr, ",") {
  429. VMap := map[string]interface{}{}
  430. if infoMap[v] == nil {
  431. //新增记录
  432. infoData := MC.CrmMysql.FindOne(entity.EMPLOY_INFO, map[string]interface{}{
  433. "id": v,
  434. }, "", "")
  435. if infoData != nil && len(*infoData) > 0 {
  436. (*infoData)["dis_id"] = v
  437. (*infoData)["is_handle"] = 0
  438. (*infoData)["is_ignore"] = 0
  439. (*infoData)["is_dis"] = 0
  440. (*infoData)["is_create_clue"] = 0
  441. (*infoData)["is_create_chance"] = 0
  442. (*infoData)["is_create_custom"] = 0
  443. (*infoData)["create_time"] = time.Now().Format(date.Date_Full_Layout)
  444. delete(*infoData, "id")
  445. } else {
  446. log.Println(v, "查询不到改收录信息")
  447. break
  448. }
  449. //更改已分发状态
  450. if common.IntAll((*infoData)["is_dis"]) != 1 {
  451. MC.CrmMysql.UpdateByTx(tx, entity.EMPLOY_INFO, map[string]interface{}{
  452. "id": v,
  453. }, map[string]interface{}{
  454. "is_dis": 1,
  455. })
  456. }
  457. infoMap[v] = *infoData
  458. VMap = *infoData
  459. } else {
  460. VMap = infoMap[v]
  461. }
  462. //查询时候收录过
  463. findMap := map[string]interface{}{
  464. "position_id": positionId,
  465. "source_id": VMap["source_id"],
  466. }
  467. if MC.CrmMysql.Count(entity.EMPLOY_INFO, findMap) == 0 {
  468. //新增
  469. //
  470. VMap["ent_user_id"] = personSmmary.EntUserId
  471. VMap["position_id"] = positionId
  472. ok := MC.CrmMysql.InsertByTx(tx, entity.EMPLOY_INFO, VMap)
  473. return ok > 0
  474. /*if ok > 0 {
  475. employType := common.If(common.Int64All(VMap["source"]) == 1, 1, 4)
  476. personSmmary.Summary[common.Int64All(employType)].Add(uint32(mongodb.StringTOBsonId(common.InterfaceToStr(VMap["source_id"])).Timestamp().Unix()))
  477. fool = true
  478. }*/
  479. }
  480. return true
  481. /*if fool {
  482. if !SummarySave(tx, positionId, personSmmary.Summary) {
  483. return false
  484. }
  485. }*/
  486. }
  487. }
  488. return true
  489. })
  490. }
  491. // 提取字符串中的数字
  492. func ExtractNumbers(str string) int64 {
  493. re := regexp.MustCompile(`\d+`)
  494. matches := re.FindAllString(str, -1)
  495. numbers := make([]int, len(matches))
  496. for i, match := range matches {
  497. number, err := strconv.Atoi(match)
  498. if err == nil {
  499. numbers[i] = number
  500. }
  501. }
  502. result := 0
  503. for _, number := range numbers {
  504. result = result*10 + number
  505. }
  506. return int64(result)
  507. }