feat(charge):增加计费记录查询功能,待测。

This commit is contained in:
徐涛 2022-08-15 11:17:21 +08:00
parent 9b2f869220
commit a48e63d798
6 changed files with 117 additions and 0 deletions

48
controller/charge.go Normal file
View File

@ -0,0 +1,48 @@
package controller
import (
"electricity_bill_calc/repository"
"electricity_bill_calc/response"
"electricity_bill_calc/security"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
type _ChargesController struct {
Router *gin.Engine
}
var ChargesController *_ChargesController
func InitializeChargesController(router *gin.Engine) {
ChargesController = &_ChargesController{
Router: router,
}
ChargesController.Router.GET("/charges", security.OPSAuthorize, listAllCharges)
}
func listAllCharges(c *gin.Context) {
result := response.NewResult(c)
requestPage, err := strconv.Atoi(c.DefaultQuery("page", "1"))
if err != nil {
result.NotAccept("查询参数[page]格式不正确。")
return
}
requestKeyword := c.DefaultQuery("keyword", "")
requestBeginDate := c.DefaultQuery("begin", "")
requestEndDate := c.DefaultQuery("end", "")
charges, total, err := repository.ChargeRepo.ListPagedChargeRecord(requestKeyword, requestBeginDate, requestEndDate, requestPage)
if err != nil {
result.NotFound(err.Error())
return
}
result.Json(
http.StatusOK, "已获取到符合条件的计费记录。",
response.NewPagedResponse(requestPage, total).ToMap(),
gin.H{"records": charges},
)
}

1
go.mod
View File

@ -28,6 +28,7 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jinzhu/copier v0.3.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect github.com/magiconair/properties v1.8.6 // indirect

2
go.sum
View File

@ -308,6 +308,8 @@ github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0f
github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jinzhu/copier v0.3.5 h1:GlvfUwHk62RokgqVNvYsku0TATCF7bAHVwEXoBh3iJg=
github.com/jinzhu/copier v0.3.5/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=

View File

@ -22,3 +22,12 @@ type UserCharge struct {
func (UserCharge) TableName() string { func (UserCharge) TableName() string {
return "user_charge" return "user_charge"
} }
type ChargeWithName struct {
UserDetail `xorm:"extends"`
UserCharge `xorm:"extends"`
}
func (ChargeWithName) TableName() string {
return "user_charge"
}

52
repository/charge.go Normal file
View File

@ -0,0 +1,52 @@
package repository
import (
"electricity_bill_calc/config"
"electricity_bill_calc/global"
"electricity_bill_calc/model"
"time"
"xorm.io/builder"
)
type _ChargeRepository struct{}
var ChargeRepo _ChargeRepository
func (_ChargeRepository) ListPagedChargeRecord(keyword, beginDate, endDate string, page int) ([]model.ChargeWithName, int64, error) {
var cond = builder.NewCond()
if len(keyword) != 0 {
cond.And(builder.Like{"u.name", keyword})
}
if len(beginDate) != 0 {
beginTime, err := time.Parse("2006-01-02", beginDate)
if err != nil {
return make([]model.ChargeWithName, 0), 0, err
}
cond.And(builder.Gte{"c.created_at": beginTime})
}
if len(endDate) != 0 {
endTime, err := time.Parse("2006-01-02", endDate)
if err != nil {
return make([]model.ChargeWithName, 0), 0, err
}
cond.And(builder.Lte{"c.created_at": endTime})
}
startItem := (page - 1) * config.ServiceSettings.ItemsPageSize
total, err := global.DBConn.
Table("user_charge").Alias("c").
Join("INNER", []string{"user_detail", "d"}, "c.user_id=d.id").
Where(cond).
Count(&model.ChargeWithName{})
if err != nil {
return nil, -1, err
}
charges := make([]model.ChargeWithName, 0)
err = global.DBConn.
Table("user_charge").Alias("c").
Join("INNER", []string{"user_detail", "d"}, "c.user_id=d.id").
Where(cond).
Limit(config.ServiceSettings.ItemsPageSize, startItem).
Find(&charges)
return charges, total, err
}

5
service/charge.go Normal file
View File

@ -0,0 +1,5 @@
package service
type _ChargeService struct{}
var ChargeService _ChargeService