package controller import ( . "app.yhyue.com/moapp/jybase/api" "app.yhyue.com/moapp/jybase/common" "context" "fmt" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/util/gconv" uuid2 "github.com/google/uuid" "github.com/pkg/errors" "jyOrderManager/internal/consts" "jyOrderManager/internal/logic/order" "jyOrderManager/internal/model" "log" "regexp" "strings" "time" ) var depositPayUrlMap map[string]interface{} func init() { depositPayUrlMap = g.Cfg().MustGet(context.Background(), "depositPayUrl").Map() if len(depositPayUrlMap) == 0 { log.Fatal("配置项有误:depositPayUrl,请检查配置文件后再启动") } } func GetReturnOnlineHandler(r *ghttp.Request) { rData, err := func() (interface{}, error) { var param model.OrderReturnOnlineParams err := gconv.Struct(r.GetBody(), ¶m) if err != nil { return nil, errors.Wrap(err, "数据校验异常") } if param.PayType == consts.ReturnParamsPayTypeDeposit { return depositHandler(r, param) // 如果是保证金的 } var ( doType = param.DoType orderCode = param.OrderCode money = gconv.Int(param.Money) payWay = param.PayWay ) orderRes, err := g.DB().GetOne(r.Context(), "SELECT buy_subject,user_phone,company_name,product_type,pay_money+(select IFNULL(sum(payMoney),0) as return_money from moneyCorrection where orderCode=?) as pay_money FROM dataexport_order WHERE order_code=?", orderCode, orderCode) if err != nil { return nil, gerror.Wrapf(err, "查询订单数据异常%s", orderCode) } calculationRes, err := g.DB().Query(r.Context(), "SELECT SUM(return_money) as returned_money FROM return_money_record WHERE order_code=? AND state=1", orderCode) if err != nil { return nil, gerror.Wrapf(err, "计算已回款出错%s", orderCode) } var ( returnedMoney = gconv.Int(calculationRes.List()[0]["returned_money"]) orderMap = orderRes.Map() payMoney = gconv.Int(orderMap["pay_money"]) ) switch doType { case "query": if gconv.Int(orderRes.Map()["pay_money"])-returnedMoney <= 0 { return nil, fmt.Errorf("待支付金额异常") } account, product := formatShow(orderMap) return map[string]interface{}{ "product": product, "account": account, "money": payMoney - returnedMoney, }, nil case "getPay": if payMoney-returnedMoney-money < 0 { return nil, fmt.Errorf("待支付金额异常") } var ( now = time.Now() exDay = now.AddDate(0, 0, 3) targetTime = time.Date(exDay.Year(), exDay.Month(), exDay.Day(), 23, 59, 59, 0, time.Local) token = uuid2.New().String() payPath string ) switch payWay { case 1: payPath = "/weixin/pay/returnMoney" case 2: payPath = "/returnMoney/aliPage" default: return nil, fmt.Errorf("方式异常") } if _, err := g.DB().Insert(r.Context(), "return_money_online", map[string]interface{}{ "status": 0, "token": token, "order_code": orderCode, "return_money": money, "expire_time": targetTime.Format("2006-01-02 15:04:05"), "creat_time": now.Format("2006-01-02 15:04:05"), }); err != nil { return nil, gerror.Wrapf(err, "保存回款数据异常") } return map[string]interface{}{ "product": fmt.Sprintf("%s%s?token=%s", g.Cfg("global").MustGet(r.Context(), "webDomain.jy").String(), payPath, token), "expire": targetTime.Unix(), }, nil } return nil, fmt.Errorf("未知操作") }() if err != nil { g.Log().Errorf(r.Context(), "获取回款码异常 %v", err) } r.Response.WriteJson(NewResult(rData, err)) } func depositHandler(r *ghttp.Request, param model.OrderReturnOnlineParams) (interface{}, error) { depositMoney, err := order.GetDepositMoney(r.Context(), param.OrderCode) if err != nil { return nil, gerror.Wrapf(err, "查询保证金异常%s", param.OrderCode) } if depositMoney == 0 { return nil, fmt.Errorf("未查询到保证金信息%s", param.OrderCode) } payMoney, err := order.GetDepositPayMoney(r.Context(), param.OrderCode) if err != nil { return nil, err } if payMoney > 0 { return nil, fmt.Errorf("该笔订单已收到保证金") } var ( doType = param.DoType orderCode = param.OrderCode money = gconv.Int(param.Money) payWay = param.PayWay ) orderRes, err := g.DB().GetOne(r.Context(), "SELECT buy_subject,user_phone,company_name,product_type,pay_money+(select IFNULL(sum(payMoney),0) as return_money from moneyCorrection where orderCode=?) as pay_money FROM dataexport_order WHERE order_code=?", orderCode, orderCode) if err != nil { return nil, gerror.Wrapf(err, "查询订单数据异常%s", orderCode) } var ( orderMap = orderRes.Map() ) switch doType { case "query": account, product := formatShow(orderMap) return map[string]interface{}{ "product": product, "account": account, "money": depositMoney, }, nil case "getPay": var ( now = time.Now() exDay = now.AddDate(0, 0, 3) targetTime = time.Date(exDay.Year(), exDay.Month(), exDay.Day(), 23, 59, 59, 0, time.Local) token = uuid2.New().String() payPath string ) // 配置 switch payWay { case 1: payPath = common.ObjToString(depositPayUrlMap["wx"]) case 2: payPath = common.ObjToString(depositPayUrlMap["ali"]) default: return nil, fmt.Errorf("方式异常") } if _, err := g.DB().Insert(r.Context(), consts.OrderDepositOnlineTableName, map[string]interface{}{ "status": 0, "token": token, "order_code": orderCode, "return_money": money, "expire_time": targetTime.Format("2006-01-02 15:04:05"), "creat_time": now.Format("2006-01-02 15:04:05"), }); err != nil { return nil, gerror.Wrapf(err, "保存回款数据异常") } return map[string]interface{}{ "product": fmt.Sprintf("%s%s?token=%s", g.Cfg("global").MustGet(r.Context(), "webDomain.jy").String(), payPath, token), "expire": targetTime.Unix(), }, nil } return nil, fmt.Errorf("未知操作") } func formatShow(orderMap map[string]interface{}) (string, string) { var account string if company_name := gconv.String(orderMap["company_name"]); company_name != "" { account = company_name } else if phone := gconv.String(orderMap["user_phone"]); len(phone) == 11 && !strings.HasPrefix(phone, "9") { account = maskPhoneNumber(phone) } product := gconv.String(orderMap["product_type"]) if product == "VIP订阅" { product = "超级订阅" } return account, product } func maskPhoneNumber(phoneNumber string) string { re := regexp.MustCompile(`(\d{3})\d{4}(\d{4})`) maskedNumber := re.ReplaceAllString(phoneNumber, "$1****$2") return maskedNumber }