employService.go 16 KB

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