// ============================================================================
// This is auto-generated by gf cli tool only once. Fill this file as you wish.
// ============================================================================

package cms_news

import (
	"gfast/app/model/admin/cms_category_news"
	"gfast/app/model/admin/user"
	"gfast/library/service"
	"gfast/library/utils"
	"github.com/gogf/gf/container/gvar"
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gtime"
	"github.com/gogf/gf/util/gconv"
)

//文章列表
type NewsList struct {
	Entity
	UserNickname string    `json:"user_nickname"`
	CateList     *gvar.Var `json:"cateList"`
}

//添加文章参数
type ReqAddParams struct {
	NewsStatus    uint                   `p:"status"    v:"in:0,1#状态只能为0或1"`                                // 状态;1:已发布;0:未发布;
	Attr          []int                  `p:"attr"`                                                         //文章标记 置顶 推荐
	PublishedTime string                 `p:"published_time"`                                               // 发布时间
	NewsTitle     string                 `p:"title"     v:"required#标题不能为空"`                                // post标题
	NewsKeywords  string                 `p:"keywords"`                                                     // seo keywords
	NewsExcerpt   string                 `p:"excerpt"`                                                      // post摘要
	NewsSource    string                 `p:"source"  `                                                     // 转载文章的来源
	Thumbnail     string                 `p:"thumbnail"    `                                                // 缩略图
	IsJump        uint                   `p:"IsJump"        v:"in:0,1#跳转类型只能为0或1"`                          // 是否跳转地址
	JumpUrl       string                 `p:"JumpUrl"      v:"required-if:IsJump,1|url#跳转地址不能为空|跳转地址格式不正确"` // 跳转地址
	ModelForm     map[string]interface{} `p:"modelForm"`
}

//文章搜索参数
type ReqListSearchParams struct {
	CateId             []int  `p:"cateId"`
	PublishedTimeStart string `p:"pubTimeStart"`
	PublishedTimeEnd   string `p:"pubTimeEnd"`
	KeyWords           string `p:"keyWords"`
	IsSlide            string `p:"isSlide"`
	IsTop              string `p:"IsTop"`
	Recommended        string `p:"Recommended"`
	NewsStatus         string `p:"NewsStatus"`
	PageNum            int    `p:"page"`     //当前页码
	PageSize           int    `p:"pageSize"` //每页数
	OrderBy            string //排序字段
}

type ReqEditParams struct {
	Id int `p:"id" v:"integer|min:1#文章ID只能为整数|文章ID只能为正数"`
	ReqAddParams
}

//添加文章操作
func AddNews(req *ReqAddParams, cateIds []int, userId uint64, tx *gdb.TX) (insId int64, err error) {
	if len(cateIds) == 0 {
		err = gerror.New("栏目不能为空")
		return
	}

	nowTime := gconv.Uint(gtime.Timestamp())
	entity := &Entity{
		UserId:        gconv.Uint64(userId),
		NewsStatus:    req.NewsStatus,
		CreateTime:    nowTime,
		UpdateTime:    nowTime,
		PublishedTime: gconv.Uint(utils.StrToTimestamp(req.PublishedTime)),
		NewsTitle:     req.NewsTitle,
		NewsKeywords:  req.NewsKeywords,
		NewsExcerpt:   req.NewsExcerpt,
		NewsSource:    req.NewsSource,
		Thumbnail:     req.Thumbnail,
		IsJump:        req.IsJump,
		JumpUrl:       req.JumpUrl,
	}
	for _, v := range req.Attr {
		if v == 1 {
			entity.IsTop = 1
		} else if v == 2 {
			entity.Recommended = 1
		} else if v == 3 {
			entity.IsSlide = 1
		}
	}
	res, e := Model.TX(tx).Insert(entity)
	if e != nil {
		g.Log().Error(e)
		err = gerror.New("保存文章失败")
		return
	}
	insId, err = res.LastInsertId()
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("添加文章失败")
		return
	}
	//保存栏目与文章关联信息
	catNewsEntity := make([]cms_category_news.Entity, len(cateIds))
	for k, cateId := range cateIds {
		catNewsEntity[k].CategoryId = gconv.Uint64(cateId)
		catNewsEntity[k].NewsId = gconv.Uint64(insId)
	}
	_, err = cms_category_news.Model.TX(tx).Data(catNewsEntity).Insert()
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("设置文章栏目失败")
		return
	}
	return
}

//修改文章操作
func EditNews(req *ReqEditParams, cateIds []int, tx *gdb.TX) (err error) {
	if len(cateIds) == 0 {
		err = gerror.New("栏目不能为空")
		return
	}
	entity, err := Model.FindOne("id", req.Id)
	if err != nil {
		g.Log().Error(err)
	}
	if err != nil || entity == nil {
		err = gerror.New("文章信息获取失败")
		return
	}
	entity.NewsStatus = req.NewsStatus
	entity.UpdateTime = gconv.Uint(gtime.Timestamp())
	entity.PublishedTime = gconv.Uint(utils.StrToTimestamp(req.PublishedTime))
	entity.NewsTitle = req.NewsTitle
	entity.NewsKeywords = req.NewsKeywords
	entity.NewsExcerpt = req.NewsExcerpt
	entity.NewsSource = req.NewsSource
	entity.Thumbnail = req.Thumbnail
	entity.IsJump = req.IsJump
	entity.JumpUrl = req.JumpUrl
	entity.IsTop = 0
	entity.Recommended = 0
	entity.IsSlide = 0
	for _, v := range req.Attr {
		if v == 1 {
			entity.IsTop = 1
		} else if v == 2 {
			entity.Recommended = 1
		} else if v == 3 {
			entity.IsSlide = 1
		}
	}
	_, err = Model.TX(tx).Replace(entity)
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("修改文章失败")
		return
	}
	//删除旧的栏目文章关联信息
	cnList, err := cms_category_news.GetCategoriesByNewsId(entity.Id)
	if err != nil {
		return
	}
	for _, cn := range cnList {
		_, err = cms_category_news.Model.TX(tx).Delete("news_id", cn.NewsId)
		if err != nil {
			g.Log().Error(err)
			err = gerror.New("更新文章栏目所属信息失败")
			return
		}
	}
	//保存栏目与文章关联信息
	catNewsEntity := make([]cms_category_news.Entity, len(cateIds))
	for k, cateId := range cateIds {
		catNewsEntity[k].CategoryId = gconv.Uint64(cateId)
		catNewsEntity[k].NewsId = gconv.Uint64(req.Id)
	}
	_, err = cms_category_news.Model.TX(tx).Data(catNewsEntity).Insert()
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("更新文章栏目所属信息失败")
		return
	}
	return
}

//文章列表查询
func ListByPage(req *ReqListSearchParams) (total, page int, list []*NewsList, err error) {
	model := g.DB().Table(Table + " news")
	if req != nil {
		if len(req.CateId) > 0 {
			model = model.InnerJoin(cms_category_news.Table+" cn", "cn.news_id=news.id").Where("cn.category_id in(?)", req.CateId)
			model = model.Group("cn.news_id")
		}
		if req.KeyWords != "" {
			model = model.Where("news.news_title like ?", "%"+req.KeyWords+"%")
		}
		if req.PublishedTimeStart != "" {
			model = model.Where("news.published_time >=?", utils.StrToTimestamp(req.PublishedTimeStart))
		}
		if req.PublishedTimeEnd != "" {
			model = model.Where("news.published_time <=?", utils.StrToTimestamp(req.PublishedTimeEnd))
		}
		if req.IsSlide != "" {
			model = model.Where("news.is_slide", gconv.Int(req.IsSlide))
		}
		if req.IsTop != "" {
			model = model.Where("news.is_top", gconv.Int(req.IsTop))
		}
		if req.Recommended != "" {
			model = model.Where("news.recommended", gconv.Int(req.Recommended))
		}
		if req.NewsStatus != "" {
			model = model.Where("news.news_status", gconv.Int(req.NewsStatus))
		}
	}
	model = model.LeftJoin(user.Table+" user", "news.user_id=user.id")
	total, err = model.Count()
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("获取总行数失败")
		return
	}
	if req.PageNum == 0 {
		req.PageNum = 1
	}
	page = req.PageNum
	if req.PageSize == 0 {
		req.PageSize = service.AdminPageNum
	}
	var datas gdb.Result
	order := "published_time desc,news.id desc"
	if req.OrderBy != "" {
		order = req.OrderBy
	}
	datas, err = model.Page(page, req.PageSize).Fields("news.*,user.user_nickname").
		Order(order).All()
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("获取数据失败")
		return
	}
	list = make([]*NewsList, len(datas))
	err = datas.Structs(&list)
	return
}

//通过文章id获取文章信息
func GetById(id int) (news *Entity, err error) {
	news, err = Model.FindOne(id)
	if err != nil {
		g.Log().Error(err)
	}
	if err != nil || news == nil {
		err = gerror.New("获取文章信息失败")
		return
	}
	return
}

func DeleteByIds(ids []int, tx *gdb.TX) error {
	_, err := Model.TX(tx).Delete("id in (?)", ids)
	if err != nil {
		g.Log().Error(err)
		return gerror.New("删除文章失败")
	}
	_, err = cms_category_news.Model.TX(tx).Delete("news_id in (?)", ids)
	if err != nil {
		g.Log().Error(err)
		return gerror.New("删除文章栏目关联信息失败")
	}
	return nil
}
