|
@@ -2,8 +2,13 @@ package service
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "errors"
|
|
|
"fmt"
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "gorm.io/gorm"
|
|
|
+ "time"
|
|
|
"youngee_m_api/db"
|
|
|
+ "youngee_m_api/model/gorm_model"
|
|
|
"youngee_m_api/model/http_model"
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
@@ -70,3 +75,50 @@ func (*projectPay) Pay(ctx context.Context, projectPay http_model.ProjectPayRequ
|
|
|
|
|
|
return recordId, nil
|
|
|
}
|
|
|
+
|
|
|
+func (p *projectPay) SpecialSettlePay(ctx *gin.Context, req *http_model.SpecialSettlePayRequest) error {
|
|
|
+ DB := db.GetReadDB(ctx)
|
|
|
+ err := DB.Transaction(func(tx *gorm.DB) error {
|
|
|
+ var balance float64
|
|
|
+ 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
|
|
|
+ }
|
|
|
+ if balance < req.Amount {
|
|
|
+ return errors.New("余额不足")
|
|
|
+ }
|
|
|
+ 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,
|
|
|
+ }).Error
|
|
|
+ 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 - ?", req.Amount), "available_balance": gorm.Expr("available_balance - ?", req.Amount),
|
|
|
+ "updated_at": time.Now()}).Error
|
|
|
+ if err != nil {
|
|
|
+ logrus.WithContext(ctx).Errorf("[projectPay service] call SpecialSettlePay error,err:%+v", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ talentId := ""
|
|
|
+ err = tx.Model(&gorm_model.YoungeeTaskInfo{}).Select("talent_id").Where("task_id = ?", req.TaskId).Find(&talentId).Error
|
|
|
+ if err != nil {
|
|
|
+ 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 {
|
|
|
+ logrus.WithContext(ctx).Errorf("[projectPay service] call SpecialSettlePay error,err:%+v", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+ return err
|
|
|
+}
|