power.go 14 KB

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