package dao

import (
	"errors"
	"fmt"
	"strings"

	"git.rosy.net.cn/baseapi/utils"

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

//测试函数
func ReplaceInsertFreight2(storeID, templateID, warehouseID, tradeLimitID int, vendorStoreID, fenceID string) error {
	if storeID == 0 || len(vendorStoreID) == 0 {
		return fmt.Errorf("storeID或vendorStoreID不允许为空")
	}
	sqlStr := `REPLACE INTO freight_template (store_id, vendor_store_id, template_id, warehouse_id, fence_id, trade_limit_id)
	SELECT ?,?,?,?,?,? FROM DUAL WHERE NOT EXISTS(SELECT COUNT(store_id) FROM freight_template WHERE store_id = ?)=0`
	sqlParam := []interface{}{storeID, vendorStoreID, templateID, warehouseID, fenceID, tradeLimitID, storeID}
	_, err := ExecuteSQL(GetDB(), sqlStr, sqlParam)
	return err
}

// 全参数更新插入
func ReplaceInsertFreight(tem *model.FreightTemplate) error {
	if tem.StoreID == 0 || len(tem.VendorStoreID) == 0 {
		return fmt.Errorf("storeID或vendorStoreID不允许为空")
	}
	sqlStr := `REPLACE INTO freight_template (store_id, vendor_store_id, template_id, warehouse_id, fence_id, trade_limit_id)
SELECT ?,?,?,?,?,? FROM DUAL WHERE NOT EXISTS(SELECT COUNT(store_id) FROM freight_template WHERE store_id = ?)=0`
	sqlParam := []interface{}{tem.StoreID, tem.VendorStoreID, tem.TemplateID, tem.WarehouseID, tem.FenceID, tem.TradeLimitID, tem.StoreID}
	_, err := ExecuteSQL(GetDB(), sqlStr, sqlParam)
	return err
}

func InsertItemFreight2(storeID, templateID, warehouseID, tradeLimitID int, vendorStoreID, fenceID string) error {
	var (
		sqlStr    []string
		tStr      = ""
		sqlParams []interface{}
	)
	if storeID == 0 && utils.Str2Int(vendorStoreID) == 0 {
		return errors.New("storeID,vendorStoreID 必传")
	}
	sql := `UPDATE freight_template a SET `
	if templateID != 0 {
		templateIDSql := ` a.template_id = ?`
		sqlParams = append(sqlParams, templateID)
		sqlStr = append(sqlStr, templateIDSql)
	}
	if warehouseID != 0 {
		warehouseIDSql := ` a.warehouse_id = ?`
		sqlParams = append(sqlParams, warehouseID)
		sqlStr = append(sqlStr, warehouseIDSql)
	}
	if len(fenceID) > 0 {
		fenceIDSql := ` a.fence_id = ?`
		sqlParams = append(sqlParams, fenceID)
		sqlStr = append(sqlStr, fenceIDSql)
	}
	if tradeLimitID != 0 {
		tradeLimitIDSql := ` a.trade_limit_id = ? `
		sqlParams = append(sqlParams, tradeLimitID)
		sqlStr = append(sqlStr, tradeLimitIDSql)
	}
	tStr = "WHERE a.store_id = ? AND a.vendor_store_id = ?"
	sqlParams = append(sqlParams, storeID, vendorStoreID)
	tStr2 := sql + strings.Join(sqlStr, ",") + tStr

	_, err := ExecuteSQL(GetDB(), tStr2, sqlParams...)
	fmt.Println(tStr2)
	return err
}

func InsertItemFreight(tem *model.FreightTemplate) error {
	if tem.StoreID == 0 || len(tem.VendorStoreID) == 0 {
		return fmt.Errorf("storeID或vendorStoreID不允许为空")
	}
	var (
		sqlStr    []string
		tStr      = ""
		sqlParams []interface{}
	)
	if tem.StoreID == 0 && utils.Str2Int(tem.VendorStoreID) == 0 {
		return errors.New("storeID,vendorStoreID 必传")
	}
	sql := `UPDATE freight_template a SET `
	if tem.TemplateID != 0 {
		templateIDSql := ` a.template_id = ?`
		sqlParams = append(sqlParams, tem.TemplateID)
		sqlStr = append(sqlStr, templateIDSql)
	}
	if tem.WarehouseID != 0 {
		warehouseIDSql := ` a.warehouse_id = ?`
		sqlParams = append(sqlParams, tem.WarehouseID)
		sqlStr = append(sqlStr, warehouseIDSql)
	}
	if len(tem.FenceID) > 0 {
		fenceIDSql := ` a.fence_id = ?`
		sqlParams = append(sqlParams, tem.FenceID)
		sqlStr = append(sqlStr, fenceIDSql)
	}
	if tem.TradeLimitID != 0 {
		tradeLimitIDSql := ` a.trade_limit_id = ? `
		sqlParams = append(sqlParams, tem.TradeLimitID)
		sqlStr = append(sqlStr, tradeLimitIDSql)
	}
	if len(sqlStr) == model.NO {
		return nil
	}
	tStr = "WHERE a.store_id = ? AND a.vendor_store_id = ?"
	sqlParams = append(sqlParams, tem.StoreID, tem.VendorStoreID)
	tStr2 := sql + strings.Join(sqlStr, ",") + tStr

	_, err := ExecuteSQL(GetDB(), tStr2, sqlParams...)
	return err
}
