task_income.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package youngee_task_service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "math"
  7. "math/rand"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "youngmini_server/app/dao"
  12. "youngmini_server/app/model"
  13. "youngmini_server/app/model/youngee_talent_model"
  14. "youngmini_server/app/utils"
  15. "github.com/gogf/gf/database/gdb"
  16. "github.com/gogf/gf/frame/g"
  17. "github.com/gogf/gf/net/ghttp"
  18. "github.com/gogf/gf/os/gtime"
  19. )
  20. func GetWithdrawTaskInfo(r *ghttp.Request) *TalentHttpResult {
  21. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  22. if err != nil {
  23. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  24. }
  25. withdrawTaskInfoList := youngee_talent_model.WithdrawTaskInfoList{}
  26. // 获取可提现任务列表
  27. var taskList []*model.YoungeeTaskInfo
  28. err = g.Model(dao.YoungeeTaskInfo.Table).Where("talent_id = ?", tid).And("withdraw_status IN(?)", g.Slice{2, 3, 4}).Scan(&taskList)
  29. if err != nil {
  30. return &TalentHttpResult{Code: -1, Msg: "Get task list failed"}
  31. }
  32. platformMap := make(map[string]model.InfoThirdPlatform)
  33. platformInfo := []*model.InfoThirdPlatform{}
  34. if len(taskList) != 0 {
  35. err := g.Model(dao.InfoThirdPlatform.Table).Scan(&platformInfo)
  36. if err != nil {
  37. return &TalentHttpResult{Code: -1, Msg: "Get platform failed"}
  38. }
  39. for i, _ := range platformInfo {
  40. platformMap[strconv.Itoa(platformInfo[i].PlatformId)] = *platformInfo[i]
  41. }
  42. }
  43. for _, v := range taskList {
  44. projectInfo, err := g.Model(dao.ProjectInfo.Table).Where("project_id = ?", v.ProjectId).One()
  45. if err != nil {
  46. return &TalentHttpResult{Code: -1, Msg: "Get fullproject info failed"}
  47. }
  48. // product, err := g.Model(dao.YounggeeProduct.Table).One("product_id = ?", projectInfo[dao.ProjectInfo.Columns.ProductId])
  49. // if err != nil {
  50. // return &TalentHttpResult{Code: -1, Msg: "Get product mainphoto failed"}
  51. // }
  52. // strategy := model.RecruitStrategy{}
  53. // err = g.Model(dao.RecruitStrategy.Table).Where("project_id = ? and strategy_id = ?", v.ProjectId, v.StrategyId).Scan(&strategy)
  54. // if err != nil {
  55. // return &TalentHttpResult{Code: -1, Msg: "Get RecruitStrategy failed"}
  56. // }
  57. taskInfoBrief := &youngee_talent_model.WithdrawTaskInfo{
  58. TaskId: v.TaskId,
  59. ProjectName: projectInfo[dao.ProjectInfo.Columns.ProjectName].String(),
  60. ProductPhoto: projectInfo[dao.ProjectInfo.Columns.ProductPhotoSnap].String(),
  61. PlatformIconUrl: platformMap[projectInfo[dao.ProjectInfo.Columns.ProjectPlatform].String()].PlatformIcon,
  62. PlatformName: platformMap[projectInfo[dao.ProjectInfo.Columns.ProjectPlatform].String()].PlatformName,
  63. TaskReward: v.TaskReward,
  64. SettleAmount: v.SettleAmount,
  65. CompleteDate: v.CompleteDate,
  66. WithdrawDate: v.WithdrawDate,
  67. Checked: false,
  68. }
  69. if v.WithdrawStatus == 2 {
  70. withdrawTaskInfoList.CanWithdrawTaskInfoList = append(withdrawTaskInfoList.CanWithdrawTaskInfoList, taskInfoBrief)
  71. } else if v.WithdrawStatus == 3 {
  72. withdrawTaskInfoList.WithdrawingTaskInfoList = append(withdrawTaskInfoList.WithdrawingTaskInfoList, taskInfoBrief)
  73. } else if v.WithdrawStatus == 4 {
  74. withdrawTaskInfoList.WithdrawedTaskInfoList = append(withdrawTaskInfoList.WithdrawedTaskInfoList, taskInfoBrief)
  75. }
  76. }
  77. return &TalentHttpResult{Code: 0, Msg: "success", Data: withdrawTaskInfoList}
  78. }
  79. func Withdraw(r *ghttp.Request) *TalentHttpResult {
  80. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  81. if err != nil {
  82. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  83. }
  84. var DataInfoReq *youngee_talent_model.WithdrawInfo
  85. err = r.ParseForm(&DataInfoReq)
  86. if err != nil {
  87. return &TalentHttpResult{Code: -2, Msg: err.Error()}
  88. }
  89. var talentBank model.YounggeeTalentBank
  90. err = g.Model(dao.YounggeeTalentBank.Table).Where("talent_id = ?", tid).Scan(&talentBank)
  91. if err != nil {
  92. return &TalentHttpResult{Code: -5, Msg: "Get Bank Info failed"}
  93. }
  94. bankJsons, errs := json.Marshal(talentBank) //转换成JSON返回的是byte[]
  95. if errs != nil {
  96. return &TalentHttpResult{Code: -6, Msg: " Json transformed failed"}
  97. }
  98. err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  99. s1 := tid[7:]
  100. s2 := fmt.Sprintf("%d", gtime.Now().YearDay())
  101. s3 := fmt.Sprintf("%02v", rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(100))
  102. s := "6" + s1 + s2 + s3
  103. RealAmount := math.Floor(float64(DataInfoReq.TotalAmount) * (1 - 0.05))
  104. taxAmount := math.Floor(float64(DataInfoReq.TotalAmount) * 0.05)
  105. // 插入提现记录
  106. _, err1 := tx.Ctx(ctx).Model(dao.YounggeeWithdrawRecord.Table).Data(model.YounggeeWithdrawRecord{
  107. WithdrawId: s,
  108. TalentId: tid,
  109. WithdrawAmount: float64(DataInfoReq.TotalAmount), //全部金额
  110. AmountPayable: RealAmount, //实际到账
  111. IncomeIdList: DataInfoReq.IncomeIdList,
  112. ReceiveInfo: string(bankJsons),
  113. BankType: 2,
  114. Status: 1,
  115. SubmitAt: gtime.Now(),
  116. TaxAmount: taxAmount, //税
  117. }).InsertAndGetId()
  118. if err1 != nil {
  119. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  120. return err1
  121. }
  122. // 更新income表中提现状态
  123. _, err1 = tx.Ctx(ctx).Model(dao.YounggeeTalentIncome.Table).Where("id IN (?)", incomeIdList).Update(g.Map{"withdraw_status": 2, "withdraw_at": gtime.Now()})
  124. if err1 != nil {
  125. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  126. return err1
  127. }
  128. // 更新talent表中提现金额
  129. _, err1 = tx.Ctx(ctx).Model(dao.YoungeeTalentInfo.Table).Where("id = ?", tid).Increment("withdrawing", float64(DataInfoReq.TotalAmount))
  130. if err1 != nil {
  131. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  132. return err1
  133. }
  134. _, err1 = tx.Ctx(ctx).Model(dao.YoungeeTalentInfo.Table).Where("id = ?", tid).Decrement("canwithdraw", float64(DataInfoReq.TotalAmount))
  135. if err1 != nil {
  136. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  137. return err1
  138. }
  139. //todo 达人提现插入消息
  140. // // 达人消息
  141. // for _, taskId := range taskIdListStr {
  142. // taskInfo := model.YoungeeTaskInfo{}
  143. // err1 = tx.Ctx(ctx).Model(dao.YoungeeTaskInfo.Table).Where("task_id = ?", taskId).Scan(&taskInfo)
  144. // if err1 != nil {
  145. // fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  146. // return err1
  147. // }
  148. // projectInfo := model.ProjectInfo{}
  149. // err1 = tx.Ctx(ctx).Model(model.ProjectInfo{}).Where("project_id = ?", taskInfo.ProjectId).Scan(&projectInfo)
  150. // if err1 != nil {
  151. // fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  152. // return err1
  153. // }
  154. // messageInfo := model.YounggeeMessageInfo{
  155. // MessageId: 12,
  156. // MessageType: 2,
  157. // CreatedAt: gtime.Now(),
  158. // TalentId: taskId,
  159. // ProjectName: projectInfo.ProjectName,
  160. // IsReaded: 0,
  161. // IsDeleted: 0,
  162. // }
  163. // _, err = tx.Ctx(ctx).Model(dao.YounggeeMessageInfo.Table).Data(&messageInfo).Insert()
  164. // if err != nil {
  165. // fmt.Printf("[Withdraw Transaction] Error:%+v\n", err)
  166. // return err
  167. // }
  168. // }
  169. return nil
  170. })
  171. if err != nil {
  172. return &TalentHttpResult{Code: -7, Msg: "Add Withdraw Record failed"}
  173. }
  174. return &TalentHttpResult{Code: 0, Msg: "success"}
  175. }
  176. func GetWithdrawList(r *ghttp.Request) *TalentHttpResult {
  177. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  178. if err != nil {
  179. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  180. }
  181. queryInt := r.GetQueryInt("status", -1)
  182. var Records []*youngee_talent_model.WithdrawRecord
  183. if queryInt == 0 {
  184. g.DB().Model("younggee_withdraw_record").Where("talent_id = ? ", tid).Scan(&Records)
  185. } else {
  186. g.DB().Model("younggee_withdraw_record").Where("talent_id = ? and status = ?", tid, queryInt).Scan(&Records)
  187. }
  188. return &TalentHttpResult{Code: 0, Msg: "success", Data: Records}
  189. }
  190. func GetWithdrawDetail(r *ghttp.Request) *TalentHttpResult {
  191. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  192. if err != nil {
  193. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  194. }
  195. withdrawId := r.GetQueryString("withdraw_id", "")
  196. var WithdrawDetail *youngee_talent_model.WithdrawDetail
  197. err = g.DB().Model("younggee_withdraw_record").Where("talent_id = ? and withdraw_id = ?", tid, withdrawId).Scan(&WithdrawDetail)
  198. if err != nil {
  199. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  200. }
  201. // 将 IncomeIdList 分割成 ID 列表
  202. incomeIDs := strings.Split(WithdrawDetail.IncomeIdList, ",")
  203. err = g.DB().Model("younggee_talent_income").Where("id IN ?", incomeIDs).Scan(WithdrawDetail.TalentIncomeInfo)
  204. if err != nil {
  205. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  206. }
  207. return &TalentHttpResult{Code: 0, Msg: "success", Data: WithdrawDetail}
  208. }