subscribe.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package entity
  2. import (
  3. "log"
  4. "app.yhyue.com/moapp/jybase/common"
  5. "app.yhyue.com/moapp/jybase/mongodb"
  6. "app.yhyue.com/moapp/jybase/mysql"
  7. )
  8. //获取用户状态
  9. type UserInfoReq struct {
  10. Mysql *mysql.Mysql
  11. Mgo mongodb.MongodbSim
  12. UserId int64 //base_user_id 不是 mongodb的userid!
  13. Types string //不传取默认规则
  14. EntId int64
  15. EntUserId int64
  16. }
  17. //默认规则:优先大会员、商机管理、超级订阅、免费订阅
  18. func (this *UserInfoReq) GetUserInfo() (ret map[string]interface{}, msg string) {
  19. data := this.userInfo()
  20. ret = map[string]interface{}{}
  21. if data == nil {
  22. return nil, "未查询到用户信息"
  23. }
  24. //获取用户权益
  25. vip_status := common.IntAll((*data)["i_vip_status"]) //超级订阅
  26. member_status := common.IntAll((*data)["i_member_status"]) //大会员
  27. //获取商机管理信息
  28. phone := common.ObjToString((*data)["s_phone"])
  29. if phone == "" {
  30. phone = common.ObjToString((*data)["s_m_phone"])
  31. }
  32. varentinfo := &EntInfo{Mysql: this.Mysql} //商机管理
  33. this.EntId, this.EntUserId = GetEntByPhone(this.Mysql, phone)
  34. entinfo := varentinfo.GetById(int(this.EntId))
  35. entniche_status := entinfo.Status
  36. //用户相关订阅设置
  37. object := &map[string]interface{}{}
  38. if this.Types == "" {
  39. if member_status > 0 {
  40. this.Types = "m"
  41. } else if entniche_status > 0 {
  42. this.Types = "e"
  43. } else if vip_status > 0 {
  44. this.Types = "v"
  45. } else {
  46. this.Types = "f"
  47. }
  48. }
  49. switch this.Types {
  50. case "m":
  51. //大会员
  52. if (*data)["o_member_jy"] != nil {
  53. memberJy := common.ObjToMap((*data)["o_member_jy"])
  54. object = this.format(memberJy)
  55. (*object)["starttime"] = (*data)["i_member_starttime"]
  56. (*object)["endtime"] = (*data)["i_member_endtime"]
  57. }
  58. case "e":
  59. //商机管理
  60. entnicheJy := this.EntnicheSub()
  61. log.Println("~~~", entnicheJy)
  62. if entnicheJy != nil {
  63. object = this.format(entnicheJy)
  64. }
  65. case "v":
  66. //超级订阅
  67. if (*data)["o_vipjy"] != nil {
  68. vipJy := common.ObjToMap((*data)["o_vipjy"])
  69. object = this.format(vipJy)
  70. (*object)["starttime"] = (*data)["l_vip_starttime"]
  71. (*object)["endtime"] = (*data)["l_vip_endtime"]
  72. }
  73. case "f":
  74. if (*data)["o_jy"] != nil {
  75. ojy := common.ObjToMap((*data)["o_jy"])
  76. if (*ojy)["a_key"] != nil {
  77. akey := common.ObjArrToMapArr((*ojy)["a_key"].([]interface{}))
  78. (*object)["a_items"] = []map[string]interface{}{ //转换至和其它结构一致
  79. map[string]interface{}{
  80. "a_key": akey,
  81. },
  82. }
  83. }
  84. if (*ojy)["o_area"] == nil {
  85. (*object)["o_area"] = map[string]interface{}{}
  86. } else {
  87. (*object)["o_area"] = (*ojy)["o_area"]
  88. }
  89. }
  90. }
  91. //
  92. ret["area"] = (*object)["o_area"]
  93. ret["items"] = (*object)["a_items"]
  94. ret["matchway"] = common.If((*object)["i_matchway"] == nil, 1, (*object)["i_matchway"]) //没有则,默认1
  95. ret["buyerclass"] = common.If((*object)["a_buyerclass"] == nil, []string{}, (*object)["a_buyerclass"])
  96. ret["infotype"] = common.If((*object)["a_infotype"] == nil, []string{}, (*object)["a_infotype"])
  97. ret["projectmatch"] = common.If((*object)["i_projectmatch"] == nil, 0, (*object)["i_projectmatch"])
  98. ret["starttime"] = (*object)["starttime"]
  99. ret["endtime"] = (*object)["endtime"]
  100. ret["phone"] = phone
  101. ret["vipStatus"] = vip_status
  102. ret["memberStatus"] = member_status
  103. ret["entnicheStatus"] = entniche_status
  104. ret["subscribeType"] = this.Types
  105. nickname := common.ObjToString((*data)["s_nickname"])
  106. if nickname == "" && phone != "" && len(phone) == 11 {
  107. nickname = string(phone[0:3]) + "****" + string(phone[len(phone)-4:])
  108. }
  109. if nickname == "" {
  110. nickname = common.ObjToString((*data)["s_jyname"])
  111. }
  112. ret["nickname"] = nickname
  113. headimg := common.ObjToString((*data)["s_headimageurl"])
  114. ret["headimg"] = headimg
  115. if headimg == "" {
  116. ret["headimg"] = common.ObjToString((*data)["s_headimage"])
  117. }
  118. return ret, ""
  119. }
  120. //获取mongodb user表相关数据
  121. func (this *UserInfoReq) userInfo() *map[string]interface{} {
  122. data, ok := this.Mgo.FindOneByField("user", map[string]interface{}{"base_user_id": this.UserId}, `{"i_vip_status":1,"l_vip_starttime":1,"l_vip_endtime":1,"i_member_status":1,"i_member_starttime":1,"i_member_endtime":1,"o_jy":1,"o_vipjy":1,"o_member_jy":1,"s_phone":1,"s_m_phone":1,"s_nickname":1,"s_jyname":1,"s_headimageurl":1,"s_headimage":1}`)
  123. if ok && data != nil && len(*data) > 0 {
  124. return data
  125. }
  126. return nil
  127. }
  128. //格式化数据 大会员/超级订阅/商机管理 结构一致 关键词格式化
  129. func (this *UserInfoReq) format(data *map[string]interface{}) *map[string]interface{} {
  130. if data == nil {
  131. return nil
  132. }
  133. //关键词
  134. if (*data)["a_items"] != nil {
  135. a_items := common.ObjArrToMapArr((*data)["a_items"].([]interface{}))
  136. for k, v := range a_items {
  137. if v["a_key"] != nil {
  138. a_keys := []map[string]interface{}{}
  139. ak, ok := v["a_key"].([]interface{})
  140. if !ok {
  141. a_keys, _ = v["a_key"].([]map[string]interface{})
  142. } else {
  143. a_keys = common.ObjArrToMapArr(ak)
  144. }
  145. for kk, vv := range a_keys {
  146. if vv["key"] != nil {
  147. keyArr, ok1 := vv["key"].([]interface{})
  148. if ok1 {
  149. if vv["appendkey"] != nil {
  150. appendkeyArr, ok2 := vv["appendkey"].([]interface{})
  151. if ok2 {
  152. key := common.ObjArrToStringArr(keyArr)
  153. appendkey := common.ObjArrToStringArr(appendkeyArr)
  154. a_keys[kk]["key"] = append(key, appendkey...)
  155. }
  156. }
  157. }
  158. }
  159. delete(a_keys[kk], "appendkey")
  160. }
  161. a_items[k]["a_key"] = a_keys
  162. }
  163. }
  164. (*data)["a_items"] = a_items
  165. }
  166. return data
  167. }
  168. //商机管理订阅设置
  169. func (this *UserInfoReq) EntnicheSub() *map[string]interface{} {
  170. currentUser := &CurrentUser{
  171. Mysql: this.Mysql,
  172. }
  173. entInfo := currentUser.EntInfo(int(this.EntId), int(this.EntUserId))
  174. model := entInfo.Ent.Model //1-统一订阅 2-个人订阅
  175. if entInfo.User_power != 1 {
  176. //用户暂无权限
  177. return nil
  178. }
  179. res := &map[string]interface{}{}
  180. switch model {
  181. case 1:
  182. deptId := entInfo.Dept.Id
  183. nodiff := false
  184. res_ := map[string]interface{}{} //临时map
  185. //我所有的父部门
  186. EntParentDept := map[int][]*EntDeptParent{}
  187. //先获取用户组织架构权益
  188. EntParentDept = GetEntDeptParent(this.Mysql, EntParentDept, deptId)
  189. EntDepts := map[int]*EntDept{}
  190. EntDepts = GetEntDepts(this.Mysql, EntDepts, this.EntId)
  191. // //用户下所有的分发规则
  192. EntUserRules := GetUserRules(this.Mysql, this.EntUserId)
  193. ///分发规则的相关设置
  194. EntDis := GetEntDistribute(this.Mgo, this.EntId)
  195. //我的上级部门
  196. for _, v := range EntParentDept[entInfo.Dept.Id] {
  197. //看我的上级部门,有没有开启订阅分发
  198. if EntDepts[v.Pid].Subdis == 0 {
  199. log.Println("暂未开启订阅设置")
  200. return nil
  201. }
  202. //看我的上级部门,有没有全员无差别接收
  203. if EntDepts[v.Pid].Nodiff == 1 {
  204. log.Println(EntDepts[v.Pid].Name, EntDepts[v.Pid].Id, "开启全员无差别")
  205. deptId = EntDepts[v.Pid].Id
  206. nodiff = true
  207. break
  208. }
  209. log.Println(len(EntUserRules[common.IntAll(this.EntUserId)]) > 0)
  210. //看我的上级部门,有没有给我设置分发规则
  211. if !nodiff && len(EntUserRules[common.IntAll(this.EntUserId)]) > 0 {
  212. for _, v := range EntUserRules[common.IntAll(this.EntUserId)] {
  213. //获取关键词组相关
  214. ruleData, _ := this.Mgo.FindOne("entniche_rule", map[string]interface{}{
  215. "i_deptid": EntDis[v.RuleId].DeptId,
  216. "i_entid": this.EntId,
  217. })
  218. o_entniche, _ := (*ruleData)["o_entniche"].(map[string]interface{})
  219. itemMap := map[string]interface{}{}
  220. items, _ := o_entniche["a_items"].([]interface{})
  221. for _, v := range items {
  222. item, _ := v.(map[string]interface{})
  223. if item == nil {
  224. continue
  225. }
  226. item_name, _ := item["s_item"].(string)
  227. if item_name == "" {
  228. continue
  229. }
  230. itemMap[item_name] = item
  231. }
  232. a_items := []interface{}{}
  233. for _, v := range EntDis[v.RuleId].Items {
  234. m := map[string]interface{}{
  235. "s_item": v,
  236. "a_key": (*common.ObjToMap(itemMap[v]))["a_key"],
  237. }
  238. a_items = append(a_items, m)
  239. }
  240. (res_)["a_buyerclass"] = EntDis[v.RuleId].Buyerclass
  241. (res_)["a_items"] = a_items
  242. (res_)["o_area"] = EntDis[v.RuleId].Area
  243. }
  244. }
  245. }
  246. res, _ = this.Mgo.FindOne("entniche_rule", map[string]interface{}{
  247. "i_deptid": deptId,
  248. "i_entid": this.EntId,
  249. })
  250. log.Println(res_, "+++")
  251. if len(res_) > 0 {
  252. oenitche, _ := (*res)["o_entniche"].(map[string]interface{})
  253. oenitche["a_buyerclass"] = res_["a_buyerclass"]
  254. oenitche["a_items"] = res_["a_items"]
  255. oenitche["o_area"] = res_["o_area"]
  256. (*res)["o_entniche"] = oenitche
  257. }
  258. case 2:
  259. res, _ = this.Mgo.FindOne("entniche_rule", map[string]interface{}{
  260. "i_userid": this.EntUserId,
  261. "i_entid": this.EntId,
  262. })
  263. }
  264. log.Println(entInfo.Ent.Name, entInfo.Ent.Id, entInfo.Ent)
  265. entnicheJy := common.ObjToMap((*res)["o_entniche"])
  266. if entnicheJy != nil && len(*entnicheJy) > 0 {
  267. (*entnicheJy)["starttime"] = entInfo.Ent.Startdate
  268. (*entnicheJy)["endtime"] = entInfo.Ent.Enddate
  269. (*entnicheJy)["power"] = entInfo.User_power //用户权益
  270. } else {
  271. entnicheJy = &map[string]interface{}{
  272. "starttime": entInfo.Ent.Startdate,
  273. "endtime": entInfo.Ent.Enddate,
  274. "power": entInfo.User_power,
  275. }
  276. }
  277. //获取商机管理的订阅设置
  278. return entnicheJy
  279. }