compatible.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package compatible
  2. import (
  3. "encoding/json"
  4. "log"
  5. "strings"
  6. util "app.yhyue.com/moapp/jybase/common"
  7. . "app.yhyue.com/moapp/jybase/mongodb"
  8. "app.yhyue.com/moapp/jybase/mysql"
  9. . "app.yhyue.com/moapp/jypkg/middleground"
  10. "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
  11. )
  12. type Compatible struct {
  13. Mgo *MongodbSim
  14. BaseService *mysql.Mysql
  15. MainMysql *mysql.Mysql
  16. Middleground *Middleground
  17. }
  18. func NewCompatible(mgo *MongodbSim, baseService, mainMysql *mysql.Mysql, middleground *Middleground) *Compatible {
  19. if middleground.UserCenter == nil {
  20. log.Fatalln("NewCompatible的时候,Middleground没有注册用户中台!")
  21. }
  22. return &Compatible{
  23. Mgo: mgo,
  24. BaseService: baseService,
  25. MainMysql: mainMysql,
  26. Middleground: middleground,
  27. }
  28. }
  29. //根据mgo库user表_id,或者企业身份的职位id查询
  30. func (c *Compatible) Select(id string, field interface{}) *map[string]interface{} {
  31. var identity *pb.Identity
  32. if !IsObjectIdHex(id) {
  33. identity = c.Middleground.UserCenter.IdentityByPositionId(util.Int64All(id))
  34. }
  35. return c.selectDo(id, identity, field)
  36. }
  37. //根据mgo库user表_id,或者企业身份的企业员工id查询
  38. func (c *Compatible) SelectByEntUserId(id string, field interface{}) *map[string]interface{} {
  39. var identity *pb.Identity
  40. if !IsObjectIdHex(id) {
  41. identity = c.Middleground.UserCenter.IdentityByEntUserId(util.Int64All(id))
  42. }
  43. return c.selectDo(id, identity, field)
  44. }
  45. //
  46. func (c *Compatible) selectDo(id string, identity *pb.Identity, fd interface{}) *map[string]interface{} {
  47. field := ObjToOth(fd)
  48. if field == nil || len(*field) == 0 {
  49. return nil
  50. }
  51. if IsObjectIdHex(id) {
  52. temp, _ := c.Mgo.FindById("user", id, field)
  53. return temp
  54. } else {
  55. if identity == nil {
  56. return nil
  57. }
  58. user := map[string]interface{}{}
  59. if identity.UserId > 0 {
  60. ufd := map[string]interface{}{}
  61. for _, k := range []string{"s_myemail", "s_company", "s_m_openid", "s_phone", "s_m_phone", "s_unionid", "s_headimage", "s_headimageurl", "s_nickname", "l_registedate", "s_appversion", "i_ispush", "s_jyname", "s_jpushid", "s_opushid", "s_appponetype", "i_applystatus"} {
  62. if v, ok := (*field)[k]; ok {
  63. ufd[k] = v
  64. }
  65. }
  66. if len(ufd) > 0 {
  67. ufd["_id"] = 0
  68. data, ok := c.Mgo.FindOneByField("user", map[string]interface{}{"base_user_id": identity.UserId}, ufd)
  69. if ok && data != nil && len(*data) > 0 {
  70. for k, v := range *data {
  71. user[k] = v
  72. }
  73. }
  74. }
  75. }
  76. if identity.EntUserId > 0 {
  77. fms := []string{}
  78. fmp := map[int]map[string]interface{}{}
  79. for k, v := range *field {
  80. if util.IntAll(v) <= 0 {
  81. continue
  82. }
  83. if k == "o_entniche" || strings.HasPrefix(k, "o_entniche.") {
  84. if fmp[0] == nil {
  85. fmp[0] = map[string]interface{}{"_id": 0}
  86. }
  87. fmp[0][k] = 1
  88. } else if k == "o_vipjy" || strings.HasPrefix(k, "o_vipjy.") {
  89. if fmp[1] == nil {
  90. fmp[1] = map[string]interface{}{"_id": 0}
  91. }
  92. fms = append(fms, "o_vipjy")
  93. fmp[1][strings.ReplaceAll(k, "o_vipjy", "o_entniche")] = 1
  94. } else if k == "o_member_jy" || strings.HasPrefix(k, "o_member_jy.") {
  95. if fmp[1] == nil {
  96. fmp[1] = map[string]interface{}{"_id": 0}
  97. }
  98. fms = append(fms, "o_member_jy")
  99. fmp[1][strings.ReplaceAll(k, "o_member_jy", "o_entniche")] = 1
  100. } else if k == "o_jy" || strings.HasPrefix(k, "o_jy.") {
  101. if fmp[2] == nil {
  102. fmp[2] = map[string]interface{}{"_id": 0}
  103. }
  104. fmp[2][strings.ReplaceAll(k, "o_jy", "o_entniche")] = 1
  105. } else {
  106. continue
  107. }
  108. }
  109. for k, v := range fmp {
  110. entniche_rule, ok := c.Mgo.FindOneByField("entniche_rule", map[string]interface{}{
  111. "i_userid": identity.EntUserId,
  112. "i_type": k,
  113. }, v)
  114. if ok && entniche_rule != nil && len(*entniche_rule) > 0 {
  115. if k == 0 {
  116. user["o_entniche"] = (*entniche_rule)["o_entniche"]
  117. } else if k == 1 {
  118. for _, vv := range fms {
  119. user[vv] = (*entniche_rule)["o_entniche"]
  120. }
  121. } else if k == 2 {
  122. user["o_jy"] = (*entniche_rule)["o_entniche"]
  123. }
  124. }
  125. }
  126. delete((*field), "o_jy")
  127. delete((*field), "o_vipjy")
  128. delete((*field), "o_member_jy")
  129. delete((*field), "o_entniche")
  130. if len((*field)) > 0 {
  131. ent_user, ok := c.Mgo.FindOneByField("ent_user", map[string]interface{}{
  132. "i_userid": identity.EntUserId,
  133. }, field)
  134. if ok && ent_user != nil && len(*ent_user) > 0 {
  135. for k, v := range *ent_user {
  136. user[k] = v
  137. }
  138. }
  139. }
  140. }
  141. if len(user) == 0 {
  142. user["0"] = 0
  143. }
  144. return &user
  145. }
  146. }
  147. //根据mgo库user表_id,或者企业身份的职位id更新
  148. func (c *Compatible) Update(id string, update map[string]interface{}) bool {
  149. var identity *pb.Identity
  150. if !IsObjectIdHex(id) {
  151. identity = c.Middleground.UserCenter.IdentityByPositionId(util.Int64All(id))
  152. }
  153. return c.updateDo(id, identity, update)
  154. }
  155. //根据mgo库user表_id,或者企业身份的企业员工id更新
  156. func (c *Compatible) UpdateByEntUserId(id string, update map[string]interface{}) bool {
  157. var identity *pb.Identity
  158. if !IsObjectIdHex(id) {
  159. identity = c.Middleground.UserCenter.IdentityByEntUserId(util.Int64All(id))
  160. }
  161. return c.updateDo(id, identity, update)
  162. }
  163. //
  164. func (c *Compatible) updateDo(id string, identity *pb.Identity, update map[string]interface{}) bool {
  165. if IsObjectIdHex(id) {
  166. return c.Mgo.UpdateById("user", id, update)
  167. } else {
  168. if identity == nil {
  169. return false
  170. }
  171. up1, up2 := map[string]interface{}{}, map[string]interface{}{}
  172. var tp int
  173. for k, v := range update {
  174. vm, ok := v.(map[string]interface{})
  175. if !ok {
  176. b, err := json.Marshal(v)
  177. if err != nil {
  178. return false
  179. } else if err = json.Unmarshal(b, &vm); err != nil {
  180. return false
  181. } else if vm == nil || len(vm) == 0 {
  182. return false
  183. }
  184. }
  185. up11, up22 := map[string]interface{}{}, map[string]interface{}{}
  186. key := ""
  187. for kk, vv := range vm {
  188. if (key == "" || key == "o_jy") && (kk == "o_jy" || strings.HasPrefix(kk, "o_jy.")) {
  189. key = "o_jy"
  190. up22[strings.ReplaceAll(kk, key, "o_entniche")] = vv
  191. tp = 2
  192. } else if (key == "" || key == "o_vipjy") && (kk == "o_vipjy" || strings.HasPrefix(kk, "o_vipjy.")) {
  193. key = "o_vipjy"
  194. up22[strings.ReplaceAll(kk, key, "o_entniche")] = vv
  195. tp = 1
  196. } else if (key == "" || key == "o_member_jy") && (kk == "o_member_jy" || strings.HasPrefix(kk, "o_member_jy.")) {
  197. key = "o_member_jy"
  198. up22[strings.ReplaceAll(kk, key, "o_entniche")] = vv
  199. tp = 1
  200. } else if (key == "" || key == "o_entniche") && (kk == "o_entniche" || strings.HasPrefix(kk, "o_entniche.")) {
  201. key = "o_entniche"
  202. up22[strings.ReplaceAll(kk, key, "o_entniche")] = vv
  203. tp = 0
  204. } else {
  205. up11[kk] = vv
  206. }
  207. }
  208. if len(up11) > 0 {
  209. up1[k] = up11
  210. }
  211. if len(up22) > 0 {
  212. up2[k] = up22
  213. }
  214. }
  215. ok1, ok2 := false, false
  216. if len(up1) > 0 {
  217. ok1 = c.Mgo.Update("ent_user", map[string]interface{}{
  218. "i_entid": identity.EntId,
  219. "i_userid": identity.EntUserId,
  220. }, up1, true, false)
  221. }
  222. if len(up2) > 0 {
  223. ok2 = c.Mgo.Update("entniche_rule", map[string]interface{}{
  224. "i_entid": identity.EntId,
  225. "i_userid": identity.EntUserId,
  226. "i_type": tp,
  227. }, up2, false, false)
  228. }
  229. return ok1 || ok2
  230. }
  231. }