power.go 18 KB

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