talent_income.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package youngee_talent_service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "math/rand"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "youngmini_server/app/dao"
  11. "youngmini_server/app/model"
  12. "youngmini_server/app/model/youngee_talent_model"
  13. "youngmini_server/app/utils"
  14. "github.com/gogf/gf/database/gdb"
  15. "github.com/gogf/gf/frame/g"
  16. "github.com/gogf/gf/net/ghttp"
  17. "github.com/gogf/gf/os/gtime"
  18. )
  19. func GetTalentIncomeInfo(r *ghttp.Request) *TalentHttpResult {
  20. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  21. if err != nil {
  22. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  23. }
  24. var getTalentIncomeReq *youngee_talent_model.GetTalentIncomeReq
  25. err = r.ParseForm(&getTalentIncomeReq)
  26. if err != nil {
  27. return &TalentHttpResult{Code: -2, Msg: "params error"}
  28. }
  29. var talentIncomerep []youngee_talent_model.TalentIncomeInfo
  30. var talentIncomeList []*model.YounggeeTalentIncome
  31. err = g.DB().Model(dao.YounggeeTalentIncome.Table).Where("talent_id = ? and withdraw_status = ?", tid, getTalentIncomeReq.Type).Scan(&talentIncomeList)
  32. if err != nil {
  33. return &TalentHttpResult{Code: -3, Msg: err.Error()}
  34. }
  35. platformMap := make(map[string]model.InfoThirdPlatform)
  36. platformInfo := []*model.InfoThirdPlatform{}
  37. if len(talentIncomeList) != 0 {
  38. err := g.Model(dao.InfoThirdPlatform.Table).Scan(&platformInfo)
  39. if err != nil {
  40. return &TalentHttpResult{Code: -1, Msg: "Get platform failed"}
  41. }
  42. for i, _ := range platformInfo {
  43. platformMap[strconv.Itoa(platformInfo[i].PlatformId)] = *platformInfo[i]
  44. }
  45. }
  46. for _, v := range talentIncomeList { //遍历收入表中的所有信息
  47. var platform model.InfoThirdPlatform
  48. switch v.Type {
  49. case 1: //种草收入
  50. projectPlatform, err := g.Model(dao.ProjectInfo.Table).Fields("project_platform").Where("project_id = ?", v.ProjectId).Value()
  51. if err != nil {
  52. return &TalentHttpResult{Code: -1, Msg: "Get fullproject info failed"}
  53. }
  54. platform = platformMap[projectPlatform.String()]
  55. talentIncomeInfo := youngee_talent_model.TalentIncomeInfo{
  56. Id: v.Id,
  57. ProjectId: v.ProjectId,
  58. TaskId: v.TaskId,
  59. Type: v.Type,
  60. BrandName: v.BrandName,
  61. TaskName: v.TaskName,
  62. PhotoUrl: v.PhotoUrl,
  63. TeamId: v.TeamId,
  64. Income: v.Income,
  65. IncomeType: v.IncomeType,
  66. WithdrawStatus: v.WithdrawStatus,
  67. IncomeAt: v.IncomeAt,
  68. WithdrawAt: v.WithdrawAt,
  69. PlatformIconUrl: platform.PlatformIcon,
  70. }
  71. talentIncomerep = append(talentIncomerep, talentIncomeInfo)
  72. break
  73. case 2: //带货收入
  74. platformId, err := g.Model(dao.YounggeeSelectionInfo.Table).Fields("platform").Where("selection_id = ?", v.SelectionId).Value()
  75. if err != nil {
  76. return &TalentHttpResult{Code: -1, Msg: "Get fullproject info failed"}
  77. }
  78. platform = platformMap[platformId.String()]
  79. talentIncomeInfo := youngee_talent_model.TalentIncomeInfo{
  80. Id: v.Id,
  81. ProjectId: v.SelectionId,
  82. TaskId: v.SectaskId,
  83. Type: v.Type,
  84. BrandName: v.BrandName,
  85. TaskName: v.TaskName,
  86. PhotoUrl: v.PhotoUrl,
  87. TeamId: v.TeamId,
  88. Income: v.Income,
  89. IncomeType: v.IncomeType,
  90. WithdrawStatus: v.WithdrawStatus,
  91. IncomeAt: v.IncomeAt,
  92. WithdrawAt: v.WithdrawAt,
  93. PlatformIconUrl: platform.PlatformIcon,
  94. }
  95. talentIncomerep = append(talentIncomerep, talentIncomeInfo)
  96. break
  97. default:
  98. }
  99. }
  100. return &TalentHttpResult{Code: 0, Msg: "success", Data: talentIncomerep}
  101. }
  102. // 提现
  103. func Withdraw(r *ghttp.Request) *TalentHttpResult {
  104. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  105. if err != nil {
  106. return &TalentHttpResult{Code: -1, Msg: err.Error()}
  107. }
  108. var DataInfoReq *youngee_talent_model.WithdrawInfo
  109. err = r.ParseForm(&DataInfoReq)
  110. if err != nil {
  111. return &TalentHttpResult{Code: -2, Msg: err.Error()}
  112. }
  113. incomeIdStrList := strings.Split(DataInfoReq.IncomeIdList, ",")
  114. incomeIdList := utils.TypeTran.String2Int(incomeIdStrList)
  115. // 检验是否均处于可提现状态,且达人id是否对应
  116. var incomeList []model.YounggeeTalentIncome
  117. err = g.Model(dao.YounggeeTalentIncome.Table).Where("talent_id = ? and withdraw_status = 1", tid).And("id IN (?)", incomeIdList).Scan(&incomeList)
  118. if err != nil {
  119. return &TalentHttpResult{Code: -4, Msg: err.Error()}
  120. }
  121. if len(incomeIdList) != len(incomeList) {
  122. return &TalentHttpResult{Code: -3, Msg: "Req IncomeIdList Error"}
  123. }
  124. // 检查达人积分是否足够
  125. totalPoint, err := g.Model(dao.YoungeeTalentInfo.Table).Fields("point").Where("id = ?", tid).Value()
  126. if err != nil {
  127. return &TalentHttpResult{Code: -5, Msg: err.Error()}
  128. }
  129. if totalPoint.Int() < DataInfoReq.Point {
  130. return &TalentHttpResult{Code: -3, Msg: "Req Point Error"}
  131. }
  132. // 查询达人提现到账账户信息
  133. var talentBank model.YounggeeTalentBank
  134. err = g.Model(dao.YounggeeTalentBank.Table).Where("talent_id = ?", tid).Scan(&talentBank)
  135. if err != nil {
  136. return &TalentHttpResult{Code: -6, Msg: err.Error()}
  137. }
  138. bankJsons, errs := json.Marshal(talentBank) //转换成JSON返回的是byte[]
  139. if errs != nil {
  140. return &TalentHttpResult{Code: -7, Msg: err.Error()}
  141. }
  142. err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  143. s1 := tid[7:]
  144. s2 := fmt.Sprintf("%d", gtime.Now().YearDay())
  145. s3 := fmt.Sprintf("%02v", rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(100))
  146. s := "6" + s1 + s2 + s3
  147. // 插入提现记录
  148. _, err1 := tx.Ctx(ctx).Model(dao.YounggeeWithdrawRecord.Table).Data(model.YounggeeWithdrawRecord{
  149. WithdrawId: s,
  150. TalentId: tid,
  151. WithdrawAmount: float64(DataInfoReq.TotalAmount),
  152. AmountPayable: float64(DataInfoReq.RealAmount),
  153. IncomeIdList: DataInfoReq.IncomeIdList,
  154. PayPoint: DataInfoReq.Point,
  155. ReceiveInfo: string(bankJsons),
  156. BankType: 2,
  157. Status: 1,
  158. SubmitAt: gtime.Now(),
  159. }).InsertAndGetId()
  160. if err1 != nil {
  161. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  162. return err1
  163. }
  164. // 更新income表中提现状态
  165. _, err1 = tx.Ctx(ctx).Model(dao.YounggeeTalentIncome.Table).Where("id IN (?)", incomeIdList).Update(g.Map{"withdraw_status": 2, "withdraw_at": gtime.Now()})
  166. if err1 != nil {
  167. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  168. return err1
  169. }
  170. // 更新talent表中提现金额
  171. _, err1 = tx.Ctx(ctx).Model(dao.YoungeeTalentInfo.Table).Where("id = ?", tid).Increment("withdrawing", float64(DataInfoReq.TotalAmount))
  172. if err1 != nil {
  173. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  174. return err1
  175. }
  176. _, err1 = tx.Ctx(ctx).Model(dao.YoungeeTalentInfo.Table).Where("id = ?", tid).Decrement("canwithdraw", float64(DataInfoReq.TotalAmount))
  177. if err1 != nil {
  178. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  179. return err1
  180. }
  181. // 达人消息
  182. for _, incomeInfo := range incomeList {
  183. messageTypeId := [4]int{25, 26, 27, 28}
  184. messageInfo := model.YounggeeMessageInfo{
  185. MessageId: messageTypeId[incomeInfo.IncomeType-1],
  186. MessageType: 2,
  187. CreatedAt: gtime.Now(),
  188. ProjectName: incomeInfo.TaskName,
  189. IsReaded: 0,
  190. IsDeleted: 0,
  191. }
  192. _, err = tx.Ctx(ctx).Model(dao.YounggeeMessageInfo.Table).Data(&messageInfo).Insert()
  193. if err != nil {
  194. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err)
  195. return err
  196. }
  197. }
  198. return nil
  199. })
  200. if err != nil {
  201. return &TalentHttpResult{Code: -8, Msg: "Add Withdraw Record failed: " + err.Error()}
  202. }
  203. return &TalentHttpResult{Code: 0, Msg: "success"}
  204. }
  205. // 查询积分明细
  206. func GetTalentPointInfo(r *ghttp.Request) *TalentHttpResult {
  207. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  208. if err != nil {
  209. return &TalentHttpResult{Code: -1, Msg: err.Error()}
  210. }
  211. var talentPointList []*model.YounggeeTalentPointRecord
  212. err = g.DB().Model(dao.YounggeeTalentPointRecord.Table).Where("talent_id = ?", tid).Order("income_at").Scan(talentPointList)
  213. if err != nil {
  214. return &TalentHttpResult{Code: -2, Msg: err.Error()}
  215. }
  216. point, err := g.DB().Model(dao.YoungeeTalentInfo.Table).Fields("point").Where("talent_id = ?", tid).Value()
  217. if err != nil {
  218. return &TalentHttpResult{Code: -2, Msg: err.Error()}
  219. }
  220. TalentPointInfo := youngee_talent_model.TalentPointInfo{
  221. Point: point.Int(),
  222. TalentPointRecord: talentPointList,
  223. }
  224. return &TalentHttpResult{Code: 0, Msg: "success", Data: TalentPointInfo}
  225. }