1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package house
- 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-household/errors"
- dbmodel "property-household/model"
- pb_v1 "property-household/pb/v1"
- )
- func checkHouseholdDelHouseParam(req *pb_v1.HouseholdDelHouseRequest) error {
- if req.GardenId == 0 || req.HouseId == 0 || req.HouseholdUid == 0 {
- return errors.ParamsError
- }
- return nil
- }
- //
- func HouseholdDelHouse(ctx context.Context, req *pb_v1.HouseholdDelHouseRequest) (reply *pb_v1.HouseholdDelHouseReply, err error) {
- reply = &pb_v1.HouseholdDelHouseReply{}
- // 捕获各个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 = checkHouseholdDelHouseParam(req)
- // 获取原始数据
- household := dbmodel.THouseApproved{}
- where := map[string]interface{}{
- "uid": req.HouseholdUid,
- "house_id": req.HouseId,
- "garden_id": req.GardenId,
- }
- err = household.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if household.ID == 0 {
- return nil, errors.ErrRecordNotFound
- }
- db := database.DB().Begin()
- err = household.Delete(db, where)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- rent := dbmodel.THouseRent{}
- where = map[string]interface{}{
- "household_uid": req.HouseholdUid,
- "house_id": req.HouseId,
- "garden_id": req.GardenId,
- }
- err = rent.Delete(db, where)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- if len(req.RentIds) > 0 {
- appoint := dbmodel.THouseRentAppointment{}
- where = map[string]interface{}{
- "rent_id in": req.RentIds,
- }
- err = appoint.Delete(db, where)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- }
- db.Commit()
- return reply, nil
- }
|