|
@@ -2,15 +2,18 @@ package service
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "encoding/json"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
- "github.com/gin-gonic/gin"
|
|
|
- "gorm.io/gorm"
|
|
|
+ "strconv"
|
|
|
"time"
|
|
|
"youngee_b_api/db"
|
|
|
"youngee_b_api/model/gorm_model"
|
|
|
"youngee_b_api/model/http_model"
|
|
|
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "gorm.io/gorm"
|
|
|
+
|
|
|
"github.com/sirupsen/logrus"
|
|
|
)
|
|
|
|
|
@@ -101,8 +104,21 @@ func (*projectPay) Pay(ctx context.Context, projectPay http_model.ProjectPayRequ
|
|
|
func (p *projectPay) SpecialSettlePay(ctx *gin.Context, req *http_model.SpecialSettlePayRequest) error {
|
|
|
DB := db.GetReadDB(ctx)
|
|
|
err := DB.Transaction(func(tx *gorm.DB) error {
|
|
|
+ projectInfo := gorm_model.ProjectInfo{}
|
|
|
+ err := tx.Model(&gorm_model.ProjectInfo{}).Where("project_id = ?", req.ProjectID).First(&projectInfo).Error
|
|
|
+ if err != nil {
|
|
|
+ logrus.WithContext(ctx).Errorf("[projectPay service] call SpecialSettlePay error,err:%+v", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ task := gorm_model.YoungeeTaskInfo{}
|
|
|
+ err = tx.Model(&gorm_model.YoungeeTaskInfo{}).Where("task_id = ?", req.TaskId).First(&task).Error
|
|
|
+ if err != nil {
|
|
|
+ logrus.WithContext(ctx).Errorf("[projectPay service] call SpecialSettlePay error,err:%+v", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ // 校验账户余额是否充足
|
|
|
var balance float64
|
|
|
- err := tx.Model(&gorm_model.Enterprise{}).Select("balance").Where("enterprise_id = ?", req.EnterPriseId).Find(&balance).Error
|
|
|
+ err = tx.Model(&gorm_model.Enterprise{}).Select("balance").Where("enterprise_id = ?", req.EnterPriseId).Find(&balance).Error
|
|
|
if err != nil {
|
|
|
logrus.WithContext(ctx).Errorf("[projectPay service] call SpecialSettlePay error,err:%+v", err)
|
|
|
return err
|
|
@@ -111,26 +127,63 @@ func (p *projectPay) SpecialSettlePay(ctx *gin.Context, req *http_model.SpecialS
|
|
|
if balance < realPay {
|
|
|
return errors.New("余额不足")
|
|
|
}
|
|
|
-
|
|
|
+ // 增加项目支付金额
|
|
|
err = tx.Model(&gorm_model.ProjectInfo{}).Where("project_id = ?", req.ProjectID).
|
|
|
Updates(map[string]interface{}{"payment_amount": gorm.Expr("payment_amount + ?", req.Amount)}).Error
|
|
|
if err != nil {
|
|
|
logrus.WithContext(ctx).Errorf("[projectPay service] call SpecialSettlePay error,err:%+v", err)
|
|
|
return err
|
|
|
}
|
|
|
-
|
|
|
- err = tx.Model(&gorm_model.YoungeeTaskInfo{}).Where("task_id = ?", req.TaskId).Updates(gorm_model.YoungeeTaskInfo{
|
|
|
- TaskReward: req.Amount,
|
|
|
- SettleAmount: req.Amount,
|
|
|
- AllPayment: req.Amount,
|
|
|
- RealPayment: req.Amount,
|
|
|
- SettleStatus: 2,
|
|
|
- WithdrawStatus: 2,
|
|
|
- }).Error
|
|
|
+ // 旧:更新专项任务收益
|
|
|
+ // err = tx.Model(&gorm_model.YoungeeTaskInfo{}).Where("task_id = ?", req.TaskId).Updates(gorm_model.YoungeeTaskInfo{
|
|
|
+ // TaskReward: req.Amount,
|
|
|
+ // SettleAmount: req.Amount,
|
|
|
+ // AllPayment: req.Amount,
|
|
|
+ // RealPayment: req.Amount,
|
|
|
+ // SettleStatus: 2,
|
|
|
+ // WithdrawStatus: 2,
|
|
|
+ // }).Error
|
|
|
+ // if err != nil {
|
|
|
+ // logrus.WithContext(ctx).Errorf("[projectPay service] call SpecialSettlePay error,err:%+v", err)
|
|
|
+ // return err
|
|
|
+ // }
|
|
|
+ // 新:创建专项任务收益,更新任务阶段
|
|
|
+ updateTaskData := gorm_model.YoungeeTaskInfo{
|
|
|
+ TaskReward: req.Amount,
|
|
|
+ SettleAmount: req.Amount,
|
|
|
+ AllPayment: req.Amount,
|
|
|
+ RealPayment: req.Amount,
|
|
|
+ TaskID: req.TaskId,
|
|
|
+ SettleStatus: 2,
|
|
|
+ }
|
|
|
+ _, err = db.UpdateTask(ctx, updateTaskData, tx)
|
|
|
+ if err != nil {
|
|
|
+ logrus.WithContext(ctx).Errorf("[projectPay service] call SpecialSettlePay error,err:%+v", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ var productInfo gorm_model.YounggeeProduct
|
|
|
+ if err = json.Unmarshal([]byte(projectInfo.ProductSnap), &productInfo); err != nil {
|
|
|
+ fmt.Println("Error:", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ income := gorm_model.YounggeeTalentIncome{
|
|
|
+ TalentID: task.TalentID,
|
|
|
+ ProjectID: task.ProjectID,
|
|
|
+ SectaskID: task.TaskID,
|
|
|
+ BrandName: productInfo.BrandName,
|
|
|
+ TaskName: projectInfo.ProjectName,
|
|
|
+ Income: strconv.FormatFloat(req.Amount, 'f', 10, 32),
|
|
|
+ IncomeType: 1,
|
|
|
+ WithdrawStatus: 1,
|
|
|
+ IncomeAt: time.Now(),
|
|
|
+ }
|
|
|
+ err = db.CreateIncome(ctx, income, tx)
|
|
|
if err != nil {
|
|
|
logrus.WithContext(ctx).Errorf("[projectPay service] call SpecialSettlePay error,err:%+v", err)
|
|
|
return err
|
|
|
}
|
|
|
+
|
|
|
+ // 扣除企业账户余额
|
|
|
err = tx.Model(&gorm_model.Enterprise{}).Where("enterprise_id = ?", req.EnterPriseId).
|
|
|
Updates(map[string]interface{}{"balance": gorm.Expr("balance - ?", realPay), "available_balance": gorm.Expr("available_balance - ?", realPay),
|
|
|
"updated_at": time.Now()}).Error
|
|
@@ -144,6 +197,7 @@ func (p *projectPay) SpecialSettlePay(ctx *gin.Context, req *http_model.SpecialS
|
|
|
logrus.WithContext(ctx).Errorf("[projectPay service] call SpecialSettlePay error,err:%+v", err)
|
|
|
return err
|
|
|
}
|
|
|
+ // 增加达人收益
|
|
|
err = tx.Model(&gorm_model.YoungeeTalentInfo{}).Where("id = ?", talentId).
|
|
|
Updates(map[string]interface{}{"income": gorm.Expr("income + ?", req.Amount), "canwithdraw": gorm.Expr("canwithdraw + ?", req.Amount)}).Error
|
|
|
if err != nil {
|