power.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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/mongodb"
  12. "app.yhyue.com/moapp/jybase/redis"
  13. "bp.jydev.jianyu360.cn/BaseService/powerCheckCenter/entity"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. // 权益
  17. type PowerService struct {
  18. *entity.Conn
  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. logx.Info("-----req------", userid, baseUserId, accountId, entId, positionType, positionId)
  46. ent := &entity.Ent{}
  47. entniche := &entity.Entniche{}
  48. vip := &entity.Vip{}
  49. member := &entity.Member{}
  50. free := &entity.Free{}
  51. userPower := entity.Power{}
  52. docs := &entity.Docs{}
  53. if positionId == 0 {
  54. free.IsFree = true
  55. return &entity.Power{
  56. Vip: vip,
  57. Member: member,
  58. Free: free,
  59. Ent: ent,
  60. Entniche: entniche,
  61. Docs: docs,
  62. }
  63. }
  64. cacheBl := false
  65. if bytes, err := redis.GetBytes("newother", GetRedisName(positionId)); err == nil && bytes != nil {
  66. if err := json.Unmarshal(*bytes, &userPower); err == nil {
  67. cacheBl = true
  68. }
  69. }
  70. if !cacheBl {
  71. if userid == "" && positionId > 0 {
  72. logx.Info("检测到异常数据:", userid, baseUserId, accountId, entId, positionType, positionId)
  73. if baseUserId <= 0 {
  74. //获取mongodb的userid
  75. pd := this.Conn.BaseMysql.SelectBySql(`select user_id from base_service.base_position where id =? limit 1`, positionId)
  76. if pd != nil && len(*pd) > 0 {
  77. baseUserId = common.Int64All((*pd)[0]["user_id"])
  78. }
  79. }
  80. if baseUserId > 0 {
  81. ud, ok := this.Conn.MgoJy.FindOneByField("user", map[string]interface{}{"base_user_id": baseUserId}, `{"_id:1"}`)
  82. if ud != nil && ok && len(*ud) > 0 {
  83. userid = mongodb.BsonIdToSId((*ud)["_id"])
  84. }
  85. }
  86. entity.Alert(fmt.Sprintf("检测到异常数据已修复: userId:%s ,positionId:%v ,positionType:%v </br>", userid, positionId, positionType))
  87. logx.Info("检测到异常数据修复后:", userid, baseUserId, accountId, entId, positionType, positionId)
  88. }
  89. //获取用户本身的注册时间和邮箱、这个与个人、企业无关
  90. mgoUserFields := map[string]interface{}{
  91. "s_myemail": 1,
  92. "l_registedate": 1,
  93. "s_phone": 1,
  94. "s_m_phone": 1,
  95. "base_user_id": 1,
  96. }
  97. var userId = userid
  98. //大会员权益表查询条件
  99. bigmemberServiceUserQuery := map[string]interface{}{"s_userid": userid, "i_status": 0}
  100. //个人身份 取user表中权益
  101. if positionType == 0 {
  102. mgoUserFields["i_vip_status"] = 1
  103. mgoUserFields["l_vip_starttime"] = 1
  104. mgoUserFields["l_vip_endtime"] = 1
  105. mgoUserFields["o_vipjy"] = 1
  106. mgoUserFields["i_member_status"] = 1
  107. mgoUserFields["i_member_give"] = 1
  108. mgoUserFields["s_member_mainid"] = 1
  109. mgoUserFields["i_member_sub_status"] = 1
  110. mgoUserFields["i_member_trial"] = 1
  111. mgoUserFields["o_member_jy"] = 1
  112. mgoUserFields["o_jy"] = 1
  113. mgoUserFields["i_ts_guide"] = 1
  114. mgoUserFields["i_member_apppushunread"] = 1
  115. mgoUserFields["i_entniche_apppushunread"] = 1
  116. mgoUserFields["i_apppushunread"] = 1
  117. mgoUserFields["i_member_starttime"] = 1
  118. mgoUserFields["i_member_endtime"] = 1
  119. mgoUserFields["i_doc_status"] = 1
  120. mgoUserFields["l_doc_starttime"] = 1
  121. mgoUserFields["l_doc_endtime"] = 1
  122. mgoUserFields["i_doc_free_download"] = 1
  123. } else {
  124. bigmemberServiceUserQuery["s_userid"] = positionId
  125. }
  126. data, ok := this.Conn.MgoJy.FindById("user", userid, mgoUserFields)
  127. if ok && data != nil && len(*data) > 0 {
  128. //基本信息
  129. registeDate := common.Int64All((*data)["l_registedate"]) //注册时间
  130. phone, _ := common.If((*data)["s_phone"] != nil, (*data)["s_phone"], (*data)["s_m_phone"]).(string) //
  131. ent.PrivateGD = this.Conn.Mysql.CountBySql(`select count(1) from privatedata where phone = ?`, phone) > 0 //广东移动DICT 用户
  132. mail := common.ObjToString((*data)["s_myemail"])
  133. i_ts_guide := common.Int64All((*data)["i_ts_guide"])
  134. i_member_apppushunread := common.Int64All((*data)["i_member_apppushunread"])
  135. i_entniche_apppushunread := common.Int64All((*data)["i_entniche_apppushunread"])
  136. i_apppushunread := common.Int64All((*data)["i_apppushunread"])
  137. free.Mail = mail
  138. free.Registedate = registeDate
  139. free.Phone = phone
  140. free.TsGuide = i_ts_guide
  141. free.Apppushunread = i_apppushunread
  142. free.EntnicheApppushunread = i_entniche_apppushunread
  143. free.MemberApppushunread = i_member_apppushunread
  144. //个人权益
  145. if positionType == 0 {
  146. o_jy, _ := (*data)["o_jy"].(map[string]interface{})
  147. a_key, _ := o_jy["a_key"].([]interface{})
  148. free.FreeHasKey = len(a_key) > 0
  149. free.OjyLength = int64(len(o_jy))
  150. if common.IntAll((o_jy)["i_newfree"]) > 0 || entity.IsNewFreeTimeCell < registeDate {
  151. //IsNewFreeTimeCell dev3.6.4版本之前发了个紧急版本处理老用户订阅问题,i_newfree字段必须用户选择地区才能生成,不能作为判断是否是新用户得唯一标识,在此版本添加了常量:IsNewFreeTimeCell作为判断标准;--ws
  152. free.IsUpgrade = true //新免费用户
  153. }
  154. //省份订阅包
  155. free.PpStatus = common.Int64All(o_jy["i_ppstatus"])
  156. free.PpStartTime = common.Int64All(o_jy["l_areaStart_p"])
  157. free.PpEndTime = common.Int64All(o_jy["l_areaEnd_p"])
  158. if ppBuyset, _ := o_jy["o_buyset_p"].(map[string]interface{}); ppBuyset != nil && len(ppBuyset) > 0 {
  159. free.PpAreaCount = common.Int64All(ppBuyset["areacount"])
  160. }
  161. //超级订阅
  162. vipStatus := common.Int64All((*data)["i_vip_status"])
  163. vipStartTime := common.Int64All((*data)["l_vip_starttime"])
  164. vipEndTime := common.Int64All((*data)["l_vip_endtime"])
  165. ovipjy := common.ObjToMap((*data)["o_vipjy"])
  166. oBuyset := common.ObjToMap((*ovipjy)["o_buyset"])
  167. upgrade := common.Int64All((*oBuyset)["upgrade"])
  168. areacount := common.Int64All((*oBuyset)["areacount"])
  169. buyerclasscount := common.Int64All((*oBuyset)["buyerclasscount"])
  170. newcitys, _ := (*oBuyset)["newcitys"].([]interface{})
  171. newcitysArr := ConfirmInt64Arr(newcitys)
  172. vip = &entity.Vip{
  173. Status: vipStatus,
  174. StartTime: vipStartTime,
  175. EndTime: vipEndTime,
  176. Upgrade: upgrade,
  177. Areacount: areacount,
  178. Buyerclasscount: buyerclasscount,
  179. MaxKeyLength: 300,
  180. PowerType: common.Int64All(common.If(vipStatus > 0, 1, 0)),
  181. NewCitys: newcitysArr,
  182. }
  183. if (*ovipjy)["a_items"] != nil {
  184. a_items := common.ObjArrToMapArr((*ovipjy)["a_items"].([]interface{}))
  185. vip.HasKey = HasKey(a_items)
  186. }
  187. //个人member
  188. i_member_sub_status := common.IntAllDef((*data)["i_member_sub_status"], 0)
  189. memberStatus := common.Int64All((*data)["i_member_status"])
  190. memberStarttime := common.Int64All((*data)["i_member_starttime"])
  191. memberEndtime := common.Int64All((*data)["i_member_endtime"])
  192. member = &entity.Member{
  193. Status: memberStatus,
  194. StartTime: memberStarttime,
  195. EndTime: memberEndtime,
  196. MaxKeyLength: 300, //最大关键词数量限制
  197. PowerType: common.Int64All(common.If(memberStatus > 0, 1, 0)),
  198. }
  199. omemberjy := common.ObjToMap((*data)["o_member_jy"])
  200. if (*omemberjy)["a_items"] != nil {
  201. if arr, ok := (*omemberjy)["a_items"].([]interface{}); ok {
  202. a_items := common.ObjArrToMapArr(arr)
  203. member.HasKey = HasKey(a_items)
  204. }
  205. }
  206. if memberStatus > 0 {
  207. member.MemberPower = 1 //是否分配了大会员;0:否 1:是
  208. }
  209. logx.Info("in--------------------->", (*data)["s_member_mainid"] != nil && common.ObjToString((*data)["s_member_mainid"]) != "" && i_member_sub_status > 0)
  210. if (*data)["s_member_mainid"] != nil && common.ObjToString((*data)["s_member_mainid"]) != "" && i_member_sub_status > 0 {
  211. member.Pid = common.ObjToString((*data)["s_member_mainid"])
  212. bigmemberServiceUserQuery["s_userid"] = member.Pid
  213. member.IsSubCount = 1
  214. }
  215. if (member.Pid != "" && common.IntAllDef((*data)["i_member_sub_status"], 0) == 1) || this.Conn.MgoJy.Count("member", map[string]interface{}{"userid": userid}) > 0 {
  216. member.Used = true
  217. }
  218. if (*data)["i_member_trial"] != nil {
  219. member.IsMemberTrial = 1
  220. }
  221. //获取大会员版本名称
  222. member.MemberName = level_map[memberStatus]
  223. //文库
  224. docs = &entity.Docs{
  225. DocStatus: common.Int64All((*data)["i_doc_status"]),
  226. StartDate: common.Int64All((*data)["l_doc_starttime"]),
  227. EndDate: common.Int64All((*data)["l_doc_endtime"]),
  228. FreeDownload: common.Int64All((*data)["i_doc_free_download"]),
  229. }
  230. } else if positionType == 1 {
  231. userId = strconv.FormatInt(positionId, 10)
  232. entnicheUserId := this.GetEntnicheUserId(entId, phone)
  233. //免费
  234. fdata, _ := this.Conn.MgoJy.FindOne("entniche_rule", map[string]interface{}{
  235. "i_entid": entId,
  236. "i_userid": entnicheUserId,
  237. "i_type": 2, //0:商机管理 1:超级订阅/大会员 2:免费
  238. })
  239. o_jy, _ := (*fdata)["o_entniche"].(map[string]interface{})
  240. free.OjyLength = int64(len(o_jy))
  241. a_key, _ := o_jy["a_key"].([]interface{})
  242. free.FreeHasKey = len(a_key) > 0
  243. if common.IntAll(o_jy["i_newfree"]) > 0 || entity.IsNewFreeTimeCell < registeDate { //IsNewFreeTimeCell dev3.6.4版本之前发了个紧急版本处理老用户订阅问题,i_newfree字段必须用户选择地区才能生成,不能作为判断是否是新用户得唯一标识,在此版本添加了常量:IsNewFreeTimeCell作为判断标准;--ws
  244. free.IsUpgrade = true //新免费用户
  245. }
  246. //企业
  247. edata, _ := this.Conn.MgoJy.FindOne("entniche_rule", map[string]interface{}{
  248. "i_entid": entId,
  249. "i_userid": entnicheUserId,
  250. "i_type": 1, //0:商机管理 1:超级订阅/大会员
  251. })
  252. eudata, _ := this.Conn.MgoJy.FindOne("ent_user", map[string]interface{}{
  253. "i_entid": entId,
  254. "i_userid": entnicheUserId,
  255. })
  256. //省份订阅包
  257. free.PpStatus = common.Int64All(o_jy["i_ppstatus"])
  258. free.PpStartTime = common.Int64All(o_jy["l_areaStart_p"])
  259. free.PpEndTime = common.Int64All(o_jy["l_areaEnd_p"])
  260. if ppBuyset, _ := o_jy["o_buyset_p"].(map[string]interface{}); ppBuyset != nil && len(ppBuyset) > 0 {
  261. free.PpAreaCount = common.Int64All(ppBuyset["areacount"])
  262. }
  263. if eudata != nil && len(*eudata) > 0 {
  264. i_ts_guide := common.Int64All((*eudata)["i_ts_guide"])
  265. free.TsGuide = i_ts_guide
  266. haskey := false
  267. o_entniche := &map[string]interface{}{}
  268. if edata != nil && len(*edata) > 0 {
  269. o_entniche = common.ObjToMap((*edata)["o_entniche"])
  270. if (*o_entniche)["a_items"] != nil {
  271. if arr, ok := (*o_entniche)["a_items"].([]interface{}); ok {
  272. a_items := common.ObjArrToMapArr(arr)
  273. haskey = HasKey(a_items)
  274. }
  275. }
  276. }
  277. if common.Int64All((*eudata)["i_vip_status"]) > 0 {
  278. oBuyset := common.ObjToMap((*o_entniche)["o_buyset"])
  279. areacount := common.Int64All((*oBuyset)["areacount"])
  280. buyerclasscount := common.Int64All((*oBuyset)["buyerclasscount"])
  281. newcitys, _ := (*oBuyset)["newcitys"].([]interface{})
  282. newcitysArr := ConfirmInt64Arr(newcitys)
  283. status := common.Int64All((*eudata)["i_vip_status"])
  284. starttime := common.Int64All((*eudata)["l_vip_starttime"])
  285. endtime := common.Int64All((*eudata)["l_vip_endtime"])
  286. vip = &entity.Vip{
  287. Status: status,
  288. StartTime: starttime,
  289. EndTime: endtime,
  290. Upgrade: 1, //企业版默认是升级版
  291. Areacount: areacount,
  292. Buyerclasscount: buyerclasscount,
  293. MaxKeyLength: 300,
  294. PowerType: 2,
  295. HasKey: haskey,
  296. NewCitys: newcitysArr,
  297. }
  298. }
  299. if common.Int64All((*eudata)["i_member_status"]) > 0 {
  300. member.PowerType = 2
  301. status := common.Int64All((*eudata)["i_member_status"])
  302. starttime := common.Int64All((*eudata)["i_member_starttime"])
  303. endtime := common.Int64All((*eudata)["i_member_endtime"])
  304. member = &entity.Member{
  305. Status: status,
  306. StartTime: starttime,
  307. EndTime: endtime,
  308. MemberName: level_map[status],
  309. PowerType: 2,
  310. HasKey: haskey,
  311. }
  312. //是否使用
  313. s_positionId := strconv.Itoa(int(positionId))
  314. if this.Conn.MgoJy.Count("member", map[string]interface{}{"userid": s_positionId}) > 0 {
  315. member.Used = true
  316. }
  317. }
  318. //文库
  319. docs = &entity.Docs{
  320. DocStatus: common.Int64All((*eudata)["i_doc_status"]),
  321. StartDate: common.Int64All((*eudata)["l_doc_starttime"]),
  322. EndDate: common.Int64All((*eudata)["l_doc_endtime"]),
  323. FreeDownload: common.Int64All((*eudata)["i_doc_free_download"]),
  324. }
  325. }
  326. }
  327. //大会员相关权益数量
  328. memberServiceMap := map[int64]bool{}
  329. serviceList := this.Conn.Mysql.Find("bigmember_service_user", bigmemberServiceUserQuery, "DISTINCT(s_serviceid),i_frequency", "", -1, -1)
  330. logx.Info("-----bigmemberServiceUserQuery-------", bigmemberServiceUserQuery)
  331. if serviceList != nil && len(*serviceList) != 0 {
  332. pCount, eCount, dailyNum, customers, buyerNum := 0, 0, 0, 10, 0
  333. for _, item := range *serviceList {
  334. serviceid := common.Int64All(item["s_serviceid"])
  335. service := this.Conn.Mysql.FindOne("bigmember_service", map[string]interface{}{
  336. "id": serviceid,
  337. }, "", "")
  338. memberServiceMap[serviceid] = true
  339. member.MemberPowerList = append(member.MemberPowerList, serviceid)
  340. if serviceid == 14 { //项目数量
  341. pCount = common.IntAll(item["i_frequency"])
  342. } else if serviceid == 4 || serviceid == 12 || serviceid == 13 { //企业情报监控 企业中标动态
  343. tEcount := common.IntAll(item["i_frequency"])
  344. if tEcount > eCount {
  345. eCount = tEcount
  346. }
  347. } else if common.IntAll((*service)["i_class"]) == 4 { //每日数据包
  348. dailyNum = common.IntAll(item["i_frequency"])
  349. } else if serviceid == 7 { //潜在客户 关注客户
  350. customers = common.IntAll(item["i_frequency"])
  351. } else if serviceid == 24 || serviceid == 25 { //业主监控
  352. bCount := common.IntAll(item["i_frequency"])
  353. if bCount > buyerNum {
  354. buyerNum = bCount
  355. }
  356. }
  357. }
  358. member.EntNum = int64(eCount)
  359. member.ProNum = int64(pCount)
  360. member.DailyNum = int64(dailyNum)
  361. member.Customers = int64(customers)
  362. member.BuyerNum = int64(buyerNum)
  363. }
  364. //
  365. if phone != "" {
  366. //查询是否是商机管理付费用户
  367. 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)
  368. if res != nil && len(*res) > 0 {
  369. for _, v := range *res {
  370. if common.IntAll(v["id"]) == 0 {
  371. continue
  372. }
  373. entnicheStatus := common.Int64All(v["status"])
  374. entnichePower := common.Int64All(v["power"])
  375. if entnicheStatus == 1 {
  376. entnicheStatus = entnichePower
  377. }
  378. entniche.IsNew = common.Int64All(v["isNew"])
  379. entniche.Status = entnicheStatus //企业是否有商机管理权限
  380. entniche.IsEntPower = entnichePower //个人是否有商机管理权限
  381. entniche.PowerSource = common.Int64All(v["power_source"])
  382. entniche.StartTime = common.Int64All(v["startdate"])
  383. entniche.EndTime = common.Int64All(v["enddate"])
  384. entniche.Model = common.Int64All(v["model"])
  385. //
  386. ent.Name = common.ObjToString(v["name"])
  387. ent.EntRoleId = common.Int64All(v["role_id"])
  388. ent.EntAuthStatus = common.Int64All(v["auth_status"])
  389. ent.EntAuthReason = common.ObjToString(v["auth_reason"])
  390. ent.DeptSubscribe = common.Int64All(v["dept_subscribe"])
  391. //判断企业是否购买企业版相关
  392. eweData := this.Conn.Mysql.SelectBySql(`select * from entniche_wait_empower where ent_id=? and end_time>?`, entId, time.Now().Format(date.Date_Full_Layout))
  393. if eweData != nil && len(*eweData) > 0 {
  394. ent.Services = true
  395. for _, v := range *eweData {
  396. product_type := common.ObjToString(v["product_type"])
  397. if strings.Contains(product_type, entity.ProductType_member) {
  398. ent.BuyMember = 1
  399. } else if strings.Contains(product_type, entity.ProductType_vip) {
  400. ent.BuyVip = 1
  401. }
  402. }
  403. }
  404. //企业管理员
  405. if ent.EntRoleId == 1 && ((ent.BuyMember == 1 || ent.BuyVip == 1) || (entniche.Status == 1 && entniche.PowerSource == 0)) {
  406. ent.EntSubscribeManager = 1
  407. }
  408. //部门管理员
  409. if ent.EntRoleId == 2 && entniche.Status == 1 && entniche.PowerSource == 0 && ent.DeptSubscribe == 1 {
  410. ent.EntSubscribeManager = 1
  411. }
  412. //
  413. if ent.EntRoleId == 1 || ent.EntRoleId == 2 {
  414. if (ent.BuyMember == 1 || ent.BuyVip == 1) || (entniche.Status == 1 && entniche.PowerSource == 0) {
  415. ent.EntSubscribe = 1
  416. }
  417. }
  418. }
  419. }
  420. }
  421. if vip.Status <= 0 && member.Status <= 0 && (entniche.Status <= 0 || (entniche.Status > 0 && entniche.PowerSource == 1)) {
  422. free.IsFree = true
  423. } else {
  424. free.IsFree = false
  425. }
  426. //查看原文
  427. var (
  428. originalData = this.Conn.BaseMysql.SelectBySql(`SELECT user_type,total FROM jianyu.original_power WHERE state = 0`)
  429. )
  430. if originalData != nil && len(*originalData) > 0 {
  431. for _, ov := range *originalData {
  432. total := common.Int64All(ov["total"])
  433. switch common.IntAll(ov["user_type"]) {
  434. case 1:
  435. vip.Original = total
  436. case 2:
  437. member.Original = total
  438. case 3:
  439. entniche.Original = total
  440. default:
  441. free.Original = total
  442. }
  443. }
  444. }
  445. //文库 非文库会员,确认免费下载机会状态 免费下载机会没有被消耗
  446. //FreeDownload 0:未留资 有一次留资免费机会;2:已留资 下载机会未使用
  447. if docs.DocStatus <= 0 && docs.FreeDownload == 0 {
  448. sc := this.MgoJy.Count("saleLeads", map[string]interface{}{"userid": userId, "source": map[string]interface{}{"$in": []string{"pc_library_details_free", "app_library_details_free", "wx_library_details_free", "h5_library_details_free"}}})
  449. if sc > 0 {
  450. docs.FreeDownload = 2 //已留资 下载机会未使用
  451. }
  452. }
  453. }
  454. userPower = entity.Power{
  455. Vip: vip,
  456. Member: member,
  457. Free: free,
  458. Ent: ent,
  459. Entniche: entniche,
  460. Docs: docs,
  461. }
  462. }
  463. //存储缓存
  464. go func() {
  465. if bytes, err := json.Marshal(userPower); err == nil && bytes != nil {
  466. oneDayMore := 60*60*24 + rand.Intn(60*60)
  467. _ = redis.PutBytes("newother", GetRedisName(positionId), &bytes, oneDayMore)
  468. }
  469. }()
  470. //实时更新的数据 没法存缓存
  471. //免费用户在企业画像/采购单位画像/附件下载留资
  472. userPower.Free.FreeEntPort, userPower.Free.FreeBuyerPort, userPower.Free.FreeFile = FreeExperience(userid, positionId, positionType)
  473. //
  474. notBigFileBool := userPower.Member.Status <= 0 || !strings.Contains(intStringsJoin(userPower.Member.MemberPowerList), "3")
  475. uk := common.If(notBigFileBool && userPower.Vip.Status > 0 && userPower.Vip.Upgrade > 0, "v", "f").(string)
  476. userPower.Vip.FileNum = FileNum(uk, userid, positionId, positionType)
  477. return &userPower
  478. }
  479. // 免费用户体验会员功能权限
  480. // 免费用户在企业画像/采购单位画像/附件下载留资 留资成功后用户才有功能使用次数
  481. func FreeExperience(userId string, positionId, positionType int64) (int64, int64, int64) {
  482. if positionType == 1 {
  483. userId = strconv.Itoa(int(positionId))
  484. }
  485. entPortKey := common.Int64All(redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.PowerCacheEntPortKey, userId)))
  486. buyerPortKey := common.Int64All(redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.PowerCacheBuyerPortKey, userId)))
  487. fileKey := common.Int64All(redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.PowerCacheFileKey, userId)))
  488. return entPortKey, buyerPortKey, fileKey
  489. }
  490. // 获取附件下载次数
  491. func FileNum(uk, userid string, positionId, positionType int64) int64 {
  492. if positionType == 1 {
  493. userid = strconv.Itoa(int(positionId))
  494. }
  495. fileUploadNum := map[string]int{
  496. "f": 0,
  497. "v": 10,
  498. }
  499. //附件下载包的剩余次数
  500. filePackKey := fmt.Sprintf(entity.FilePackNumKey, userid, fmt.Sprint(time.Now().Month()))
  501. filePackNum := redis.GetInt(entity.PowerCacheDb, filePackKey)
  502. num := fileUploadNum[uk] - redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.VipFileUploadNumKey, userid, fmt.Sprint(time.Now().Month()))) + filePackNum
  503. return int64(num)
  504. }
  505. /*
  506. 判断是企业或个人
  507. 1.企业
  508. entniche_rule
  509. 查询 vip/member free ent entniche
  510. 2.个人
  511. 查询vip member free
  512. */
  513. func (this *PowerService) GetEntnicheUserId(entId int64, phone string) int64 {
  514. data := this.Conn.Mysql.SelectBySql(`select id from entniche_user where phone =? and ent_id =?`, phone, entId)
  515. if data != nil && len(*data) > 0 {
  516. return common.Int64All((*data)[0]["id"])
  517. }
  518. return -1
  519. }
  520. // 是否有关键词
  521. func HasKey(a_items []map[string]interface{}) bool {
  522. for _, v := range a_items {
  523. akey, _ := v["a_key"].([]interface{})
  524. if len(akey) > 0 {
  525. return true
  526. }
  527. }
  528. return false
  529. }
  530. // 获取redis key
  531. func GetRedisName(positionId int64) string {
  532. if positionId == 0 {
  533. return ""
  534. }
  535. return fmt.Sprintf("user_power_info_%v", positionId)
  536. }
  537. // 清除redis缓存
  538. func (this *PowerService) DelRedisPower(positionId int64) bool {
  539. if positionId == 0 {
  540. return false
  541. }
  542. return redis.Del("newother", GetRedisName(positionId))
  543. }
  544. func intStringsJoin(is []int64) string {
  545. var str = []string{}
  546. for _, v := range is {
  547. str = append(str, strconv.Itoa(int(v)))
  548. }
  549. return strings.Join(str, ",")
  550. }
  551. func ConfirmInt64Arr(arr []interface{}) []int64 {
  552. tmp := make([]int64, 0)
  553. for _, v := range arr {
  554. tmp = append(tmp, common.Int64All(v))
  555. }
  556. return tmp
  557. }