package dao

import (
	"time"

	"git.rosy.net.cn/baseapi/platformapi/aliupcapi"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/model"
)

type SkuCategoryWithVendor struct {
	*model.SkuCategory
	MapList []*model.ThingMap `json:"mapList"`
}

type SkuNamePlace struct {
	model.Place
	NameID int `orm:"column(name_id)" json:"nameID"`
	SkuID  int `orm:"column(sku_id)" json:"skuID"`
}

func GetSellCities(db *DaoDB, nameID int, vendorID int) (cities []*model.Place, err error) {
	cities = []*model.Place{}
	sql := `
	SELECT DISTINCT t3.*
	FROM sku_name_place_bind t1
	JOIN place t2 ON t1.place_code = t2.code
	JOIN place t3 ON (t2.level = 2 AND t2.code = t3.code) OR (t2.level = 1 AND t2.code = t3.parent_code)
	WHERE t1.name_id = ?
	`
	if vendorID == model.VendorIDJD {
		sql += "AND t3.jd_code <> 0\n"
	}
	return cities, GetRows(db, &cities, sql, nameID)
}

func DeleteSkuNamePlace(db *DaoDB, nameID int, placeCodes []int) (num int64, err error) {
	sql := `
	DELETE
	FROM sku_name_place_bind
	WHERE name_id = ?
	`
	sqlParams := []interface{}{
		nameID,
	}
	if len(placeCodes) > 0 {
		sql += " AND place_code IN (" + GenQuestionMarks(len(placeCodes)) + ")"
		sqlParams = append(sqlParams, placeCodes)
	}
	return ExecuteSQL(db, sql, sqlParams...)
}

func GetCategoriesByName(db *DaoDB, name string, jdCategoryId string) (cats []*model.SkuCategory, err error) {
	sql := `
		SELECT t1.*
		FROM sku_category t1
		WHERE t1.deleted_at = ?`
	params := []interface{}{
		utils.DefaultTimeValue,
	}
	if name != "" {
		sql += " AND t1.name = ?"
		params = append(params, name)
	}

	if jdCategoryId != "" {
		sql += " AND t1.jd_category_id = ?"
		params = append(params, jdCategoryId)
	}
	return cats, GetRows(db, &cats, sql, params)
}

func GetCategories(db *DaoDB, parentID, level int, catIDs []int, isExd bool) (cats []*model.SkuCategory, err error) {
	sql := `
		SELECT t1.*
		FROM sku_category t1
		WHERE t1.deleted_at = ?`
	params := []interface{}{
		utils.DefaultTimeValue,
	}
	if parentID != -1 {
		sql += " AND t1.parent_id = ?"
		params = append(params, parentID)
	}
	if len(catIDs) > 0 {
		sql += " AND t1.id IN (" + GenQuestionMarks(len(catIDs)) + ")"
		params = append(params, catIDs)
	}
	if level > 0 {
		sql += " AND t1.level = ?"
		params = append(params, level)
	}
	if isExd {
		sql += " AND t1.is_exd_sync <> ?"
		params = append(params, 1)
		sql += " ORDER BY t1.level, t1.exd_seq"
	} else {
		sql += ` AND t1.is_exd_spec = 0
				 ORDER BY t1.level, t1.seq`
	}
	return cats, GetRows(db, &cats, sql, params)
}

func GetCategoriesForStore(db *DaoDB, parentID, level int, catIDs []int) (cats []*model.StoreCategoryMap, err error) {
	sql := `
		SELECT t1.*, t1.seq store_category_seq, t1.id category_id, t1.name store_category_name
		FROM sku_category t1
		WHERE t1.deleted_at = ?`
	params := []interface{}{
		utils.DefaultTimeValue,
	}
	if parentID != -1 {
		sql += " AND t1.parent_id = ?"
		params = append(params, parentID)
	}
	if len(catIDs) > 0 {
		sql += " AND t1.id IN (" + GenQuestionMarks(len(catIDs)) + ")"
		params = append(params, catIDs)
	}
	if level > 0 {
		sql += " AND t1.level = ?"
		params = append(params, level)
	}
	sql += ` AND t1.is_exd_spec = 0
				 ORDER BY t1.level, t1.seq`
	return cats, GetRows(db, &cats, sql, params)
}

func GetSkus(db *DaoDB, skuIDs, nameIDs, statuss, catIDs []int, eclpIDs []string) (skuList []*model.SkuAndName, err error) {
	sql := `
	SELECT t1.*, t2.name, t2.unit, t2.prefix, t2.is_spu, t2.ex_prefix, t2.ex_prefix_begin, t2.ex_prefix_end, t2.upc
	FROM sku t1
	JOIN sku_name t2 ON t2.id = t1.name_id AND t2.deleted_at = ?
	`
	sqlWhere := `
	WHERE t1.deleted_at = ?
	`
	sqlParams := []interface{}{
		utils.DefaultTimeValue,
		utils.DefaultTimeValue,
	}
	if len(skuIDs) > 0 {
		sqlWhere += " AND t1.id IN (" + GenQuestionMarks(len(skuIDs)) + ")"
		sqlParams = append(sqlParams, skuIDs)
	}
	if len(nameIDs) > 0 {
		sqlWhere += " AND t1.name_id IN (" + GenQuestionMarks(len(nameIDs)) + ")"
		sqlParams = append(sqlParams, nameIDs)
	}
	if len(statuss) > 0 {
		sqlWhere += " AND t1.status IN (" + GenQuestionMarks(len(statuss)) + ") AND t2.status IN (" + GenQuestionMarks(len(statuss)) + ")"
		sqlParams = append(sqlParams, statuss, statuss)
	}
	if len(catIDs) > 0 {
		sql += `
		JOIN sku_category t3 ON t3.id = t2.category_id AND t3.is_exd_spec = ?
		LEFT JOIN sku_category t3p ON t3p.id = t3.parent_id AND t3p.is_exd_spec = ?
		`
		sqlWhere += " AND (t3.id IN (" + GenQuestionMarks(len(catIDs)) + ")"
		sqlWhere += " OR t3p.id IN (" + GenQuestionMarks(len(catIDs)) + ") )"
		sqlWhere += " AND t1.exd_sku_id = ''"
		sqlParams = append(sqlParams, model.NO, model.NO, catIDs, catIDs)
	}
	if len(eclpIDs) > 0 {
		sqlWhere += " AND t1.eclp_id IN (" + GenQuestionMarks(len(eclpIDs)) + ")"
		sqlParams = append(sqlParams, eclpIDs)
	}
	sql += sqlWhere
	if err = GetRows(db, &skuList, sql, sqlParams...); err == nil {
		return skuList, nil
	}
	return nil, err
}

func UpdateSkuNameTiktokAttr(db *DaoDB, id int64, attr string) {
	sql := ` UPDATE sku_name SET tiktok_attribute = ? WHERE id = ? `
	ExecuteSQL(db, sql, []interface{}{attr, id}...)
}

func UpdateSkuNameTiktokBrandName(db *DaoDB, upc, brandName, brandId string) {
	sql := ` UPDATE sku_name SET upc_brand_name = ?,upc_tiktok_brand_id = ? WHERE upc = ? `
	ExecuteSQL(db, sql, []interface{}{brandName, brandId, upc}...)
}

func UpdateSkuNameMtAttr(db *DaoDB, id int64, attr string) {
	sql := ` UPDATE sku_name SET mt_attribute = ? WHERE id = ? `
	ExecuteSQL(db, sql, []interface{}{attr, id}...)
}

func GetSkuNames(db *DaoDB, nameIDs []int, upcs []string, name string, isExd bool) (skuNameList []*model.SkuName, err error) {
	sql := `
	SELECT t1.*
	FROM sku_name t1
	LEFT JOIN sku t2 ON t2.name_id = t1.id 
	WHERE t1.deleted_at = ?
	`
	sqlParams := []interface{}{
		utils.DefaultTimeValue,
	}
	if len(nameIDs) > 0 {
		sql += " AND t1.id IN (" + GenQuestionMarks(len(nameIDs)) + ")"
		sqlParams = append(sqlParams, nameIDs)
	}
	if len(upcs) > 0 {
		sql += " AND t1.upc IN (" + GenQuestionMarks(len(upcs)) + ")"
		sqlParams = append(sqlParams, upcs)
	}
	if name != "" {
		sql += " AND t1.name LIKE ?"
		sqlParams = append(sqlParams, "%"+name+"%")
	}
	if isExd {
		sql += " AND t2.exd_sku_id <> ''"
	}
	if err = GetRows(db, &skuNameList, sql, sqlParams...); err == nil {
		return skuNameList, nil
	}
	return nil, err
}

func GetSkuIDByNames(db *DaoDB, nameIDs []int) (skuIDs []int, err error) {
	skuList, err := GetSkus(db, nil, nameIDs, nil, nil, nil)
	if err == nil {
		for _, sku := range skuList {
			skuIDs = append(skuIDs, sku.ID)
		}
	}
	return skuIDs, err
}

func GetSkuByCats(db *DaoDB, catIDs []int) (skuList []*model.Sku, err error) {
	skuExList, err := GetSkus(db, nil, nil, nil, catIDs, nil)
	if err == nil {
		for _, v := range skuExList {
			skuList = append(skuList, &v.Sku)
		}
	}
	return skuList, err
}

// todo, GetSkuCategoryWithVendor与GetSkusWithVendor，
// 如果mustDirty为true，应该是要thing_map为基表，LEFT JOIN原始实体表，否则当原始实体记录在未同步前被物理删除后，无法真正同步

// 多门店平台使用，当前只有京东
func GetSkuCategoryWithVendor(db *DaoDB, vendorIDs []int, appOrgCodes []string, parentCatID int, catIDs []int, mustDirty bool) (catList []*SkuStoreCatInfo, err error) {
	sqlParams := []interface{}{}
	sql := `
	SELECT
	t1m.vendor_id, t1m.vendor_org_code,

	t1m.id map_id,
	t1.*,
	t1m.vendor_thing_id vendor_cat_id,
	t1m.sync_status cat_sync_status,
	a.is_jx_cat,
	b.vendor_category_name,
	b.vendor_category_seq,
	b.id vendor_category_id,
	t1pm.id parent_map_id,
	t1p.name parent_cat_name,
	t1pm.vendor_thing_id parent_vendor_cat_id,
	t1pm.sync_status parent_cat_sync_status
	FROM sku_category t1
	LEFT JOIN thing_map t1m ON t1m.thing_id = t1.id AND t1m.thing_type = ? AND t1m.deleted_at = ?
	`
	sqlParams = append(sqlParams,
		model.ThingTypeCategory,
		utils.DefaultTimeValue)
	if len(vendorIDs) > 0 {
		sql += " AND t1m.vendor_id IN (" + GenQuestionMarks(len(vendorIDs)) + ")"
		sqlParams = append(sqlParams, vendorIDs)
	}
	if len(appOrgCodes) > 0 {
		sql += " AND t1m.vendor_org_code IN (" + GenQuestionMarks(len(appOrgCodes)) + ")"
		sqlParams = append(sqlParams, appOrgCodes)
	}
	sql += `
	LEFT JOIN vendor_org_code a ON a.vendor_org_code = t1m.vendor_org_code AND a.vendor_id = t1m.vendor_id AND a.deleted_at = ?
	LEFT JOIN vendor_category_map b ON b.vendor_org_code = t1m.vendor_org_code AND b.vendor_id = t1m.vendor_id AND b.category_id = t1m.thing_id AND b.deleted_at = ?
	`
	sqlParams = append(sqlParams,
		utils.DefaultTimeValue,
		utils.DefaultTimeValue,
	)
	sql += `
	LEFT JOIN sku_category t1p ON t1p.id = t1.parent_id
	LEFT JOIN thing_map t1pm ON t1pm.thing_id = t1p.id AND t1pm.thing_type = ? AND t1m.deleted_at = ?
		AND t1pm.vendor_id = t1m.vendor_id AND t1pm.vendor_org_code = t1m.vendor_org_code
	LEFT JOIN vendor_category_map c ON c.vendor_org_code = t1pm.vendor_org_code AND c.vendor_id = t1pm.vendor_id AND c.category_id = t1pm.thing_id AND c.deleted_at = ?
	WHERE 1 = 1
	AND t1.is_sync <> ?
	`
	sqlParams = append(sqlParams, model.ThingTypeCategory, utils.DefaultTimeValue, utils.DefaultTimeValue, model.YES)
	if mustDirty {
		sql += " AND t1m.sync_status IS NOT NULL AND t1m.sync_status <> 0"
	} else {
		sql += " AND t1.deleted_at = ?"
		sqlParams = append(sqlParams, utils.DefaultTimeValue)
	}
	if len(catIDs) > 0 {
		sql += " AND t1.id IN (" + GenQuestionMarks(len(catIDs)) + ")"
		sqlParams = append(sqlParams, catIDs)
	}
	if parentCatID >= 0 {
		sql += " AND t1.parent_id = ?"
		sqlParams = append(sqlParams, parentCatID)
	}
	sql += " ORDER BY t1.seq"
	err = GetRows(db, &catList, sql, sqlParams...)
	return catList, err
}

// 多门店平台使用，当前只有京东
func GetSkusWithVendor(db *DaoDB, vendorIDs []int, appOrgCodes []string, nameIDs, skuIDs []int, mustDirty bool) (skuList []*StoreSkuSyncInfo, err error) {
	sql := `
	SELECT
	t1m.vendor_id, t1m.vendor_org_code,

	t1m.id bind_id,
	t1.*,
	t1.id sku_id,
	t1m.vendor_thing_id vendor_sku_id,
	t1m.sync_status sku_sync_status,

	t2.price,
	t2.price unit_price,
	t2.prefix,
	t2.name,
	t2.unit,
	t2.upc,
	t2.is_global,
	t2.status name_status,
	IF(t11.resource_type IS NULL OR t11.resource_type <> ?, t2.img, '') img,
	IF(t12.resource_type IS NULL OR t12.resource_type <> ?, t2.img2, '') img2,
	t2.desc_img,
	t2.category_id local_category_id,
	t3.jd_category_id vendor_vendor_cat_id,
	tsu.ex_prefix, tsu.begin_at ex_prefix_begin, tsu.end_at ex_prefix_end, tsu.img_watermark,
	t3m.sync_status cat_sync_status,
	t3m.vendor_thing_id vendor_cat_id,
	tsy.vendor_category_id sku_vendor_map_cat_id

	FROM sku t1
	LEFT JOIN thing_map t1m ON t1m.thing_id = t1.id AND t1m.thing_type = ? AND t1m.deleted_at = ?`
	sqlParams := []interface{}{
		model.MimeTypeGif,
		model.MimeTypeGif,
		model.ThingTypeSku,
		utils.DefaultTimeValue,
	}

	if len(vendorIDs) > 0 {
		sql += " AND t1m.vendor_id IN (" + GenQuestionMarks(len(vendorIDs)) + ")"
		sqlParams = append(sqlParams, vendorIDs)
	}
	if len(appOrgCodes) > 0 {
		sql += " AND t1m.vendor_org_code IN (" + GenQuestionMarks(len(appOrgCodes)) + ")"
		sqlParams = append(sqlParams, appOrgCodes)
	}
	sql += `
	JOIN sku_name t2 ON t2.id = t1.name_id
	LEFT JOIN sku_category t3 ON t3.id = t2.category_id
	LEFT JOIN thing_map t3m ON t3m.thing_id = t3.id AND t3m.thing_type = ? AND t3m.deleted_at = ?
		AND t3m.vendor_id = t1m.vendor_id AND t3m.vendor_org_code = t1m.vendor_org_code
	LEFT JOIN data_resource t11 ON t11.main_url = t2.img
	LEFT JOIN data_resource t12 ON t12.main_url = t2.img2
	LEFT JOIN sku_exinfo_map tsu ON tsu.name_id = t2.id AND tsu.deleted_at = ? AND tsu.vendor_id = t1m.vendor_id
	LEFT JOIN sku_vendor_category_map tsy ON tsy.name_id = t2.id AND tsy.vendor_id = ? AND tsy.deleted_at = ?
	WHERE 1 = 1
	`
	sqlParams = append(sqlParams, model.ThingTypeCategory, utils.DefaultTimeValue, utils.DefaultTimeValue, model.VendorIDJD, utils.DefaultTimeValue)
	if mustDirty {
		sql += " AND t1m.sync_status IS NOT NULL AND t1m.sync_status <> 0"
	} else {
		sql += " AND t1.deleted_at = ?"
		sqlParams = append(sqlParams, utils.DefaultTimeValue)
	}
	if len(nameIDs) > 0 {
		sql += " AND t1.name_id IN (" + GenQuestionMarks(len(nameIDs)) + ")"
		sqlParams = append(sqlParams, nameIDs)
	}
	if len(skuIDs) > 0 {
		sql += " AND t1.id IN (" + GenQuestionMarks(len(skuIDs)) + ")"
		sqlParams = append(sqlParams, skuIDs)
	}
	sql += " ORDER BY t1.seq"
	if err = GetRows(db, &skuList, sql, sqlParams...); err == nil {
		skuPlaceList, err2 := GetSkuNamePlaces(db, nameIDs, skuIDs)
		if err = err2; err == nil {
			skuPlaceMap := make(map[int][]*SkuNamePlace)
			for _, v := range skuPlaceList {
				skuPlaceMap[v.SkuID] = append(skuPlaceMap[v.SkuID], v)
			}
			for _, v := range skuList {
				if v.IsGlobal == 0 {
					for _, v2 := range skuPlaceMap[v.SkuID] {
						// 京东到家
						if v2.JdCode > 0 {
							v.SellCities = append(v.SellCities, utils.Int2Str(v2.JdCode))
						}
					}
				}
			}
		}
	}
	return skuList, err
}

func GetSkuNamePlaces(db *DaoDB, nameIDs, skuIDs []int) (skuPlaceList []*SkuNamePlace, err error) {
	sql := `
	SELECT
	t4.*,
	t2.id sku_id, t2.name_id
	FROM sku t2
	JOIN sku_name_place_bind t3 ON t3.name_id = t2.name_id
	JOIN place t4 ON t4.code = t3.place_code
	WHERE t2.deleted_at = ?
	`
	sqlParams := []interface{}{utils.DefaultTimeValue}
	if len(nameIDs) > 0 {
		sql += " AND t2.name_id IN (" + GenQuestionMarks(len(nameIDs)) + ")"
		sqlParams = append(sqlParams, nameIDs)
	}
	if len(skuIDs) > 0 {
		sql += " AND t2.id IN (" + GenQuestionMarks(len(skuIDs)) + ")"
		sqlParams = append(sqlParams, skuIDs)
	}
	err = GetRows(db, &skuPlaceList, sql, sqlParams...)
	return skuPlaceList, err
}

func GetUpcDepot(db *DaoDB, upc string) (result *aliupcapi.GetAliUpcInfoResult, err error) {
	sql := `
		SELECT * FROM upc_depot WHERE code = ?
	`
	sqlParams := []interface{}{upc}
	err = GetRow(db, &result, sql, sqlParams...)
	return result, err
}

func InsertUpcDepot(db *DaoDB, result *aliupcapi.GetAliUpcInfoResult) (err error) {
	sql := `
		INSERT INTO upc_depot (code, goods_name, manu_name, manu_address, spec, price, img, goods_type, ycg, trade_mark, remark)
		VALUES (?,?,?,?,?,?,?,?,?,?,?)
	`
	sqlParams := []interface{}{
		result.Code,
		result.GoodsName,
		result.ManuName,
		result.ManuAddress,
		result.Spec,
		result.Price,
		result.Img,
		result.GoodsType,
		result.Ycg,
		result.Trademark,
		result.Remark,
	}
	_, err = ExecuteSQL(db, sql, sqlParams)
	return err
}

func GetSkuExinfos(db *DaoDB, nameIDs []int, vendorIDs []int, exPrefix string, fromTime, toTime time.Time) (skuExinfoMaps []*model.SkuExinfoMap, err error) {
	sql := `
	SELECT * 
	  FROM sku_exinfo_map
	 WHERE deleted_at = ?
	`
	sqlParams := []interface{}{
		utils.DefaultTimeValue,
	}
	if len(nameIDs) > 0 {
		sql += " AND name_id IN (" + GenQuestionMarks(len(nameIDs)) + ")"
		sqlParams = append(sqlParams, nameIDs)
	}
	if len(vendorIDs) > 0 {
		sql += " AND vendor_id IN (" + GenQuestionMarks(len(vendorIDs)) + ")"
		sqlParams = append(sqlParams, vendorIDs)
	}
	if exPrefix != "" {
		sql += " AND ex_prefix LIKE ?"
		sqlParams = append(sqlParams, "%"+exPrefix+"%")
	}
	if fromTime != utils.ZeroTimeValue && toTime != utils.ZeroTimeValue {
		sql += " AND NOT (begin_at > ? OR end_at < ?)"
		sqlParams = append(sqlParams, toTime, fromTime)
	}
	if err = GetRows(db, &skuExinfoMaps, sql, sqlParams...); err == nil {
		return skuExinfoMaps, nil
	}
	return nil, err
}

func GetSkuVendorCategoryMaps(db *DaoDB, nameIDs, vendorIDs []int, vendorCategoryIDs []string) (skuVendorCategoryMaps []*model.SkuVendorCategoryMap, err error) {
	sql := `
	SELECT * 
	  FROM sku_vendor_category_map
	 WHERE deleted_at = ?
	`
	sqlParams := []interface{}{
		utils.DefaultTimeValue,
	}
	if len(nameIDs) > 0 {
		sql += " AND name_id IN (" + GenQuestionMarks(len(nameIDs)) + ")"
		sqlParams = append(sqlParams, nameIDs)
	}
	if len(vendorIDs) > 0 {
		sql += " AND vendor_id IN (" + GenQuestionMarks(len(vendorIDs)) + ")"
		sqlParams = append(sqlParams, vendorIDs)
	}
	if len(vendorCategoryIDs) > 0 {
		sql += " AND vendor_category_id IN (" + GenQuestionMarks(len(vendorCategoryIDs)) + ")"
		sqlParams = append(sqlParams, vendorCategoryIDs)
	}
	if err = GetRows(db, &skuVendorCategoryMaps, sql, sqlParams...); err == nil {
		return skuVendorCategoryMaps, nil
	}
	return nil, err
}

func GetVendorCategoryMap(db *DaoDB, parentID, level, vendorID int, vendorOrgCode string, categoryID int) (vendorMaps []*model.VendorCategoryMap, err error) {
	sql := `
		SELECT a.* 
		FROM vendor_category_map a
		WHERE a.deleted_at = ?
	`
	sqlParams := []interface{}{
		utils.DefaultTimeValue,
	}
	if parentID >= 0 {
		sql += " AND a.parent_id = ?"
		sqlParams = append(sqlParams, parentID)
	}
	if level > 0 {
		sql += " AND a.level = ?"
		sqlParams = append(sqlParams, level)
	}
	if vendorID != -1 {
		sql += " AND a.vendor_id = ?"
		sqlParams = append(sqlParams, vendorID)
	}
	if vendorOrgCode != "" {
		sql += " AND a.vendor_org_code = ?"
		sqlParams = append(sqlParams, vendorOrgCode)
	}
	if categoryID > 0 {
		sql += " AND a.category_id = ?"
		sqlParams = append(sqlParams, categoryID)
	}
	sql += " ORDER BY a.level, a.vendor_category_seq"
	err = GetRows(db, &vendorMaps, sql, sqlParams)
	if err != nil {
		return nil, err
	}
	return vendorMaps, err
}

type GetVendorCategoryMapExtResult struct {
	model.VendorCategoryMap
	VendorThingID string `orm:"column(vendor_thing_id)" json:"vendorThingID"`
}

func GetVendorCategoryMapExt(db *DaoDB, parentID, level, vendorID int, vendorOrgCode string, categoryID int) (vendorMaps []*GetVendorCategoryMapExtResult, err error) {
	sql := `
		SELECT a.*, b.vendor_thing_id 
		FROM vendor_category_map a
		JOIN thing_map b ON b.thing_id = a.category_id AND b.vendor_org_code = a.vendor_org_code AND b.vendor_id = a.vendor_id AND b.thing_type = ?
		WHERE a.deleted_at = ?
	`
	sqlParams := []interface{}{
		model.ThingTypeCategory,
		utils.DefaultTimeValue,
	}
	if parentID >= 0 {
		sql += " AND a.parent_id = ?"
		sqlParams = append(sqlParams, parentID)
	}
	if level > 0 {
		sql += " AND a.level = ?"
		sqlParams = append(sqlParams, level)
	}
	if vendorID != -1 {
		sql += " AND a.vendor_id = ?"
		sqlParams = append(sqlParams, vendorID)
	}
	if vendorOrgCode != "" {
		sql += " AND a.vendor_org_code = ?"
		sqlParams = append(sqlParams, vendorOrgCode)
	}
	if categoryID > 0 {
		sql += " AND a.category_id = ?"
		sqlParams = append(sqlParams, categoryID)
	}
	sql += " ORDER BY a.vendor_category_seq"
	err = GetRows(db, &vendorMaps, sql, sqlParams)
	if err != nil {
		return nil, err
	}
	return vendorMaps, err
}

func GetMtJdCategoryMap(db *DaoDB, mtID, jdID string) (cats []*model.MtJdCategoryMap, err error) {
	sql := `
		SELECT a.*
		FROM mt_jd_category_map a
		WHERE 1 = 1
	`
	sqlParams := []interface{}{}
	if mtID != "" {
		sql += " AND a.mt_id = ?"
		sqlParams = append(sqlParams, mtID)
	}
	if jdID != "" {
		sql += " AND a.jd_id = ?"
		sqlParams = append(sqlParams, jdID)
	}
	err = GetRows(db, &cats, sql, sqlParams)
	if err != nil {
		return nil, err
	}
	return cats, err
}

type GetVendorCategoriesWithMapResult struct {
	model.SkuVendorCategory

	CatMapID string `orm:"column(cat_map_id)" json:"catMapID"`
}

func GetVendorCategoriesWithMap(db *DaoDB, vendorID, vendorCategoryID int) (results []*GetVendorCategoriesWithMapResult, err error) {
	sql := `
		SELECT a.*, b.jd_id cat_map_id
		  FROM sku_vendor_category a
	 LEFT JOIN mt_jd_category_map b ON a.vendor_category_id = b.mt_id
         WHERE vendor_id = ?
	`
	sqlParams := []interface{}{vendorID}
	if vendorCategoryID != 0 {
		sql += " AND a.vendor_category_id = ?"
		sqlParams = append(sqlParams, vendorCategoryID)
	}
	GetRows(db, &results, sql, sqlParams)
	return results, err
}
