EmployService.go 15 KB

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