package models

import (
	"bwallet/pkg/setting"
	"github.com/jinzhu/gorm"
)

type Article struct {
	Model

	Title      string `gorm:"size:255;not null;" json:"title"`
	Source     string `gorm:"not null;" json:"source"`
	LogoPic    string `gorm:"not null;" json:"logo_pic"`
	ImgUrl     string `gorm:"not null;" json:"img_url"`
	Content    string `gorm:"not null;" json:"content"`
	Top        uint8  `gorm:"not null;default:0" json:"top"`
	Highlight  uint8  `gorm:"not null;default:0" json:"highlight"`
	PlatformId int    `gorm:"not null;default:1" json:"platform_id"`
	CreateTime int64  `gorm:"not null;" json:"create_time"`
}

func (w Article) TableName() string {
	return setting.DatabaseSetting.Name_Coin + ".article"
}

func ExistArticleById(id int) (bool, error) {
	var article Article
	err := db.Select("id").Where("id = ?", id).First(&article).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return false, err
	}

	if article.ID > 0 {
		return true, nil
	}

	return false, nil
}

func GetArticleTotal(maps interface{}) (int, error) {
	var count int

	if _, ok := maps.(map[string]interface{})["search_type"]; ok {
		if err := db.Model(&Article{}).Where("platform_id in (?)", maps.(map[string]interface{})["search_type"]).Count(&count).Error; err != nil {
			return 0, err
		}
	} else {
		if err := db.Model(&Article{}).Where(maps).Count(&count).Error; err != nil {
			return 0, err
		}
	}

	return count, nil
}

func GetArticle(pageNum, pageSize int, maps interface{}) ([]*Article, error) {
	var article []*Article
	if _, ok := maps.(map[string]interface{})["search_type"]; ok {
		err := db.Where("platform_id in (?)", maps.(map[string]interface{})["search_type"]).Order("top desc").Order("create_time desc").Offset(pageNum).Limit(pageSize).Find(&article).Error
		if err != nil && err != gorm.ErrRecordNotFound {
			return nil, err
		}
	} else {
		err := db.Where(maps).Order("top desc").Order("create_time desc").Offset(pageNum).Limit(pageSize).Find(&article).Error
		if err != nil && err != gorm.ErrRecordNotFound {
			return nil, err
		}
	}

	return article, nil
}

func GetOneArticle(maps interface{}) (*Article, error) {
	var article Article
	err := db.Where(maps).First(&article).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	err = db.Model(&article).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	return &article, nil
}

func AddArticle(data map[string]interface{}) (error) {
	article := Article{
		Title:      data["title"].(string),
		Source:     data["source"].(string),
		LogoPic:    data["logo_pic"].(string),
		ImgUrl:     data["img_url"].(string),
		Content:    data["content"].(string),
		Top:        data["top"].(uint8),
		Highlight:  data["highlight"].(uint8),
		PlatformId: data["platform_id"].(int),
		CreateTime: data["create_time"].(int64),
	}
	if err := db.Create(&article).Error; err != nil {
		return err
	}

	return nil
}

func EditArticle(id int, data interface{}) error {
	if err := db.Model(&Article{}).Where("id = ?", id).Updates(data).Error; err != nil {
		return err
	}

	return nil
}

func DeleteArticle(id int) error {
	if err := db.Where("id = ?", id).Delete(Article{}).Error; err != nil {
		return err
	}

	return nil
}
