package dao

import (
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxutils"
	"git.rosy.net.cn/jx-callback/business/model"
)

type FoodRecipeWithAction struct {
	model.FoodRecipe
	AuthorName string `json:"authorName"`
	ActionType int8   `json:"actionType"`
}

type FoodRecipeItemChoiceExt struct {
	model.FoodRecipeItemChoice

	Img     string `orm:"size(255)" json:"img"`
	SkuName string `json:"skuName"`

	Prefix      string  `json:"-"`
	SkuNameName string  `orm:"column(sku_name_name)" json:"-"`
	Unit        string  `orm:"size(8)" json:"-"`
	SpecQuality float32 `json:"-"`
	SpecUnit    string  `json:"-"`
	Comment     string  `json:"-"`
}

func QueryFoodRecipes(db *DaoDB, keyword string, recipeID int, authorID, userID string, skuIDs []int, offset, pageSize int) (recipeList []*FoodRecipeWithAction, totalCount int, err error) {
	var sql string
	var sqlParams []interface{}
	if userID != "" {
		sql = `
		SELECT SQL_CALC_FOUND_ROWS
		t1.*, t2.action_type, t3.name author_name
		FROM food_recipe t1
		LEFT JOIN food_recipe_user t2 ON t2.recipe_id = t1.id AND t2.user_id = ? AND t2.deleted_at = ?
		LEFT JOIN user t3 ON t3.user_id = t1.author_id
		WHERE t1.deleted_at = ?`
		sqlParams = []interface{}{
			userID,
			utils.DefaultTimeValue,
			utils.DefaultTimeValue,
		}
	} else {
		sql = `
		SELECT SQL_CALC_FOUND_ROWS t1.*
		FROM food_recipe t1
		WHERE t1.deleted_at = ?`
		sqlParams = []interface{}{
			utils.DefaultTimeValue,
		}
	}
	if keyword != "" {
		keywordLike := "%" + keyword + "%"
		sql += " AND (t1.name LIKE ?"
		sqlParams = append(sqlParams, keywordLike)
		sql += ")"
	}
	if recipeID > 0 {
		sql += " AND t1.id = ?"
		sqlParams = append(sqlParams, recipeID)
	}
	if authorID != "" {
		sql += " AND t1.author_id = ?"
		sqlParams = append(sqlParams, authorID)
	}
	if len(skuIDs) > 0 {
		sql += ` AND (
			SELECT COUNT(*)
			FROM food_recipe_item_choice t11
			WHERE t11.recipe_id = t1.id AND t11.sku_id IN (` + GenQuestionMarks(len(skuIDs)) + `)
		) > 0`
		sqlParams = append(sqlParams, skuIDs)
	}
	offset = jxutils.FormalizePageOffset(offset)
	pageSize = jxutils.FormalizePageSize(pageSize)
	sql += `
	ORDER BY t1.created_at DESC
	LIMIT ? OFFSET ?`
	sqlParams = append(sqlParams, pageSize, offset)

	txDB, _ := Begin(db)
	defer Commit(db, txDB)
	if err = GetRowsTx(txDB, &recipeList, sql, sqlParams...); err == nil {
		totalCount = GetLastTotalRowCount2(db, txDB)
	}
	return recipeList, totalCount, err
}

func GetRecommendFoodRecipes(db *DaoDB, keyword, userID string, offset, pageSize int) (recipeList []*model.FoodRecipe, totalCount int, err error) {
	list, totalCount, err := QueryFoodRecipes(db, keyword, 0, userID, "", nil, offset, pageSize)
	if err == nil {
		recipeList = FoodRecipeWithActionList2Recipe(list)
	}
	return recipeList, totalCount, err
}

func FoodRecipeWithActionList2Recipe(recipeList []*FoodRecipeWithAction) (outRecipeList []*model.FoodRecipe) {
	for _, v := range recipeList {
		outRecipeList = append(outRecipeList, &v.FoodRecipe)
	}
	return outRecipeList
}

func QueryFoodRecipesItems(db *DaoDB, recipeID int) (recipeItemList []*model.FoodRecipeItem, err error) {
	sql := `
	SELECT t1.*
	FROM food_recipe_item t1
	WHERE t1.deleted_at = ? AND t1.recipe_id = ?`
	sqlParams := []interface{}{
		utils.DefaultTimeValue,
		recipeID,
	}
	sql += " ORDER BY t1.index"
	err = GetRows(db, &recipeItemList, sql, sqlParams...)
	return recipeItemList, err
}

func QueryFoodRecipesItemChoices(db *DaoDB, recipeID int) (recipeItemChoiceList []*FoodRecipeItemChoiceExt, err error) {
	sql := `
	SELECT t1.*,
	t2.spec_quality, t2.spec_unit, t2.comment,
	t3.img, t3.prefix, t3.name sku_name_name, t3.unit
	FROM food_recipe_item_choice t1
	LEFT JOIN sku t2 ON t2.id = t1.sku_id
	LEFT JOIN sku_name t3 ON t3.id = t2.name_id
	WHERE t1.deleted_at = ? AND t1.recipe_id = ?`
	sqlParams := []interface{}{
		utils.DefaultTimeValue,
		recipeID,
	}
	sql += " ORDER BY t1.index, t1.choice_index"
	err = GetRows(db, &recipeItemChoiceList, sql, sqlParams...)
	return recipeItemChoiceList, err
}

func QueryFoodRecipesSteps(db *DaoDB, recipeID int) (recipeStepList []*model.FoodRecipeStep, err error) {
	sql := `
	SELECT t1.*
	FROM food_recipe_step t1
	WHERE t1.deleted_at = ? AND t1.recipe_id = ?`
	sqlParams := []interface{}{
		utils.DefaultTimeValue,
		recipeID,
	}
	sql += " ORDER BY t1.index"
	err = GetRows(db, &recipeStepList, sql, sqlParams...)
	return recipeStepList, err
}
