package service import ( "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/date" "app.yhyue.com/moapp/jybase/encrypt" elastic "app.yhyue.com/moapp/jybase/es" "app.yhyue.com/moapp/jybase/mongodb" "app.yhyue.com/moapp/jypkg/ent/util" "app.yhyue.com/moapp/jypkg/public" MC "bp.jydev.jianyu360.cn/CRM/application/api/common" "bp.jydev.jianyu360.cn/CRM/application/api/internal/types" "bp.jydev.jianyu360.cn/CRM/application/entity" "database/sql" "fmt" "github.com/RoaringBitmap/roaring" "go.mongodb.org/mongo-driver/bson" "time" ) const ( BuyerIndex = "buyer" // 采购单位index BuyerType = "buyer" query = `{%s "query":{"bool":{"must":[%s],"must_not": [{"term": {"buyer_name": ""}}]}}}` ) type EmPloyService struct{} // 收录情况查询 func (e *EmPloyService) InfoEmployinfo(in *types.InfoEmployinfoReq) []map[string]interface{} { data := []map[string]interface{}{} for _, v := range in.Id { vint := int64(0) table := entity.EMPLOY_CUSTOM id := "" source := 0 if in.EmployType == 3 { //判断传过来的是名字还是ID //id转中文 if len(v) > 0 && len([]rune(v)) == len(v) { //此数据是id //获取中文名字 id = util.DecodeId(v) } else { //次数据传的名字 id = getBuyerIdByName(v) } } else { id = util.DecodeId(v) } valueMap := map[string]interface{}{ "id": v, } employKey := "company_id" findKey := "employ_custom_id" //有收录情况 rb := roaring.NewBitmap() bytes := []byte{} byte1 := []byte{} byte2 := []byte{} byte3 := []byte{} byte4 := []byte{} MC.CrmMysql.DB.QueryRow("select search_tencent, search_buyer, search_ent, search_nzj from employ_summary where position_id=?", in.PositionId).Scan(&byte1, &byte2, &byte3, &byte4) if byte1 != nil { search_tencent := roaring.NewBitmap() search_buyer := roaring.NewBitmap() search_ent := roaring.NewBitmap() search_nzj := roaring.NewBitmap() if byte1 != nil && len(byte1) > 0 { search_tencent.UnmarshalBinary(byte1) } if byte2 != nil && len(byte2) > 0 { search_buyer.UnmarshalBinary(byte2) } if byte3 != nil && len(byte3) > 0 { search_ent.UnmarshalBinary(byte3) } if byte4 != nil && len(byte4) > 0 { search_nzj.UnmarshalBinary(byte4) } switch in.EmployType { case 1: bytes = byte1 table = entity.EMPLOY_INFO employKey = "source_id" findKey = "employ_info_id" source = 1 case 2: bytes = byte2 source = 1 case 3: bytes = byte3 source = 2 case 4: bytes = byte4 source = 2 table = entity.EMPLOY_INFO employKey = "source_id" findKey = "employ_info_id" } err := rb.UnmarshalBinary(bytes) if err != nil { break } vint = mongodb.StringTOBsonId(v).Timestamp().Unix() valueMap["isEmploy"] = rb.Contains(uint32(vint)) } if len(in.Id) == 1 { //列表查询 //是否忽略处理 employData := MC.CrmMysql.FindOne(table, map[string]interface{}{ employKey: id, "position_id": in.PositionId, "source": source, }, "", "") if employData != nil && len(*employData) > 0 { valueMap["isIgnore"] = common.Int64All((*employData)["is_ignore"]) } else { valueMap["isIgnore"] = 0 } //客户数量 customCount := MC.CrmMysql.Count(entity.CUSTOM, map[string]interface{}{ findKey: id, "position_id": in.PositionId, }) valueMap["customCount"] = customCount if in.EmployType == 1 || in.EmployType == 4 { //销售线索数量 clueCount := MC.CrmMysql.Count(entity.SALE_CLUE, map[string]interface{}{ "employ_info_id": id, "position_id": in.PositionId, }) valueMap["clueCount"] = clueCount //销售机会数量 chanceCount := MC.CrmMysql.Count(entity.SALE_CHANCE, map[string]interface{}{ "employ_info_id": id, "position_id": in.PositionId, }) valueMap["chanceCount"] = chanceCount } } data = append(data, valueMap) } return data } // 收录操作 func (e *EmPloyService) EmployOperate(in *types.EmployOperateReq) bool { //收录汇总表 employSummaryData := MC.CrmMysql.FindOne(entity.EMPLOY_SUMMARY, map[string]interface{}{ "position_id": in.PositionId, }, "", "") byte1 := []byte{} byte2 := []byte{} byte3 := []byte{} byte4 := []byte{} MC.CrmMysql.DB.QueryRow("select search_tencent, search_buyer, search_ent, search_nzj from employ_summary where position_id=?", in.PositionId).Scan(&byte1, &byte2, &byte3, &byte4) search_tencent := roaring.NewBitmap() search_buyer := roaring.NewBitmap() search_ent := roaring.NewBitmap() search_nzj := roaring.NewBitmap() if byte1 != nil && len(byte1) > 0 { search_tencent.UnmarshalBinary(byte1) } if byte2 != nil && len(byte2) > 0 { search_buyer.UnmarshalBinary(byte2) } if byte3 != nil && len(byte3) > 0 { search_ent.UnmarshalBinary(byte3) } if byte4 != nil && len(byte4) > 0 { search_nzj.UnmarshalBinary(byte4) } table := "" switch in.EmployType { case 1, 4: table = entity.EMPLOY_INFO case 2, 3: table = entity.EMPLOY_CUSTOM } return MC.CrmMysql.ExecTx("收录操作", func(tx *sql.Tx) bool { for _, v1 := range in.IdArr { //id转中文 id := "" name := "" findMap := map[string]interface{}{} findMap["position_id"] = in.PositionId if in.EmployType == 3 { //判断传过来的是名字还是ID //id转中文 if len(v1) > 0 && len([]rune(v1)) == len(v1) { //此数据是id //获取中文名字 id = util.DecodeId(v1) name = getBuyerIdByName(id) } else { //次数据传的名字 name = v1 id = getBuyerIdByName(v1) } findMap["company_id"] = id } else if in.EmployType == 2 { id = encrypt.DecodeArticleId2ByCheck(v1)[0] findMap["company_id"] = id } else { id = encrypt.CommonDecodeArticle("content", v1)[0] findMap["source_id"] = id } if in.IsEmploy { if MC.CrmMysql.Count(table, findMap) > 0 { MC.CrmMysql.UpdateByTx(tx, table, findMap, map[string]interface{}{ "status": 1, }) } else { //收录新增 data := map[string]interface{}{} if in.EmployType == 2 { data = CustomFind(id, "", in.EmployType) } else if in.EmployType == 3 { //id转中文 data = CustomFind("", name, in.EmployType) } else { data = InfoFind(id, in.EmployType) data["type"] = in.SourceType data["source"] = in.EmployType data["employ_way"] = 1 data["jybx_url"] = "/article/content/" + v1 + ".html" } data["position_id"] = in.PositionId data["ent_id"] = in.EntId MC.CrmMysql.InsertByTx(tx, table, data) } } else { //取消收录 MC.CrmMysql.UpdateByTx(tx, table, findMap, map[string]interface{}{ "status": -1, }) } //收录汇总表处理 KeyHandle(mongodb.StringTOBsonId(id).Timestamp().Unix(), in.EmployType, in.IsEmploy, search_tencent, search_buyer, search_ent, search_nzj) } search_tencent_byte, _ := search_tencent.ToBytes() search_buyer_byte, _ := search_buyer.ToBytes() search_ent_byte, _ := search_ent.ToBytes() search_nzj_byte, _ := search_nzj.ToBytes() if employSummaryData != nil && len(*employSummaryData) > 0 { //修改 MC.CrmMysql.UpdateByTx(tx, entity.EMPLOY_SUMMARY, map[string]interface{}{ "position_id": in.PositionId, }, map[string]interface{}{ "search_tencent": search_tencent_byte, "search_buyer": search_buyer_byte, "search_ent": search_ent_byte, "search_nzj": search_nzj_byte, }) } else { //新增 MC.CrmMysql.InsertByTx(tx, entity.EMPLOY_SUMMARY, map[string]interface{}{ "position_id ": in.PositionId, "search_tencent": search_tencent_byte, "search_buyer": search_buyer_byte, "search_ent": search_ent_byte, "search_nzj": search_nzj_byte, }) } return true }) } // 收录数据处理 func KeyHandle(id int64, employType int64, isEmploy bool, search_tencent, search_buyer, search_ent, search_nzj *roaring.Bitmap) { switch employType { case 1: if isEmploy { search_tencent.Add(uint32(id)) } else { search_tencent.Remove(uint32(id)) } case 2: if isEmploy { search_buyer.Add(uint32(id)) } else { search_buyer.Remove(uint32(id)) } case 3: if isEmploy { search_ent.Add(uint32(id)) } else { search_ent.Remove(uint32(id)) } case 4: if isEmploy { search_nzj.Add(uint32(id)) } else { search_nzj.Remove(uint32(id)) } } return } // 企业信息查询 func CustomFind(id string, name string, employType int64) map[string]interface{} { data := map[string]interface{}{} //company_id 企业id name户名称 address 地址 if employType == 2 { //企业详情 entinfo, _ := MC.MgoEnt.FindOneByField("qyxy_std", map[string]interface{}{"_id": id}, map[string]interface{}{ "company_address": 1, //注册地 "company_name": 1, }) if entinfo != nil && len(*entinfo) > 0 { data["name"] = (*entinfo)["company_name"] data["address"] = (*entinfo)["company_address"] data["company_id"] = (*entinfo)["id"] } } else { //采购单位详情 entNameQuery := fmt.Sprintf(`{"query":{"bool":{"must":[{"term":{"buyer_name":"%s"}}]}},"size":1,"_source":["city","buyer_name","id"]}`, name) rs := elastic.Get(BuyerIndex, BuyerType, entNameQuery) if rs != nil && len(*rs) > 0 { data["name"] = (*rs)[0]["buyer_name"] data["address"] = (*rs)[0]["city"] data["company_id"] = (*rs)[0]["id"] } } data["status"] = 1 data["create_time"] = time.Now().Format(date.Date_Full_Layout) return data } // 标讯信息查询 func InfoFind(id string, employType int64) map[string]interface{} { data := map[string]interface{}{} //source_id 信息id、项目id- title 标题-area 省 -city 市 -subtype 信息类型二级分类 //buyerclass 采购单位行业 -budget 预算 -bidamount 中标金额 buyer采购单位 annex有无附件 publishtime发布时间 projectname 项目时间 //ownerclass 业主类型(拟在建搜索) expurasing_time 预计采购时间 jybx_url 标讯详情页 if employType == 1 { //标讯信息 obj := map[string]interface{}{} brobj, ok := MC.Mgo.Find("bidding_rec", bson.M{"s_id": id}, `{"l_recoverydate":-1}`, public.MgoBiddingFields, false, 0, 1) if ok && (*brobj) != nil && len(*brobj) == 1 && (*brobj)[0] != nil { obj = (*brobj)[0] } else { aobj, ok := MC.MgoBidding.FindById("bidding", id, public.MgoBiddingFields) if ok && (aobj == nil || *aobj == nil || len(*aobj) == 0) { aobj, ok = MC.MgoBidding.FindById("bidding_back", id, public.MgoBiddingFields) } obj = *aobj } if ok && obj != nil && len(obj) > 0 { titleTmp := common.ObjToString(obj["title"]) if len([]rune(titleTmp)) > 100 { titleTmp = string([]rune(titleTmp)[:100]) + "..." } titleTmp = public.ClearHtml.ReplaceAllString(titleTmp, "") data["title"] = titleTmp data["city"] = obj["city"] data["area"] = obj["area"] data["subtype"] = obj["subtype"] data["buyerclass"] = obj["buyerclass"] data["budget"] = obj["budget"] data["bidamount"] = obj["bidamount"] data["annex"] = 0 if obj["projectinfo"] != nil { projectinfo := common.ObjToMap(obj["projectinfo"]) if (*projectinfo)["attachments"] != nil { data["annex"] = 1 } } data["publishtime"] = time.Unix(common.Int64All(obj["publishtime"]), 0).Format(date.Date_Full_Layout) data["show_time"] = time.Unix(common.Int64All(obj["publishtime"]), 0).Format(date.Date_Full_Layout) data["projectname"] = obj["projectname"] procurementlist, _ := obj["procurementlist"].([]interface{}) for _, vs := range procurementlist { //1.采购意向清单数据集打码处理 vsMap, _ := vs.(map[string]interface{}) if vsMap["expurasingtime"] != nil { data["expurasing_time"] = vsMap["expurasingtime"] break } } } } data["status"] = 1 data["employ_way"] = 1 data["source_id"] = id data["create_time"] = time.Now().Format(date.Date_Full_Layout) return data } func getBuyerNameById(buyerId string) (buyerName string) { r := elastic.Get(BuyerIndex, BuyerType, fmt.Sprintf(`{"query":{"bool":{"must":[{"terms":{"_id":["%s"]}}]}},"size":1,"_source":["name"]}`, buyerId)) if r == nil || len(*r) == 0 { return } buyerName, _ = (*r)[0]["name"].(string) return } func getBuyerIdByName(buyeName string) (buyerId string) { r := elastic.Get("buyer", "buyer", fmt.Sprintf(`{"query":{"bool":{"must":[{"term":{"buyer_name":"%s"}}]}},"size":1,"_source":["city","id"]}`, buyeName)) if r == nil || len(*r) == 0 { return } buyerId, _ = (*r)[0]["_id"].(string) return }