package service

import (
	"errors"
	"time"
	"youngee_b_api/app/dao"
	"youngee_b_api/app/entity"
	"youngee_b_api/app/vo"
)

type StoreService struct{}

// 获取某商家下的所有门店
func (p StoreService) GetStoresByEnterpriseId(param vo.GetAllStoreParam) (vo.ResultVO, error) {
	if param.Page == 0 {
		param.Page = 1
	}
	if param.PageSize == 0 {
		param.PageSize = 10
	}
	var result vo.ResultVO
	var stores []entity.Store
	var err error
	var total int64
	enterpriseId := param.EnterpriseId
	if enterpriseId == "" {
		return result, errors.New("enterpriseId is empty")
	}
	stores, total, err = (&dao.StoreDao{}).GetStoresByEnterpriseIDAndStoreName(enterpriseId, param.StoreName, param.Page, param.PageSize)
	if err != nil {
		// 数据库查询error
		return result, err
	}
	var reStores []vo.ReStorePreview
	var creatorName string
	for _, store := range stores {
		photoUrl, e := dao.ProductPhotoDAO{}.GetMainPhotoByStoreID(store.StoreID)
		if store.SubAccountID == 0 {
			enterprise, err := dao.EnterpriseDao{}.GetEnterprise(store.EnterpriseID)
			if err == nil && enterprise != nil {
				creatorName = enterprise.BusinessName
			}
		} else {
			subAccount, err := dao.SubAccountDao{}.GetSubAccount(store.SubAccountID)
			if err == nil && subAccount != nil {
				creatorName = subAccount.SubAccountName
			}
		}
		if e != nil {
			photoUrl = ""
		}
		reStore := vo.ReStorePreview{
			StoreID:       store.StoreID,
			StoreName:     store.StoreName,
			StoreLocation: store.StoreLocation,
			StoreType:     store.StoreType,
			StoreCategory: store.StoreCategory,
			TeamNum:       store.TeamNum,
			StoreDetail:   store.StoreDetail,
			CreatedAt:     store.CreatedAt.Format("2006-01-02 15:04:05"),
			PhotoUrl:      photoUrl,
			CreateName:    creatorName,
		}
		reStores = append(reStores, reStore)
	}
	result = vo.ResultVO{
		Page:     param.Page,
		PageSize: param.PageSize,
		Total:    total,
		Data:     reStores,
	}
	return result, nil
}

// 新建门店
func (p StoreService) CreateStore(param *vo.StoreCreateParam) (int64, error) {
	newStore := entity.Store{
		StoreName:          param.StoreName,
		StoreCategory:      param.StoreCategory,
		StoreType:          param.StoreType,
		StoreLocation:      param.StoreLocation,
		StoreDetail:        param.StoreDetail,
		StoreLink:          param.StoreLink,
		BelongEnterpriseID: param.EnterpriseId,
		EnterpriseID:       param.EnterpriseId,
		SubAccountID:       param.SubAccountId,
		OperateType:        1,
		CreatedAt:          time.Now(),
	}
	storeId, err := dao.StoreDao{}.CreateStore(newStore)
	if err != nil {
		return 0, err
	}
	if param.StorePhotos != nil {
		storePhotos := []entity.ProductPhoto{}
		for _, photo := range param.StorePhotos {
			storePhoto := entity.ProductPhoto{
				PhotoUrl:         photo.PhotoUrl,
				PhotoUid:         photo.PhotoUid,
				Symbol:           photo.Symbol,
				StoreID:          storeId,
				ProductPhotoType: 2,
				CreatedAt:        time.Now(),
			}
			storePhotos = append(storePhotos, storePhoto)
		}
		err = dao.ProductPhotoDAO{}.CreateProductPhoto(storePhotos)
		if err != nil {
			return 0, err
		}
	}
	return storeId, nil
}

// 门店详情
func (s StoreService) GetStoreDetail(param *vo.StoreSearchParam) (*vo.ReStoreInfo, error) {
	var reStoreInfo *vo.ReStoreInfo
	store, err := dao.StoreDao{}.GetStoreByID(param.StoreId)
	if err != nil {
		return nil, err
	}
	photoMain, _ := dao.ProductPhotoDAO{}.GetMainPhotoByStoreID(param.StoreId)
	photosAll, _ := dao.ProductPhotoDAO{}.GetProductPhotoByStoreID(param.StoreId)
	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)
		}
	}
	reStoreInfo = &vo.ReStoreInfo{
		StoreID:       store.StoreID,
		StoreLink:     store.StoreLink,
		StoreName:     store.StoreName,
		StoreCategory: store.StoreCategory,
		StoreType:     store.StoreType,
		StoreLocation: store.StoreLocation,
		TeamNum:       store.TeamNum,
		StoreDetail:   store.StoreDetail,
		PhotoMain:     photoMain,
		PhotoRotates:  photoRotates,
		PhotoDetails:  photoDetails,
		CreatedAt:     store.CreatedAt.Format("2006-01-02 15:04:05"),
	}

	return reStoreInfo, nil
}

// 更新门店
func (p StoreService) UpdateStore(param *vo.StoreUpdateParam) (int64, error) {
	newStore := entity.Store{
		StoreID:       param.StoreId,
		StoreName:     param.StoreName,
		StoreCategory: param.StoreCategory,
		StoreType:     param.StoreType,
		StoreLocation: param.StoreLocation,
		StoreDetail:   param.StoreDetail,
		StoreLink:     param.StoreLink,
		UpdatedAt:     time.Now(),
	}
	storeId, err := dao.StoreDao{}.UpdateStore(newStore)
	if err != nil {
		return 0, err
	}
	if param.StorePhotos != nil {
		storePhotos := []entity.ProductPhoto{}
		for _, photo := range param.StorePhotos {
			storePhoto := entity.ProductPhoto{
				PhotoUrl:         photo.PhotoUrl,
				PhotoUid:         photo.PhotoUid,
				Symbol:           photo.Symbol,
				StoreID:          storeId,
				ProductPhotoType: 2,
				CreatedAt:        time.Now(),
			}
			storePhotos = append(storePhotos, storePhoto)
		}
		err = dao.ProductPhotoDAO{}.CreateProductPhoto(storePhotos)
		if err != nil {
			return 0, err
		}
	}
	return storeId, nil
}

// 删除门店
func (p StoreService) DeleteStore(param *vo.StoreUpdateParam) (int64, error) {
	err := dao.StoreDao{}.DeleteStore(param.StoreId)
	if err != nil {
		return 0, err
	}
	return param.StoreId, nil
}

// 门店类目
func (p StoreService) GetStoreCategory() ([]vo.ReStoreCategory, error) {
	var reStoreCategories []vo.ReStoreCategory
	storeCategory, err := dao.StoreDao{}.GetStoreCategory()
	if err != nil {
		return nil, err
	}
	for _, category := range storeCategory {
		reStoreCategory := vo.ReStoreCategory{
			ID:       category.ID,
			Category: category.Category,
			Detail:   category.Detail,
		}
		reStoreCategories = append(reStoreCategories, reStoreCategory)
	}
	return reStoreCategories, nil
}
