From ddfe88071a7a1328e171656c1f2c8e1ce4d93748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Mon, 22 Aug 2022 16:27:42 +0800 Subject: [PATCH] =?UTF-8?q?feat(report):=E5=AE=8C=E6=88=90=E6=8A=A5?= =?UTF-8?q?=E8=A1=A8=E4=B8=AD=E6=88=B7=E8=A1=A8=E5=88=97=E8=A1=A8=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/end_user.go | 40 ++++++++++++++++++++++++++++++++++++++++ router/router.go | 1 + service/end_user.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 controller/end_user.go create mode 100644 service/end_user.go diff --git a/controller/end_user.go b/controller/end_user.go new file mode 100644 index 0000000..558297a --- /dev/null +++ b/controller/end_user.go @@ -0,0 +1,40 @@ +package controller + +import ( + "electricity_bill_calc/response" + "electricity_bill_calc/security" + "electricity_bill_calc/service" + "net/http" + "strconv" + + "github.com/gin-gonic/gin" +) + +func InitializeEndUserController(router *gin.Engine) { + router.GET("/report/:rid/submeter", security.EnterpriseAuthorize, fetchEndUserInReport) +} + +func fetchEndUserInReport(c *gin.Context) { + result := response.NewResult(c) + requestReportId := c.Param("rid") + if !ensureReportBelongs(c, result, requestReportId) { + return + } + keyword := c.DefaultQuery("keyword", "") + requestPage, err := strconv.Atoi(c.DefaultQuery("page", "1")) + if err != nil { + result.NotAccept("查询参数[page]格式不正确。") + return + } + endUsers, totalItem, err := service.EndUserService.SearchEndUserRecord(requestReportId, keyword, requestPage) + if err != nil { + result.NotFound(err.Error()) + return + } + result.Json( + http.StatusOK, + "已获取到符合条件的终端用户集合", + response.NewPagedResponse(requestPage, totalItem).ToMap(), + gin.H{"meters": endUsers}, + ) +} diff --git a/router/router.go b/router/router.go index f847ac6..0ed81ff 100644 --- a/router/router.go +++ b/router/router.go @@ -22,6 +22,7 @@ func Router() *gin.Engine { controller.InitializeMaintenanceFeeController(router) controller.InitializeMeter04kVController(router) controller.InitializeReportController(router) + controller.InitializeEndUserController(router) controller.InitializeWithdrawController(router) return router diff --git a/service/end_user.go b/service/end_user.go new file mode 100644 index 0000000..cc569b4 --- /dev/null +++ b/service/end_user.go @@ -0,0 +1,40 @@ +package service + +import ( + "electricity_bill_calc/config" + "electricity_bill_calc/global" + "electricity_bill_calc/model" + + "xorm.io/builder" +) + +type _EndUserService struct{} + +var EndUserService _EndUserService + +func (_EndUserService) SearchEndUserRecord(reportId, keyword string, page int) ([]model.EndUserDetail, int64, error) { + cond := builder.NewCond().And(builder.Eq{"report_id": reportId}) + if len(keyword) > 0 { + cond = cond.And( + builder.Like{"customer_name", keyword}. + Or(builder.Like{"contact_name", keyword}). + Or(builder.Like{"contact_phone", keyword}). + Or(builder.Like{"meter_04kv_id", keyword}), + ) + } + total, err := global.DBConn. + Table(&model.EndUserDetail{}). + Where(cond). + Count() + if err != nil { + return make([]model.EndUserDetail, 0), -1, err + } + startItem := (page - 1) * config.ServiceSettings.ItemsPageSize + endUsers := make([]model.EndUserDetail, 0) + err = global.DBConn. + Where(cond). + Limit(config.ServiceSettings.ItemsPageSize, startItem). + Asc("seq"). + Find(&endUsers) + return endUsers, total, err +}