package dao

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

func GetDataResource(db *DaoDB, hashCode, fullURL string) (dataRes *model.DataResource, err error) {
	sql := `
	SELECT t1.*
	FROM data_resource t1
	WHERE 0 = 1`
	sqlParams := []interface{}{}
	if hashCode != "" {
		sql += " OR t1.hash_code = ?"
		sqlParams = append(sqlParams, hashCode)
	}
	if fullURL != "" {
		sql += " OR t1.main_url = ? OR t1.qiniu_url = ? OR t1.ebai_url = ? OR t1.mtwm_url = ?"
		sqlParams = append(sqlParams, fullURL, fullURL, fullURL, fullURL)
	}
	err = GetRow(db, &dataRes, sql, sqlParams...)
	return dataRes, err
}

func GetNeedUploadDataResource(db *DaoDB) (dataResList []*model.DataResource, err error) {
	sql := `
	SELECT t1.*
	FROM data_resource t1
	WHERE t1.use_type <> 0 AND (t1.ebai_url = '' OR t1.mtwm_url = '')`
	err = GetRows(db, &dataResList, sql)
	return dataResList, err
}

func GetVendorOrgCode(db *DaoDB, vendorID int, vendorOrgCode, vendorType string) (vendorOrges []*model.VendorOrgCode, err error) {
	sql := ` 
		SELECT *
		  FROM vendor_org_code  
		 WHERE deleted_at = ? 
	`
	sqlParams := []interface{}{utils.DefaultTimeValue}
	if vendorID != -1 {
		sql += " AND vendor_id = ?"
		sqlParams = append(sqlParams, vendorID)
	}
	if vendorOrgCode != "" {
		sql += " AND vendor_org_code = ?"
		sqlParams = append(sqlParams, vendorOrgCode)
	}
	if vendorType != "" {
		sql += " AND vendor_type = ?"
		sqlParams = append(sqlParams, vendorType)
	}
	sql += " ORDER BY comment"
	err = GetRows(db, &vendorOrges, sql, sqlParams)
	return vendorOrges, err
}

func GetVendorOrgCodeByKey(db *DaoDB, vendorID int, appKey string) (vendorOrg *model.VendorOrgCode, err error) {
	sql := ` 
		SELECT *
		  FROM vendor_org_code 
		 WHERE deleted_at = ? 
	`
	sqlParams := []interface{}{utils.DefaultTimeValue}
	if vendorID != -1 {
		sql += " AND vendor_id = ?"
		sqlParams = append(sqlParams, vendorID)
	}
	if appKey != "" {
		sql += " AND app_key = ?"
		sqlParams = append(sqlParams, appKey)
	}
	err = GetRow(db, &vendorOrg, sql, sqlParams)
	return vendorOrg, err
}
