// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package charge import ( "context" "encoding/json" "fmt" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "gorm.io/gorm" "property-garden/errors" "property-garden/impl/v1/charge_utils" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" "strings" "time" ) func checkChargeOrderPayParam(req *pb_v1.ChargeOrderPayRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") case req.OrderId == "": return status.Error(10003, "订单号不能为空") case req.TransactionId == "": return status.Error(10003, "交易号不能为空") } return nil } func changeBindTimestamp(start, end, bindId int64, chargeType int32, db *gorm.DB, dbname string) error { p := dbmodel.NewChargeBind(dbname) where := map[string]interface{}{ "id": bindId, "bill_last_time <": end, } values := map[string]interface{}{ "bill_last_time": end, } if chargeType == charge_utils.ChargeTypeVehicle { values["start"] = start values["end"] = end } err := p.Update(db, where, values) if err != nil { return errors.DataBaseError } return nil } func ChargeOrderConfirm(ctx context.Context, req *pb_v1.ChargeOrderPayRequest) (reply *pb_v1.ChargeOrderPayReply, err error) { switch { case req.GardenId == 0: return nil, status.Error(10003, "小区不能为空") case req.OrderId == "": return nil, status.Error(10003, "订单号不能为空") } reply = &pb_v1.ChargeOrderPayReply{} // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() dbname := utils.GetGardenDbName(req.GardenId) // 查看订单 order := dbmodel.NewChargeOrder(dbname) where := map[string]interface{}{ "order_id": req.OrderId, } err = order.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if order.OrderId == "" { return nil, errors.ErrRecordNotFound } if req.PayTime == 0 { req.PayTime = time.Now().Unix() } db := database.DB().Begin() defer func() { if err != nil { db.Rollback() } }() onlineBill := dbmodel.TChargeBill{} if order.OnlinePay == 1 { json.Unmarshal([]byte(order.OnlinePayBill), &onlineBill) } // 插入已支付账单表 bill := dbmodel.NewChargeBill(dbname) where = map[string]interface{}{ "order_id": req.OrderId, } var list []dbmodel.TChargeBill if order.OnlinePay == 1 { list = []dbmodel.TChargeBill{onlineBill} } else { list, err = bill.List(database.DB(), where, nil, -1, -1, "") if err != nil { return nil, errors.DataBaseError } } if len(list) > 0 { for i, _ := range list { list[i].ID = 0 list[i].PayTime = req.PayTime list[i].Status = charge_utils.PayStatusPayed } bill.SetTablePayed(dbname) err = bill.InsertMulti(db, &list) if err != nil { if strings.Contains(strings.ToLower(err.Error()), "duplicate") { return nil, status.Error(10003, "已存在相同已支付账单(费项和时间相同)") } return nil, errors.DataBaseError } // 删除待支付表中的信息 if order.OnlinePay == 0 { bill.SetTable(dbname) err = bill.Delete(db, where) if err != nil { return nil, errors.DataBaseError } } } billIds := []string{} for _, v := range list { billIds = append(billIds, fmt.Sprintf("%d", v.ID)) } // 更新订单状态 values := map[string]interface{}{ "status": charge_utils.PayStatusPayed, "pay_time": req.PayTime, "bill_ids": strings.Join(billIds, ","), "comment": order.Comment + "(手动确认支付)", "deleted": 0, } if order.OnlinePay == 1 { onlineBill.Status = charge_utils.PayStatusPayed bytes, _ := json.Marshal(onlineBill) values["online_pay_bill"] = string(bytes) } err = order.Update(db, where, values) if err != nil { return nil, errors.DataBaseError } // 线上预存费用要修改关系表时间戳 if order.OnlinePay == 1 { err = changeBindTimestamp(onlineBill.ChargeStart, onlineBill.ChargeEnd, onlineBill.ChargeBindId, onlineBill.ChargeType, db, dbname) if err != nil { return nil, err } } if err = charge_utils.CompanyDealIncrease(req.GardenId, order.PayAmount, req.PayTime); err != nil { return nil, err } db.Commit() return reply, nil } func ChargeOrderPay(ctx context.Context, req *pb_v1.ChargeOrderPayRequest) (reply *pb_v1.ChargeOrderPayReply, err error) { reply = &pb_v1.ChargeOrderPayReply{} // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() // 参数检查 if req.IsComfirm { return ChargeOrderConfirm(ctx, req) } err = checkChargeOrderPayParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) // 查看订单 order := dbmodel.NewChargeOrder(dbname) where := map[string]interface{}{ "order_id": req.OrderId, } err = order.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if order.OrderId == "" { return nil, errors.ErrRecordNotFound } if req.PayTime == 0 { req.PayTime = time.Now().Unix() } onlineBill := dbmodel.TChargeBill{} if order.OnlinePay == 1 { json.Unmarshal([]byte(order.OnlinePayBill), &onlineBill) } db := database.DB().Begin() defer func() { if err != nil { db.Rollback() } }() // 插入已支付账单表 bill := dbmodel.NewChargeBill(dbname) where = map[string]interface{}{ "order_id": req.OrderId, } var list []dbmodel.TChargeBill if order.OnlinePay == 1 { list = []dbmodel.TChargeBill{onlineBill} } else { list, err = bill.List(database.DB(), where, nil, -1, -1, "") if err != nil { return nil, errors.DataBaseError } } if len(list) > 0 { for i, _ := range list { list[i].ID = 0 list[i].PayTime = req.PayTime list[i].Status = charge_utils.PayStatusPayed } bill.SetTablePayed(dbname) err = bill.InsertMulti(db, &list) if err != nil { if strings.Contains(strings.ToLower(err.Error()), "duplicate") { return nil, status.Error(10003, "已存在相同已支付账单(费项和时间相同)") } return nil, errors.DataBaseError } // 删除待支付表中的信息 if order.OnlinePay == 0 { bill.SetTable(dbname) err = bill.Delete(db, where) if err != nil { return nil, errors.DataBaseError } } } billIds := []string{} for _, v := range list { billIds = append(billIds, fmt.Sprintf("%d", v.ID)) } // 更新订单状态 values := map[string]interface{}{ "status": charge_utils.PayStatusPayed, "pay_time": req.PayTime, "bill_ids": strings.Join(billIds, ","), "transaction_id": req.TransactionId, } if order.OnlinePay == 1 { onlineBill.Status = charge_utils.PayStatusPayed bytes, _ := json.Marshal(onlineBill) values["online_pay_bill"] = string(bytes) } if req.Amount != order.Amount { values["comment"] = fmt.Sprintf("%s", order.Comment) + fmt.Sprintf("三方订单已支付,金额不一致%d", req.Amount) } err = order.Update(db, where, values) if err != nil { return nil, errors.DataBaseError } // 线上预存费用要修改关系表时间戳 if order.OnlinePay == 1 { err = changeBindTimestamp(onlineBill.ChargeStart, onlineBill.ChargeEnd, onlineBill.ChargeBindId, onlineBill.ChargeType, db, dbname) if err != nil { return nil, err } } if err = charge_utils.CompanyDealIncrease(req.GardenId, order.PayAmount, req.PayTime); err != nil { return nil, err } db.Commit() return reply, nil }