123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- // 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"
- "property-household/errors"
- dbmodel "property-household/model"
- "property-household/pb"
- pb_v1 "property-household/pb/v1"
- "property-household/utils"
- "strings"
- )
- func checkHouseholdListParam(req *pb_v1.HouseholdListRequest) error {
- switch {
- case req.Uid < 1 && req.GardenId < 1:
- return status.Error(10003, "id不能为空")
- case req.ApproveStatus < 0 || req.ApproveStatus > 4:
- return status.Error(10003, "请选择认证状态")
- }
- if req.Page == 0 {
- req.Page = 1
- }
- if req.PageSize == 0 {
- req.PageSize = 10
- }
- return nil
- }
- /*
- func getUserInfos(uids []int64) (map[int64]dbmodel.TUser, error) {
- user := dbmodel.TUser{}
- where := map[string]interface{}{
- "id in":uids,
- }
- list, err := user.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- ret := map[int64]dbmodel.TUser{}
- for _, v := range list {
- ret[v.ID] = v
- }
- return ret, nil
- }
- */
- func getGardenInfos(ids []int64) (map[int64]pb_v1.GardenItem, error) {
- ret := map[int64]pb_v1.GardenItem{}
- if len(ids) == 0 {
- return ret, nil
- }
- mreq := pb_v1.GardenInfosRequest{
- Ids:ids,
- }
- mreply, err := pb.System.GardenInfos(context.Background(), &mreq)
- if err != nil {
- return nil, err
- }
- for _, v := range mreply.List {
- ret[v.Id] = *v
- }
- return ret, nil
- }
- func householdApprovedList(ctx context.Context, req *pb_v1.HouseholdListRequest)(reply *pb_v1.HouseholdListReply, err error) {
- reply = &pb_v1.HouseholdListReply{}
- p := dbmodel.THouseApproved{}
- where := map[string]interface{}{}
- if req.GardenId > 0 {
- where["garden_id"] = req.GardenId
- }
- if req.Uid > 0 {
- where["uid"] = req.Uid
- }
- if req.Name != "" {
- where["name"] = req.Name
- }
- reply.Page = req.Page
- reply.Total, err = p.Count(database.DB(), where, nil)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if reply.Total == 0 {
- return reply, nil
- }
- list, err := p.ListByJoin(database.DB(), where, nil, int(req.Page), int(req.PageSize))
- if err != nil {
- return nil, errors.DataBaseError
- }
- gardenIdsM := map[int64]bool{}
- gardenIds := []int64{}
- for _, v := range list {
- if gardenIdsM[v.GardenId] {
- continue
- }
- gardenIdsM[v.GardenId] = true
- gardenIds = append(gardenIds, v.GardenId)
- }
- gardenInfosM, err := getGardenInfos(gardenIds)
- if err != nil {
- return nil, err
- }
- reply.List = make([]*pb_v1.HouseholdItem, len(list))
- for i, v := range list {
- reply.List[i] = &pb_v1.HouseholdItem{
- Id: v.ID,
- HouseName: fmt.Sprintf("%v-%v-%v", v.BuildingNumber, v.UnitNumber, v.HouseNumber),
- UserType: v.UserType,
- ApproveStatus: HouseholdApproveStatusSuccess,
- GardenName: gardenInfosM[v.GardenId].GardenName,
- IdType:v.IdType,
- IdNumber:v.IdNumber,
- Phone:v.Phone,
- Name:v.Name,
- GardenId:v.GardenId,
- HouseId:v.HouseId,
- Uid:v.Uid,
- }
- decrypt := utils.CertDecrypt(reply.List[i].IdNumber)
- if decrypt != "" {
- reply.List[i].IdNumber = decrypt
- }
- if v.Appendix != "" {
- reply.List[i].Appendix = strings.Split(v.Appendix, ";")
- }
- if !strings.Contains(v.HouseNumber, "-") {
- if v.UnitNumber > 0 {
- reply.List[i].HouseName = fmt.Sprintf("%s-%d-%s", v.BuildingNumber, v.UnitNumber, v.HouseNumber)
- } else {
- reply.List[i].HouseName = fmt.Sprintf("%s-%s", v.BuildingNumber, v.HouseNumber)
- }
- }
- }
- return reply, err
- }
- //
- func HouseholdList(ctx context.Context, req *pb_v1.HouseholdListRequest) (reply *pb_v1.HouseholdListReply, err error) {
- reply = &pb_v1.HouseholdListReply{}
- // 捕获各个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 = checkHouseholdListParam(req)
- if err != nil {
- return nil, err
- }
- if req.ApproveStatus == HouseholdApproveStatusSuccess {
- return householdApprovedList(ctx, req)
- }
- p := dbmodel.THouse{}
- where := map[string]interface{}{}
- if req.GardenId > 0 {
- where["garden_id"] = req.GardenId
- }
- if req.Uid > 0 {
- where["uid"] = req.Uid
- }
- if req.Name != "" {
- where["name"] = req.Name
- }
- if req.ApproveStatus > 0 && req.ApproveStatus != HouseholdApproveStatusFailAndWait{
- where["approve_status"] = req.ApproveStatus
- }
- reply.Page = req.Page
- reply.Total, err = p.Count(database.DB(), where, nil)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if reply.Total == 0 {
- return reply, nil
- }
- list, err := p.ListByJoin(database.DB(), where, nil, int(req.Page), int(req.PageSize))
- if err != nil {
- return nil, errors.DataBaseError
- }
- gardenIdsM := map[int64]bool{}
- gardenIds := []int64{}
- for _, v := range list {
- if gardenIdsM[v.GardenId] {
- continue
- }
- gardenIdsM[v.GardenId] = true
- gardenIds = append(gardenIds, v.GardenId)
- }
- gardenInfosM, err := getGardenInfos(gardenIds)
- if err != nil {
- return nil, err
- }
- reply.List = make([]*pb_v1.HouseholdItem, len(list))
- for i, v := range list {
- reply.List[i] = &pb_v1.HouseholdItem{
- Id: v.ID,
- HouseName: fmt.Sprintf("%v-%v-%v", v.BuildingNumber, v.UnitNumber, v.HouseNumber),
- UserType: v.UserType,
- ApproveStatus: int32(v.ApproveStatus),
- GardenName: gardenInfosM[v.GardenId].GardenName,
- IdType:v.IdType,
- IdNumber:v.IdNumber,
- Phone:v.Phone,
- Name:v.Name,
- GardenId:v.GardenId,
- HouseId:v.HouseId,
- Uid:v.Uid,
- }
- decrypt := utils.CertDecrypt(reply.List[i].IdNumber)
- if decrypt != "" {
- reply.List[i].IdNumber = decrypt
- }
- if v.Appendix != "" {
- reply.List[i].Appendix = strings.Split(v.Appendix, ";")
- }
- if !strings.Contains(v.HouseNumber, "-") {
- if v.UnitNumber > 0 {
- reply.List[i].HouseName = fmt.Sprintf("%s-%d-%s", v.BuildingNumber, v.UnitNumber, v.HouseNumber)
- } else {
- reply.List[i].HouseName = fmt.Sprintf("%s-%s", v.BuildingNumber, v.HouseNumber)
- }
- }
- }
- return reply, nil
- }
|