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