123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- // 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
- }
|