power.go 17 KB

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