power.go 16 KB

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