123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- 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"
- "property-household/impl/v1/public_msg"
- dbmodel "property-household/model"
- "property-household/pb"
- pb_v1 "property-household/pb/v1"
- "time"
- )
- func checkHouseholdApproveParam(req *pb_v1.HouseholdApproveRequest) error {
- switch {
- case req.Id < 1:
- return status.Error(10003, "id不能为空")
- }
- return nil
- }
- const (
- HandleTypeRepair = 1
- HandleTypeSuggestion = 2
- HandleTypeHousehold = 3
- )
- //
- func HouseholdApprove(ctx context.Context, req *pb_v1.HouseholdApproveRequest) (reply *pb_v1.HouseholdApproveReply, err error) {
- reply = &pb_v1.HouseholdApproveReply{}
- // 捕获各个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 = checkHouseholdApproveParam(req)
- if err != nil {
- return nil, err
- }
- p := dbmodel.THouse{}
- 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.ErrRecordNotFound
- }
- if p.ApproveStatus != HouseholdApproveStatusWait {
- return nil, status.Error(10003, "当前状态不能审核")
- }
- u := dbmodel.TUser{}
- filter := map[string]interface{}{
- "id": p.Uid,
- }
- err = u.Find(database.DB(), filter)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if u.ID == 0 {
- return nil, errors.ErrRecordNotFound
- }
- now := time.Now()
- values := map[string]interface{}{
- "feedback": req.Feedback,
- "approve_status": HouseholdApproveStatusSuccess,
- "approved_at": now,
- "updated_at": now,
- }
- if !req.Status {
- values["approve_status"] = HouseholdApproveStatusFail
- values["uniq_flag"] = nil
- }
- db := database.DB().Begin()
- insertId := int64(0)
- if req.Status {
- err = p.Delete(db, where)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- approvedHouse := dbmodel.THouseApproved{
- GardenId: p.GardenId,
- Uid: p.Uid,
- CreatedAt: p.CreatedAt,
- UpdatedAt: now,
- UserType: p.UserType,
- HouseId: p.HouseId,
- BuildingNumber: p.BuildingNumber,
- UnitNumber: p.UnitNumber,
- HouseNumber: p.HouseNumber,
- BuildingId: p.BuildingId,
- UnitId: p.UnitId,
- Appendix: p.Appendix,
- Feedback: req.Feedback,
- Phone: p.Phone,
- Name: p.Name,
- IdType: p.IdType,
- IdNumber: p.IdNumber,
- ApprovedAt: now,
- OpenId: u.OpenId,
- PublicOpenId: u.PublicOpenId,
- }
- err = approvedHouse.Insert(db)
- insertId = approvedHouse.ID
- } else {
- err = p.Update(db, where, values)
- if err == nil {
- // 业主审核统计数增加
- statisticReq := pb_v1.RepairStatisticSetRequest{
- HandleType: HandleTypeHousehold,
- FinishIncrease: int64(1),
- GardenId: p.GardenId,
- }
- _, err = pb.Garden.RepairStatisticSet(ctx, &statisticReq)
- }
- }
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- if req.Status {
- mreq := &pb_v1.HouseholdSyncRequest{
- GardenId: p.GardenId,
- OpenId: p.OpenId,
- Uid: p.Uid,
- CreatedAt: p.CreatedAt.Unix(),
- UpdatedAt: now.Unix(),
- UserType: p.UserType,
- HouseId: p.HouseId,
- BuildingNumber: p.BuildingNumber,
- UnitNumber: p.UnitNumber,
- HouseNumber: p.HouseNumber,
- BuildingId: p.BuildingId,
- UnitId: p.UnitId,
- Appendix: p.Appendix,
- Feedback: req.Feedback,
- Phone: p.Phone,
- Name: p.Name,
- IdType: p.IdType,
- IdNumber: p.IdNumber,
- ApprovedAt: now.Unix(),
- Id: insertId,
- PublicOpenId: u.PublicOpenId,
- }
- mreq.HouseholdUser = &pb_v1.HouseholdUserData{
- Id: u.ID,
- Phone: u.Phone,
- IdNumber: u.IdNumber,
- IdType: u.IdType,
- OpenId: u.OpenId,
- UnionId: u.UnionId,
- NickName: u.NickName,
- RealName: u.RealName,
- Avatar: u.Avatar,
- PublicOpenId: u.PublicOpenId,
- }
- _, err = pb.Garden.HouseholdSync(ctx, mreq)
- if err != nil {
- db.Rollback()
- return nil, err
- }
- }
- db.Commit()
- householdGardensDelFromRedis(p.Uid)
- serviceInfo := public_msg.ServiceInfo{
- Name: "房屋认证",
- State: "已通过",
- }
- if !req.Status {
- serviceInfo.State = "未通过(" + req.Feedback + ")"
- }
- public_msg.SendMsgWx(p.Uid, p.GardenId, serviceInfo)
- return reply, nil
- }
|