100 lines
3.2 KiB
Go
100 lines
3.2 KiB
Go
package dto
|
||
|
||
import (
|
||
"fmt"
|
||
"hospital-admin-api/api/dao"
|
||
"hospital-admin-api/api/model"
|
||
)
|
||
|
||
// CouponGrantDto 发放优惠券表
|
||
type CouponGrantDto struct {
|
||
GrantId string `json:"grant_id"` // 主键id
|
||
CouponId string `json:"coupon_id"` // 优惠卷id
|
||
GrantType int `json:"grant_type"` // 发放类型(1:具体用户 2:未拥有用户)
|
||
UserId string `json:"user_id"` // 用户id(发放类型为具体用户时存在)
|
||
TotalQuantity int `json:"total_quantity"` // 目标发放数量
|
||
GrantQuantity int `json:"grant_quantity"` // 已发放数量
|
||
GrantResult int `json:"grant_result"` // 发放结果(1:成功 2:发放中 3:部分 4:失败)
|
||
StopReason string `json:"stop_reason"` // 停止原因
|
||
AdminUserId string `json:"admin_user_id"` // 后台操作用户id
|
||
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
||
UpdatedAt model.LocalTime `json:"updated_at"` // 修改时间
|
||
UserName string `json:"user_name"` // 用户名称
|
||
AdminUserName string `json:"admin_user_name"` // 操作人名称
|
||
}
|
||
|
||
// GetCouponGrantDto 优惠卷详情
|
||
func GetCouponGrantDto(m *model.CouponGrant) *CouponGrantDto {
|
||
return &CouponGrantDto{
|
||
GrantId: fmt.Sprintf("%d", m.GrantId),
|
||
CouponId: fmt.Sprintf("%d", m.CouponId),
|
||
GrantType: m.GrantType,
|
||
UserId: fmt.Sprintf("%d", m.UserId),
|
||
TotalQuantity: m.TotalQuantity,
|
||
GrantQuantity: m.GrantQuantity,
|
||
GrantResult: m.GrantResult,
|
||
StopReason: m.StopReason,
|
||
AdminUserId: fmt.Sprintf("%d", m.AdminUserId),
|
||
CreatedAt: m.CreatedAt,
|
||
UpdatedAt: m.UpdatedAt,
|
||
}
|
||
}
|
||
|
||
// GetCouponGrantListDto 优惠卷列表
|
||
func GetCouponGrantListDto(m []*model.CouponGrant) []*CouponGrantDto {
|
||
// 处理返回值
|
||
responses := make([]*CouponGrantDto, len(m))
|
||
|
||
if len(m) > 0 {
|
||
for i, v := range m {
|
||
response := &CouponGrantDto{
|
||
GrantId: fmt.Sprintf("%d", v.GrantId),
|
||
CouponId: fmt.Sprintf("%d", v.CouponId),
|
||
GrantType: v.GrantType,
|
||
UserId: fmt.Sprintf("%d", v.UserId),
|
||
TotalQuantity: v.TotalQuantity,
|
||
GrantQuantity: v.GrantQuantity,
|
||
GrantResult: v.GrantResult,
|
||
StopReason: v.StopReason,
|
||
AdminUserId: fmt.Sprintf("%d", v.AdminUserId),
|
||
CreatedAt: v.CreatedAt,
|
||
UpdatedAt: v.UpdatedAt,
|
||
}
|
||
|
||
if v.User != nil {
|
||
response.LoadUserAttr(v.User)
|
||
}
|
||
|
||
if v.AdminUserId != 0 {
|
||
response.LoadAdminUserName(v.AdminUserId)
|
||
}
|
||
|
||
// 将转换后的结构体添加到新切片中
|
||
responses[i] = response
|
||
}
|
||
}
|
||
|
||
return responses
|
||
}
|
||
|
||
// LoadUserAttr 加载用户属性
|
||
func (r *CouponGrantDto) LoadUserAttr(m *model.User) *CouponGrantDto {
|
||
if m != nil {
|
||
r.UserName = m.UserName
|
||
}
|
||
return r
|
||
}
|
||
|
||
// LoadAdminUserName 加载后台用户属性
|
||
func (r *CouponGrantDto) LoadAdminUserName(adminUserId int64) *CouponGrantDto {
|
||
if adminUserId != 0 {
|
||
// 获取后台操作人记录
|
||
adminUserDao := dao.AdminUserDao{}
|
||
adminUser, _ := adminUserDao.GetAdminUserFirstById(adminUserId)
|
||
if adminUser != nil {
|
||
r.AdminUserName = adminUser.NickName
|
||
}
|
||
}
|
||
return r
|
||
}
|