Procházet zdrojové kódy

支付结果更新

Ethan před 5 měsíci
rodič
revize
6632e90e4b

+ 1 - 1
app/dao/project_dao.go

@@ -69,7 +69,7 @@ func (d ProjectDAO) UpdateProject(project entity.Project) error {
 
 // 更新开票状态字段
 func (d ProjectDAO) UpdateInvoiceStatus(projectIDs []string) error {
-	err := Db.Model(&entity.Project{}).Where("project_id IN ?", projectIDs).Updates(entity.Project{InvoiceStatus: 1}).Error
+	err := Db.Debug().Model(&entity.Project{}).Where("project_id IN ?", projectIDs).Updates(entity.Project{InvoiceStatus: 1}).Error
 	if err != nil {
 		return err
 	}

+ 27 - 7
app/dao/recharge_record_dao.go

@@ -51,7 +51,7 @@ func (d RechargeRecordDao) RechargeInfoList(param *vo.RechargeParam) ([]entity.R
 }
 
 // 更新充值状态
-func (d RechargeRecordDao) UpdateRechargeStatus(enterpriseId string, subAccountId int64, status int64, t time.Time) error {
+func (d RechargeRecordDao) UpdateRechargeStatus(rechargeId string, status int64, t time.Time) error {
 	rechargeRecord := entity.RechargeRecord{
 		Status: status,
 	}
@@ -60,23 +60,43 @@ func (d RechargeRecordDao) UpdateRechargeStatus(enterpriseId string, subAccountI
 		rechargeRecord.ConfirmAt = t
 	}
 	if status == 3 {
-		rechargeRecord.FailReason = "支付失败"
+		rechargeRecord.FailReason = "微信支付失败"
 		rechargeRecord.RefuseAt = t
 	}
-	err = Db.Debug().Model(&entity.RechargeRecord{}).Where("enterprise_id = ? AND sub_account_id = ?", enterpriseId, subAccountId).Updates(rechargeRecord).Error
+	err = Db.Debug().Model(&entity.RechargeRecord{}).Where("recharge_id = ?", rechargeId).Updates(rechargeRecord).Error
 	if err != nil {
 		return err
 	}
 	return nil
 }
 
-// 更新所有充值记录中微信支付超时的记录
-func (d RechargeRecordDao) UpdateRechargeFailedList() error {
+// 查找微信充值中超时且状态为充值未确认的记录
+func (d RechargeRecordDao) GetWXRechargeByStatusList(status int64) ([]*entity.RechargeRecord, error) {
 	now := time.Now()
-	err := Db.Debug().Model(&entity.RechargeRecord{}).Where("recharge_method = ? AND status = ? AND refuse_at < ?", 2, 1, now).
-		Updates(map[string]interface{}{"status": 3, "fail_reason": "微信支付失败"}).Error
+	var rechargeRecords []*entity.RechargeRecord
+	err := Db.Debug().Model(&entity.RechargeRecord{}).Where("recharge_method = ? AND status = ? AND refuse_at < ?", 2, status, now).Find(&rechargeRecords).Error
+	if err != nil {
+		return nil, err
+	}
+	return rechargeRecords, nil
+}
+
+// 批量更新指定id的充值记录状态
+func (d RechargeRecordDao) UpdateRechargeFailedList(noPayIds []int64) error {
+	err := Db.Debug().Model(&entity.RechargeRecord{}).Where("id IN ?", noPayIds).Updates(entity.RechargeRecord{Status: 3, FailReason: "微信支付失败"}).Error
 	if err != nil {
 		return err
 	}
 	return nil
 }
+
+//// 更新所有充值记录中微信支付超时的记录
+//func (d RechargeRecordDao) UpdateRechargeFailedList() error {
+//	now := time.Now()
+//	err := Db.Debug().Model(&entity.RechargeRecord{}).Where("recharge_method = ? AND status = ? AND refuse_at < ?", 2, 1, now).
+//		Updates(map[string]interface{}{"status": 3, "fail_reason": "微信支付失败"}).Error
+//	if err != nil {
+//		return err
+//	}
+//	return nil
+//}

+ 23 - 1
app/schedule/auto_task2.go

@@ -2,9 +2,11 @@ package schedule
 
 import (
 	"github.com/robfig/cron/v3"
+	"github.com/sirupsen/logrus"
 	"log"
 	"time"
 	"youngee_b_api/app/dao"
+	"youngee_b_api/app/service"
 )
 
 func AutoTask2() error {
@@ -28,6 +30,26 @@ func AutoTask2() error {
 // 定时任务  检查微信支付是否充值成功
 func AutoCheckWXRechargeTask() {
 	log.Println("AutoCheckWXRechargeTask running Start, Time :", time.Now())
-	_ = dao.RechargeRecordDao{}.UpdateRechargeFailedList()
+	rechargeRecords, err := dao.RechargeRecordDao{}.GetWXRechargeByStatusList(1)
+	if err != nil {
+		log.Println("GetWXRechargeByStatusList", err)
+	}
+	var rechargeId string
+	var noPayIds []int64
+	for _, rechargeRecord := range rechargeRecords {
+		rechargeId = rechargeRecord.RechargeID
+		tradeState, err := service.RechargeService{}.QueryOrderByTradeId(rechargeRecord.EnterpriseID, rechargeRecord.SubAccountId, rechargeId)
+		if err != nil {
+			logrus.Errorf("AutoCheckWXRechargeTask [QueryOrderByTradeId] call Show err:%+v\n", err)
+		}
+		// 支付成功的状态会在方法里更新,未支付的需要单独更新
+		if "NOTPAY" == tradeState {
+			noPayIds = append(noPayIds, rechargeRecord.ID)
+		}
+	}
+	err2 := dao.RechargeRecordDao{}.UpdateRechargeFailedList(noPayIds)
+	if err2 != nil {
+		log.Println("UpdateRechargeFailedList", err2)
+	}
 	log.Println("AutoCheckWXRechargeTask running End, Time :", time.Now())
 }

+ 2 - 2
app/service/recharge_service.go

@@ -172,11 +172,11 @@ func (s RechargeService) QueryOrderByTradeId(enterpriseId string, subAccountId i
 	if "SUCCESS" == *resp.TradeState {
 		payTime := resp.SuccessTime
 		t, _ := time.Parse("2006-01-02 15:04:05", *payTime)
-		err = dao.RechargeRecordDao{}.UpdateRechargeStatus(enterpriseId, subAccountId, 2, t)
+		err = dao.RechargeRecordDao{}.UpdateRechargeStatus(tradeId, 2, t)
 		amount := float64(*resp.Amount.Total) / 100
 		_, err = dao.EnterpriseDao{}.UpdateEnterpriseBalance(enterpriseId, amount)
 	} else if "CLOSED" == *resp.TradeState {
-		err = dao.RechargeRecordDao{}.UpdateRechargeStatus(enterpriseId, subAccountId, 3, time.Now())
+		err = dao.RechargeRecordDao{}.UpdateRechargeStatus(tradeId, 3, time.Now())
 	}
 
 	return *resp.TradeState, nil

+ 0 - 5
db/auto_task.go

@@ -194,11 +194,6 @@ func GetAutoDraftDefaultTask() error {
 
 	// 3. 对初次初稿未上传的操作
 	for _, taskNeedModId := range taskNeedModIds {
-		if taskNeedModId != "3601319242" {
-			break
-		}
-		fmt.Println("Task right")
-
 		// 获取taskId对应的autoTaskId
 		autoTaskId := projectIdToAutoTaskIdMap[TaskIdToProjectId[taskNeedModId]]