// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package fee import ( "context" "git.getensh.com/common/gopkgs/database" "property-garden/errors" "property-garden/impl/v1/charge_utils" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" "time" ) type BillObj struct { ObjId int64 `json:"obj_id"` ObjType int32 `json:"obj_type"` HouseId int64 `json:"house_id"` } type BillUser struct { HouseId int64 `json:"house_id"` Phone string `json:"phone"` PublicOpenId string `json:"public_open_id"` } func getAllBills(dbname string, isNew bool) ([]BillObj, error) { p := dbmodel.TChargeBill{} p.SetTable(dbname) where := map[string]interface{}{ "pay_mode": 1, "house_id >": 0, "charge_time_type": charge_utils.ChargeTimeTypePeriod, } if isNew { where["created_at >"] = time.Now().Format("2006-01-02") } list := []BillObj{} err := p.SelectGroup(database.DB(), where, nil, "obj_id, obj_type, house_id", "obj_id,obj_type", -1, -1, &list) if err != nil { return nil, errors.DataBaseError } //return []BillObj{{ObjId:1, ObjType:1, HouseId:1},{ObjId:1, ObjType:2, HouseId:2}}, nil return list, nil } func GetPublicOpenIds(dbname string, houseIds []int64, userType []int32) ([]BillUser, error) { p := dbmodel.NewHouseApprovedGarden(dbname) where := map[string]interface{}{ "house_id in": houseIds, } if len(userType) > 0 { where["user_type in"] = userType } list := []BillUser{} err := p.SelectGroup(database.DB(), where, nil, "house_id, phone, public_open_id", "house_id, uid", -1, -1, &list) if err != nil { return nil, errors.DataBaseError } return list, nil } // 生成催缴单 func GenerateReminder(ctx context.Context, req *pb_v1.GenerateReminderRequest) (reply *pb_v1.GenerateReminderReply, err error) { reply = &pb_v1.GenerateReminderReply{} if req.GardenId < 0 { return nil, errors.ParamsError } dbname := utils.GetGardenDbName(req.GardenId) billObjs, err := getAllBills(dbname, req.IsNew) if err != nil { return reply, err } if len(billObjs) == 0 { return reply, nil } houseIds := make([]int64, len(billObjs)) for i, v := range billObjs { houseIds[i] = v.HouseId } billUsers, err := GetPublicOpenIds(dbname, houseIds, req.UserType) if err != nil { return reply, err } m := map[int64][]*pb_v1.ReminderUserInfo{} for _, v := range billUsers { m[v.HouseId] = append(m[v.HouseId], &pb_v1.ReminderUserInfo{Phone: v.Phone, PublicOpenId: v.PublicOpenId}) } reply.List = make([]*pb_v1.ReminderItem, len(billObjs)) for i, v := range billObjs { reply.List[i] = &pb_v1.ReminderItem{ ObjId: v.ObjId, ObjType: v.ObjType, } if len(m[v.HouseId]) == 0 { m[v.HouseId] = make([]*pb_v1.ReminderUserInfo, 0) } reply.List[i].Users = m[v.HouseId] } return reply, nil }