package merchant import ( logger "eta/eta_mini_ht_api/common/component/log" "eta/eta_mini_ht_api/common/utils/page" "eta/eta_mini_ht_api/domian/user" "eta/eta_mini_ht_api/models/merchant" subscribeDao "eta/eta_mini_ht_api/models/merchant" "sync" "time" ) type UserAccessDTO struct { ID int `json:"id"` TemplateUserId int `json:"templateUserId"` ProductID int `json:"productID"` ProductType string `json:"productType"` BeginDate string `json:"beginDate"` EndDate string `json:"endDate"` Status string `json:"status"` } var ( productMap = map[string]merchant.MerchantProductType{ "package": merchant.Package, "video": merchant.Video, "audio": merchant.Audio, "report": merchant.Report, } ) func convertToUserAccessDTO(userSubscriptionAccessList merchant.UserSubscriptionAccessList) UserAccessDTO { return UserAccessDTO{ ID: userSubscriptionAccessList.ID, TemplateUserId: userSubscriptionAccessList.TemplateUserId, ProductID: userSubscriptionAccessList.ProductID, ProductType: string(userSubscriptionAccessList.ProductType), BeginDate: userSubscriptionAccessList.BeginDate.Format(time.DateOnly), EndDate: userSubscriptionAccessList.EndDate.Format(time.DateOnly), Status: string(userSubscriptionAccessList.Status), } } func OpenProduct(templateUserId int, productOrderNo string, product MerchantProductDTO) error { userAccess := merchant.UserSubscriptionAccessList{ TemplateUserId: templateUserId, ProductID: product.Id, ProductName: product.Title, ProductType: productMap[product.Type], BeginDate: time.Now(), Status: merchant.SubscribeValid, ProductOrderNo: productOrderNo, } if !product.IsPermanent { userAccess.EndDate = time.Now().Add(time.Duration(product.ValidDays) * 24 * time.Hour) } return merchant.InsertOrUpdateUserSubscribe(userAccess) } func ExpireProduct(deadLine string) (err error) { total := user.GetNeedExpiredAccessCount(deadLine) if total == 0 { logger.Info("没有需要过期的产品") return } for total > 0 { list, _ := user.GetExpiredAccessList(100, deadLine) if len(list) > 0 { var wg sync.WaitGroup wg.Add(len(list)) for _, access := range list { go func(access subscribeDao.UserSubscriptionAccessList) { defer wg.Done() access.Status = subscribeDao.SubscribeExpired expireErr := subscribeDao.ExpireUserAccess(access) if expireErr != nil { logger.Error("更新产品过期状态失败:%v,用户:%d,产品:%d", expireErr, access.TemplateUserId, access.ProductID) } }(access) } wg.Wait() } total = user.GetNeedExpiredAccessCount(deadLine) } //if err != nil { // logger.Error("获取产品订单信息失败:%v", err) // return //} //product, err := merchantService.GetMerchantProductById(productOrder.ProductID) //if err != nil { // logger.Error("获取产品信息失败:%v", err) // return //} //return merchantService.CloseProduct(productOrderNo, product) return } func CloseProduct(templateUserId int, productOrderNo string, product MerchantProductDTO) error { userAccess := merchant.UserSubscriptionAccessList{ TemplateUserId: templateUserId, ProductID: product.Id, ProductName: product.Title, ProductType: productMap[product.Type], Status: merchant.SubscribeClose, ProductOrderNo: productOrderNo, } return merchant.CloseUserAccess(userAccess) } func SubscribeList(templateUserId int, productType string, pageInfo page.PageInfo) (list []UserAccessDTO, err error) { offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize) var subscribeList []merchant.UserSubscriptionAccessList subscribeList, err = merchant.SubscribeList(templateUserId, productType, pageInfo.LatestId, offset, pageInfo.PageSize) if err != nil { return } for _, userSubscriptionAccessList := range subscribeList { list = append(list, convertToUserAccessDTO(userSubscriptionAccessList)) } return } func GetUserSubscribe(templateUserId int, productId int) (dto UserAccessDTO, err error) { var subscribe merchant.UserSubscriptionAccessList subscribe, err = merchant.GetUserSubscribeById(templateUserId, productId) if err != nil { return } dto = convertToUserAccessDTO(subscribe) return } func GetTotalUserPageCountByProductType(productType string, id int) (int64, int64) { return merchant.GetTotalUserPageCountByProductType(productType, id) }