platform_info.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package talent_service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "github.com/gogf/gf/database/gdb"
  7. "github.com/gogf/gf/frame/g"
  8. "github.com/gogf/gf/net/ghttp"
  9. "github.com/gogf/gf/os/gtime"
  10. "reflect"
  11. "youngmini_server/app/dao"
  12. "youngmini_server/app/model"
  13. "youngmini_server/app/model/talent_model"
  14. "youngmini_server/app/utils"
  15. )
  16. type platformId int
  17. const (
  18. PlatformIdLittleRedBook platformId = iota + 1
  19. PlatformIdTiktok
  20. PlatformIdWeibo
  21. PlatformIdKuaishou
  22. PlatformIdBilibili
  23. PlatformIdDianping
  24. PlatformIdZhihu
  25. )
  26. var PlatformDataMap = map[platformId]interface{} {
  27. PlatformIdLittleRedBook: talent_model.PlatformLittleRedBook{},
  28. PlatformIdTiktok: talent_model.PlatformTiktok{},
  29. PlatformIdWeibo: talent_model.PlatformWeibo{},
  30. PlatformIdKuaishou: talent_model.PlatformKuaishou{},
  31. PlatformIdBilibili: talent_model.PlatformBilibili{},
  32. PlatformIdDianping: talent_model.PlatformDianping{},
  33. PlatformIdZhihu: talent_model.PlatformZhihu{},
  34. }
  35. // GetTalentAllPlatformBriefInfo 获取达人所有平台的简略信息
  36. func GetTalentAllPlatformBriefInfo(r *ghttp.Request) *TalentHttpResult {
  37. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  38. if err != nil {
  39. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  40. }
  41. res, err := g.DB().Model("r_talent_platform_table").All("tid", tid)
  42. if err != nil {
  43. return &TalentHttpResult{Code: -2, Msg: "query database error"}
  44. }
  45. if res == nil {
  46. return &TalentHttpResult{Code: -3, Msg: "未绑定任何平台"}
  47. }
  48. var platformBriefInfo talent_model.TalentPlatformBriefInfo
  49. err = g.DB().Model("talent_info").WithAll().Where("id", tid).Scan(&platformBriefInfo)
  50. if err != nil {
  51. return &TalentHttpResult{Code: -4, Msg: "query data failed"}
  52. }
  53. return &TalentHttpResult{Code: 0, Msg: "success", Data: platformBriefInfo}
  54. }
  55. // OnGetTalentPlatformDetail 获取达人社媒平台的详细信息
  56. func OnGetTalentPlatformDetail(r *ghttp.Request) *TalentHttpResult {
  57. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  58. if err != nil {
  59. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  60. }
  61. pid := r.GetQueryInt("pid", 0)
  62. if tid == 0 || pid == 0 {
  63. return &TalentHttpResult{Code: -2, Msg: "request param error"}
  64. }
  65. // 通过达人与平台关联表(平台账号审核表)查找达人已有平台
  66. rtpRec, err := g.DB().Model("r_talent_platform_table").One("tid=? and p_id=?", tid, pid)
  67. if err != nil {
  68. return &TalentHttpResult{Code: -3, Msg: "query database failed"}
  69. }
  70. // 达人还没有提交过此平台信息
  71. if rtpRec == nil {
  72. return &TalentHttpResult{Code: -4, Msg: "talent have no info of platform"}
  73. }
  74. // 根据平台id查找平台信息表名
  75. pRec, err := g.DB().Model(dao.InfoThirdPlatform.Table).Fields(dao.InfoThirdPlatform.Columns.PlatformTableName).
  76. One("platform_id", rtpRec[dao.RTalentPlatformTable.Columns.PId])
  77. if err != nil {
  78. return &TalentHttpResult{Code: -5, Msg: "query database failed"}
  79. }
  80. if pRec == nil {
  81. return &TalentHttpResult{Code: -6, Msg: "platform info not found"}
  82. }
  83. // 用得到的信息表名查找平台信息
  84. infoRec, err := g.DB().Model(pRec[dao.InfoThirdPlatform.Columns.PlatformTableName].String()).One("talent_id", tid)
  85. if err != nil {
  86. return &TalentHttpResult{Code: -7, Msg: "query talent platform info failed"}
  87. }
  88. if infoRec == nil {
  89. return &TalentHttpResult{Code: -8, Msg: "not found talent platform info"}
  90. }
  91. return &TalentHttpResult{Code: 0, Msg: "success", Data: infoRec}
  92. }
  93. func getPlatformById(pid int32) (interface{}, error) {
  94. switch pid {
  95. case 1:
  96. return &talent_model.PlatformLittleRedBook{}, nil
  97. case 2:
  98. return &talent_model.PlatformTiktok{}, nil
  99. case 3:
  100. return &talent_model.PlatformWeibo{}, nil
  101. case 4:
  102. return &talent_model.PlatformKuaishou{}, nil
  103. case 5:
  104. return &talent_model.PlatformBilibili{}, nil
  105. case 6:
  106. return &talent_model.PlatformDianping{}, nil
  107. case 7:
  108. return &talent_model.PlatformZhihu{}, nil
  109. }
  110. return nil, errors.New("id error")
  111. }
  112. // 将三个擅长领域转换为json
  113. func combineSkillsOnToJson(skillsOn1 int32, skillsOn2 int32, skillsOn3 int32) string {
  114. r := []int32{skillsOn1, skillsOn2, skillsOn3}
  115. bytes, err := json.Marshal(r)
  116. if err != nil {
  117. return "[]"
  118. }
  119. return string(bytes)
  120. }
  121. // OnPostPlatformDetailInfo 上传平台信息
  122. func OnPostPlatformDetailInfo(r *ghttp.Request) *TalentHttpResult {
  123. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  124. if err != nil {
  125. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  126. }
  127. pid := r.GetRequestInt32("platformId", 0)
  128. if tid == 0 || pid == 0 {
  129. return &TalentHttpResult{Code: -2, Msg: "param tid and pid must be provided"}
  130. }
  131. platformData, err := getPlatformById(pid)
  132. if err = r.ParseForm(platformData); err != nil {
  133. return &TalentHttpResult{Code: -2, Msg: err.Error()}
  134. }
  135. // 根据平台id获取平台信息
  136. platformInfoRec, err := g.DB().Model(dao.InfoThirdPlatform.Table).
  137. Fields(dao.InfoThirdPlatform.Columns.PlatformTableName, dao.InfoThirdPlatform.Columns.PlatformIcon).One("platform_id", pid)
  138. if err != nil {
  139. return &TalentHttpResult{Code: -4, Msg: "get platform table name failed"}
  140. }
  141. //_, exist1 := reflect.TypeOf(platformData).Elem().FieldByName("SkilledAt1")
  142. //_, exist2 := reflect.TypeOf(platformData).Elem().FieldByName("SkilledAt2")
  143. //_, exist3 := reflect.TypeOf(platformData).Elem().FieldByName("SkilledAt3")
  144. //_, exist := reflect.TypeOf(platformData).Elem().FieldByName("SkilledAt")
  145. //
  146. //if exist1 && exist2 && exist3 &&exist {
  147. // // 如果上传的信息中包括SkilledAt1 SkilledAt2 SkilledAt3字段,则将三个字段转换为json,并存入SkilledAt字段
  148. // skilledAt1 := reflect.ValueOf(platformData).Elem().FieldByName("SkilledAt1").Int()
  149. // skilledAt2 := reflect.ValueOf(platformData).Elem().FieldByName("SkilledAt2").Int()
  150. // skilledAt3 := reflect.ValueOf(platformData).Elem().FieldByName("SkilledAt3").Int()
  151. //
  152. // reflect.ValueOf(platformData).Elem().FieldByName("SkilledAt").SetString(
  153. // combineSkillsOnToJson(int32(skilledAt1), int32(skilledAt2), int32(skilledAt3)))
  154. //}
  155. reflect.ValueOf(platformData).Elem().FieldByName("SkilledAt").SetString("[]")
  156. // 将达人id存入结构体
  157. reflect.ValueOf(platformData).Elem().FieldByName("TalentId").SetInt(int64(tid))
  158. // 平台名称写入结构体
  159. reflect.ValueOf(platformData).Elem().FieldByName("PlatformName").SetString(platformInfoRec[dao.InfoThirdPlatform.Columns.PlatformName].String())
  160. // 平台图标url写入结构体
  161. reflect.ValueOf(platformData).Elem().FieldByName("PlatformIconUrl").SetString(platformInfoRec[dao.InfoThirdPlatform.Columns.PlatformIcon].String())
  162. err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  163. // 将平台账号信息存入对应的平台表
  164. _, err1 := tx.Ctx(ctx).Model(platformInfoRec["platform_table_name"].String()).Save(platformData)
  165. if err1 != nil {
  166. return err1
  167. }
  168. // 将账号审核信息写入r_talent_platform_table表, 无论是新建还是更新都把状态回复为原始状态
  169. _, err1 = tx.Ctx(ctx).Model(dao.RTalentPlatformTable.Table).
  170. Save(model.RTalentPlatformTable{
  171. Tid: tid,
  172. PId: int(pid),
  173. PName: platformInfoRec[dao.InfoThirdPlatform.Columns.PlatformName].String(),
  174. PNickname: reflect.ValueOf(platformData).Elem().FieldByName("PlatformNickname").String(),
  175. PAccountId: reflect.ValueOf(platformData).Elem().FieldByName("PlatformAccountId").String(),
  176. FansCount: reflect.ValueOf(platformData).Elem().FieldByName("FansCount").Int(),
  177. HomePageUrl: reflect.ValueOf(platformData).Elem().FieldByName("HomePageUrl").String(),
  178. ExamineState: 1,
  179. FailReason: "",
  180. ExamineAdminId: 0,
  181. CreateDate: gtime.Now(),
  182. ExamineDate: nil,
  183. DisableDate: nil,
  184. Deleted: 0,
  185. })
  186. return err1
  187. })
  188. if err != nil {
  189. return &TalentHttpResult{Code: -5, Msg: "save platformData failed"}
  190. }
  191. return &TalentHttpResult{Code: 0, Msg: "success"}
  192. }