subscribe.go 9.7 KB

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