package service

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

type BillService struct{}

// 电商带货账单支付
func (s BillService) PaySelection(param *vo.PayParam) error {
	selectionId := param.ObjectId
	selectionInfo, err1 := dao.SelectionInfoDAO{}.GetSelectionInfoById(selectionId)
	if err1 != nil {
		return err1
	}
	selectionStatus := selectionInfo.SelectionStatus
	if selectionStatus != 4 {
		return errors.New("状态异常")
	}
	_, err2 := dao.EnterpriseDao{}.UpdateEnterpriseBalanceAndFrozen(selectionInfo.EnterpriseID, selectionInfo.EstimatedCost)
	if err2 != nil {
		return err2
	}
	err3 := dao.SelectionInfoDAO{}.UpdateSelectionInfo(entity.SelectionInfo{SelectionID: selectionId, SelectionStatus: 6, PayAt: time.Now()})
	if err3 != nil {
		return err3
	}

	return nil
}

// 品牌种草账单支付
func (s BillService) PayProject(param *vo.PayParam) error {
	projectId := param.ObjectId
	projectInfo, err1 := dao.ProjectDAO{}.GetProjectById(projectId)
	if err1 != nil {
		return err1
	}
	projectStatus := projectInfo.ProjectStatus
	if projectStatus != 6 {
		return errors.New("状态异常")
	}
	_, err2 := dao.EnterpriseDao{}.UpdateEnterpriseBalanceAndFrozen(projectInfo.EnterpriseID, projectInfo.NeedPay)
	if err2 != nil {
		return err2
	}
	err3 := dao.ProjectDAO{}.UpdateProject(entity.Project{ProjectId: projectId, ProjectStatus: 8, PayAt: time.Now()})
	if err3 != nil {
		return err3
	}

	return nil
}

// 本地生活账单支付
func (s BillService) PayLocalLife(param *vo.PayParam) error {
	localId := param.ObjectId
	localInfo, err1 := dao.LocalLifeDao{}.GetLocalById(localId)
	if err1 != nil {
		return err1
	}
	localStatus := localInfo.TaskStatus
	if localStatus != 6 {
		return errors.New("状态异常")
	}
	_, err2 := dao.EnterpriseDao{}.UpdateEnterpriseBalanceAndFrozen(localInfo.EnterpriseID, localInfo.NeedPay)
	if err2 != nil {
		return err2
	}
	err3 := dao.LocalLifeDao{}.UpdateLocal(entity.LocalLifeInfo{LocalID: localId, TaskStatus: 8, PayAt: time.Now()})
	if err3 != nil {
		return err3
	}

	return nil
}

// 电商带货账单列表
func (s BillService) GetBillSelectionTaskList(param *vo.SelectionSearchParam) (vo.ResultVO, error) {
	if param.Page == 0 {
		param.Page = 1
	}
	if param.PageSize == 0 {
		param.PageSize = 10
	}
	var result vo.ResultVO
	reBillSelectionTaskPreviews, total, err := (&dao.SelectionInfoDAO{}).GetBillSelectionPreviews(param)
	if err != nil {
		return result, err
	}
	for i := range reBillSelectionTaskPreviews {
		var creatorName string
		var productName string
		var productPrice float64
		var mainImage string
		var reward float64
		if reBillSelectionTaskPreviews[i].SubAccountId == 0 {
			enterprise, err := dao.EnterpriseDao{}.GetEnterprise(reBillSelectionTaskPreviews[i].EnterpriseId)
			if err == nil && enterprise != nil {
				creatorName = enterprise.BusinessName
			}
		} else {
			subAccount, err := dao.SubAccountDao{}.GetSubAccount(reBillSelectionTaskPreviews[i].SubAccountId)
			if err == nil && subAccount != nil {
				creatorName = subAccount.SubAccountName
			}
		}
		product, err := dao.ProductDAO{}.GetProductByID(reBillSelectionTaskPreviews[i].ProductId)
		if err == nil && product != nil {
			productName = product.ProductName
			productPrice = product.ProductPrice
		}
		mainImage, err = dao.ProductPhotoDAO{}.GetMainPhotoByProductID(reBillSelectionTaskPreviews[i].ProductId)
		rewardStrategys, err := dao.RewardStrategyDao{}.GetRewardStrategyBySelectionId(reBillSelectionTaskPreviews[i].SelectionId)
		for _, rewardStrategy := range rewardStrategys {
			reward += rewardStrategy.Reward
		}
		reBillSelectionTaskPreviews[i].CreatorName = creatorName
		reBillSelectionTaskPreviews[i].ProductName = productName
		reBillSelectionTaskPreviews[i].ProductPrice = productPrice
		reBillSelectionTaskPreviews[i].MainImage = mainImage
		reBillSelectionTaskPreviews[i].Reward = reward
	}
	result = vo.ResultVO{
		Page:     param.Page,
		PageSize: param.PageSize,
		Total:    total,
		Data:     reBillSelectionTaskPreviews,
	}
	return result, nil
}

// 品牌种草账单列表
func (s BillService) GetBillProjectTaskList(param *vo.ProjectSearchParam) (vo.ResultVO, error) {
	if param.Page == 0 {
		param.Page = 1
	}
	if param.PageSize == 0 {
		param.PageSize = 10
	}
	var result vo.ResultVO
	reBillProjectTaskPreviews, total, err := (&dao.ProjectDAO{}).GetBillProjectPreviews(param)
	if err != nil {
		return result, err
	}
	for i := range reBillProjectTaskPreviews {
		var creatorName string
		var productName string
		var productPrice float64
		var mainImage string
		if reBillProjectTaskPreviews[i].SubAccountId == 0 {
			enterprise, err := dao.EnterpriseDao{}.GetEnterprise(reBillProjectTaskPreviews[i].EnterpriseId)
			if err == nil && enterprise != nil {
				creatorName = enterprise.BusinessName
			}
		} else {
			subAccount, err := dao.SubAccountDao{}.GetSubAccount(reBillProjectTaskPreviews[i].SubAccountId)
			if err == nil && subAccount != nil {
				creatorName = subAccount.SubAccountName
			}
		}
		product, err := dao.ProductDAO{}.GetProductByID(reBillProjectTaskPreviews[i].ProductId)
		if err == nil && product != nil {
			productName = product.ProductName
			productPrice = product.ProductPrice
		}
		mainImage, err = dao.ProductPhotoDAO{}.GetMainPhotoByProductID(reBillProjectTaskPreviews[i].ProductId)
		reBillProjectTaskPreviews[i].CreatorName = creatorName
		reBillProjectTaskPreviews[i].ProductName = productName
		reBillProjectTaskPreviews[i].ProductPrice = productPrice
		reBillProjectTaskPreviews[i].MainImage = mainImage
	}
	result = vo.ResultVO{
		Page:     param.Page,
		PageSize: param.PageSize,
		Total:    total,
		Data:     reBillProjectTaskPreviews,
	}
	return result, nil
}

// 本地生活账单列表
func (s BillService) GetBillLocalLifeTaskList(param *vo.LocalSearchParam) (vo.ResultVO, error) {
	if param.Page == 0 {
		param.Page = 1
	}
	if param.PageSize == 0 {
		param.PageSize = 10
	}
	var result vo.ResultVO
	reBillLocalTaskPreviews, total, err := (&dao.LocalLifeDao{}).GetBillLocalPreviews(param)
	if err != nil {
		return result, err
	}
	for i := range reBillLocalTaskPreviews {
		var creatorName string
		var storeName string
		var storeLocation string
		var mainImage string
		if reBillLocalTaskPreviews[i].SubAccountId == 0 {
			enterprise, err := dao.EnterpriseDao{}.GetEnterprise(reBillLocalTaskPreviews[i].EnterpriseId)
			if err == nil && enterprise != nil {
				creatorName = enterprise.BusinessName
			}
		} else {
			subAccount, err := dao.SubAccountDao{}.GetSubAccount(reBillLocalTaskPreviews[i].SubAccountId)
			if err == nil && subAccount != nil {
				creatorName = subAccount.SubAccountName
			}
		}
		store, err := dao.StoreDao{}.GetStoreByID(reBillLocalTaskPreviews[i].StoreId)
		if err == nil && store != nil {
			storeName = store.StoreName
			storeLocation = store.StoreLocation
		}
		mainImage, err = dao.ProductPhotoDAO{}.GetMainPhotoByStoreID(reBillLocalTaskPreviews[i].StoreId)
		reBillLocalTaskPreviews[i].CreatorName = creatorName
		reBillLocalTaskPreviews[i].StoreName = storeName
		reBillLocalTaskPreviews[i].StoreLocation = storeLocation
		reBillLocalTaskPreviews[i].MainImage = mainImage
	}
	result = vo.ResultVO{
		Page:     param.Page,
		PageSize: param.PageSize,
		Total:    total,
		Data:     reBillLocalTaskPreviews,
	}
	return result, nil
}
