forked from free-lancers/electricity_bill_calc_service
		
	feat(report):完成多端共用的报表检索功能。
This commit is contained in:
		| @@ -9,6 +9,7 @@ import ( | ||||
| 	"electricity_bill_calc/tools" | ||||
| 	"log" | ||||
| 	"net/http" | ||||
| 	"strconv" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/fufuok/utils" | ||||
| @@ -34,6 +35,7 @@ func InitializeReportController(router *gin.Engine) { | ||||
| 	router.PUT("/report/:rid/step/diluted/fees", security.EnterpriseAuthorize, progressReportWillDilutedFee) | ||||
| 	router.PUT("/report/:rid/step/meter/register", security.EnterpriseAuthorize, progressEndUserRegister) | ||||
| 	router.POST("/report/:rid/publish", security.EnterpriseAuthorize, publishReport) | ||||
| 	router.GET("/reports", security.MustAuthenticated, searchReports) | ||||
| } | ||||
|  | ||||
| func ensureReportBelongs(c *gin.Context, result *response.Result, requestReportId string) bool { | ||||
| @@ -398,3 +400,47 @@ func publishReport(c *gin.Context) { | ||||
| 	} | ||||
| 	result.Success("指定的公示报表已经发布。") | ||||
| } | ||||
|  | ||||
| func searchReports(c *gin.Context) { | ||||
| 	result := response.NewResult(c) | ||||
| 	session, err := _retreiveSession(c) | ||||
| 	if err != nil { | ||||
| 		result.Unauthorized(err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	requestUser := lo. | ||||
| 		If(session.Type == model.USER_TYPE_ENT, session.Uid). | ||||
| 		Else(c.DefaultQuery("user", "")) | ||||
| 	requestPark := c.DefaultQuery("park", "") | ||||
| 	if len(requestPark) > 0 && session.Type == model.USER_TYPE_ENT { | ||||
| 		if !ensureParkBelongs(c, result, requestPark) { | ||||
| 			return | ||||
| 		} | ||||
| 	} | ||||
| 	requestPeriodString := c.DefaultQuery("period", "") | ||||
| 	var requestPeriod *time.Time = nil | ||||
| 	if len(requestPeriodString) > 0 { | ||||
| 		parsedPeriod, err := time.Parse("2006-01", requestPeriodString) | ||||
| 		if err != nil { | ||||
| 			result.NotAccept("参数[period]的格式不正确。") | ||||
| 			return | ||||
| 		} | ||||
| 		requestPeriod = lo.ToPtr(parsedPeriod) | ||||
| 	} | ||||
| 	requestKeyword := c.DefaultQuery("keyword", "") | ||||
| 	requestPage, err := strconv.Atoi(c.DefaultQuery("page", "1")) | ||||
| 	if err != nil { | ||||
| 		result.NotAccept("查询参数[page]格式不正确。") | ||||
| 		return | ||||
| 	} | ||||
| 	records, totalItems, err := service.ReportService.SearchReport(requestUser, requestPark, requestKeyword, requestPeriod, requestPage) | ||||
| 	if err != nil { | ||||
| 		result.NotFound(err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	result.Success( | ||||
| 		"已经取得符合条件的公示报表记录。", | ||||
| 		response.NewPagedResponse(requestPage, totalItems).ToMap(), | ||||
| 		gin.H{"reports": records}, | ||||
| 	) | ||||
| } | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| package service | ||||
|  | ||||
| import ( | ||||
| 	"electricity_bill_calc/config" | ||||
| 	"electricity_bill_calc/exceptions" | ||||
| 	"electricity_bill_calc/global" | ||||
| 	"electricity_bill_calc/model" | ||||
| @@ -379,3 +380,45 @@ func (_ReportService) PublishReport(report model.Report) (err error) { | ||||
| 	_, err = global.DBConn.ID(report.Id).Cols("step_state", "published", "published_at").Update(report) | ||||
| 	return | ||||
| } | ||||
|  | ||||
| func (_ReportService) SearchReport(requestUser, requestPark, requestKeyword string, requestPeriod *time.Time, requestPage int) ([]model.JoinedReportForWithdraw, int64, error) { | ||||
| 	cond := builder.NewCond().And(builder.Eq{"r.published": true}) | ||||
| 	if len(requestUser) > 0 { | ||||
| 		cond = cond.And(builder.Eq{"u.id": requestUser}) | ||||
| 	} | ||||
| 	if len(requestPark) > 0 { | ||||
| 		cond = cond.And(builder.Eq{"p.id": requestPark}) | ||||
| 	} | ||||
| 	if requestPeriod != nil { | ||||
| 		cond = cond.And(builder.Eq{"r.period": *requestPeriod}) | ||||
| 	} | ||||
| 	if len(requestKeyword) > 0 { | ||||
| 		cond = cond.And( | ||||
| 			builder.Like{"u.name", requestKeyword}. | ||||
| 				Or(builder.Like{"p.name", requestKeyword}). | ||||
| 				Or(builder.Like{"u.abbr", requestKeyword}). | ||||
| 				Or(builder.Like{"p.abbr", requestKeyword}). | ||||
| 				Or(builder.Like{"u.address", requestKeyword}). | ||||
| 				Or(builder.Like{"p.address", requestKeyword}), | ||||
| 		) | ||||
| 	} | ||||
| 	total, err := global.DBConn. | ||||
| 		Table("report").Alias("r"). | ||||
| 		Join("INNER", []string{"park", "p"}, "p.id=r.park_id"). | ||||
| 		Join("INNER", []string{"user_detail", "u"}, "u.id=p.user_id"). | ||||
| 		Where(cond). | ||||
| 		Count() | ||||
| 	if err != nil { | ||||
| 		return make([]model.JoinedReportForWithdraw, 0), -1, err | ||||
| 	} | ||||
| 	startItem := (requestPage - 1) * config.ServiceSettings.ItemsPageSize | ||||
| 	records := make([]model.JoinedReportForWithdraw, 0) | ||||
| 	err = global.DBConn. | ||||
| 		Table("report").Alias("r"). | ||||
| 		Join("INNER", []string{"park", "p"}, "p.id=r.park_id"). | ||||
| 		Join("INNER", []string{"user_detail", "u"}, "u.id=p.user_id"). | ||||
| 		Where(cond). | ||||
| 		Limit(config.ServiceSettings.ItemsPageSize, startItem). | ||||
| 		Find(&records) | ||||
| 	return records, total, err | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user