// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package garden import ( "context" "encoding/json" "fmt" "gorm.io/gorm" "property-system/errors" dbmodel "property-system/model" "property-system/pb" pb_v1 "property-system/pb/v1" "time" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" ) func checkGardenKeyInfoApproveParam(req *pb_v1.GardenKeyInfoApproveRequest) error { switch { case req.Id < 1: return status.Error(10003, "id不能为空") } return nil } // func GardenKeyInfoApprove(ctx context.Context, req *pb_v1.GardenKeyInfoApproveRequest) (reply *pb_v1.GardenKeyInfoApproveReply, err error) { reply = &pb_v1.GardenKeyInfoApproveReply{} // 捕获各个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 = checkGardenKeyInfoApproveParam(req) if err != nil { return nil, err } // 获取修改信息 p := &dbmodel.TGardenChange{} where := map[string]interface{}{ "id": req.Id, } err = p.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if p.ID == 0 { return nil, errors.GardenNotExist } if p.Status > 1 { return nil, status.Error(10003, "已审核,不能重复审批") } gardenId := p.GardenId // 拒绝申请 if !req.Status { gChange := dbmodel.TGardenChange{} where = map[string]interface{}{ "id":req.Id, } values := map[string]interface{}{ "status": 3, "approved_at": time.Now(), "feedback": req.Feedback, } err = gChange.Update(database.DB(), where, values) if err != nil { return nil, errors.DataBaseError } return reply, nil } // 组装申请信息 addrChange := false nameChange := false gardenName := "" array := []KeyInfoChangeContent{} json.Unmarshal([]byte(p.Content), &array) values := map[string]interface{}{} for _, v := range array { values[v.Key] = v.NewValue if v.Key == "committee_code" { addrChange = true } if v.Key == "garden_name" { nameChange = true gardenName = v.NewValue } } // 检查 不能修改省市区街道社区是否可以修改 if addrChange { mreq := pb_v1.GardenHouseIsInRequest{GardenId: req.Id} mreply, err := pb.Garden.GardenHouseIsIn(ctx, &mreq) if err != nil { return nil, err } if mreply.In { // 自动拒绝 p := dbmodel.TGardenChange{} values := map[string]interface{}{ "status": 3, "approved_at": time.Now(), "feedback": "已入驻房屋 不能修改省市区街道社区", } err = p.Update(database.DB(), where, values) if err != nil { return nil, errors.DataBaseError } } } db := database.DB().Begin() defer func() { if err != nil { db.Rollback() } }() // 更新小区 garden := dbmodel.TGardenApproved{} where = map[string]interface{}{ "id":p.GardenId, } err = garden.Update(db, where, values) if err != nil { return nil, errors.DataBaseError } // 通过申请 gChange := dbmodel.TGardenChange{} where = map[string]interface{}{ "id":req.Id, } values = map[string]interface{}{ "status": 2, "approved_at": time.Now(), "feedback": req.Feedback, } err = gChange.Update(db, where, values) if err != nil { return nil, errors.DataBaseError } // 拒绝住户申请 if addrChange { if err = RefuseHouseApply(req.Id, 0, 0, 0); err != nil { return nil, err } } // 同步小区名信息到租房信息表 if nameChange { mreq := pb_v1.GardenHouseRentChangeFieldRequest{ GardenId: gardenId, GardenName: gardenName, } _, err = pb.Garden.GardenHouseRentChangeField(ctx, &mreq) if err != nil { return nil, err } } db.Commit() return reply, nil }