package dao

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

func GetThingMapList(db *DaoDB, thingType int, vendorIDs, thingIDs []int, vendorOrgCodes []string) (cats []*model.ThingMap, err error) {
	sql := `
	SELECT t1.*
	FROM thing_map t1
	WHERE t1.deleted_at = ? AND t1.thing_type = ?
	`
	sqlParams := []interface{}{
		utils.DefaultTimeValue,
		thingType,
	}
	if len(thingIDs) > 0 { // 必须要指定ID
		sql += " AND t1.thing_id IN (" + GenQuestionMarks(len(thingIDs)) + ")"
		sqlParams = append(sqlParams, thingIDs)
	}
	if len(vendorOrgCodes) > 0 {
		sql += " AND t1.vendor_org_code IN (" + GenQuestionMarks(len(vendorOrgCodes)) + ")"
		sqlParams = append(sqlParams, vendorOrgCodes)
	}
	err = GetRows(db, &cats, sql, sqlParams...)
	return cats, err
}

func CreateThingMap(thingId int64, vendorThingID, appOrgCode, err string, thingType, syncStatus int8) error {
	thingMap := &model.ThingMap{
		ThingID:       thingId,
		ThingType:     thingType,
		VendorID:      model.VendorIDDD,
		VendorOrgCode: appOrgCode,
		VendorThingID: vendorThingID,
		Remark:        err,
		SyncStatus:    syncStatus,
	}
	if thingMap.VendorThingID == "" {
		return nil
	}

	WrapAddIDCULDEntity(thingMap, "jxadmin")
	return CreateEntity(GetDB(), thingMap)
}

func UpdateThingMap(db *DaoDB, syncStatus int8, vendorThingId string, thingId int, vendorId int, vendorOrgCode string) error {
	sql := `
	UPDATE thing_map t1
	SET t1.sync_status = ?,t1.vendor_thing_id = ?
	WHERE t1.deleted_at = ? AND t1.thing_id = ? AND t1.vendor_id = ? AND t1.vendor_org_code = ? 
`
	sqlParams := []interface{}{
		syncStatus,
		vendorThingId,
		utils.DefaultTimeValue,
		thingId,
		vendorId,
		vendorOrgCode,
	}
	_, err := ExecuteSQL(db, sql, sqlParams...)
	return err
}

// GetThingToTiktokMapList 抖店获取同步类型
func GetThingToTiktokMapList(db *DaoDB, vendorId int, thingId int64, vendorOrgCode string) (cats []*model.ThingMap, err error) {
	sql := `
	SELECT t1.*
	FROM thing_map t1
	WHERE t1.thing_id = ? AND t1.vendor_id = ? AND vendor_org_code = ? AND t1.deleted_at = ? 
	`
	sqlParams := []interface{}{
		thingId,
		vendorId,
		vendorOrgCode,
		utils.DefaultTimeValue,
	}

	err = GetRows(db, &cats, sql, sqlParams...)
	return cats, err
}

// DeleteThingToTiktokMapList 删除同步关联关系
func DeleteThingToTiktokMapList(vendorId int, vendorThingId string, skuId int) error {
	sql := ` DELETE FROM thing_map WHERE vendor_thing_id = ? AND vendor_id = ? AND thing_id = ? `
	param := []interface{}{vendorThingId, vendorId, skuId}
	_, err := ExecuteSQL(GetDB(), sql, param...)
	return err
}

func GetThingMapMap(db *DaoDB, thingType int, vendorIDs, thingIDs []int) (thingMapMap map[int64][]*model.ThingMap, err error) {
	thingMapList, err := GetThingMapList(db, thingType, vendorIDs, thingIDs, nil)
	if err == nil {
		thingMapMap = make(map[int64][]*model.ThingMap)
		for _, thingMap := range thingMapList {
			thingMapMap[thingMap.ThingID] = append(thingMapMap[thingMap.ThingID], thingMap)
		}
	}
	return thingMapMap, err
}

func SetThingMapSyncStatus(db *DaoDB, vendorIDs []int, vendorOrgCodes []string, thingType int, thingIDs []int, syncStatus int8) (num int64, err error) {
	sql := `
	UPDATE thing_map t1
	SET t1.sync_status = t1.sync_status | ?
	WHERE t1.deleted_at = ? AND t1.thing_type = ?
	`
	sqlParams := []interface{}{
		syncStatus,
		utils.DefaultTimeValue,
		thingType,
	}
	if len(vendorIDs) > 0 {
		sql += " AND t1.vendor_id IN (" + GenQuestionMarks(len(vendorIDs)) + ")"
		sqlParams = append(sqlParams, vendorIDs)
	}
	if len(vendorOrgCodes) > 0 {
		sql += " AND t1.vendor_org_code IN (" + GenQuestionMarks(len(vendorOrgCodes)) + ")"
		sqlParams = append(sqlParams, vendorOrgCodes)
	}
	if len(thingIDs) > 0 {
		sql += " AND t1.thing_id IN (" + GenQuestionMarks(len(thingIDs)) + ")"
		sqlParams = append(sqlParams, thingIDs)
	}
	num, err = ExecuteSQL(db, sql, sqlParams...)
	return num, err
}

func SetSkuNameSyncStatus(db *DaoDB, vendorIDs []int, vendorOrgCodes []string, nameIDs []int, syncStatus int8) (num int64, err error) {
	skuIDs, err2 := GetSkuIDByNames(db, nameIDs)
	if err = err2; err == nil {
		num, err = SetThingMapSyncStatus(db, vendorIDs, vendorOrgCodes, model.ThingTypeSku, skuIDs, syncStatus)
	}
	return num, err
}

func SetSkuSyncStatus(db *DaoDB, vendorID int, skuIDs []int, syncStatus int8) (num int64, err error) {
	num, err = SetThingMapSyncStatus(db, []int{vendorID}, nil, model.ThingTypeSku, skuIDs, syncStatus)
	return num, err
}
