|
@@ -15,6 +15,7 @@ import (
|
|
"jyOrderManager/internal/model"
|
|
"jyOrderManager/internal/model"
|
|
"log"
|
|
"log"
|
|
"math"
|
|
"math"
|
|
|
|
+ "strings"
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -85,6 +86,7 @@ func SaveDepositPayment(ctx context.Context, sDepositPayment model.SDepositPayme
|
|
var (
|
|
var (
|
|
price, realPrice int64
|
|
price, realPrice int64
|
|
orderArr = make(map[string]map[string]interface{})
|
|
orderArr = make(map[string]map[string]interface{})
|
|
|
|
+ depositId []string
|
|
)
|
|
)
|
|
for _, dl := range sDepositPayment.DepositList {
|
|
for _, dl := range sDepositPayment.DepositList {
|
|
if dl.OrderCode == "" || dl.Money == 0 {
|
|
if dl.OrderCode == "" || dl.Money == 0 {
|
|
@@ -128,7 +130,7 @@ func SaveDepositPayment(ctx context.Context, sDepositPayment model.SDepositPayme
|
|
payMoney = gconv.Int64(value["realPrice"])
|
|
payMoney = gconv.Int64(value["realPrice"])
|
|
)
|
|
)
|
|
err := g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
|
err := g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
|
- _, err := g.DB().Insert(ctx, model.DepositPaymentTable, g.Map{ //保证金支付信息
|
|
|
|
|
|
+ res, err := g.DB().Insert(ctx, model.DepositPaymentTable, g.Map{ //保证金支付信息
|
|
"order_code": key,
|
|
"order_code": key,
|
|
"pay_time": sDepositPayment.PayTime,
|
|
"pay_time": sDepositPayment.PayTime,
|
|
"pay_money": YuanToFen(payMoney),
|
|
"pay_money": YuanToFen(payMoney),
|
|
@@ -140,6 +142,8 @@ func SaveDepositPayment(ctx context.Context, sDepositPayment model.SDepositPayme
|
|
"flow_money": YuanToFen(sDepositPayment.FlowMoney),
|
|
"flow_money": YuanToFen(sDepositPayment.FlowMoney),
|
|
"bank_name": sDepositPayment.BankName,
|
|
"bank_name": sDepositPayment.BankName,
|
|
"bank_flow": sDepositPayment.BankFlow,
|
|
"bank_flow": sDepositPayment.BankFlow,
|
|
|
|
+ "pay_code": sDepositPayment.PayCode,
|
|
|
|
+ "transaction_id": sDepositPayment.TransactionId,
|
|
"pay_voucher": sDepositPayment.PayVoucher,
|
|
"pay_voucher": sDepositPayment.PayVoucher,
|
|
"pay_account_name": sDepositPayment.PayAccountName,
|
|
"pay_account_name": sDepositPayment.PayAccountName,
|
|
"create_time": time.Now().Format(time.DateTime),
|
|
"create_time": time.Now().Format(time.DateTime),
|
|
@@ -147,70 +151,77 @@ func SaveDepositPayment(ctx context.Context, sDepositPayment model.SDepositPayme
|
|
if err != nil {
|
|
if err != nil {
|
|
g.Log().Errorf(ctx, "新增保证金支付信息异常 %s", err.Error())
|
|
g.Log().Errorf(ctx, "新增保证金支付信息异常 %s", err.Error())
|
|
return errors.Wrap(err, "新增保证金支付信息异常")
|
|
return errors.Wrap(err, "新增保证金支付信息异常")
|
|
- } else if realPrice == price { //保证金总金额 等于剩余需要回款的总金额
|
|
|
|
- //todo 1、是否需要关闭对应保证金的支付订单;2、保证金支付金额=合同金额 需要开通权限
|
|
|
|
- //是否需要开通权限 当前订单 保证金金额 == 剩余回款金额
|
|
|
|
- if payMoney == gconv.Int64(value["outstandingPayment"]) {
|
|
|
|
- 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 and status =1`, key)) //是否选中自动开通权限
|
|
|
|
- if !productDetail.IsEmpty() {
|
|
|
|
- var (
|
|
|
|
- uData map[string]interface{}
|
|
|
|
- entId, userPositionId int64
|
|
|
|
- )
|
|
|
|
- uData, entId, userPositionId, err = jyutil.GetCreateUserData(gconv.String(value["user_phone"]), gconv.String(value["company_name"]), gconv.Int(value["buy_subject"]) == 2)
|
|
|
|
- if err != nil {
|
|
|
|
- return errors.New("用户企业信息初始化失败")
|
|
|
|
- }
|
|
|
|
- if err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
|
|
|
- // 产品服务开通
|
|
|
|
- for _, m := range productDetail.List() {
|
|
|
|
- if !jyutil.IsServiceOpen(m) {
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- //参数注入
|
|
|
|
- m["userMap"] = map[string]interface{}{
|
|
|
|
- "userData": uData, "entId": entId, "userPositionId": userPositionId,
|
|
|
|
- }
|
|
|
|
- m["phone"] = value["user_phone"]
|
|
|
|
- m["order_code"] = key
|
|
|
|
- m["reqCompanyName"] = value["company_name"]
|
|
|
|
- m["amount"] = m["final_price"]
|
|
|
|
- m["reqSubject"] = value["buy_subject"]
|
|
|
|
- m["linked_orderId"] = m["linked_detail_id"]
|
|
|
|
- productCode := gconv.String(m["product_code"])
|
|
|
|
- pFunc, err4 := product.JyProFunc.GetProductInitFuncByCode(productCode)
|
|
|
|
- if err4 != nil {
|
|
|
|
- return err4
|
|
|
|
- }
|
|
|
|
- pObj, err5 := pFunc(m)
|
|
|
|
- if err5 != nil {
|
|
|
|
- return gerror.Wrap(err5, fmt.Sprintf("获取%s商品异常", productCode))
|
|
|
|
- }
|
|
|
|
- if err = pObj.OpenService(ctx, time.Now()); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if gconv.Int(value["buy_subject"]) == 2 {
|
|
|
|
- uData["userId"] = userPositionId
|
|
|
|
|
|
+ } else {
|
|
|
|
+ var insertId int64
|
|
|
|
+ insertId, err = res.LastInsertId()
|
|
|
|
+ if err == nil && insertId > 0 {
|
|
|
|
+ depositId = append(depositId, fmt.Sprint(insertId))
|
|
|
|
+ }
|
|
|
|
+ if realPrice == price { //保证金总金额 等于剩余需要回款的总金额
|
|
|
|
+ //todo 1、是否需要关闭对应保证金的支付订单;2、保证金支付金额=合同金额 需要开通权限
|
|
|
|
+ //是否需要开通权限 当前订单 保证金金额 == 剩余回款金额
|
|
|
|
+ if payMoney == gconv.Int64(value["outstandingPayment"]) {
|
|
|
|
+ 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 and status =1`, key)) //是否选中自动开通权限
|
|
|
|
+ if !productDetail.IsEmpty() {
|
|
|
|
+ var (
|
|
|
|
+ uData map[string]interface{}
|
|
|
|
+ entId, userPositionId int64
|
|
|
|
+ )
|
|
|
|
+ uData, entId, userPositionId, err = jyutil.GetCreateUserData(gconv.String(value["user_phone"]), gconv.String(value["company_name"]), gconv.Int(value["buy_subject"]) == 2)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return errors.New("用户企业信息初始化失败")
|
|
}
|
|
}
|
|
- if orderUserId := gconv.String(value["user_id"]); orderUserId == "" || orderUserId != gconv.String(uData["userId"]) || (gconv.Int(value["buy_subject"]) == 2 && gconv.Int64(value["ent_id"]) != entId) {
|
|
|
|
- log.Printf("同步更新订单用户身份:orderUserId:%s,userId:%v,entId:%d\n", orderUserId, uData["userId"], entId)
|
|
|
|
- upData := g.Map{
|
|
|
|
- "user_id": uData["userId"],
|
|
|
|
|
|
+ if err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
|
|
|
+ // 产品服务开通
|
|
|
|
+ for _, m := range productDetail.List() {
|
|
|
|
+ if !jyutil.IsServiceOpen(m) {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ //参数注入
|
|
|
|
+ m["userMap"] = map[string]interface{}{
|
|
|
|
+ "userData": uData, "entId": entId, "userPositionId": userPositionId,
|
|
|
|
+ }
|
|
|
|
+ m["phone"] = value["user_phone"]
|
|
|
|
+ m["order_code"] = key
|
|
|
|
+ m["reqCompanyName"] = value["company_name"]
|
|
|
|
+ m["amount"] = m["final_price"]
|
|
|
|
+ m["reqSubject"] = value["buy_subject"]
|
|
|
|
+ m["linked_orderId"] = m["linked_detail_id"]
|
|
|
|
+ productCode := gconv.String(m["product_code"])
|
|
|
|
+ pFunc, err4 := product.JyProFunc.GetProductInitFuncByCode(productCode)
|
|
|
|
+ if err4 != nil {
|
|
|
|
+ return err4
|
|
|
|
+ }
|
|
|
|
+ pObj, err5 := pFunc(m)
|
|
|
|
+ if err5 != nil {
|
|
|
|
+ return gerror.Wrap(err5, fmt.Sprintf("获取%s商品异常", productCode))
|
|
|
|
+ }
|
|
|
|
+ if err = pObj.OpenService(ctx, time.Now()); err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if gconv.Int(value["buy_subject"]) == 2 {
|
|
|
|
+ uData["userId"] = userPositionId
|
|
}
|
|
}
|
|
- if entId > 0 { //企业服务
|
|
|
|
- upData["ent_id"] = entId
|
|
|
|
- if personPhone := gconv.String(value["personPhone"]); personPhone != "" {
|
|
|
|
- jyutil.EndAddUser(ctx, entId, gconv.String(value["user_phone"]), personPhone, gconv.String(value["personName"]))
|
|
|
|
|
|
+ if orderUserId := gconv.String(value["user_id"]); orderUserId == "" || orderUserId != gconv.String(uData["userId"]) || (gconv.Int(value["buy_subject"]) == 2 && gconv.Int64(value["ent_id"]) != entId) {
|
|
|
|
+ log.Printf("同步更新订单用户身份:orderUserId:%s,userId:%v,entId:%d\n", orderUserId, uData["userId"], entId)
|
|
|
|
+ upData := g.Map{
|
|
|
|
+ "user_id": uData["userId"],
|
|
}
|
|
}
|
|
|
|
+ if entId > 0 { //企业服务
|
|
|
|
+ upData["ent_id"] = entId
|
|
|
|
+ if personPhone := gconv.String(value["personPhone"]); personPhone != "" {
|
|
|
|
+ jyutil.EndAddUser(ctx, entId, gconv.String(value["user_phone"]), personPhone, gconv.String(value["personName"]))
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //更新订单
|
|
|
|
+ _, err = g.DB().Update(ctx, consts.OrderListTableName, upData, "order_code=?", key)
|
|
}
|
|
}
|
|
- //更新订单
|
|
|
|
- _, err = g.DB().Update(ctx, consts.OrderListTableName, upData, "order_code=?", key)
|
|
|
|
|
|
+ return err
|
|
|
|
+ }); err != nil {
|
|
|
|
+ log.Println(err)
|
|
|
|
+ return err
|
|
}
|
|
}
|
|
- return err
|
|
|
|
- }); err != nil {
|
|
|
|
- log.Println(err)
|
|
|
|
- return err
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -219,20 +230,34 @@ func SaveDepositPayment(ctx context.Context, sDepositPayment model.SDepositPayme
|
|
})
|
|
})
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
+ //cbs回款流水
|
|
|
|
+ //查询回款流水记录
|
|
|
|
+ if sDepositPayment.FlowType == 0 {
|
|
|
|
+ //修改回款流水记
|
|
|
|
+ updateTransactionData := map[string]interface{}{
|
|
|
|
+ "deposit_id": strings.Join(depositId, ","),
|
|
|
|
+ "ISRELATION": 3,
|
|
|
|
+ }
|
|
|
|
+ _, err := g.DB("cbs").Ctx(ctx).Update(ctx, "transaction", updateTransactionData, map[string]interface{}{"id": transactionMap["id"]})
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
// todo 保证金支付款-退还/转回款
|
|
// todo 保证金支付款-退还/转回款
|
|
func DepositRefundOrRetMoney(ctx context.Context, sDeposit model.SDeposit) error {
|
|
func DepositRefundOrRetMoney(ctx context.Context, sDeposit model.SDeposit) error {
|
|
- //保证金信息 未删除
|
|
|
|
- deposits, err := g.DB().GetOne(ctx, fmt.Sprintf(`SELECT * FROM %s od WHERE od.order_code = ? AND od.pay_money > 0 AND od.is_del = 0 ORDER BY od.operate_time DESC ;`, model.DepositPaymentTable), sDeposit.OrderCode)
|
|
|
|
|
|
+ //保证金支付信息 未删除
|
|
|
|
+ depositPayment, err := g.DB().GetOne(ctx, fmt.Sprintf(`SELECT * FROM %s od WHERE od.order_code = ? AND od.pay_money > 0 AND od.is_del = 0 ORDER BY od.operate_time DESC ;`, model.DepositPaymentTable), sDeposit.OrderCode)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
- if deposits.IsEmpty() {
|
|
|
|
|
|
+ if depositPayment.IsEmpty() {
|
|
return fmt.Errorf("未查到当前订单 有效的保证金信息")
|
|
return fmt.Errorf("未查到当前订单 有效的保证金信息")
|
|
}
|
|
}
|
|
- payMoney := FenToYuanInt64(gconv.Int64(deposits.Map()["pay_money"]))
|
|
|
|
|
|
+ payMoney := FenToYuanInt64(gconv.Int64(depositPayment.Map()["pay_money"]))
|
|
if payMoney != sDeposit.RefundMoney+sDeposit.ReturnedMoney {
|
|
if payMoney != sDeposit.RefundMoney+sDeposit.ReturnedMoney {
|
|
return fmt.Errorf("保证金金额异常")
|
|
return fmt.Errorf("保证金金额异常")
|
|
}
|
|
}
|
|
@@ -256,19 +281,20 @@ func DepositRefundOrRetMoney(ctx context.Context, sDeposit model.SDeposit) error
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
var (
|
|
var (
|
|
- orderId = record.Map()["id"] //订单id
|
|
|
|
- payWay = deposits.Map()["pay_way"] //支付方式
|
|
|
|
- bankFlow = gconv.String(deposits.Map()["bank_flow"]) //银行流水号
|
|
|
|
- bankName = gconv.String(deposits.Map()["bank_name"]) //银行名称
|
|
|
|
- payVoucher = gconv.String(deposits.Map()["pay_voucher"]) //支付凭证
|
|
|
|
- payTime = gconv.String(deposits.Map()["pay_time"]) //支付时间
|
|
|
|
- payAccountName = gconv.String(deposits.Map()["pay_account_name"]) //支付户名
|
|
|
|
- transactionId = gconv.Int(deposits.Map()["transaction_id"]) //支付单号
|
|
|
|
|
|
+ orderId = record.Map()["id"] //订单id
|
|
|
|
+ payWay = gconv.Int(depositPayment.Map()["pay_way"]) //支付方式,1-微信 2-支付宝 3-对公转账
|
|
|
|
+ bankFlow = gconv.String(depositPayment.Map()["bank_flow"]) //对公转账 银行流水号,手动录入
|
|
|
|
+ bankName = gconv.String(depositPayment.Map()["bank_name"]) //银行名称
|
|
|
|
+ payVoucher = gconv.String(depositPayment.Map()["pay_voucher"]) //支付凭证
|
|
|
|
+ payTime = gconv.String(depositPayment.Map()["pay_time"]) //支付时间
|
|
|
|
+ payAccountName = gconv.String(depositPayment.Map()["pay_account_name"]) //支付户名
|
|
|
|
+ payCode = gconv.String(depositPayment.Map()["pay_code"]) //支付宝、微信、第三方平台 支付单号
|
|
|
|
+ transactionId = gconv.Int(depositPayment.Map()["transaction_id"]) //对公转账银行流水号 cbs 自动录入
|
|
)
|
|
)
|
|
var param = model.OrdersEditBatchReturn{
|
|
var param = model.OrdersEditBatchReturn{
|
|
- ReturnType: 1,
|
|
|
|
- FlowType: 0,
|
|
|
|
- ReturnCode: "",
|
|
|
|
|
|
+ ReturnType: payWay,
|
|
|
|
+ FlowType: 0, //保证金转回款,即使保证金是CBS回款,转回款不走CBS逻辑
|
|
|
|
+ ReturnCode: payCode,
|
|
TransactionId: transactionId,
|
|
TransactionId: transactionId,
|
|
OrderArr: []g.Map{
|
|
OrderArr: []g.Map{
|
|
{
|
|
{
|
|
@@ -418,21 +444,7 @@ func HasDepositOperate(ctx context.Context, orderCode string) (bool, error) {
|
|
|
|
|
|
// GetDepositPayMoney 获取保证金已支付金额
|
|
// GetDepositPayMoney 获取保证金已支付金额
|
|
func GetDepositPayMoney(ctx context.Context, orderCode string) (int64, error) {
|
|
func GetDepositPayMoney(ctx context.Context, orderCode string) (int64, error) {
|
|
- data, err := g.DB().Query(ctx, "SELECT pay_money FROM order_deposit_payment WHERE order_code=? and is_del=0 ", orderCode)
|
|
|
|
- if err != nil {
|
|
|
|
- log.Println("获取保证金已支付金额异常:", err, orderCode)
|
|
|
|
- return 0, err
|
|
|
|
- }
|
|
|
|
- if data.Len() == 0 {
|
|
|
|
- return 0, nil
|
|
|
|
- }
|
|
|
|
- dataMap := data[0].Map()
|
|
|
|
- return common.Int64All(dataMap["pay_money"]), nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// GetDepositMoney 获取保证金金额
|
|
|
|
-func GetDepositMoney(ctx context.Context, orderCode string) (int64, error) {
|
|
|
|
- data, err := g.DB().Query(ctx, "SELECT money FROM order_deposit WHERE order_code=?", orderCode)
|
|
|
|
|
|
+ data, err := g.DB().Query(ctx, "SELECT pay_money FROM order_deposit WHERE order_code=? and state=1 and is_del=0 ", orderCode)
|
|
if err != nil {
|
|
if err != nil {
|
|
log.Println("获取保证金已支付金额异常:", err, orderCode)
|
|
log.Println("获取保证金已支付金额异常:", err, orderCode)
|
|
return 0, err
|
|
return 0, err
|