// 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" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" ) func checkChargeUnbindParam(req *pb_v1.ChargeUnbindRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") case req.ObjId == 0: return status.Error(10003, "id不能为空") case req.ChargeId == 0: return status.Error(10003, "项目不能为空") } return nil } func setPowerBillIdZero(db *gorm.DB, billId int64, dbname string) error { p := dbmodel.NewChargePower(dbname) where := map[string]interface{}{ "bill_id":billId, } values := map[string]interface{}{ "bill_id":0, } err := p.Update(db, where, values) if err != nil { return errors.DataBaseError } return nil } func ChargeUnbind(ctx context.Context, req *pb_v1.ChargeUnbindRequest) (reply *pb_v1.ChargeUnbindReply, err error) { reply = &pb_v1.ChargeUnbindReply{} // 捕获各个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")) } } }() // 参数检查 err = checkChargeUnbindParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) old := dbmodel.NewChargeBind(dbname) where := map[string]interface{}{ "obj_id": req.ObjId, "charge_id": req.ChargeId, } err = old.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if old.ID == 0 { return nil, errors.ErrRecordNotFound } reply.Origin = &pb_v1.ChargeUnbindData{ Id: old.ID, GargenId: req.GardenId, ObjId: old.ObjId, ChargeId: old.ChargeId, } //chargeHouseId := old.ID //chargeType := old.ChargeType //objType := old.ObjType db := database.DB().Begin() defer func() { if err != nil { db.Rollback() } }() err = old.Delete(db, where) if err != nil { return nil, errors.DataBaseError } /* 先不删账单 // 更新抄表记录 billid; 删除未缴账单,不删除支付中账 bill := dbmodel.NewChargeBill(dbname) where = map[string]interface{}{ "charge_bind_id":old.ID, } billList, err := bill.List(database.DB(), where, nil, -1, -1) if err != nil { return nil, errors.DataBaseError } for _, v := range billList { if v.ChargePowerId > 0 { err = setPowerBillIdZero(db, v.ID, dbname) if err != nil { return nil, err } } } err = bill.Delete(db, where) if err != nil { return nil, errors.DataBaseError } */ db.Commit() return reply, nil }