power.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "math/rand"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "app.yhyue.com/moapp/jybase/common"
  11. "app.yhyue.com/moapp/jybase/date"
  12. "app.yhyue.com/moapp/jybase/redis"
  13. "bp.jydev.jianyu360.cn/BaseService/powerCheckCenter/entity"
  14. )
  15. //权益
  16. type PowerService struct {
  17. *entity.Conn
  18. }
  19. //
  20. func NewPower(conn *entity.Conn) *PowerService {
  21. return &PowerService{
  22. conn,
  23. }
  24. }
  25. var level_map = map[int64]string{
  26. 1: "专家版",
  27. 2: "智慧版",
  28. 3: "商机版",
  29. 4: "试用版",
  30. 5: "自定义",
  31. 6: "商机版2.0",
  32. 7: "专家版2.0",
  33. }
  34. /*
  35. 权益相关
  36. userid //mongodb用户id
  37. baseUserId //base_user用户id
  38. accountId //账户id
  39. entId =5; //企业id
  40. positionType //职位类型 0个人 1企业
  41. positionId //职位id
  42. 切换到企业身份后,session中userId存的是企业雇员的职位id,个人身份,userId还是原来mgo库里的_id
  43. */
  44. func (this *PowerService) Power(userid string, baseUserId, accountId, entId, positionType, positionId int64) *entity.Power {
  45. ent := &entity.Ent{}
  46. entniche := &entity.Entniche{}
  47. vip := &entity.Vip{}
  48. member := &entity.Member{}
  49. free := &entity.Free{}
  50. userPower := entity.Power{}
  51. cacheBl := false
  52. if bytes, err := redis.GetBytes("newother", GetRedisName(positionId)); err == nil && bytes != nil {
  53. if err := json.Unmarshal(*bytes, &userPower); err == nil {
  54. cacheBl = true
  55. }
  56. }
  57. if !cacheBl {
  58. //获取用户本身的注册时间和邮箱、这个与个人、企业无关
  59. mgoUserFields := map[string]interface{}{
  60. "s_myemail": 1,
  61. "l_registedate": 1,
  62. "s_phone": 1,
  63. "s_m_phone": 1,
  64. "base_user_id": 1,
  65. }
  66. //大会员权益表查询条件
  67. bigmemberServiceUserQuery := map[string]interface{}{"s_userid": userid, "i_status": 0}
  68. //个人身份 取user表中权益
  69. if positionType == 0 {
  70. mgoUserFields["i_vip_status"] = 1
  71. mgoUserFields["l_vip_starttime"] = 1
  72. mgoUserFields["l_vip_endtime"] = 1
  73. mgoUserFields["o_vipjy"] = 1
  74. mgoUserFields["i_member_status"] = 1
  75. mgoUserFields["i_member_give"] = 1
  76. mgoUserFields["s_member_mainid"] = 1
  77. mgoUserFields["i_member_sub_status"] = 1
  78. mgoUserFields["i_member_trial"] = 1
  79. mgoUserFields["o_member_jy"] = 1
  80. mgoUserFields["o_jy"] = 1
  81. mgoUserFields["i_ts_guide"] = 1
  82. mgoUserFields["i_member_apppushunread"] = 1
  83. mgoUserFields["i_entniche_apppushunread"] = 1
  84. mgoUserFields["i_apppushunread"] = 1
  85. } else {
  86. bigmemberServiceUserQuery["s_userid"] = positionId
  87. }
  88. data, ok := this.Conn.MgoJy.FindById("user", userid, mgoUserFields)
  89. if ok && data != nil && len(*data) > 0 {
  90. //基本信息
  91. registeDate := common.Int64All((*data)["l_registedate"]) //注册时间
  92. phone, _ := common.If((*data)["s_phone"] != nil, (*data)["s_phone"], (*data)["s_m_phone"]).(string) //
  93. ent.PrivateGD = this.Conn.Mysql.CountBySql(`select count(1) from privatedata where phone = ?`, phone) > 0 //广东移动DICT 用户
  94. mail := common.ObjToString((*data)["s_myemail"])
  95. i_ts_guide := common.Int64All((*data)["i_ts_guide"])
  96. i_member_apppushunread := common.Int64All((*data)["i_member_apppushunread"])
  97. i_entniche_apppushunread := common.Int64All((*data)["i_entniche_apppushunread"])
  98. i_apppushunread := common.Int64All((*data)["i_apppushunread"])
  99. free.Mail = mail
  100. free.Registedate = registeDate
  101. free.Phone = phone
  102. free.TsGuide = i_ts_guide
  103. free.Apppushunread = i_apppushunread
  104. free.EntnicheApppushunread = i_entniche_apppushunread
  105. free.MemberApppushunread = i_member_apppushunread
  106. //个人权益
  107. if positionType == 0 {
  108. o_jy, _ := (*data)["o_jy"].(map[string]interface{})
  109. a_key, _ := o_jy["a_key"].([]interface{})
  110. free.FreeHasKey = len(a_key) > 0
  111. free.OjyLength = int64(len(o_jy))
  112. if common.IntAll((o_jy)["i_newfree"]) > 0 || entity.IsNewFreeTimeCell < registeDate {
  113. //IsNewFreeTimeCell dev3.6.4版本之前发了个紧急版本处理老用户订阅问题,i_newfree字段必须用户选择地区才能生成,不能作为判断是否是新用户得唯一标识,在此版本添加了常量:IsNewFreeTimeCell作为判断标准;--ws
  114. free.IsUpgrade = true //新免费用户
  115. }
  116. //超级订阅
  117. vipStatus := common.Int64All((*data)["i_vip_status"])
  118. vipStartTime := common.Int64All((*data)["l_vip_starttime"])
  119. vipEndTime := common.Int64All((*data)["l_vip_endtime"])
  120. ovipjy := common.ObjToMap((*data)["o_vipjy"])
  121. oBuyset := common.ObjToMap((*ovipjy)["o_buyset"])
  122. upgrade := common.Int64All((*oBuyset)["upgrade"])
  123. areacount := common.Int64All((*oBuyset)["areacount"])
  124. buyerclasscount := common.Int64All((*oBuyset)["buyerclasscount"])
  125. newcitys, _ := (*oBuyset)["newcitys"].([]interface{})
  126. newcitysArr := common.InterfaceArrToint64Arr(newcitys)
  127. vip = &entity.Vip{
  128. Status: vipStatus,
  129. StartTime: vipStartTime,
  130. EndTime: vipEndTime,
  131. Upgrade: upgrade,
  132. Areacount: areacount,
  133. Buyerclasscount: buyerclasscount,
  134. MaxKeyLength: 300,
  135. PowerType: common.Int64All(common.If(vipStatus > 0, 1, 0)),
  136. NewCitys: newcitysArr,
  137. }
  138. if (*ovipjy)["a_items"] != nil {
  139. a_items := common.ObjArrToMapArr((*ovipjy)["a_items"].([]interface{}))
  140. vip.HasKey = HasKey(a_items)
  141. }
  142. //个人member
  143. i_member_sub_status := common.IntAllDef((*data)["i_member_sub_status"], 0)
  144. memberStatus := common.Int64All((*data)["i_member_status"])
  145. memberStarttime := common.Int64All((*data)["i_member_starttime"])
  146. memberEndtime := common.Int64All((*data)["i_member_endtime"])
  147. member = &entity.Member{
  148. Status: memberStatus,
  149. StartTime: memberStarttime,
  150. EndTime: memberEndtime,
  151. MaxKeyLength: 300, //最大关键词数量限制
  152. PowerType: common.Int64All(common.If(memberStatus > 0, 1, 0)),
  153. }
  154. omemberjy := common.ObjToMap((*data)["o_member_jy"])
  155. if (*omemberjy)["a_items"] != nil {
  156. if arr, ok := (*omemberjy)["a_items"].([]interface{}); ok {
  157. a_items := common.ObjArrToMapArr(arr)
  158. member.HasKey = HasKey(a_items)
  159. }
  160. }
  161. if memberStatus > 0 {
  162. member.MemberPower = 1 //是否分配了大会员;0:否 1:是
  163. }
  164. if (*data)["s_member_mainid"] != nil && common.ObjToString((*data)["s_member_mainid"]) != "" && i_member_sub_status > 0 {
  165. member.Pid = common.ObjToString((*data)["s_member_mainid"])
  166. bigmemberServiceUserQuery["s_userid"] = member.Pid
  167. member.IsSubCount = 1
  168. }
  169. if (member.Pid != "" && common.IntAllDef((*data)["i_member_sub_status"], 0) == 1) || this.Conn.MgoJy.Count("member", map[string]interface{}{"userid": userid}) > 0 {
  170. member.Used = true
  171. }
  172. if (*data)["i_member_trial"] != nil {
  173. member.IsMemberTrial = 1
  174. }
  175. //获取大会员版本名称
  176. member.MemberName = level_map[memberStatus]
  177. } else if positionType == 1 {
  178. entnicheUserId := this.GetEntnicheUserId(entId, phone)
  179. //免费
  180. fdata, _ := this.Conn.MgoJy.FindOne("entniche_rule", map[string]interface{}{
  181. "i_ent": entId,
  182. "i_userid": entnicheUserId,
  183. "i_type": 2, //0:商机管理 1:超级订阅/大会员 2:免费
  184. })
  185. o_jy, _ := (*fdata)["o_entniche"].(map[string]interface{})
  186. free.OjyLength = int64(len(o_jy))
  187. a_key, _ := o_jy["a_key"].([]interface{})
  188. free.FreeHasKey = len(a_key) > 0
  189. if common.IntAll((o_jy)["i_newfree"]) > 0 || entity.IsNewFreeTimeCell < registeDate { //IsNewFreeTimeCell dev3.6.4版本之前发了个紧急版本处理老用户订阅问题,i_newfree字段必须用户选择地区才能生成,不能作为判断是否是新用户得唯一标识,在此版本添加了常量:IsNewFreeTimeCell作为判断标准;--ws
  190. free.IsUpgrade = true //新免费用户
  191. }
  192. //企业
  193. edata, _ := this.Conn.MgoJy.FindOne("entniche_rule", map[string]interface{}{
  194. "i_ent": entId,
  195. "i_userid": entnicheUserId,
  196. "i_type": 1, //0:商机管理 1:超级订阅/大会员
  197. "l_endtime": map[string]interface{}{
  198. "$gt": time.Now().Unix(),
  199. },
  200. })
  201. eudata, _ := this.Conn.MgoJy.FindOne("ent_user", map[string]interface{}{
  202. "i_entid": entId,
  203. "i_userid": entnicheUserId,
  204. })
  205. if eudata != nil && len(*eudata) > 0 {
  206. i_ts_guide := common.Int64All((*eudata)["i_ts_guide"])
  207. free.TsGuide = i_ts_guide
  208. haskey := false
  209. o_entniche := &map[string]interface{}{}
  210. if edata != nil && len(*edata) > 0 {
  211. o_entniche = common.ObjToMap((*edata)["o_entniche"])
  212. if (*o_entniche)["a_items"] != nil {
  213. if arr, ok := (*o_entniche)["a_items"].([]interface{}); ok {
  214. a_items := common.ObjArrToMapArr(arr)
  215. haskey = HasKey(a_items)
  216. }
  217. }
  218. }
  219. if common.Int64All((*eudata)["i_vip_status"]) > 0 {
  220. oBuyset := common.ObjToMap((*o_entniche)["o_buyset"])
  221. areacount := common.Int64All((*oBuyset)["areacount"])
  222. buyerclasscount := common.Int64All((*oBuyset)["buyerclasscount"])
  223. newcitys, _ := (*oBuyset)["newcitys"].([]interface{})
  224. newcitysArr := common.InterfaceArrToint64Arr(newcitys)
  225. status := common.Int64All((*eudata)["i_vip_status"])
  226. starttime := common.Int64All((*eudata)["l_vip_starttime"])
  227. endtime := common.Int64All((*eudata)["l_vip_endtime"])
  228. vip = &entity.Vip{
  229. Status: status,
  230. StartTime: starttime,
  231. EndTime: endtime,
  232. Upgrade: 1, //企业版默认是升级版
  233. Areacount: areacount,
  234. Buyerclasscount: buyerclasscount,
  235. MaxKeyLength: 300,
  236. PowerType: 2,
  237. HasKey: haskey,
  238. NewCitys: newcitysArr,
  239. }
  240. }
  241. if common.Int64All((*eudata)["i_member_status"]) > 0 {
  242. member.PowerType = 2
  243. status := common.Int64All((*eudata)["i_member_status"])
  244. starttime := common.Int64All((*eudata)["i_member_starttime"])
  245. endtime := common.Int64All((*eudata)["i_member_endtime"])
  246. member = &entity.Member{
  247. Status: status,
  248. StartTime: starttime,
  249. EndTime: endtime,
  250. MemberName: level_map[status],
  251. PowerType: 2,
  252. HasKey: haskey,
  253. }
  254. //是否使用
  255. s_positionId := strconv.Itoa(int(positionId))
  256. if this.Conn.MgoJy.Count("member", map[string]interface{}{"userid": s_positionId}) > 0 {
  257. member.Used = true
  258. }
  259. }
  260. }
  261. }
  262. //大会员相关权益数量
  263. memberServiceMap := map[int64]bool{}
  264. serviceList := this.Conn.Mysql.Find("bigmember_service_user", bigmemberServiceUserQuery, "DISTINCT(s_serviceid),i_frequency", "", -1, -1)
  265. if serviceList != nil && len(*serviceList) != 0 {
  266. pCount, eCount, dailyNum, customers := 0, 0, 0, 10
  267. for _, item := range *serviceList {
  268. serviceid := common.Int64All(item["s_serviceid"])
  269. memberServiceMap[serviceid] = true
  270. member.MemberPowerList = append(member.MemberPowerList, serviceid)
  271. if serviceid == 14 { //项目数量
  272. pCount = common.IntAll(item["i_frequency"])
  273. } else if serviceid == 4 || serviceid == 12 || serviceid == 13 { //企业情报监控 企业中标动态
  274. tEcount := common.IntAll(item["i_frequency"])
  275. if tEcount > eCount {
  276. eCount = tEcount
  277. }
  278. } else if serviceid == 17 || serviceid == 18 { //每日数据包
  279. dailyNum = common.IntAll(item["i_frequency"])
  280. } else if serviceid == 7 { //潜在客户 关注客户
  281. customers = common.IntAll(item["i_frequency"])
  282. }
  283. }
  284. member.EntNum = int64(eCount)
  285. member.ProNum = int64(pCount)
  286. member.DailyNum = int64(dailyNum)
  287. member.Customers = int64(customers)
  288. }
  289. //
  290. if phone != "" {
  291. //查询是否是商机管理付费用户
  292. res := this.Conn.Mysql.SelectBySql(`SELECT i. STATUS AS status,i.model , i.isNew, i.power_source, r.role_id, u.power, i.name,i.id,i.startdate,i.enddate,i.auth_status,i.auth_reason,i.dept_subscribe FROM (entniche_user u LEFT JOIN entniche_user_role r ON r.user_id = u.id ) LEFT JOIN entniche_info i ON u.ent_id = i.id WHERE u.phone = ? and i.id = ? ORDER BY i. STATUS DESC, i.auth_status DESC`, phone, entId)
  293. if res != nil && len(*res) > 0 {
  294. for _, v := range *res {
  295. if common.IntAll(v["id"]) == 0 {
  296. continue
  297. }
  298. entnicheStatus := common.Int64All(v["status"])
  299. entnichePower := common.Int64All(v["power"])
  300. if entnicheStatus == 1 {
  301. entnicheStatus = entnichePower
  302. }
  303. entniche.IsNew = common.Int64All(v["isNew"])
  304. entniche.Status = entnicheStatus //企业是否有商机管理权限
  305. entniche.IsEntPower = entnichePower //个人是否有商机管理权限
  306. entniche.PowerSource = common.Int64All(v["power_source"])
  307. entniche.StartTime = common.Int64All(v["startdate"])
  308. entniche.EndTime = common.Int64All(v["enddate"])
  309. entniche.Model = common.Int64All(v["model"])
  310. //
  311. ent.Name = common.ObjToString(v["name"])
  312. ent.EntRoleId = common.Int64All(v["role_id"])
  313. ent.EntAuthStatus = common.Int64All(v["auth_status"])
  314. ent.EntAuthReason = common.ObjToString(v["auth_reason"])
  315. ent.DeptSubscribe = common.Int64All(v["dept_subscribe"])
  316. //判断企业是否购买企业版相关
  317. eweData := this.Conn.Mysql.SelectBySql(`select * from entniche_wait_empower where ent_id=? and end_time>?`, entId, time.Now().Format(date.Date_Full_Layout))
  318. if eweData != nil && len(*eweData) > 0 {
  319. for _, v := range *eweData {
  320. product_type := common.ObjToString(v["product_type"])
  321. if strings.Contains(product_type, entity.ProductType_member) {
  322. ent.BuyMember = 1
  323. } else if strings.Contains(product_type, entity.ProductType_vip) {
  324. ent.BuyVip = 1
  325. }
  326. }
  327. }
  328. //企业管理员
  329. if ent.EntRoleId == 1 && ((ent.BuyMember == 1 || ent.BuyVip == 1) || (entniche.Status == 1 && entniche.PowerSource == 0)) {
  330. ent.EntSubscribeManager = 1
  331. }
  332. //部门管理员
  333. if ent.EntRoleId == 2 && entniche.Status == 1 && entniche.PowerSource == 0 && ent.DeptSubscribe == 1 {
  334. ent.EntSubscribeManager = 1
  335. }
  336. //
  337. if ent.EntRoleId == 1 || ent.EntRoleId == 2 {
  338. if (ent.BuyMember == 1 || ent.BuyVip == 1) || (entniche.Status == 1 && entniche.PowerSource == 0) {
  339. ent.EntSubscribe = 1
  340. }
  341. }
  342. }
  343. }
  344. }
  345. if vip.Status <= 0 && member.Status <= 0 && entniche.Status <= 0 {
  346. free.IsFree = true
  347. } else {
  348. free.IsFree = false
  349. }
  350. }
  351. userPower = entity.Power{
  352. Vip: vip,
  353. Member: member,
  354. Free: free,
  355. Ent: ent,
  356. Entniche: entniche,
  357. }
  358. }
  359. //存储缓存
  360. go func() {
  361. if bytes, err := json.Marshal(userPower); err == nil && bytes != nil {
  362. oneDayMore := 60*60*24 + rand.Intn(60*60)
  363. _ = redis.PutBytes("newother", GetRedisName(positionId), &bytes, oneDayMore)
  364. }
  365. }()
  366. //实时更新的数据 没法存缓存
  367. //免费用户在企业画像/采购单位画像/附件下载留资
  368. userPower.Free.FreeEntPort, userPower.Free.FreeBuyerPort, userPower.Free.FreeFile = FreeExperience(userid, positionId, positionType)
  369. //
  370. notBigFileBool := userPower.Member.Status <= 0 || !strings.Contains(intStringsJoin(userPower.Member.MemberPowerList), "3")
  371. uk := common.If(notBigFileBool && userPower.Vip.Status > 0 && userPower.Vip.Upgrade > 0, "v", "f").(string)
  372. userPower.Vip.FileNum = FileNum(uk, userid, positionId, positionType)
  373. return &userPower
  374. }
  375. //免费用户体验会员功能权限
  376. //免费用户在企业画像/采购单位画像/附件下载留资 留资成功后用户才有功能使用次数
  377. func FreeExperience(userId string, positionId, positionType int64) (int64, int64, int64) {
  378. if positionType == 1 {
  379. userId = strconv.Itoa(int(positionId))
  380. }
  381. entPortKey := common.Int64All(redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.PowerCacheEntPortKey, userId)))
  382. buyerPortKey := common.Int64All(redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.PowerCacheBuyerPortKey, userId)))
  383. fileKey := common.Int64All(redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.PowerCacheFileKey, userId)))
  384. return entPortKey, buyerPortKey, fileKey
  385. }
  386. //获取附件下载次数
  387. func FileNum(uk, userid string, positionId, positionType int64) int64 {
  388. if positionType == 1 {
  389. userid = strconv.Itoa(int(positionId))
  390. }
  391. fileUploadNum := map[string]int{
  392. "f": 0,
  393. "v": 10,
  394. }
  395. //附件下载包的剩余次数
  396. filePackKey := fmt.Sprintf(entity.FilePackNumKey, userid, fmt.Sprint(time.Now().Month()))
  397. filePackNum := redis.GetInt(entity.PowerCacheDb, filePackKey)
  398. num := fileUploadNum[uk] - redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.VipFileUploadNumKey, userid, fmt.Sprint(time.Now().Month()))) + filePackNum
  399. return int64(num)
  400. }
  401. /*
  402. 判断是企业或个人
  403. 1.企业
  404. entniche_rule
  405. 查询 vip/member free ent entniche
  406. 2.个人
  407. 查询vip member free
  408. */
  409. func (this *PowerService) GetEntnicheUserId(entId int64, phone string) int64 {
  410. data := this.Conn.Mysql.SelectBySql(`select id from entniche_user where phone =? and ent_id =?`, phone, entId)
  411. if data != nil && len(*data) > 0 {
  412. return common.Int64All((*data)[0]["id"])
  413. }
  414. return -1
  415. }
  416. //是否有关键词
  417. func HasKey(a_items []map[string]interface{}) bool {
  418. for _, v := range a_items {
  419. akey, _ := v["a_key"].([]interface{})
  420. if len(akey) > 0 {
  421. return true
  422. }
  423. }
  424. return false
  425. }
  426. //获取redis key
  427. func GetRedisName(positionId int64) string {
  428. if positionId == 0 {
  429. return ""
  430. }
  431. return fmt.Sprintf("user_power_info_%v", positionId)
  432. }
  433. //清除redis缓存
  434. func (this *PowerService) DelRedisPower(positionId int64) bool {
  435. if positionId == 0 {
  436. return false
  437. }
  438. return redis.Del("newother", GetRedisName(positionId))
  439. }
  440. func intStringsJoin(is []int64) string {
  441. var str = []string{}
  442. for _, v := range is {
  443. str = append(str, strconv.Itoa(int(v)))
  444. }
  445. return strings.Join(str, ",")
  446. }