package dao

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

/*VendorID = -2 的时候是通用,-1 查询所有，保留之前的1，看后面会不会有扩展*/
func GetSensitiveWordList(vendorID int) (wordList []*model.SensitiveWord, err error) {
	if vendorID == 0 {
		vendorID = -2
	}
	if vendorID == -1 {
		sql := `SELECT * FROM sensitive_word WHERE deleted_at = ?`
		err = GetRows(nil, &wordList, sql, utils.DefaultTimeValue)
	} else {
		sql := `SELECT * FROM sensitive_word WHERE deleted_at = ? AND vendor_id = ?`
		err = GetRows(nil, &wordList, sql, utils.DefaultTimeValue, vendorID)
	}
	return wordList, err
}

func InsertSensitiveWord(word string, vendorID int, userName string) error {
	if vendorID == 0 {
		vendorID = -2
	}
	sensitiveWord := &model.SensitiveWord{Word: word, VendorID: vendorID}
	WrapAddIDCULDEntity(sensitiveWord, userName)
	return CreateOrUpdate(nil, sensitiveWord)
}

func DeleteSensitiveWord(wordList *model.SensitiveWord, id int, userName string, vendorID int) (word *model.SensitiveWord, err error) {
	if vendorID == 0 {
		vendorID = -2
	}
	if id != 0 {
		wordList.ID = id
	}
	wordList.VendorID = id
	wordList.DeletedAt = time.Now()
	wordList.LastOperator = userName
	if _, err := UpdateEntity(nil, wordList, "deleted_at", "id", "last_operator", "vendor_id"); err != nil {
		return nil, err
	}
	return wordList, nil
}

func UpdateSensitiveWord(wordList *model.SensitiveWord, vendorID int, id int, userName string) (word *model.SensitiveWord, err error) {
	if vendorID == 0 {
		vendorID = -2
	}
	if id != 0 {
		wordList.ID = id
	}
	wordList.VendorID = vendorID
	wordList.UpdatedAt = time.Now()
	wordList.LastOperator = userName
	if id != 0 {
		if _, err := UpdateEntity(nil, wordList, "id", "word", "vendor_id", "last_operator"); err != nil {
			return nil, err
		}
	} else {
		if _, err := UpdateEntity(nil, wordList, "word", "vendor_id", "last_operator"); err != nil {
			return nil, err
		}
	}
	return wordList, err
}
