package dao

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

type PageShopWithPlaceName struct {
	model.PageShop

	CityName     string `json:"cityName"`
	DistrictName string `json:"districtName"`
	Distance     int    `json:"distance"`
	BrandName    string `json:"brandName"`
	Category     string `json:"category"`
	ShippingTime string `json:"shippingTime"`
}

func QueryPageStores(db *DaoDB, pageSize, offset int, keyword string, vendorStoreID string, vendorID int, orgCode string,
	cityCode, districtCode int, tel string, minShopScore float32, minRecentOrderNum, minSkuCount int,
	lng1, lat1, lng2, lat2 float64) (pagedInfo *model.PagedInfo, err error) {
	sql := `
	SELECT SQL_CALC_FOUND_ROWS
	t1.*,
	t2.name city_name, t3.name district_name
	FROM page_shop t1
	LEFT JOIN place t2 ON t2.code = t1.city_code
	LEFT JOIN place t3 ON t3.code = t1.district_code
	WHERE 1 = 1
	`
	sqlParams := []interface{}{}

	if vendorStoreID != "" {
		sql += " AND t1.vendor_store_id = ?"
		sqlParams = append(sqlParams, vendorStoreID)
	}
	if vendorID != -1 {
		sql += " AND t1.vendor_id = ?"
		sqlParams = append(sqlParams, vendorID)
	}
	if orgCode != "" {
		sql += " AND t1.org_code = ?"
		sqlParams = append(sqlParams, orgCode)
	}
	if cityCode != 0 {
		sql += " AND t1.city_code = ?"
		sqlParams = append(sqlParams, cityCode)
	}
	if districtCode != 0 {
		sql += " AND t1.district_code = ?"
		sqlParams = append(sqlParams, districtCode)
	}
	if tel != "" {
		sql += " AND t1.tel1 = ?"
		sqlParams = append(sqlParams, tel)
	}
	if minShopScore > 0 {
		sql += " AND t1.shop_score >= ?"
		sqlParams = append(sqlParams, minShopScore)
	}
	if minRecentOrderNum > 0 {
		sql += " AND t1.recent_order_num >= ?"
		sqlParams = append(sqlParams, minRecentOrderNum)
	}
	if minSkuCount > 0 {
		sql += " AND t1.sku_count >= ?"
		sqlParams = append(sqlParams, minSkuCount)
	}
	if lng1 > 0 {
		sql += " AND t1.lng >= ? AND t1.lat >= ? AND t1.lng <= ? AND t1.lat <= ?"
		sqlParams = append(sqlParams, lng1, lat1, lng2, lat2)
	}
	if keyword != "" {
		keywordLike := "%" + keyword + "%"
		sql += " AND (t1.name LIKE ? OR t1.tel1 LIKE ? OR t1.tel2 LIKE ? OR t1.org_code LIKE ? OR t1.address LIKE ? OR t2.name LIKE ? OR t3.name LIKE ? OR t1.licence_code LIKE ?"
		sqlParams = append(sqlParams, keywordLike, keywordLike, keywordLike, keywordLike, keywordLike, keywordLike, keywordLike, keywordLike)
		sql += ")"
	}
	sql += `
	ORDER BY t1.recent_order_num DESC
	LIMIT ? OFFSET ?
	`
	pageSize = jxutils.FormalizePageSize(pageSize)
	offset = jxutils.FormalizePageOffset(offset)
	sqlParams = append(sqlParams, pageSize, offset)
	var shopList []*PageShopWithPlaceName
	txDB, _ := Begin(db)
	defer func() {
		if r := recover(); r != nil {
			Rollback(db, txDB)
			panic(r)
		}
	}()
	if err = GetRowsTx(txDB, &shopList, sql, sqlParams...); err == nil {
		pagedInfo = &model.PagedInfo{
			TotalCount: GetLastTotalRowCount2(db, txDB),
			Data:       shopList,
		}
		Commit(db, txDB)
	} else {
		Rollback(db, txDB)
	}
	return pagedInfo, err
}
