// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package charge 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-garden/errors" "property-garden/impl/v1/charge_utils" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" "time" ) func checkChargeDelBillListParam(req *pb_v1.ChargeDelBillListRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") } if req.Page == 0 { req.Page = 1 } if req.PageSize == 0 { req.PageSize = 10 } return nil } type HouseholdInfo struct { Name string Phone string } func GetHouseholdInfoFromHouse(houseIds []int64, uids []int64, dbname string) (map[int64]HouseholdInfo, error) { if len(houseIds) == 0 && len(uids) == 0 { return map[int64]HouseholdInfo{}, nil } p := dbmodel.NewHouseApprovedGarden(dbname) where := map[string]interface{}{ "house_id in": houseIds, "user_type": 1, } if len(uids) > 0 { where = map[string]interface{}{ "uid in": uids, } } list, err := p.List(database.DB(), where, nil, -1, -1) if err != nil { return nil, errors.DataBaseError } if len(houseIds) > 0 { m := map[int64]HouseholdInfo{} for _, v := range list { m[v.HouseId] = HouseholdInfo{Name: v.Name, Phone: v.Phone} } return m, nil } m := map[int64]HouseholdInfo{} for _, v := range list { m[v.Uid] = HouseholdInfo{Name: v.Name, Phone: v.Phone} } return m, nil } func GetHouseholdInfoFromSpace(objIds []int64, dbname string) (map[int64]HouseholdInfo, error) { if len(objIds) == 0 { return map[int64]HouseholdInfo{}, nil } space := dbmodel.NewParkSpace(dbname) where := map[string]interface{}{ "id in": objIds, "house_id >": 0, } spaceList, err := space.List(database.DB(), where, nil, -1, -1) if err != nil { return nil, errors.DataBaseError } spaceHouseM := map[int64]int64{} houseIds := []int64{} for _, v := range spaceList { spaceHouseM[v.ID] = v.HouseId houseIds = append(houseIds, v.HouseId) } householdInfosM, err := GetHouseholdInfoFromHouse(houseIds, nil, dbname) if err != nil { return nil, errors.DataBaseError } ret := map[int64]HouseholdInfo{} for spaceId, houseId := range spaceHouseM { ret[spaceId] = householdInfosM[houseId] } return ret, nil } func GethouseholdInfoFromVehicle(vehicleIds []int64, dbname string) (map[int64]HouseholdInfo, error) { if len(vehicleIds) == 0 { return map[int64]HouseholdInfo{}, nil } vehicle := dbmodel.NewVehicle(dbname) where := map[string]interface{}{ "id in": vehicleIds, } vlist, err := vehicle.List(database.DB(), where, nil, -1, -1) if err != nil { return nil, errors.DataBaseError } vehicleUserM := map[int64]int64{} uids := []int64{} for _, v := range vlist { vehicleIds[v.ID] = v.HouseholdUid uids = append(uids, v.HouseholdUid) } householdInfosM, err := GetHouseholdInfoFromHouse(nil, uids, dbname) if err != nil { return nil, errors.DataBaseError } ret := map[int64]HouseholdInfo{} for vehicleId, uid := range vehicleUserM { ret[vehicleId] = householdInfosM[uid] } return ret, nil } func ChargeDelBillList(ctx context.Context, req *pb_v1.ChargeDelBillListRequest) (reply *pb_v1.ChargeDelBillListReply, err error) { reply = &pb_v1.ChargeDelBillListReply{} // 捕获各个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 = checkChargeDelBillListParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) bill := dbmodel.NewChargeBillDel(dbname) where := map[string]interface{}{} if req.ChargeType > 0 { where["charge_type"] = req.ChargeType } if req.ObjName != "" { where["obj_name"] = req.ObjName } if req.ChargeName != "" { where["charge_name"] = req.ChargeName } if req.ObjType > 0 { where["obj_type"] = req.ObjType } // 账单数量 reply.Total, err = bill.Count(database.DB(), where, nil) if err != nil { return nil, errors.DataBaseError } if reply.Total == 0 { return reply, nil } reply.Page = req.Page chargeInfoM, err := charge_utils.GetAllChargeInfos(dbname) if err != nil { return nil, err } // 账单列表 list, err := bill.List(database.DB(), where, nil, int(req.Page), int(req.PageSize)) if err != nil { return nil, errors.DataBaseError } vehicleIds, spaceIds, houseIds := []int64{}, []int64{}, []int64{} for _, v := range list { switch v.ObjType { case charge_utils.ObjTypeHouse: houseIds = append(houseIds, v.ObjId) case charge_utils.ObjTypeSpace: spaceIds = append(spaceIds, v.ObjId) case charge_utils.ObjTypeVehicle: vehicleIds = append(vehicleIds, v.ObjId) } } vHouseholdM, err := GethouseholdInfoFromVehicle(vehicleIds, dbname) if err != nil { return nil, errors.DataBaseError } sHouseholdM, err := GetHouseholdInfoFromSpace(spaceIds, dbname) if err != nil { return nil, errors.DataBaseError } hHouseholdM, err := GetHouseholdInfoFromHouse(houseIds, nil, dbname) if err != nil { return nil, errors.DataBaseError } reply.List = make([]*pb_v1.ChargeDelBillItem, len(list)) for i, v := range list { chargeDesc := dbmodel.ChargeDesc{} json.Unmarshal([]byte(v.ChargeDesc), &chargeDesc) chargeInfo := chargeInfoM[v.ChargeId] lfee, _ := charge_utils.GetLateFee(v.Amount, v.ChargeEnd, &chargeInfo) item := &pb_v1.ChargeDelBillItem{ Start: time.Unix(v.ChargeStart, 0).Format("2006-01-02"), End: time.Unix(v.ChargeEnd, 0).Format("2006-01-02"), ChargeType: v.ChargeType, ChargeName: v.ChargeName, Amount: v.Amount, LastUsed: chargeDesc.PowerLastUsed, CurrentUsed: chargeDesc.PowerCurrentUsed, LateFee: lfee, BillId: v.ID, ChargeDesc: charge_utils.GetChargeDesc(chargeDesc), PayTime: time.Unix(v.PayTime, 0).Format("2006-01-02 15:04:05"), PayType: v.PayType, ObjName: v.ObjName, ObjType: v.ObjType, DelAt: v.DelAt.Format("2006-01-02 15:04:05"), DelReason: v.DelReason, } switch v.ObjType { case charge_utils.ObjTypeHouse: item.HouseholdName = hHouseholdM[v.ObjId].Name item.HouseholdPhone = hHouseholdM[v.ObjId].Phone case charge_utils.ObjTypeSpace: item.HouseholdName = sHouseholdM[v.ObjId].Name item.HouseholdPhone = sHouseholdM[v.ObjId].Phone case charge_utils.ObjTypeVehicle: item.HouseholdName = vHouseholdM[v.ObjId].Name item.HouseholdPhone = vHouseholdM[v.ObjId].Phone } reply.List[i] = item } return reply, nil }