123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package house
- import (
- "context"
- "encoding/json"
- "fmt"
- "gorm.io/gorm"
- "property-household/errors"
- "property-household/impl/v1/oss_utils"
- dbmodel "property-household/model"
- "property-household/pb"
- pb_v1 "property-household/pb/v1"
- "property-household/utils"
- "strings"
- "time"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- )
- // 房屋状态 未入住 已入住
- const (
- HouseStatusWait = 1
- HouseStatusUsed = 2
- //HouseStatusRented = 3
- )
- // 认证状态
- const (
- HouseholdApproveStatusWait = 1
- HouseholdApproveStatusSuccess = 2
- HouseholdApproveStatusFail = 3
- HouseholdApproveStatusFailAndWait = 4
- )
- const (
- SystemMsgCodeHouseholdBindHouse = "1"
- SystemMsgCodeRepairOrder = "2"
- SystemMsgCodeRepairOrderSended = "3"
- SystemMsgCodeSuggestionOrder = "4"
- SystemMsgCodeSuggestionOrderSended = "5"
- SystemMsgCodeRent = "6"
- SystemMsgCodeRentAppointment = "7"
- )
- func checkHouseholdApplyParam(req *pb_v1.HouseholdApplyRequest) error {
- switch {
- case req.Phone == "":
- //return status.Error(10003, "手机号不能为空")
- case req.HouseId < 1:
- return status.Error(10003, "房屋不能为空")
- case req.Uid < 1:
- return status.Error(10003, "用户不能为空")
- case req.UserType < 1:
- return status.Error(10003, "业主类型不能为空")
- case req.IdType < 1:
- //return status.Error(10003, "证件类型不能为空,请先完成实名认证")
- case req.IdNumber == "":
- //return status.Error(10003, "证件号不能为空,请先完成实名认证")
- case req.Name == "":
- //return status.Error(10003, "姓名不能为空")
- case req.GardenId < 1:
- return status.Error(10003, "小区不能为空")
- }
- return nil
- }
- func GetHouseInfo(id int64, gardenId int64) (data *pb_v1.HouseInfoReply, err error) {
- mreq := &pb_v1.HouseInfoRequest{
- HouseId: id,
- GardenId: gardenId,
- }
- return pb.Garden.HouseInfo(context.Background(), mreq)
- }
- //
- func HouseholdApply(ctx context.Context, req *pb_v1.HouseholdApplyRequest) (reply *pb_v1.HouseholdApplyReply, err error) {
- reply = &pb_v1.HouseholdApplyReply{}
- // 捕获各个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 = checkHouseholdApplyParam(req)
- if err != nil {
- return nil, err
- }
- info, err := GetHouseInfo(req.HouseId, req.GardenId)
- if err != nil {
- return nil, err
- }
- idNumber := ""
- if req.IdNumber != "" {
- idNumber = utils.CertEncrypt(idNumber)
- }
- // 检查用户
- user := dbmodel.TUser{}
- where := map[string]interface{}{
- "id": req.Uid,
- }
- err = user.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if user.ID == 0 {
- return nil, errors.ErrRecordNotFound
- }
- // 检查是否与已审核的记录重复
- houseApproved := dbmodel.THouseApproved{}
- where = map[string]interface{}{
- "uid": req.Uid,
- "garden_id": req.GardenId,
- "house_id": req.HouseId,
- }
- count, err := houseApproved.Count(database.DB(), where, nil)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if count > 0 {
- return nil, errors.ErrDuplicate
- }
- if user.Phone == "" || user.IdType < 1 || user.RealName == "" || user.IdNumber == "" {
- return nil, status.Error(10003, "请先实名认证")
- }
- now := time.Now()
- house := dbmodel.THouse{
- GardenId: info.GardenId,
- Uid: req.Uid,
- ApproveStatus: HouseholdApproveStatusWait,
- CreatedAt: now,
- UpdatedAt: now,
- UserType: req.UserType,
- HouseId: req.HouseId,
- BuildingNumber: info.BuildingNumber,
- UnitNumber: info.UnitNumber,
- HouseNumber: info.HouseNumber,
- UniqFlag: 1,
- BuildingId: info.BuildingId,
- UnitId: info.UnitId,
- IdType: user.IdType,
- IdNumber: user.IdNumber,
- Phone: user.Phone,
- Name: user.RealName,
- ApprovedAt: now,
- OpenId: user.OpenId,
- }
- if len(req.Appendix) > 0 {
- house.Appendix = strings.Join(req.Appendix, ";")
- }
- db := database.DB().Begin()
- err = house.Insert(db)
- if err != nil {
- db.Rollback()
- msg := strings.ToLower(err.Error())
- if strings.Contains(msg, "duplicate") {
- return nil, errors.ErrDuplicate
- }
- return nil, errors.DataBaseError
- }
- reply.Id = house.ID
- statisticReq := pb_v1.RepairStatisticSetRequest{
- HandleType: 3,
- TotalIncrease: int64(1),
- GardenId: req.GardenId,
- }
- if err := oss_utils.OssObjAdd(req.Appendix, nil); err != nil {
- db.Rollback()
- return nil, err
- }
- _, err = pb.Garden.RepairStatisticSet(ctx, &statisticReq)
- if err != nil {
- db.Rollback()
- return nil, err
- }
- db.Commit()
- mreq := pb_v1.SystemMsgAddRequest{
- GardenId: req.GardenId,
- Content: "新的住户入住申请",
- Code: SystemMsgCodeHouseholdBindHouse,
- }
- pb.Garden.SystemMsgAdd(ctx, &mreq)
- return reply, nil
- }
|