package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"strconv"
)

// 获取书列表
func GetReadBooks() (error, *[]model.ReadBook) {
	bookList := make([]model.ReadBook, 0)

	field := " id, name, author, description, image_url, create_time "

	table := " read_books "

	conditions := " AND delflag = 0 "

	orderby := " create_time desc "

	sqlStr := "SELECT " + field +
		" FROM " + table +
		" WHERE 1>0 " + conditions +
		" ORDER BY " + orderby
	global.GVA_DB.Raw(sqlStr).Scan(&bookList)
	if global.GVA_DB.Error != nil {
		return global.GVA_DB.Error, nil
	}
	return nil, &bookList
}

// 获取目录
func GetReadCatalog(bookId int) (error, []*model.ReadCatalog) {
	readCatalogs := make([]*model.ReadCatalog, 0)

	field := " dc.id, dc.read_books_id, dc.name, dc.parent_id, dc.sort_order, dc.create_time, dc.update_time" +
		", IF(rc.content!='',1,0) as content "

	table := " read_catalog as dc LEFT JOIN read_contents AS rc ON dc.id = rc.read_catalog_id AND rc.delflag = 0 "

	conditions := " AND dc.read_books_id = ? AND dc.delflag = 0 "

	orderby := " dc.sort_order asc"

	sqlStr := "SELECT " + field +
		" FROM " + table +
		" WHERE 1>0 " + conditions +
		" ORDER BY " + orderby
	global.GVA_DB.Raw(sqlStr, bookId).Scan(&readCatalogs)
	if global.GVA_DB.Error != nil {
		return global.GVA_DB.Error, nil
	}
	return nil, readCatalogs
}

// 获取文章
func GetReadContent(cataId int) (error, *model.ReadContents) {
	var content model.ReadContents
	global.GVA_DB.Where("read_catalog_id = ? and delflag = 0", cataId).Find(&content)
	if global.GVA_DB.Error != nil {
		return global.GVA_DB.Error, nil
	}
	return nil, &content
}

// 获取文章 （admin端）
func GetAdminReadContent(req request.GetReadContentReq) (err error, list []response.ReadContentsRes, total int64) {
	content := make([]response.ReadContentsRes, 0)
	pagesize := 10
	page := 1
	if req.PageSize != 0 {
		pagesize = req.PageSize
	}
	if req.Page != 0 {
		page = req.Page
	}
	currentpage := pagesize * (page - 1)
	table := " read_contents as dc " +
		" left join read_catalog as dl on dc.read_catalog_id = dl.id " +
		" LEFT JOIN read_books as db on db.id = dl.read_books_id "
	field := " db.name as book_name, dl.name as catalog_name, dl.id as read_catalog_id, dc.id, db.id as book_id, dc.keywords"
	conditions := " AND dc.delflag=0 "
	orderby := " dl.id asc"

	if req.Name != "" {
		conditions += " AND dl.name like '%" + req.Name + "%'"
	}
	if req.ReadBooksId != 0 {
		conditions += " AND db.id = " + strconv.Itoa(req.ReadBooksId)
	}
	//@@总条数，总页数
	var totalItem int64 = 0

	sqlStr := "SELECT count(dc.id) as totalItem FROM " + table + " where 1=1 " + conditions
	global.GVA_DB.Raw(sqlStr).Count(&totalItem) //获取总条数
	sqlStr2 := "SELECT " + field +
		" FROM " + table +
		" where 1>0 " + conditions +
		" ORDER BY " + orderby +
		" LIMIT " + strconv.Itoa(currentpage) + "," + strconv.Itoa(pagesize)

	global.GVA_DB.Raw(sqlStr2).Scan(&content)

	if global.GVA_DB.Error != nil {
		return global.GVA_DB.Error, nil, totalItem
	}
	return nil, content, totalItem
}

// 创建目录
func CreateReadCatalog(req request.CreateReadCatalog) error {
	sqlStr := "INSERT INTO read_catalog(read_books_id, name, parent_id, sort_order) VALUE(?,?,?,?)"
	global.GVA_DB.Exec(sqlStr, req.ReadBooksId, req.Name, req.ParentId, req.SortOrder)
	return global.GVA_DB.Error
}

// 创建文章
func CreateReadContent(req request.CreateReadContent) error {
	sqlStr := "INSERT INTO read_contents(read_catalog_id, content, keywords) VALUE(?,?,?)"
	global.GVA_DB.Exec(sqlStr, req.ReadCatalogId, req.Content, req.Keywords)
	return global.GVA_DB.Error
}

// 更新目录
func UpdateReadCatalog(req request.CreateReadCatalog) error {
	err := global.GVA_DB.Table("read_catalog").Where("id=?", req.Id).Updates(&req).Error
	return err
}

// 更新文章
func UpdateReadContent(req request.CreateReadContent) error {
	err := global.GVA_DB.Table("read_contents").Where("id=?", req.Id).Updates(&req).Error
	return err
}

// 删除目录
func DeleteReadCatalog(req request.CreateReadCatalog) error {
	sqlStr := "UPDATE read_catalog SET delflag=? WHERE id=?"
	global.GVA_DB.Exec(sqlStr, 1, req.Id)
	return global.GVA_DB.Error
}

// 删除文章
func DeleteReadContent(req request.CreateReadContent) error {
	sqlStr := "UPDATE read_contents SET delflag=? WHERE id=?"
	global.GVA_DB.Exec(sqlStr, 1, req.Id)
	return global.GVA_DB.Error
}

// 获取浏览记录
func GetReadHistory(req request.GetReadHistoryReq) (error, *[]model.ReadHistory) {
	list := make([]model.ReadHistory, 0)

	field := " id, user_id, read_book_id, read_history_id, current, create_time "

	table := " read_history "

	conditions := " AND user_id = ? AND read_book_id = ? "

	orderby := " create_time desc "

	sqlStr := "SELECT " + field +
		" FROM " + table +
		" WHERE 1>0 " + conditions +
		" ORDER BY " + orderby
	global.GVA_DB.Raw(sqlStr, req.UserId, req.ReadBookId).Scan(&list)
	if global.GVA_DB.Error != nil {
		return global.GVA_DB.Error, nil
	}
	return nil, &list
}

// 新增浏览记录
func CreateReadHistory(req request.CreateReadHistory) error {
	sqlStr := "INSERT INTO read_history(read_book_id, user_id, read_history_id, current) VALUE(?,?,?,?)"
	global.GVA_DB.Exec(sqlStr, req.ReadBookId, req.UserId, req.ReadHistoryId, req.Current)
	return global.GVA_DB.Error
}

// 更新浏览记录
func UpdateReadHistory(req request.CreateReadHistory) error {
	err := global.GVA_DB.Table("read_history").Where("id=?", req.Id).Updates(&req).Error
	return err
}
