task_income.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. incomeIdList := strings.Split(DataInfoReq.IncomeIdList, ",")
  90. // taskIdListInt := utils.TypeTran.String2Int(taskIdListStr)
  91. // 检验是否taskIdList中所有task均处于可提现状态,且talent_id是否正确
  92. var incomeList []model.YounggeeTalentIncome
  93. err = g.Model(dao.YounggeeTalentIncome.Table).Where("talent_id = ? and withdraw_status = 1", tid).And("task_id IN (?)", incomeIdList).Scan(&incomeList)
  94. if err != nil {
  95. return &TalentHttpResult{Code: -3, Msg: "Get task list failed"}
  96. }
  97. if len(incomeIdList) != len(incomeList) {
  98. return &TalentHttpResult{Code: -4, Msg: "Task TalentID Error"}
  99. }
  100. var talentBank model.YounggeeTalentBank
  101. err = g.Model(dao.YounggeeTalentBank.Table).Where("talent_id = ?", tid).Scan(&talentBank)
  102. if err != nil {
  103. return &TalentHttpResult{Code: -5, Msg: "Get Bank Info failed"}
  104. }
  105. bankJsons, errs := json.Marshal(talentBank) //转换成JSON返回的是byte[]
  106. if errs != nil {
  107. return &TalentHttpResult{Code: -6, Msg: " Json transformed failed"}
  108. }
  109. err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  110. s1 := tid[7:]
  111. s2 := fmt.Sprintf("%d", gtime.Now().YearDay())
  112. s3 := fmt.Sprintf("%02v", rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(100))
  113. s := "6" + s1 + s2 + s3
  114. RealAmount := math.Floor(float64(DataInfoReq.TotalAmount) * (1 - 0.05))
  115. taxAmount := math.Floor(float64(DataInfoReq.TotalAmount) * 0.05)
  116. // 插入提现记录
  117. _, err1 := tx.Ctx(ctx).Model(dao.YounggeeWithdrawRecord.Table).Data(model.YounggeeWithdrawRecord{
  118. WithdrawId: s,
  119. TalentId: tid,
  120. WithdrawAmount: float64(DataInfoReq.TotalAmount), //全部金额
  121. AmountPayable: RealAmount, //实际到账
  122. IncomeIdList: DataInfoReq.IncomeIdList,
  123. ReceiveInfo: string(bankJsons),
  124. BankType: 2,
  125. Status: 1,
  126. SubmitAt: gtime.Now(),
  127. TaxAmount: taxAmount, //税
  128. }).InsertAndGetId()
  129. if err1 != nil {
  130. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  131. return err1
  132. }
  133. // 更新income表中提现状态
  134. _, err1 = tx.Ctx(ctx).Model(dao.YounggeeTalentIncome.Table).Where("task_id IN (?)", incomeIdList).Update(g.Map{"withdraw_status": 2, "withdraw_at": gtime.Now()})
  135. if err1 != nil {
  136. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  137. return err1
  138. }
  139. // 更新talent表中提现金额
  140. _, err1 = tx.Ctx(ctx).Model(dao.YoungeeTalentInfo.Table).Where("id = ?", tid).Increment("withdrawing", float64(DataInfoReq.TotalAmount))
  141. if err1 != nil {
  142. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  143. return err1
  144. }
  145. _, err1 = tx.Ctx(ctx).Model(dao.YoungeeTalentInfo.Table).Where("id = ?", tid).Decrement("canwithdraw", float64(DataInfoReq.TotalAmount))
  146. if err1 != nil {
  147. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  148. return err1
  149. }
  150. //todo 达人提现插入消息
  151. // // 达人消息
  152. // for _, taskId := range taskIdListStr {
  153. // taskInfo := model.YoungeeTaskInfo{}
  154. // err1 = tx.Ctx(ctx).Model(dao.YoungeeTaskInfo.Table).Where("task_id = ?", taskId).Scan(&taskInfo)
  155. // if err1 != nil {
  156. // fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  157. // return err1
  158. // }
  159. // projectInfo := model.ProjectInfo{}
  160. // err1 = tx.Ctx(ctx).Model(model.ProjectInfo{}).Where("project_id = ?", taskInfo.ProjectId).Scan(&projectInfo)
  161. // if err1 != nil {
  162. // fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  163. // return err1
  164. // }
  165. // messageInfo := model.YounggeeMessageInfo{
  166. // MessageId: 12,
  167. // MessageType: 2,
  168. // CreatedAt: gtime.Now(),
  169. // TalentId: taskId,
  170. // ProjectName: projectInfo.ProjectName,
  171. // IsReaded: 0,
  172. // IsDeleted: 0,
  173. // }
  174. // _, err = tx.Ctx(ctx).Model(dao.YounggeeMessageInfo.Table).Data(&messageInfo).Insert()
  175. // if err != nil {
  176. // fmt.Printf("[Withdraw Transaction] Error:%+v\n", err)
  177. // return err
  178. // }
  179. // }
  180. return nil
  181. })
  182. if err != nil {
  183. return &TalentHttpResult{Code: -7, Msg: "Add Withdraw Record failed"}
  184. }
  185. return &TalentHttpResult{Code: 0, Msg: "success"}
  186. }
  187. func GetWithdrawList(r *ghttp.Request) *TalentHttpResult {
  188. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  189. if err != nil {
  190. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  191. }
  192. queryInt := r.GetQueryInt("status", -1)
  193. var Records []*youngee_talent_model.WithdrawRecord
  194. if queryInt == 0 {
  195. g.DB().Model("younggee_withdraw_record").Where("talent_id = ? ", tid).Scan(&Records)
  196. } else {
  197. g.DB().Model("younggee_withdraw_record").Where("talent_id = ? and status = ?", tid, queryInt).Scan(&Records)
  198. }
  199. return &TalentHttpResult{Code: 0, Msg: "success", Data: Records}
  200. }
  201. func GetWithdrawDetail(r *ghttp.Request) *TalentHttpResult {
  202. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  203. if err != nil {
  204. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  205. }
  206. withdrawId := r.GetQueryString("withdraw_id", "")
  207. var WithdrawDetail *youngee_talent_model.WithdrawDetail
  208. err = g.DB().Model("younggee_withdraw_record").Where("talent_id = ? and withdraw_id = ?", tid, withdrawId).Scan(&WithdrawDetail)
  209. if err != nil {
  210. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  211. }
  212. // 将 IncomeIdList 分割成 ID 列表
  213. incomeIDs := strings.Split(WithdrawDetail.IncomeIdList, ",")
  214. err = g.DB().Model("younggee_talent_income").Where("id IN ?", incomeIDs).Scan(WithdrawDetail.TalentIncomeInfo)
  215. if err != nil {
  216. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  217. }
  218. return &TalentHttpResult{Code: 0, Msg: "success", Data: WithdrawDetail}
  219. }