package service

import (
	"context"
	"fmt"
	"github.com/sirupsen/logrus"
	"youngee_b_api/db"
	"youngee_b_api/model/common_model"
	"youngee_b_api/model/http_model"
)

var LocalLife *localLife

type localLife struct {
}

func (*localLife) GetFullLocalLifeList(ctx context.Context, pageSize, pageNum int32, supplierId int, condition *common_model.SLocalLifeCondition) (*http_model.FullListData, error) {

	// 1. 查询本地生活任务基本信息
	fullLocals, total, err := db.GetFullLocalLifeList(ctx, pageSize, pageNum, condition)
	if err != nil {
		logrus.WithContext(ctx).Errorf("[fullLocals service] call GetFullLocalLifeList error,err:%+v", err)
		return nil, err
	}
	var fullLocalData *http_model.FullListData
	fullLocalData = &http_model.FullListData{}
	fullLocalData.Total = total
	for _, fullLocal := range fullLocals {
		var fullLocalPreview *http_model.FullPreview
		fullLocalPreview = &http_model.FullPreview{}
		fullLocalPreview.LocalId = fullLocal.LocalId
		fullLocalPreview.LocalName = fullLocal.LocalName
		fullLocalPreview.TaskStatus = fullLocal.TaskStatus
		fullLocalPreview.LocalPlatform = fullLocal.LocalPlatform
		fullLocalPreview.TaskForm = fullLocal.TaskForm
		fullLocalPreview.LocalType = fullLocal.LocalType
		fullLocalPreview.LocalContentType = fullLocal.ContentType
		fullLocalData.FullPreview = append(fullLocalData.FullPreview, fullLocalPreview)
	}

	// 2. 查询本地生活补充信息：门店信息，招募策略
	for _, project := range fullLocalData.FullPreview {

		// 2.1. 门店信息
		storeInfo, productErr := db.FindStoreById(ctx, project.StoreId)
		if productErr != nil {
			return nil, productErr
		}
		if storeInfo != nil {
			project.StoreId = storeInfo.StoreId
			// project.ProductPrice = storeInfo.S
			project.StoreName = storeInfo.StoreName
		}

		// 2.2. 门店图片信息
		productPhotoInfo, productPhotoErr := db.GetStorePhotoByStoreID(ctx, project.StoreId)
		if productPhotoErr != nil {
			return nil, productPhotoErr
		}
		if productPhotoInfo != nil {
			for _, photo := range productPhotoInfo {
				fmt.Println(photo)
				if photo.Symbol == 1 {
					project.ProductPhotoSymbol = 1
					project.ProductPhotoUrl = photo.PhotoUrl
					project.ProductPhotoUid = photo.PhotoUid
				}
			}
		}

		// 2.3. 招募策略信息
		recruitStrategyInfo, recruitErr := db.GetRecruitStrategyByProjectId(ctx, project.LocalId)
		if recruitErr != nil {
			return nil, recruitErr
		}
		if recruitStrategyInfo != nil {
			for _, strategy := range recruitStrategyInfo {
				var recruitStrategy *http_model.EasyRecruitStrategy
				recruitStrategy = &http_model.EasyRecruitStrategy{}
				recruitStrategy.StrategyId = strategy.StrategyID
				recruitStrategy.FeeForm = strategy.FeeForm
				recruitStrategy.RecruitNumber = strategy.RecruitNumber
				project.RecruitStrategy = append(project.RecruitStrategy, recruitStrategy)
			}
		}

		// 2.4. 判断是否加入商单
		//sProjectCount, sProjectErr := db.UpdateSProjectByProjectIdAndSupplierId(ctx, project.LocalId, supplierId)
		//if sProjectErr != nil {
		//	return nil, sProjectErr
		//}
		//if sProjectCount > 0 {
		//	project.AddToListStatus = 1
		//} else {
		//	project.AddToListStatus = 2
		//}
	}
	return fullLocalData, nil
}
