animeic 2 years ago
parent
commit
ede4347424
4 changed files with 23 additions and 15 deletions
  1. 7 6
      boxcost/api/plan.go
  2. 7 1
      boxcost/api/sms.go
  3. 8 6
      boxcost/api/supplier.go
  4. 1 2
      boxcost/api/utils.go

+ 7 - 6
boxcost/api/plan.go

@@ -91,6 +91,7 @@ func PlanAllocBatch(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		return nil, errors.New("未找到单据信息")
 	}
 
+	var wg sync.WaitGroup
 	for _, tId := range typeBillIds {
 		var err error
 		billType := ""
@@ -112,17 +113,17 @@ func PlanAllocBatch(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 			_, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, billId.Hex(), &model.ProductBill{IsSend: true, SendTime: time.Now()})
 		}
 
-		if err != nil {
+		if err == nil {
 			// 给供应商发送通知短信
 			smsInfo, err := genSupplierSmsTemp(billId, billType, apictx)
-			fmt.Println(err)
-			if err != nil {
-				err = SendSmsNotify(smsInfo.Phone, &SupplierSmsReq{smsInfo.Product, smsInfo.SerialNumber})
-				fmt.Println(err)
-				log.Error(err)
+			fmt.Println(smsInfo)
+			if err == nil {
+				wg.Add(1)
+				go SendSmsNotify(smsInfo.Phone, &SupplierSmsReq{smsInfo.Product, smsInfo.SerialNumber}, &wg)
 			}
 		}
 	}
+	wg.Wait()
 
 	return true, nil
 

+ 7 - 1
boxcost/api/sms.go

@@ -1,9 +1,11 @@
 package api
 
 import (
+	"box-cost/log"
 	"encoding/json"
 	"errors"
 	"fmt"
+	"sync"
 
 	openapi "github.com/alibabacloud-go/darabonba-openapi/client"
 	dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
@@ -28,11 +30,13 @@ type SupplierSmsReq struct {
 	SerialNumber string
 }
 
-func SendSmsNotify(phone string, info *SupplierSmsReq) error {
+func SendSmsNotify(phone string, info *SupplierSmsReq, wg *sync.WaitGroup) error {
+	defer wg.Done()
 	// TODO dev
 	return errors.New("待配置短信模板")
 	client, _err := createSmsClient()
 	if _err != nil {
+		log.Error(_err)
 		return _err
 	}
 
@@ -47,10 +51,12 @@ func SendSmsNotify(phone string, info *SupplierSmsReq) error {
 
 	resp, err := client.SendSms(sendSmsRequest)
 	if err != nil {
+		log.Error(err)
 		return err
 	}
 	if *resp.Body.Code == "OK" {
 		return nil
 	}
+
 	return fmt.Errorf("code err %s", *resp.Body.Code)
 }

+ 8 - 6
boxcost/api/supplier.go

@@ -6,6 +6,7 @@ import (
 	"box-cost/log"
 	"errors"
 	"fmt"
+	"sync"
 	"time"
 
 	"github.com/gin-gonic/gin"
@@ -168,14 +169,15 @@ func SupplierBillAlloc(c *gin.Context, apictx *ApiSession) (interface{}, error)
 	default:
 		return result, nil
 	}
-	if err != nil {
+	if err == nil {
 		// 给供应商发送通知短信
 		smsInfo, err := genSupplierSmsTemp(billId, billType, apictx)
-		fmt.Println(err)
-		if err != nil {
-			err = SendSmsNotify(smsInfo.Phone, &SupplierSmsReq{smsInfo.Product, smsInfo.SerialNumber})
-			fmt.Println(err)
-			log.Error(err)
+		fmt.Println(smsInfo)
+		if err == nil {
+			var wg sync.WaitGroup
+			wg.Add(1)
+			go SendSmsNotify(smsInfo.Phone, &SupplierSmsReq{smsInfo.Product, smsInfo.SerialNumber}, &wg)
+			wg.Wait()
 		}
 	}
 	return result, err

+ 1 - 2
boxcost/api/utils.go

@@ -148,13 +148,12 @@ func isManager(roles []string) bool {
 	return false
 }
 func getUserById(apictx *ApiSession, id primitive.ObjectID) (*model.UserSmaple, error) {
-	fmt.Println(id.Hex())
 	user := &model.UserSmaple{}
 	_, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
 		Db:          "box-user",
 		CollectName: repo.CollectionUsers,
 		Query:       repo.Map{"_id": id},
-		Project:     []string{"name", "avatar", "city", "loginName", "roles"},
+		Project:     []string{"name", "avatar", "city", "loginName", "roles", "phone"},
 	}, user)
 	return user, err
 }