task_income.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package youngee_task_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 GetWithdrawTaskInfo(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. withdrawTaskInfoList := youngee_talent_model.WithdrawTaskInfoList{}
  25. // 获取可提现任务列表
  26. var taskList []*model.YoungeeTaskInfo
  27. err = g.Model(dao.YoungeeTaskInfo.Table).Where("talent_id = ?", tid).And("withdraw_status IN(?)", g.Slice{2, 3, 4}).Scan(&taskList)
  28. if err != nil {
  29. return &TalentHttpResult{Code: -1, Msg: "Get task list failed"}
  30. }
  31. platformMap := make(map[string]model.InfoThirdPlatform)
  32. platformInfo := []*model.InfoThirdPlatform{}
  33. if len(taskList) != 0 {
  34. err := g.Model(dao.InfoThirdPlatform.Table).Scan(&platformInfo)
  35. if err != nil {
  36. return &TalentHttpResult{Code: -1, Msg: "Get platform failed"}
  37. }
  38. for i, _ := range platformInfo {
  39. platformMap[strconv.Itoa(platformInfo[i].PlatformId)] = *platformInfo[i]
  40. }
  41. }
  42. for _, v := range taskList {
  43. projectInfo, err := g.Model(dao.ProjectInfo.Table).Where("project_id = ?", v.ProjectId).One()
  44. if err != nil {
  45. return &TalentHttpResult{Code: -1, Msg: "Get fullproject info failed"}
  46. }
  47. // product, err := g.Model(dao.YounggeeProduct.Table).One("product_id = ?", projectInfo[dao.ProjectInfo.Columns.ProductId])
  48. // if err != nil {
  49. // return &TalentHttpResult{Code: -1, Msg: "Get product mainphoto failed"}
  50. // }
  51. // strategy := model.RecruitStrategy{}
  52. // err = g.Model(dao.RecruitStrategy.Table).Where("project_id = ? and strategy_id = ?", v.ProjectId, v.StrategyId).Scan(&strategy)
  53. // if err != nil {
  54. // return &TalentHttpResult{Code: -1, Msg: "Get RecruitStrategy failed"}
  55. // }
  56. taskInfoBrief := &youngee_talent_model.WithdrawTaskInfo{
  57. TaskId: v.TaskId,
  58. ProjectName: projectInfo[dao.ProjectInfo.Columns.ProjectName].String(),
  59. ProductPhoto: projectInfo[dao.ProjectInfo.Columns.ProductPhotoSnap].String(),
  60. PlatformIconUrl: platformMap[projectInfo[dao.ProjectInfo.Columns.ProjectPlatform].String()].PlatformIcon,
  61. PlatformName: platformMap[projectInfo[dao.ProjectInfo.Columns.ProjectPlatform].String()].PlatformName,
  62. TaskReward: v.TaskReward,
  63. SettleAmount: v.SettleAmount,
  64. CompleteDate: v.CompleteDate,
  65. WithdrawDate: v.WithdrawDate,
  66. Checked: false,
  67. }
  68. if v.WithdrawStatus == 2 {
  69. withdrawTaskInfoList.CanWithdrawTaskInfoList = append(withdrawTaskInfoList.CanWithdrawTaskInfoList, taskInfoBrief)
  70. } else if v.WithdrawStatus == 3 {
  71. withdrawTaskInfoList.WithdrawingTaskInfoList = append(withdrawTaskInfoList.WithdrawingTaskInfoList, taskInfoBrief)
  72. } else if v.WithdrawStatus == 4 {
  73. withdrawTaskInfoList.WithdrawedTaskInfoList = append(withdrawTaskInfoList.WithdrawedTaskInfoList, taskInfoBrief)
  74. }
  75. }
  76. return &TalentHttpResult{Code: 0, Msg: "success", Data: withdrawTaskInfoList}
  77. }
  78. func Withdraw(r *ghttp.Request) *TalentHttpResult {
  79. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  80. if err != nil {
  81. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  82. }
  83. var DataInfoReq *youngee_talent_model.WithdrawInfo
  84. err = r.ParseForm(&DataInfoReq)
  85. if err != nil {
  86. return &TalentHttpResult{Code: -2, Msg: err.Error()}
  87. }
  88. incomeIdList := strings.Split(DataInfoReq.IncomeIdList, ",")
  89. // taskIdListInt := utils.TypeTran.String2Int(taskIdListStr)
  90. // 检验是否taskIdList中所有task均处于可提现状态,且talent_id是否正确
  91. var incomeList []model.YounggeeTalentIncome
  92. err = g.Model(dao.YounggeeTalentIncome.Table).Where("talent_id = ? and withdraw_status = 1", tid).And("task_id IN (?)", incomeIdList).Scan(&incomeList)
  93. if err != nil {
  94. return &TalentHttpResult{Code: -3, Msg: "Get task list failed"}
  95. }
  96. if len(incomeIdList) != len(incomeList) {
  97. return &TalentHttpResult{Code: -4, Msg: "Task TalentID Error"}
  98. }
  99. var talentBank model.YounggeeTalentBank
  100. err = g.Model(dao.YounggeeTalentBank.Table).Where("talent_id = ?", tid).Scan(&talentBank)
  101. if err != nil {
  102. return &TalentHttpResult{Code: -5, Msg: "Get Bank Info failed"}
  103. }
  104. bankJsons, errs := json.Marshal(talentBank) //转换成JSON返回的是byte[]
  105. if errs != nil {
  106. return &TalentHttpResult{Code: -6, Msg: " Json transformed failed"}
  107. }
  108. err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  109. s1 := tid[7:]
  110. s2 := fmt.Sprintf("%d", gtime.Now().YearDay())
  111. s3 := fmt.Sprintf("%02v", rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(100))
  112. s := "6" + s1 + s2 + s3
  113. // 插入提现记录
  114. _, err1 := tx.Ctx(ctx).Model(dao.YounggeeWithdrawRecord.Table).Data(model.YounggeeWithdrawRecord{
  115. WithdrawId: s,
  116. TalentId: tid,
  117. WithdrawAmount: float64(DataInfoReq.TotalAmount),
  118. AmountPayable: float64(DataInfoReq.RealAmount),
  119. IncomeIdList: DataInfoReq.IncomeIdList,
  120. ReceiveInfo: string(bankJsons),
  121. BankType: 2,
  122. Status: 1,
  123. SubmitAt: gtime.Now(),
  124. }).InsertAndGetId()
  125. if err1 != nil {
  126. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  127. return err1
  128. }
  129. // 更新income表中提现状态
  130. _, err1 = tx.Ctx(ctx).Model(dao.YounggeeTalentIncome.Table).Where("task_id IN (?)", incomeIdList).Update(g.Map{"withdraw_status": 2, "withdraw_at": gtime.Now()})
  131. if err1 != nil {
  132. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  133. return err1
  134. }
  135. // 更新talent表中提现金额
  136. _, err1 = tx.Ctx(ctx).Model(dao.YoungeeTalentInfo.Table).Where("id = ?", tid).Increment("withdrawing", float64(DataInfoReq.TotalAmount))
  137. if err1 != nil {
  138. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  139. return err1
  140. }
  141. _, err1 = tx.Ctx(ctx).Model(dao.YoungeeTalentInfo.Table).Where("id = ?", tid).Decrement("canwithdraw", float64(DataInfoReq.TotalAmount))
  142. if err1 != nil {
  143. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  144. return err1
  145. }
  146. // // 达人消息
  147. // for _, taskId := range taskIdListStr {
  148. // taskInfo := model.YoungeeTaskInfo{}
  149. // err1 = tx.Ctx(ctx).Model(dao.YoungeeTaskInfo.Table).Where("task_id = ?", taskId).Scan(&taskInfo)
  150. // if err1 != nil {
  151. // fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  152. // return err1
  153. // }
  154. // projectInfo := model.ProjectInfo{}
  155. // err1 = tx.Ctx(ctx).Model(model.ProjectInfo{}).Where("project_id = ?", taskInfo.ProjectId).Scan(&projectInfo)
  156. // if err1 != nil {
  157. // fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  158. // return err1
  159. // }
  160. // messageInfo := model.YounggeeMessageInfo{
  161. // MessageId: 12,
  162. // MessageType: 2,
  163. // CreatedAt: gtime.Now(),
  164. // TalentId: taskId,
  165. // ProjectName: projectInfo.ProjectName,
  166. // IsReaded: 0,
  167. // IsDeleted: 0,
  168. // }
  169. // _, err = tx.Ctx(ctx).Model(dao.YounggeeMessageInfo.Table).Data(&messageInfo).Insert()
  170. // if err != nil {
  171. // fmt.Printf("[Withdraw Transaction] Error:%+v\n", err)
  172. // return err
  173. // }
  174. // }
  175. return nil
  176. })
  177. if err != nil {
  178. return &TalentHttpResult{Code: -7, Msg: "Add Withdraw Record failed"}
  179. }
  180. return &TalentHttpResult{Code: 0, Msg: "success"}
  181. }