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
}
