power.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package service
  2. import (
  3. "fmt"
  4. "log"
  5. "strings"
  6. "time"
  7. "app.yhyue.com/moapp/jybase/common"
  8. "app.yhyue.com/moapp/jybase/date"
  9. "app.yhyue.com/moapp/jybase/redis"
  10. "bp.jydev.jianyu360.cn/BaseService/powerCheckCenter/entity"
  11. )
  12. //权益
  13. type PowerService struct {
  14. *entity.Conn
  15. }
  16. //
  17. func NewPower(conn *entity.Conn) *PowerService {
  18. return &PowerService{
  19. conn,
  20. }
  21. }
  22. //权益相关 userid用户mongodbid baseUserId
  23. func (this *PowerService) Power(userid string, baseUserId, accountId, entId int64) *entity.Power {
  24. //获取权益
  25. //大会员状态
  26. data, ok := this.Conn.MgoJy.FindById("user", userid, `{"l_vip_starttime":1,"l_vip_endtime":1,"s_phone":1,"s_m_phone":1,i_member_status":1,"i_member_give":1,"s_member_mainid":1,"i_member_sub_status":1,"i_member_trial":1,"i_vip_status":1,"o_vipjy":1,"o_jy":1,"l_registedate":1,"s_myemail":1,"base_user_id":1}`)
  27. if ok && *data != nil && len(*data) > 0 {
  28. ent := &entity.Ent{}
  29. entniche := &entity.Entniche{}
  30. vip := &entity.Vip{}
  31. member := &entity.Member{}
  32. registeDate := common.Int64All((*data)["l_registedate"]) //注册时间
  33. //VIP
  34. vipStatus := common.Int64All((*data)["i_vip_status"])
  35. vipStartTime := common.Int64All((*data)["l_vip_starttime"])
  36. vipEndTime := common.Int64All((*data)["l_vip_endtime"])
  37. ovipjy := common.ObjToMap((*data)["o_vipjy"])
  38. oBuyset := common.ObjToMap((*ovipjy)["o_buyset"])
  39. upgrade := common.Int64All((*oBuyset)["oBuyset"])
  40. areacount := common.Int64All((*oBuyset)["areacount"])
  41. buyerclasscount := common.Int64All((*oBuyset)["buyerclasscount"])
  42. vip = &entity.Vip{
  43. Status: vipStatus,
  44. StartTime: vipStartTime,
  45. EndTime: vipEndTime,
  46. Upgrade: upgrade,
  47. Areacount: areacount,
  48. Buyerclasscount: buyerclasscount,
  49. MaxKeyLength: 300,
  50. PowerType: common.Int64All(common.If(vipStatus > 0, 1, 0)),
  51. //TODO 附件下载次数
  52. }
  53. //免费相关
  54. free := &entity.Free{}
  55. o_jy, _ := (*data)["o_jy"].(map[string]interface{})
  56. a_key, _ := o_jy["a_key"].([]interface{})
  57. free.FreeHasKey = len(a_key) > 0
  58. //免费用户可查看企业画像次数//免费用户可查看采购单位画像次数//免费用户可以进行附件下载次数
  59. freeEntPort, freeBuyerPort, freeFile := FreeExperience(userid)
  60. free.FreeEntPort, free.FreeBuyerPort, free.FreeFile = int64(freeEntPort), int64(freeBuyerPort), int64(freeFile)
  61. //
  62. //子账号被启用
  63. i_member_sub_status := common.IntAllDef((*data)["i_member_sub_status"], 0)
  64. memberStatus := common.Int64All((*data)["i_member_status"])
  65. memberStarttime := common.Int64All((*data)["i_member_starttime"])
  66. memberEndtime := common.Int64All((*data)["i_member_endtime"])
  67. member = &entity.Member{
  68. Status: memberStatus,
  69. StartTime: memberStarttime,
  70. EndTime: memberEndtime,
  71. MaxKeyLength: 300, //最大关键词数量限制
  72. }
  73. if memberStatus > 0 {
  74. member.MemberPower = 1 //是否分配了大会员;0:否 1:是
  75. }
  76. if (*data)["s_member_mainid"] != nil && common.ObjToString((*data)["s_member_mainid"]) != "" && i_member_sub_status > 0 {
  77. member.Pid = common.ObjToString((*data)["s_member_mainid"])
  78. member.IsSubCount = 1
  79. }
  80. if (member.Pid != "" && common.IntAllDef((*data)["i_member_sub_status"], 0) == 1) || this.Conn.MgoJy.Count("member", map[string]interface{}{"userid": userid}) > 0 {
  81. member.Used = true
  82. }
  83. if (*data)["i_member_trial"] != nil {
  84. member.IsMemberTrial = 1
  85. }
  86. //获取大会员版本名称 //TODO 自定义有问题
  87. if combo := this.Conn.Mysql.SelectBySql(`select s_name from bigmember_combo where id=?`, memberStatus); combo != nil && len(*combo) > 0 {
  88. member.MemberName = common.ObjToString((*combo)[0]["s_name"])
  89. }
  90. memberServiceMap := map[int64]bool{}
  91. //获取大会员相关操作
  92. serviceList := this.Conn.Mysql.Find("bigmember_service_user", map[string]interface{}{"s_userid": userid, "i_status": 0}, "DISTINCT(s_serviceid),i_frequency", "", -1, -1)
  93. if serviceList != nil && len(*serviceList) != 0 {
  94. pCount, eCount, dailyNum, customers := 0, 0, 0, 10
  95. for _, item := range *serviceList {
  96. serviceid := common.Int64All(item["s_serviceid"])
  97. memberServiceMap[serviceid] = true
  98. member.MemberPowerList = append(member.MemberPowerList, serviceid)
  99. if serviceid == 14 { //项目数量
  100. pCount = common.IntAll(item["i_frequency"])
  101. } else if serviceid == 4 || serviceid == 12 || serviceid == 13 { //企业情报监控 企业中标动态
  102. tEcount := common.IntAll(item["i_frequency"])
  103. if tEcount > eCount {
  104. eCount = tEcount
  105. }
  106. } else if serviceid == 17 || serviceid == 18 { //每日数据包
  107. dailyNum = common.IntAll(item["i_frequency"])
  108. } else if serviceid == 7 { //潜在客户 关注客户
  109. customers = common.IntAll(item["i_frequency"])
  110. }
  111. }
  112. member.EntNum = int64(eCount)
  113. member.ProNum = int64(pCount)
  114. member.DailyNum = int64(dailyNum)
  115. member.Customers = int64(customers)
  116. }
  117. //
  118. phone, _ := common.If((*data)["s_phone"] != nil, (*data)["s_phone"], (*data)["s_m_phone"]).(string)
  119. if phone != "" {
  120. //用于判断是否含有企业主体的超级订阅、大会员服务
  121. epowerData := this.Conn.Mysql.SelectBySql(`SELECT c.product_type FROM entniche_user a INNER JOIN entniche_power b
  122. INNER JOIN entniche_wait_empower c
  123. ON a.id =b.ent_user_id AND c.id = b.wait_empower_id WHERE a.phone =? AND c.end_time >?`, phone, time.Now().Format(date.Date_Full_Layout))
  124. if epowerData != nil && len(*epowerData) > 0 {
  125. for _, v := range *epowerData {
  126. product_type := common.ObjToString(v["product_type"])
  127. if strings.Contains(product_type, entity.ProductType_vip) {
  128. vip.PowerType = 2
  129. } else if strings.Contains(product_type, entity.ProductType_member) {
  130. member.PowerType = 2
  131. }
  132. }
  133. }
  134. //免费用户画像和附件下载权限
  135. o_jy := common.ObjToMap((*data)["o_jy"])
  136. //IsNewFreeTimeCell dev3.6.4版本之前发了个紧急版本处理老用户订阅问题,i_newfree字段必须用户选择地区才能生成,不能作为判断是否是新用户得唯一标识,在此版本添加了常量:IsNewFreeTimeCell作为判断标准;--ws
  137. if common.IntAll((*o_jy)["i_newfree"]) > 0 || entity.IsNewFreeTimeCell < registeDate {
  138. free.IsUpgrade = true //新免费用户
  139. }
  140. //查询是否是商机管理付费用户
  141. 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)
  142. if res != nil && len(*res) > 0 {
  143. for _, v := range *res {
  144. if common.IntAll(v["id"]) == 0 {
  145. continue
  146. }
  147. entniche.IsNew = common.Int64All(v["isNew"])
  148. entniche.Status = common.Int64All(v["status"])
  149. entniche.IsEntPower = common.Int64All(v["power"])
  150. entniche.PowerSource = common.Int64All(v["power_source"])
  151. entniche.StartTime = common.Int64All(v["startdate"])
  152. entniche.EndTime = common.Int64All(v["enddate"])
  153. //
  154. ent.Name = common.ObjToString((*res)[0]["name"])
  155. ent.EntRoleId = common.Int64All(v["role_id"])
  156. ent.EntAuthStatus = common.Int64All(v["auth_status"])
  157. ent.EntAuthReason = common.ObjToString(v["auth_reason"])
  158. ent.DeptSubscribe = common.Int64All(v["dept_subscribe"])
  159. //判断企业是否购买企业版相关
  160. eweData := this.Conn.Mysql.SelectBySql(`select * from entniche_wait_empower where ent_id=? and end_time>?`, entId, time.Now().Format(date.Date_Full_Layout))
  161. if eweData != nil && len(*eweData) > 0 {
  162. for _, v := range *eweData {
  163. product_type := common.ObjToString(v["product_type"])
  164. if strings.Contains(product_type, entity.ProductType_member) {
  165. ent.BuyMember = 1
  166. } else if strings.Contains(product_type, entity.ProductType_vip) {
  167. ent.BuyVip = 1
  168. }
  169. }
  170. }
  171. //企业管理员
  172. if ent.EntRoleId == 1 && ((ent.BuyMember == 1 || ent.BuyVip == 1) || (entniche.Status == 1 && entniche.PowerSource == 0)) {
  173. ent.EntSubscribeManager = 1
  174. }
  175. //部门管理员
  176. if ent.EntRoleId == 2 && entniche.Status == 1 && entniche.PowerSource == 0 && ent.DeptSubscribe == 1 {
  177. ent.EntSubscribeManager = 1
  178. }
  179. //
  180. if ent.EntRoleId == 1 || ent.EntRoleId == 2 {
  181. if (ent.BuyMember == 1 || ent.BuyVip == 1) || (entniche.Status == 1 && entniche.PowerSource == 0) {
  182. ent.EntSubscribe = 1
  183. }
  184. }
  185. }
  186. //广东移动DICT 用户
  187. ent.PrivateGD = this.Conn.Mysql.CountBySql(`select count(1) from privatedata where phone = ?`, phone) > 0
  188. }
  189. }
  190. log.Println("vip:", vip)
  191. log.Println("member:", member)
  192. log.Println("free:", free)
  193. log.Println("ent:", ent)
  194. log.Println("entniche:", entniche)
  195. return &entity.Power{
  196. Vip: vip,
  197. Member: member,
  198. Free: free,
  199. Ent: ent,
  200. Entniche: entniche,
  201. }
  202. }
  203. return nil
  204. }
  205. //免费用户体验会员功能权限
  206. //免费用户在企业画像/采购单位画像/附件下载留资 留资成功后用户才有功能使用次数
  207. func FreeExperience(userId string) (int, int, int) {
  208. return redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.PowerCacheEntPortKey, userId)),
  209. redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.PowerCacheBuyerPortKey, userId)),
  210. redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.PowerCacheFileKey, userId))
  211. }