task_income.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. incomeIdStrList := strings.Split(DataInfoReq.IncomeIdList, ",") //to String 类型数组,
  90. incomeIdList := utils.TypeTran.String2Int(incomeIdStrList) //to int类型数组
  91. // 检验是否均处于可提现状态,且达人id是否对应
  92. var incomeList []model.YounggeeTalentIncome
  93. err = g.Model(dao.YounggeeTalentIncome.Table).Where("talent_id = ? and withdraw_status = 1", tid).And("id IN (?)", incomeIdList).Scan(&incomeList)
  94. if err != nil {
  95. return &TalentHttpResult{Code: -4, Msg: err.Error()}
  96. }
  97. if len(incomeIdList) != len(incomeList) {
  98. return &TalentHttpResult{Code: -3, Msg: "Req IncomeIdList Error"}
  99. }
  100. var talentBank youngee_talent_model.TalentBankInfo
  101. err = g.Model("younggee_talent_bank").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("younggee_withdraw_record").Data(youngee_talent_model.WithdrawRecord{
  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. Name: talentBank.Name,
  129. IdcardNum: talentBank.BankCardId,
  130. BankNum: talentBank.BankCardNumber,
  131. PhoneNum: talentBank.Phone,
  132. }).InsertAndGetId()
  133. if err1 != nil {
  134. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  135. return err1
  136. }
  137. // 更新income表中提现状态
  138. _, err1 = tx.Ctx(ctx).Model(dao.YounggeeTalentIncome.Table).Where("id IN (?)", incomeIdList).Update(g.Map{"withdraw_status": 2, "withdraw_at": gtime.Now()})
  139. if err1 != nil {
  140. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  141. return err1
  142. }
  143. // 更新talent表中提现金额
  144. _, err1 = tx.Ctx(ctx).Model(dao.YoungeeTalentInfo.Table).Where("id = ?", tid).Increment("withdrawing", float64(DataInfoReq.TotalAmount))
  145. if err1 != nil {
  146. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  147. return err1
  148. }
  149. _, err1 = tx.Ctx(ctx).Model(dao.YoungeeTalentInfo.Table).Where("id = ?", tid).Decrement("canwithdraw", float64(DataInfoReq.TotalAmount))
  150. if err1 != nil {
  151. fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  152. return err1
  153. }
  154. //todo 达人提现插入消息
  155. // // 达人消息
  156. // for _, taskId := range taskIdListStr {
  157. // taskInfo := model.YoungeeTaskInfo{}
  158. // err1 = tx.Ctx(ctx).Model(dao.YoungeeTaskInfo.Table).Where("task_id = ?", taskId).Scan(&taskInfo)
  159. // if err1 != nil {
  160. // fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  161. // return err1
  162. // }
  163. // projectInfo := model.ProjectInfo{}
  164. // err1 = tx.Ctx(ctx).Model(model.ProjectInfo{}).Where("project_id = ?", taskInfo.ProjectId).Scan(&projectInfo)
  165. // if err1 != nil {
  166. // fmt.Printf("[Withdraw Transaction] Error:%+v\n", err1)
  167. // return err1
  168. // }
  169. // messageInfo := model.YounggeeMessageInfo{
  170. // MessageId: 12,
  171. // MessageType: 2,
  172. // CreatedAt: gtime.Now(),
  173. // TalentId: taskId,
  174. // ProjectName: projectInfo.ProjectName,
  175. // IsReaded: 0,
  176. // IsDeleted: 0,
  177. // }
  178. // _, err = tx.Ctx(ctx).Model(dao.YounggeeMessageInfo.Table).Data(&messageInfo).Insert()
  179. // if err != nil {
  180. // fmt.Printf("[Withdraw Transaction] Error:%+v\n", err)
  181. // return err
  182. // }
  183. // }
  184. return nil
  185. })
  186. if err != nil {
  187. return &TalentHttpResult{Code: -7, Msg: "Add Withdraw Record failed"}
  188. }
  189. return &TalentHttpResult{Code: 0, Msg: "success"}
  190. }
  191. func GetWithdrawList(r *ghttp.Request) *TalentHttpResult {
  192. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  193. if err != nil {
  194. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  195. }
  196. queryInt := r.GetQueryInt("status", -1)
  197. var Records []*youngee_talent_model.WithdrawRecord
  198. if queryInt == 0 {
  199. g.DB().Model("younggee_withdraw_record").Where("talent_id = ? ", tid).Scan(&Records)
  200. } else {
  201. g.DB().Model("younggee_withdraw_record").Where("talent_id = ? and status = ?", tid, queryInt).Scan(&Records)
  202. }
  203. return &TalentHttpResult{Code: 0, Msg: "success", Data: Records}
  204. }
  205. func GetWithdrawDetail(r *ghttp.Request) *TalentHttpResult {
  206. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  207. if err != nil {
  208. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  209. }
  210. withdrawId := r.GetQueryString("withdraw_id", "")
  211. var WithdrawDetail *youngee_talent_model.WithdrawDetail
  212. err = g.DB().Model("younggee_withdraw_record").Where("talent_id = ? and withdraw_id = ?", tid, withdrawId).Scan(&WithdrawDetail)
  213. if err != nil {
  214. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  215. }
  216. // 将 IncomeIdList 分割成 ID 列表
  217. incomeIDs := strings.Split(WithdrawDetail.IncomeIdList, ",")
  218. err = g.DB().Model("younggee_talent_income").Where("id IN ?", incomeIDs).Scan(WithdrawDetail.TalentIncomeInfo)
  219. if err != nil {
  220. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  221. }
  222. return &TalentHttpResult{Code: 0, Msg: "success", Data: WithdrawDetail}
  223. }