Browse Source

fix:线上回款-合并至回款表

duxin 2 months ago
parent
commit
b47a25c0a0

+ 1 - 0
config.yaml

@@ -86,6 +86,7 @@ entAnalysis: 19
 jyResources: "http://172.31.31.205:8889"
 jyPayRpc: "192.168.3.149:8600"
 autoReturnTaskTime: "0 */3 * * * ?"
+onlineReturnMoney: "0 */2 * * * ?"
 
 examine:
   - name: 部门经理

+ 1 - 2
internal/jyutil/order.go

@@ -92,8 +92,7 @@ func IsServiceOpen(m map[string]interface{}) bool {
 	if err != nil {
 		return false
 	}
-	if gconv.Int(m["is_service_open"]) == 1 ||
-		mc.Auto == 0 {
+	if mc.Auto == 0 {
 		return false
 	}
 	return true

+ 1 - 1
internal/logic/order/editBatchReturn.go

@@ -178,7 +178,7 @@ func EditBatchReturn(ctx context.Context, param model.OrdersEditBatchReturn) err
 				if !consts.PhoneRegex.MatchString(gconv.String(value["user_phone"])) {
 					return errors.New("手机号格式异常")
 				}
-				productDetail, err := g.DB().Ctx(ctx).Query(ctx, fmt.Sprintf(`SELECT * FROM jy_order_detail WHERE order_code ='%s' and returned_open =1`, orderCode))
+				productDetail, err := g.DB().Ctx(ctx).Query(ctx, fmt.Sprintf(`SELECT * FROM jy_order_detail WHERE order_code ='%s' and returned_open =1 and is_service_open = 0`, orderCode))
 				if err != nil || productDetail.IsEmpty() {
 					return err
 				}

+ 1 - 1
internal/logic/order/redPunch.go

@@ -146,7 +146,7 @@ func OperateOrderRedPunch(ctx context.Context, params model.OrderRedPunchParams)
 			if !consts.PhoneRegex.MatchString(gconv.String(orderData["user_phone"])) {
 				return errors.New("手机号格式异常")
 			}
-			productDetail, _ := g.DB().Ctx(ctx).Query(ctx, fmt.Sprintf(`SELECT * FROM jy_order_detail WHERE order_code ='%s' and returned_open =1`, params.OrderCode))
+			productDetail, _ := g.DB().Ctx(ctx).Query(ctx, fmt.Sprintf(`SELECT * FROM jy_order_detail WHERE order_code ='%s' and returned_open =1 and is_service_open = 0`, params.OrderCode))
 			if !productDetail.IsEmpty() {
 				if err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
 					// 产品服务开通

+ 1 - 1
internal/logic/timedTask/automaticPayment.go

@@ -146,7 +146,7 @@ WHERE
 									if !consts.PhoneRegex.MatchString(gconv.String(o["user_phone"])) {
 										continue
 									}
-									productDetail, err := g.DB().Ctx(ctx).Query(ctx, fmt.Sprintf(`SELECT * FROM jy_order_detail WHERE order_code ='%s' and returned_open =1`, orderCode))
+									productDetail, err := g.DB().Ctx(ctx).Query(ctx, fmt.Sprintf(`SELECT * FROM jy_order_detail WHERE order_code ='%s' and returned_open =1 and is_service_open = 0`, orderCode))
 									if err != nil || productDetail.IsEmpty() {
 										continue
 									}

+ 3 - 1
internal/logic/timedTask/init.go

@@ -12,7 +12,9 @@ func init() {
 
 func AutoReturnTask() {
 	cr := cron.New()
-	AutomaticPayment() //首次执行
+	AutomaticPayment()  //首次执行
+	OnlineReturnMoney() //首次执行
 	cr.AddFunc(g.Cfg().MustGet(context.Background(), "autoReturnTaskTime", "0 */3 * * * ?").String(), AutomaticPayment)
+	cr.AddFunc(g.Cfg().MustGet(context.Background(), "onlineReturnMoney", "0 */2 * * * ?").String(), OnlineReturnMoney)
 	cr.Start()
 }

+ 147 - 0
internal/logic/timedTask/onlineReturnMoney.go

@@ -0,0 +1,147 @@
+package timedTask
+
+import (
+	"context"
+	"fmt"
+	"github.com/gogf/gf/v2/database/gdb"
+	"github.com/gogf/gf/v2/errors/gerror"
+	"github.com/gogf/gf/v2/frame/g"
+	"github.com/gogf/gf/v2/util/gconv"
+	"jyOrderManager/internal/jyutil"
+	"jyOrderManager/internal/logic/order"
+	"jyOrderManager/internal/logic/product"
+	"log"
+	"math"
+	"strings"
+	"time"
+)
+
+// 线上回款-合并至回款表
+func OnlineReturnMoney() {
+	var ctx = context.Background()
+	data, _ := g.DB().Query(ctx, "SELECT r.id,r.order_code,r.return_money,p.pay_way,p.transaction_id,p.pay_time FROM return_money_online r inner join return_money_online_pay  p on (r.id=p.return_id)  WHERE r.status=1 and p.status =1 ")
+	if !data.IsEmpty() {
+		for _, m := range data.List() {
+			var (
+				rowId         = gconv.String(m["id"])
+				orderCode     = gconv.String(m["order_code"])
+				returnMoney   = gconv.Int(m["return_money"])
+				payTime       = gconv.String(m["pay_time"])
+				transactionId = gconv.String(m["transaction_id"])
+				payWay        = gconv.String(m["pay_way"])
+			)
+
+			orderData, _ := g.DB().GetOne(ctx, "dataexport_order", map[string]interface{}{
+				"order_code": orderCode,
+			}, "", "")
+			if orderData.IsEmpty() {
+				continue
+			}
+			var (
+				money        = gconv.Int(orderData.Map()["pay_money"])
+				order_status = gconv.Int(orderData.Map()["order_status"])
+			)
+
+			//计算已回款金额
+			var returned_money int
+			if calculation, _ := g.DB().GetOne(ctx, "SELECT SUM(return_money) as returned_money  FROM return_money_record WHERE order_code=? AND state=1", orderCode); !calculation.IsEmpty() {
+				returned_money = gconv.Int(calculation.Map()["returned_money"])
+			}
+
+			if err := g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
+				if returned_money == 0 { //首次回款业绩生效
+					if err := order.CommonChange(ctx, orderCode, payTime, order.ReturnMoney); err != nil {
+						log.Printf("%s 回款销售业绩生效异常 %v", orderCode, err)
+					}
+				}
+				orderUpdate := map[string]interface{}{}
+				if order_status != 1 {
+					orderUpdate["order_status"] = 1
+				}
+				if returnMoney+returned_money == money { //全额回款
+					orderUpdate["return_status"] = 1
+				} else {
+					orderUpdate["return_status"] = 2
+				}
+
+				if _, err := g.DB().Update(ctx, "dataexport_order", orderUpdate, map[string]interface{}{"order_code": orderCode}); err != nil {
+					return err
+				}
+
+				var (
+					procedures  float64
+					return_type int
+				)
+				if strings.Contains(payWay, "wx") {
+					return_type = 1
+					procedures = math.Round(gconv.Float64(returnMoney) * order.WxProceduresMoney / 100)
+				} else {
+					return_type = 2
+					procedures = math.Round(gconv.Float64(returnMoney) * order.AliProceduresMoney / 100)
+				}
+
+				if _, err := g.DB().Update(ctx, "return_money_online", map[string]interface{}{
+					"status": 2,
+				}, map[string]interface{}{
+					"id": rowId,
+				}); err != nil {
+					return err
+				}
+
+				if _, err := g.DB().Insert(ctx, "return_money_record", map[string]interface{}{
+					"return_time":      payTime,
+					"return_money":     returnMoney,
+					"procedures_money": procedures,  //手续费
+					"return_type":      return_type, //'回款方式,1-微信 2-支付宝 3-对公转账',
+					"return_code":      transactionId,
+					"order_code":       orderCode,
+					"operate_person":   "系统自动",
+					"operate_time":     time.Now().Format("2006-01-02 15:04:05"),
+					"operate_type":     3,
+					"flow_money":       returnMoney,
+					"state":            1,
+				}); err != nil {
+					return err
+				}
+
+				if orderUpdate["return_status"] == 1 {
+					productDetail, _ := g.DB().Ctx(ctx).Query(ctx, fmt.Sprintf(`SELECT * FROM jy_order_detail WHERE order_code ='%s' and returned_open =1 and returned_open = 0 and is_service_open = 1`, orderCode))
+					if !productDetail.IsEmpty() {
+						//全额回款开通权益
+						for _, o := range productDetail.List() {
+							if !jyutil.IsServiceOpen(m) {
+								continue
+							}
+							//参数注入
+							m["phone"] = o["user_phone"]
+							m["order_code"] = orderCode
+							m["amount"] = m["final_price"]
+							m["reqCompanyName"] = o["company_name"]
+							m["reqSubject"] = o["buy_subject"]
+							productCode := gconv.String(m["product_code"])
+							pFunc, err := product.JyProFunc.GetProductInitFuncByCode(productCode)
+							if err != nil {
+								continue
+							}
+							pObj, err := pFunc(m)
+							if err != nil {
+								gerror.Wrap(err, fmt.Sprintf("获取%s商品异常", productCode))
+								continue
+							}
+							if err = pObj.OpenService(ctx, time.Now()); err != nil {
+								log.Println("自动回款 OpenService err ", err)
+								continue
+							}
+						}
+					}
+				}
+				return nil
+			}); err != nil {
+				log.Println("回款更新失败", orderCode)
+			}
+
+		}
+	}
+	//关闭过期订单
+	g.DB().Update(ctx, "return_money_online", map[string]interface{}{"state": 1}, " state=0 AND expire_time<=? ", time.Now().Format(time.DateTime))
+}