employService.go 16 KB

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