123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- package service
- import (
- "time"
- "youngee_b_api/app/dao"
- "youngee_b_api/app/entity"
- "youngee_b_api/app/vo"
- )
- type TeamBuyingService struct{}
- func (p TeamBuyingService) GetTeamBuyingsByStoreId(param vo.GetAllTeamBuyingParam) (vo.ResultVO, error) {
- if param.Page == 0 {
- param.Page = 1
- }
- if param.PageSize == 0 {
- param.PageSize = 10
- }
- var result vo.ResultVO
- var teamBuyings []entity.TeamBuying
- var err error
- var total int64
- storeId := param.StoreId
- teamBuyings, total, err = (&dao.TeamBuyingDao{}).GetTeamBuyingsByStoreIdAndTeamBuyingTitle(storeId, param.TeamBuyingTitle, param.Page, param.PageSize)
- if err != nil {
- // 数据库查询error
- return result, err
- }
- var reTeamBuyings []vo.ReTeamBuyingPreview
- for _, teamBuying := range teamBuyings {
- var createrName string
- photoUrl, e := dao.ProductPhotoDAO{}.GetMainPhotoByTeamBuyingID(teamBuying.TeamBuyingID)
- if e != nil {
- photoUrl = ""
- }
- if teamBuying.SubAccountID == 0 {
- enterprise, err := dao.EnterpriseDao{}.GetEnterprise(teamBuying.EnterpriseID)
- if err == nil && enterprise != nil {
- createrName = enterprise.BusinessName
- }
- } else {
- subAccount, err := dao.SubAccountDao{}.GetSubAccount(teamBuying.SubAccountID)
- if err == nil && subAccount != nil {
- createrName = subAccount.SubAccountName
- }
- }
- reTeamBuying := vo.ReTeamBuyingPreview{
- TeamBuyingID: teamBuying.TeamBuyingID,
- TeamBuyingName: teamBuying.TeamBuyingName,
- TeamBuyingPrice: teamBuying.TeamBuyingPrice,
- TeamBuyingCategory: teamBuying.TeamBuyingCategory,
- TeamBuyingDetail: teamBuying.TeamBuyingDetail,
- CreatedAt: teamBuying.CreatedAt.Format("2006-01-02 15:04:05"),
- PhotoUrl: photoUrl,
- CreateName: createrName,
- }
- reTeamBuyings = append(reTeamBuyings, reTeamBuying)
- }
- result = vo.ResultVO{
- Page: param.Page,
- PageSize: param.PageSize,
- Total: total,
- Data: reTeamBuyings,
- }
- return result, nil
- }
- // 新建团购
- func (p TeamBuyingService) CreateTeamBuying(param *vo.TeamBuyingCreateParam) (int64, error) {
- newTeamBuying := entity.TeamBuying{
- TeamBuyingName: param.TeamBuyingName,
- TeamBuyingCategory: param.TeamBuyingCategory,
- TeamBuyingPrice: param.TeamBuyingPrice,
- PublicCommission: param.PublicCommission,
- TeamBuyingDetail: param.TeamBuyingDetail,
- TeamBuyingLink: param.TeamBuyingLink,
- StoreID: param.StoreId,
- CreatedAt: time.Now(),
- EnterpriseID: param.EnterpriseId,
- SubAccountID: param.SubAccountId,
- OperateType: 1,
- }
- teamBuyingId, err := dao.TeamBuyingDao{}.CreateTeamBuying(newTeamBuying)
- if err != nil {
- return 0, err
- }
- err = dao.StoreDao{}.IncrementTeamNum(param.StoreId)
- if err != nil {
- return 0, err
- }
- if param.TeamBuyingPhotos != nil {
- teamBuyingPhotos := []entity.ProductPhoto{}
- for _, photo := range param.TeamBuyingPhotos {
- teamBuyingPhoto := entity.ProductPhoto{
- PhotoUrl: photo.PhotoUrl,
- PhotoUid: photo.PhotoUid,
- Symbol: photo.Symbol,
- TeamBuyingID: teamBuyingId,
- ProductPhotoType: 3,
- CreatedAt: time.Now(),
- }
- teamBuyingPhotos = append(teamBuyingPhotos, teamBuyingPhoto)
- }
- err = dao.ProductPhotoDAO{}.CreateProductPhoto(teamBuyingPhotos)
- if err != nil {
- return 0, err
- }
- }
- return teamBuyingId, nil
- }
- // 团购详情
- func (s TeamBuyingService) GetTeamBuyingDetail(param *vo.TeamBuyingSearchParam) (*vo.ReTeamBuyingInfo, error) {
- var reTeamBuyingInfo *vo.ReTeamBuyingInfo
- teamBuying, err := dao.TeamBuyingDao{}.GetTeamBuyingByID(param.TeamBuyingId)
- if err != nil {
- return nil, err
- }
- photoMain, _ := dao.ProductPhotoDAO{}.GetMainPhotoByTeamBuyingID(param.TeamBuyingId)
- photosAll, _ := dao.ProductPhotoDAO{}.GetProductPhotoByTeamBuyingID(param.TeamBuyingId)
- var photoRotates []vo.Photo
- var photoDetails []vo.Photo
- for _, photosOne := range photosAll {
- if photosOne.Symbol == 2 || photosOne.Symbol == 3 {
- photo := vo.Photo{
- PhotoUrl: photosOne.PhotoUrl,
- Symbol: photosOne.Symbol,
- }
- photoRotates = append(photoRotates, photo)
- } else if photosOne.Symbol == 4 || photosOne.Symbol == 5 {
- photo := vo.Photo{
- PhotoUrl: photosOne.PhotoUrl,
- Symbol: photosOne.Symbol,
- }
- photoDetails = append(photoDetails, photo)
- }
- }
- reTeamBuyingInfo = &vo.ReTeamBuyingInfo{
- TeamBuyingID: teamBuying.StoreID,
- TeamBuyingName: teamBuying.TeamBuyingName,
- TeamBuyingPrice: teamBuying.TeamBuyingPrice,
- PublicCommission: teamBuying.PublicCommission,
- TeamBuyingCategory: teamBuying.TeamBuyingCategory,
- TeamBuyingDetail: teamBuying.TeamBuyingDetail,
- TeamBuyingLink: teamBuying.TeamBuyingLink,
- PhotoMain: photoMain,
- PhotoRotates: photoRotates,
- PhotoDetails: photoDetails,
- CreatedAt: teamBuying.CreatedAt.Format("2006-01-02 15:04:05"),
- }
- return reTeamBuyingInfo, nil
- }
- // 更新团购
- func (p TeamBuyingService) UpdateTeamBuying(param *vo.TeamBuyingUpdateParam) (int64, error) {
- newTeamBuying := entity.TeamBuying{
- TeamBuyingID: param.TeamBuyingId,
- TeamBuyingName: param.TeamBuyingName,
- TeamBuyingCategory: param.TeamBuyingCategory,
- TeamBuyingPrice: param.TeamBuyingPrice,
- PublicCommission: param.PublicCommission,
- TeamBuyingDetail: param.TeamBuyingDetail,
- TeamBuyingLink: param.TeamBuyingLink,
- UpdatedAt: time.Now(),
- }
- teamBuyingId, err := dao.TeamBuyingDao{}.UpdateTeamBuying(newTeamBuying)
- if err != nil {
- return 0, err
- }
- if param.TeamBuyingPhotos != nil {
- teamBuyingPhotos := []entity.ProductPhoto{}
- for _, photo := range param.TeamBuyingPhotos {
- teamBuyingPhoto := entity.ProductPhoto{
- PhotoUrl: photo.PhotoUrl,
- PhotoUid: photo.PhotoUid,
- Symbol: photo.Symbol,
- StoreID: teamBuyingId,
- ProductPhotoType: 3,
- CreatedAt: time.Now(),
- }
- teamBuyingPhotos = append(teamBuyingPhotos, teamBuyingPhoto)
- }
- err = dao.ProductPhotoDAO{}.CreateProductPhoto(teamBuyingPhotos)
- if err != nil {
- return 0, err
- }
- }
- return teamBuyingId, nil
- }
- // 删除团购
- func (p TeamBuyingService) DeleteTeamBuying(param *vo.TeamBuyingUpdateParam) (int64, error) {
- err := dao.TeamBuyingDao{}.DeleteTeamBuying(param.TeamBuyingId)
- if err != nil {
- return 0, err
- }
- return param.TeamBuyingId, nil
- }
|