forked from free-lancers/electricity_bill_calc_service
		
	修改分页检索核算报表一些细节,完成审核撤回报表功能
This commit is contained in:
		| @@ -4,6 +4,8 @@ import ( | ||||
| 	"electricity_bill_calc/logger" | ||||
| 	"electricity_bill_calc/repository" | ||||
| 	"electricity_bill_calc/response" | ||||
| 	"electricity_bill_calc/security" | ||||
| 	"electricity_bill_calc/vo" | ||||
| 	"github.com/gofiber/fiber/v2" | ||||
| 	"go.uber.org/zap" | ||||
| 	"net/http" | ||||
| @@ -12,10 +14,11 @@ import ( | ||||
| var withdrawLog = logger.Named("Handler", "Withdraw") | ||||
|  | ||||
| func InitializeWithdrawHandlers(router *fiber.App) { | ||||
| 	router.Get("/withdraw", withdraw) | ||||
| 	router.Get("/withdraw", security.OPSAuthorize, withdraw) | ||||
| 	router.Put("/withdraw/:rid", ReviewRequestWithdraw) | ||||
| } | ||||
|  | ||||
| //用于检索用户的核算报表 | ||||
| //用于分页检索用户的核算报表 | ||||
| func withdraw(c *fiber.Ctx) error { | ||||
| 	//记录日志 | ||||
| 	withdrawLog.Info("带分页的待审核的核算撤回申请列表") | ||||
| @@ -27,14 +30,55 @@ func withdraw(c *fiber.Ctx) error { | ||||
| 	//中间数据库操作暂且省略。。。。 | ||||
| 	//首先进行核算报表的分页查询 | ||||
| 	withdraws, total, err := repository.WithdrawRepository.FindWithdraw(uint(page), &keyword) | ||||
| 		if err != nil { | ||||
| 			withdrawLog.Error("检索用户核算报表失败。", zap.Error(err)) | ||||
| 			return result.Error(http.StatusInternalServerError, err.Error()) | ||||
| 	if err != nil { | ||||
| 		withdrawLog.Error("检索用户核算报表失败。", zap.Error(err)) | ||||
| 		return result.Error(http.StatusInternalServerError, err.Error()) | ||||
| 	} | ||||
| 	//TODO: 2023-07-18 此处返回值是个示例,具体返回值需要查询数据库 | ||||
| 	//TODO: 2023-07-18 此处返回值是个示例,具体返回值需要查询数据库(完成) | ||||
| 	return result.Success( | ||||
| 		"withdraw请求成功", | ||||
| 		response.NewPagedResponse(page, total).ToMap(), | ||||
| 		fiber.Map{"records": withdraws}, | ||||
| 	) | ||||
| } | ||||
|  | ||||
| //用于审核撤回报表 | ||||
| func ReviewRequestWithdraw(c *fiber.Ctx) error { | ||||
| 	Rid := c.Params("rid", "") | ||||
| 	Data := new(vo.ReviewWithdraw) | ||||
| 	result := response.NewResult(c) | ||||
|  | ||||
| 	if err := c.BodyParser(&Data); err != nil { | ||||
| 		withdrawLog.Error("无法解析审核指定报表的请求数据", zap.Error(err)) | ||||
| 		return result.BadRequest("无法解析审核指定报表的请求数据。") | ||||
| 	} | ||||
|  | ||||
| 	if Data.Audit == true { //审核通过 | ||||
| 		ok, err := repository.WithdrawRepository.ReviewTrueReportWithdraw(Rid) | ||||
| 		if err != nil { | ||||
| 			withdrawLog.Error("审核同意撤回报表失败") | ||||
| 			return result.Error(http.StatusInternalServerError, err.Error()) | ||||
| 		} | ||||
|  | ||||
| 		if !ok { | ||||
| 			withdrawLog.Error("审核同意撤回报表失败") | ||||
| 			return result.NotAccept("审核同意撤回报表失败") | ||||
| 		} else { | ||||
| 			return result.Success("审核同意撤回报表成功!") | ||||
| 		} | ||||
| 	} else { //审核不通过 | ||||
| 		ok, err := repository.WithdrawRepository.ReviewFalseReportWithdraw(Rid) | ||||
| 		if err != nil { | ||||
| 			withdrawLog.Error("审核拒绝撤回报表失败") | ||||
| 			return result.Error(http.StatusInternalServerError, err.Error()) | ||||
| 		} | ||||
|  | ||||
| 		if !ok { | ||||
| 			withdrawLog.Error("审核拒绝撤回报表失败") | ||||
| 			return result.NotAccept("审核拒绝撤回报表失败") | ||||
| 		} else { | ||||
| 			return result.Success("审核拒绝撤回报表成功!") | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -6,6 +6,7 @@ import ( | ||||
| 	"electricity_bill_calc/logger" | ||||
| 	"electricity_bill_calc/model" | ||||
| 	"electricity_bill_calc/tools" | ||||
| 	"electricity_bill_calc/types" | ||||
| 	"fmt" | ||||
| 	"github.com/doug-martin/goqu/v9" | ||||
| 	"github.com/georgysavva/scany/v2/pgxscan" | ||||
| @@ -22,17 +23,9 @@ var WithdrawRepository = &_WithdrawRepository{ | ||||
| 	ds:  goqu.Dialect("postgres"), | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @author: ZiHangQin | ||||
|  * 该方法用于分页查询核算报表 | ||||
|  * @param:page | ||||
|  * @param: keyword | ||||
|  * @return:[]object | ||||
|  * @return:total | ||||
|  * @return: error | ||||
|  */ | ||||
| //该方法用于分页查询核算报表 | ||||
| func (wd _WithdrawRepository) FindWithdraw(page uint, keyword *string) ([]model.Withdraw, int64, error) { | ||||
| 	wd.log.Info("查询用户的充值记录。", zap.Stringp("keyword", keyword), zap.Int("page", int(page))) | ||||
| 	wd.log.Info("查询核算报表", zap.Stringp("keyword", keyword), zap.Int("page", int(page))) | ||||
| 	ctx, cancel := global.TimeoutContext() | ||||
| 	defer cancel() | ||||
|  | ||||
| @@ -66,6 +59,8 @@ func (wd _WithdrawRepository) FindWithdraw(page uint, keyword *string) ([]model. | ||||
| 		Where(goqu.I("withdraw").Eq(1)). | ||||
| 		Join(goqu.T("park").As("p"), goqu.On(goqu.I("r.park_id").Eq(goqu.I("p.id")))). | ||||
| 		Join(goqu.T("user_detail").As("ud"), goqu.On(goqu.I("p.user_id").Eq(goqu.I("ud.id")))). | ||||
| 		Where(goqu.I("p.deleted_at").IsNull()). | ||||
| 		Where(goqu.I("ud.deleted_at").IsNull()). | ||||
| 		Select( | ||||
| 			goqu.I("r.id").As("report_id"), goqu.I("r.last_withdraw_applied_at"), goqu.I("r.last_withdraw_audit_at"), | ||||
| 			goqu.I("r.park_id").As("report_park_id"), goqu.I("r.period"), goqu.I("r.published"), goqu.I("r.published_at"), goqu.I("r.withdraw"), | ||||
| @@ -99,7 +94,6 @@ func (wd _WithdrawRepository) FindWithdraw(page uint, keyword *string) ([]model. | ||||
|  | ||||
| 	countReportQuerySql, countReportQueryArgs, _ := countReportQuery.Prepared(true).ToSQL() | ||||
|  | ||||
|  | ||||
| 	var ( | ||||
| 		reports []*model.ReportRes = make([]*model.ReportRes, 0) | ||||
| 		total   int64 | ||||
| @@ -122,9 +116,8 @@ func (wd _WithdrawRepository) FindWithdraw(page uint, keyword *string) ([]model. | ||||
| 		return make([]model.Withdraw, 0), total, nil | ||||
| 	} | ||||
|  | ||||
| 	fmt.Println(&reports) | ||||
| 	var withdrawReses []model.Withdraw | ||||
| 	//TODO: 2023.07.24对查询到的数据进行拼接 | ||||
| 	//TODO: 2023.07.24对查询到的数据进行拼接(完成) | ||||
| 	for _, v := range reports { | ||||
| 		Begin := v.Period.SafeLower().Format("2006-01-02") | ||||
| 		End := v.Period.SafeUpper().Format("2006-01-02") | ||||
| @@ -174,3 +167,75 @@ func (wd _WithdrawRepository) FindWithdraw(page uint, keyword *string) ([]model. | ||||
|  | ||||
| 	return withdrawReses, total, nil | ||||
| } | ||||
|  | ||||
| //该方法用于审核同意报表撤回 | ||||
| func (wd _WithdrawRepository) ReviewTrueReportWithdraw( rid string) (bool, error) { | ||||
| 	wd.log.Info("审核指定的报表", zap.String("rid", rid)) | ||||
| 	ctx, cancel := global.TimeoutContext() | ||||
| 	defer cancel() | ||||
| 	//update report set withdraw=$2, | ||||
| 	//last_withdraw_audit_at=$3, published=false, | ||||
| 	//published_at=null where id=$1 | ||||
|  | ||||
| 	tx, err := global.DB.Begin(ctx) | ||||
| 	if err != nil { | ||||
| 		wd.log.Error("开启数据库事务失败", zap.Error(err)) | ||||
| 	} | ||||
| 	updateQuerySql, updateArgs, _ := wd.ds. | ||||
| 		Update(goqu.T("report")). | ||||
| 		Set(goqu.Record{ | ||||
| 			"withdraw": 3, | ||||
| 			"last_withdraw_audit_at": types.Now(), | ||||
| 			"published": false, | ||||
| 			"published_at": nil, | ||||
| 		}). | ||||
| 		Where(goqu.I("id").Eq(rid)). | ||||
| 		Prepared(true).ToSQL() | ||||
|  | ||||
| 	rs, err := tx.Exec(ctx, updateQuerySql, updateArgs...) | ||||
| 	if err != nil { | ||||
| 		wd.log.Error("审核报表失败", zap.Error(err)) | ||||
| 		return false, err | ||||
| 	} | ||||
| 	err = tx.Commit(ctx) | ||||
| 	if err != nil { | ||||
| 		wd.log.Error("提交数据库事务失败", zap.Error(err)) | ||||
| 		return false, err | ||||
| 	} | ||||
|  | ||||
| 	return rs.RowsAffected() > 0, nil | ||||
| } | ||||
|  | ||||
| func (wd _WithdrawRepository) ReviewFalseReportWithdraw( rid string) (bool, error) { | ||||
| 	wd.log.Info("审核指定的报表", zap.String("rid", rid)) | ||||
| 	ctx, cancel := global.TimeoutContext() | ||||
| 	defer cancel() | ||||
|  | ||||
| 	tx, err := global.DB.Begin(ctx) | ||||
| 	if err != nil { | ||||
| 		wd.log.Error("开启数据库事务失败", zap.Error(err)) | ||||
| 	} | ||||
| 	updateQuerySql, updateArgs, _ := wd.ds. | ||||
| 		Update(goqu.T("report")). | ||||
| 		Set(goqu.Record{ | ||||
| 			"withdraw": 2, | ||||
| 			"last_withdraw_audit_at": types.Now(), | ||||
| 			"published": false, | ||||
| 			"published_at": nil, | ||||
| 		}). | ||||
| 		Where(goqu.I("id").Eq(rid)). | ||||
| 		Prepared(true).ToSQL() | ||||
|  | ||||
| 	rs, err := tx.Exec(ctx, updateQuerySql, updateArgs...) | ||||
| 	if err != nil { | ||||
| 		wd.log.Error("审核报表失败", zap.Error(err)) | ||||
| 		return false, err | ||||
| 	} | ||||
| 	err = tx.Commit(ctx) | ||||
| 	if err != nil { | ||||
| 		wd.log.Error("提交数据库事务失败", zap.Error(err)) | ||||
| 		return false, err | ||||
| 	} | ||||
|  | ||||
| 	return rs.RowsAffected() > 0, nil | ||||
| } | ||||
							
								
								
									
										6
									
								
								vo/withdraw.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								vo/withdraw.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | ||||
| package vo | ||||
|  | ||||
| //用于接收审核报表的参数 | ||||
| type ReviewWithdraw struct { | ||||
| 	Audit bool `json:"audit"` | ||||
| } | ||||
		Reference in New Issue
	
	Block a user