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

package model_category

import (
	"gfast/library/service"
	"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 SearchReq struct {
	Name     string `p:"cName"`
	Status   string `p:"cStatus"`
	PageNum  int    `p:"page"`     //当前页码
	PageSize int    `p:"pageSize"` //每页数
}

//添加请求数据
type AddReq struct {
	CName    string `p:"cName" v:"required#分类名称不能为空"`
	CSort    int    `p:"cSort"`
	CStatus  uint   `p:"cStatus"`
	CreateBy uint
}

//修改请求数据
type EditReq struct {
	CId      int64  `p:"cId" v:"required#参数错误"`
	CName    string `p:"cName" v:"required#分类名称不能为空"`
	CSort    int    `p:"cSort"`
	CStatus  uint   `p:"cStatus"`
	UpdateBy uint
}

func GetList(req *SearchReq) (total int, list []*Entity, err error) {
	m := Model
	if req != nil {
		if req.Name != "" {
			m = m.Where(Columns.CName+" like ?", "%"+req.Name+"%")
		}
		if req.Status != "" {
			m = m.Where(Columns.CStatus, gconv.Int(req.Status))
		}
	}
	total, err = m.Count()
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("获取总行数失败")
		return
	}
	if req.PageNum == 0 {
		req.PageNum = 1
	}

	if req.PageSize == 0 {
		req.PageSize = service.AdminPageNum
	}
	list, err = m.Page(req.PageNum, req.PageSize).Order(Columns.CSort + " asc," + Columns.CId + " asc").All()
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("获取数据失败")
		return
	}
	return
}

//添加分类操作
func Add(req *AddReq) error {
	entity := new(Entity)
	entity.CName = req.CName
	entity.CSort = req.CSort
	entity.CStatus = req.CStatus
	now := gconv.Uint64(gtime.Timestamp())
	entity.CreateTime = now
	entity.UpdateTime = now
	entity.CreateBy = req.CreateBy
	if _, err := entity.Save(); err != nil {
		g.Log().Error(err)
		return gerror.New("添加失败")
	}
	return nil
}

//修改
func Edit(req *EditReq) error {
	entity, err := Model.FindOne(req.CId)
	if err != nil || entity == nil {
		g.Log().Error(err)
		return gerror.New("获取分类信息失败")
	}
	entity.UpdateBy = req.UpdateBy
	entity.CName = req.CName
	entity.CSort = req.CSort
	entity.CStatus = req.CStatus
	entity.UpdateTime = gconv.Uint64(gtime.Timestamp())
	_, err = entity.Save()
	if err != nil {
		g.Log().Error(err)
		return gerror.New("修改分类信息失败")
	}
	return nil
}

func GetById(id int64) (entity *Entity, err error) {
	entity, err = Model.FindOne(id)
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("获取分类信息失败")
	}
	return
}

//删除模型分类
func DeleteByIds(ids []int) error {
	_, err := Model.Where(Columns.CId+" in (?)", ids).Delete()
	if err != nil {
		g.Log().Error(err)
		return gerror.New("删除失败")
	}
	return nil
}

func GetCategoryAll() (entity []*Entity, err error) {
	entity, err = Model.Where(Columns.CStatus, 1).Order(Columns.CSort + " ASC," + Columns.CId + " ASC").All()
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("获取模型分类数据失败")
		return
	}
	return
}
