talent.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/caixw/lib.go/conv"
  6. "github.com/sirupsen/logrus"
  7. "gorm.io/gorm"
  8. "strings"
  9. "youngee_m_api/consts"
  10. "youngee_m_api/model/gorm_model"
  11. "youngee_m_api/model/http_model"
  12. "youngee_m_api/pack"
  13. )
  14. func PlatformAccInfo(ctx context.Context, talentId string) (*http_model.PlatformAccInfoPreView, error) {
  15. db := GetReadDB(ctx)
  16. var gormPlatform []*gorm_model.YoungeePlatformAccountInfo
  17. // 根据 talent_id 进行筛选
  18. db = db.Debug().Model(gorm_model.YoungeePlatformAccountInfo{}).Where("talent_id = ?", talentId)
  19. err := db.Find(&gormPlatform).Order("bind_date").Error
  20. if err != nil {
  21. if err == gorm.ErrRecordNotFound {
  22. return nil, nil
  23. } else {
  24. return nil, err
  25. }
  26. }
  27. // 查询总数
  28. var total int64
  29. if err = db.Count(&total).Error; err != nil {
  30. logrus.WithContext(ctx).Errorf("[PlatformAccInfo] error query mysql total, err:%+v", err)
  31. return nil, err
  32. }
  33. platform := new(http_model.PlatformAccInfoPreView)
  34. platform.PlatformAccInfoData = pack.GormPlatformToHttpPlatform(gormPlatform)
  35. platform.Total = conv.MustString(total, "")
  36. return platform, err
  37. }
  38. func AccountIncome(ctx context.Context, talentId string) (*http_model.TalentInfoResponse, error) {
  39. db := GetReadDB(ctx)
  40. var talentInfo *gorm_model.YoungeeTalentInfo
  41. // 根据 talent_id 进行筛选
  42. err := db.Model(gorm_model.YoungeeTalentInfo{}).Where("id = ?", talentId).
  43. First(&talentInfo).Error
  44. if err != nil {
  45. if err == gorm.ErrRecordNotFound {
  46. return nil, nil
  47. } else {
  48. return nil, err
  49. }
  50. }
  51. data := new(http_model.TalentInfoResponse)
  52. data.Income = float32(talentInfo.Income)
  53. data.Withdrawed = float32(talentInfo.Withdrawed)
  54. data.Canwithdraw = float32(talentInfo.Canwithdraw)
  55. data.Withdrawing = float32(talentInfo.Withdrawing)
  56. // 查询是否绑定物流地址
  57. IsBindLocation := talentInfo.IsBindLocation
  58. if IsBindLocation == 1 {
  59. var deliveryInfo *gorm_model.YoungeeTalentDeliveryAddress
  60. db1 := GetReadDB(ctx)
  61. err = db1.Debug().Model(gorm_model.YoungeeTalentDeliveryAddress{}).
  62. Where("talent_id = ? AND default_tag = ?", talentId, 1).
  63. First(&deliveryInfo).Error
  64. //fmt.Printf("deliveryInfo%+v\n", deliveryInfo)
  65. if err != nil {
  66. if err == gorm.ErrRecordNotFound {
  67. return nil, nil
  68. } else {
  69. return nil, err
  70. }
  71. }
  72. region := GetRegion(ctx, deliveryInfo.RegionCode)
  73. data.ReceiverName = deliveryInfo.ReceiverName
  74. data.PhoneNumber = deliveryInfo.PhoneNumber
  75. data.DetailAddr = region + deliveryInfo.DetailAddr
  76. } else {
  77. data.ReceiverName = "暂无"
  78. data.PhoneNumber = "暂无"
  79. data.DetailAddr = "暂无"
  80. }
  81. region := ""
  82. if talentInfo.IsBindBank == 1 {
  83. db2 := GetReadDB(ctx)
  84. var talentBankInfo *gorm_model.YounggeeTalentBank
  85. err = db2.Model(gorm_model.YounggeeTalentBank{}).Where("talent_id = ?", talentId).
  86. First(&talentBankInfo).Error
  87. if err != nil {
  88. if err == gorm.ErrRecordNotFound {
  89. return nil, nil
  90. } else {
  91. return nil, err
  92. }
  93. }
  94. if talentBankInfo.BankOpenAddress != 0 {
  95. region = GetRegion(ctx, talentBankInfo.BankOpenAddress)
  96. }
  97. data.Bank = talentBankInfo.Bank
  98. data.BankOpenAddress = region
  99. data.BankCardNumber = talentBankInfo.BankCardNumber
  100. data.Name = talentBankInfo.Name
  101. data.AliPayNumber = "暂无"
  102. data.AliPayRealName = "暂无"
  103. } else {
  104. data.Bank = "暂无"
  105. data.BankCardNumber = "暂无"
  106. data.BankOpenAddress = "暂无"
  107. data.Name = "暂无"
  108. data.AliPayNumber = "暂无"
  109. data.AliPayRealName = "暂无"
  110. }
  111. return data, err
  112. }
  113. func GetRegion(ctx context.Context, regionCode int) string {
  114. db4 := GetReadDB(ctx)
  115. var infoRegion *gorm_model.InfoRegion
  116. println(regionCode)
  117. db4.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", regionCode).First(&infoRegion)
  118. provinceCode := conv.MustString(regionCode, "")[0:2] + "0000"
  119. var province *gorm_model.InfoRegion
  120. db4.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", conv.MustInt(provinceCode, 0)).First(&province)
  121. cityCode := conv.MustString(regionCode, "")[0:4] + "00"
  122. var city *gorm_model.InfoRegion
  123. db4.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", conv.MustInt(cityCode, 0)).First(&city)
  124. fmt.Println("province,city,infoRegion Code :", provinceCode, cityCode, regionCode)
  125. fmt.Println("province,city,infoRegion RegionName :", province.RegionName, city.RegionName, infoRegion.RegionName)
  126. return province.RegionName + city.RegionName + infoRegion.RegionName
  127. }
  128. func GetTaskRecord(ctx context.Context, talentId string) (*http_model.GetTaskRecordResponse, error) {
  129. db := GetReadDB(ctx)
  130. var taskInfos []*gorm_model.YoungeeTaskInfo
  131. // 根据 talent_id 进行筛选
  132. db = db.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("talent_id = ?", talentId)
  133. // 查询总数
  134. var total int64
  135. if err := db.Count(&total).Error; err != nil {
  136. logrus.WithContext(ctx).Errorf("[GetTaskRecord] error query mysql total, err:%+v", err)
  137. return nil, err
  138. }
  139. err := db.Order("update_at desc").Find(&taskInfos).Error
  140. if err != nil {
  141. if err == gorm.ErrRecordNotFound {
  142. return nil, nil
  143. } else {
  144. return nil, err
  145. }
  146. }
  147. projectIdMap := map[string]gorm_model.ProjectInfo{}
  148. for _, taskInfo := range taskInfos {
  149. if _, ok := projectIdMap[taskInfo.ProjectID]; !ok {
  150. db1 := GetReadDB(ctx)
  151. projectInfo := gorm_model.ProjectInfo{}
  152. db1.Where("project_id = ?", taskInfo.ProjectID).First(&projectInfo)
  153. projectIdMap[taskInfo.ProjectID] = projectInfo
  154. }
  155. }
  156. var taskRecordDatas []*http_model.TaskRecordData
  157. for _, taskInfo := range taskInfos {
  158. updateAt := conv.MustString(taskInfo.UpdateAt, "")
  159. updateAt = updateAt[0:19]
  160. taskRecordData := http_model.TaskRecordData{
  161. ProjectId: conv.MustString(taskInfo.ProjectID, ""),
  162. ProjectName: projectIdMap[taskInfo.ProjectID].ProjectName,
  163. ProjectType: consts.GetProjectType(projectIdMap[taskInfo.ProjectID].ProjectType),
  164. ProjectPlatform: consts.GetProjectPlatform(projectIdMap[taskInfo.ProjectID].ProjectPlatform),
  165. TaskId: conv.MustString(taskInfo.TaskID, ""),
  166. TaskStage: consts.GetTaskStage(taskInfo.TaskStage),
  167. UpdatedAt: updateAt,
  168. }
  169. taskRecordDatas = append(taskRecordDatas, &taskRecordData)
  170. }
  171. taskRecordResponse := new(http_model.GetTaskRecordResponse)
  172. taskRecordResponse.TaskRecordData = taskRecordDatas
  173. taskRecordResponse.Total = conv.MustString(total, "")
  174. return taskRecordResponse, nil
  175. }
  176. // GetKuaishouUserInfoByTalentId 根据达人ID去查找快手平台授权信息
  177. func GetKuaishouUserInfoByTalentId(ctx context.Context, talentId string) gorm_model.PlatformKuaishouUserInfo {
  178. db4 := GetReadDB(ctx)
  179. // db = db.Model(gorm_model.YounggeeSecTaskInfo{}).Where("selection_id = ? and free_strategy_id = ? and task_stage = ?", selectionId, strategyId, taskStage)
  180. kuaishouUser := gorm_model.PlatformKuaishouUserInfo{}
  181. db4 = db4.Model(gorm_model.PlatformKuaishouUserInfo{}).Where("talent_id = ?", talentId).Scan(&kuaishouUser)
  182. return kuaishouUser
  183. }
  184. // GetAllKuaishouUser 返回所有快手用户信息
  185. func GetAllKuaishouUser(ctx context.Context) []gorm_model.PlatformKuaishouUserInfo {
  186. db5 := GetReadDB(ctx)
  187. var talentList []gorm_model.PlatformKuaishouUserInfo
  188. err := db5.Model(gorm_model.PlatformKuaishouUserInfo{}).Find(&talentList)
  189. fmt.Println(err)
  190. return talentList
  191. }
  192. func UpdateKuaishouUserInfoByTalentID(ctx context.Context, talentInfo gorm_model.PlatformKuaishouUserInfo) error {
  193. db6 := GetReadDB(ctx)
  194. whereCondition := gorm_model.PlatformKuaishouUserInfo{TalentId: talentInfo.TalentId}
  195. err := db6.Model(gorm_model.PlatformKuaishouUserInfo{}).Where(whereCondition).Updates(talentInfo).Error
  196. if err != nil {
  197. return err
  198. }
  199. return err
  200. }
  201. // UpdateKuaishouUserInfoById 根据platform_user_id更新
  202. func UpdateKuaishouUserInfoById(ctx context.Context, talentInfo gorm_model.PlatformKuaishouUserInfo) error {
  203. db6 := GetReadDB(ctx)
  204. whereCondition := gorm_model.PlatformKuaishouUserInfo{Id: talentInfo.Id}
  205. err := db6.Model(gorm_model.PlatformKuaishouUserInfo{}).Where(whereCondition).Updates(talentInfo).Error
  206. if err != nil {
  207. return err
  208. }
  209. return err
  210. }
  211. func GetProvince(ctx context.Context, request http_model.GetProviceRequest) (*http_model.GetProviceResponse, error) {
  212. db := GetReadDB(ctx)
  213. var userInfo []gorm_model.PlatformKuaishouUserInfo
  214. // 从数据库中获取所有 city 字段的数据
  215. err := db.Model(&gorm_model.PlatformKuaishouUserInfo{}).Pluck("city", &userInfo).Error
  216. if err != nil {
  217. return nil, err
  218. }
  219. // 使用 map 来去重省份
  220. provinceMap := make(map[string]struct{})
  221. for _, user := range userInfo {
  222. if user.City != "" {
  223. // 按照空格分割 city 字段,取第一个部分作为省份
  224. cityParts := strings.Split(user.City, " ")
  225. if len(cityParts) > 0 {
  226. provinceMap[cityParts[0]] = struct{}{} // 使用空结构体来去重
  227. }
  228. }
  229. }
  230. // 将 map 中的省份名称放入切片
  231. var provinces []string
  232. for province := range provinceMap {
  233. provinces = append(provinces, province)
  234. }
  235. // 返回省份列表
  236. return &http_model.GetProviceResponse{
  237. Provices: provinces,
  238. }, nil
  239. }