employService.go 16 KB

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