package gmodel

import "context"

// TODO: 使用model的属性做你想做的
func (m *LdapMenusModel) Create(ctx context.Context, data *LdapMenus) error {
	return m.db.WithContext(ctx).Model(&LdapMenus{}).Create(&data).Error
}
func (m *LdapMenusModel) FindOne(ctx context.Context, id int64) (resp *LdapMenus, err error) {
	err = m.db.WithContext(ctx).Model(&LdapMenus{}).Where("id= ? and status = ?", id, 1).Take(&resp).Error
	return resp, err
}

func (m *LdapMenusModel) FindByPath(ctx context.Context, path string) (resp *LdapMenus, err error) {
	err = m.db.WithContext(ctx).Model(&LdapMenus{}).Where("path= ? and status = ?", path, 1).Take(&resp).Error
	return resp, err
}
func (m *LdapMenusModel) Update(ctx context.Context, id int64, data *LdapMenus) error {
	return m.db.WithContext(ctx).Model(&LdapMenus{}).Where("id = ?", id).Updates(&data).Error
}

type GetMenuListByParamReq struct {
	Ids      []int64
	Name     string
	Title    string
	Path     string
	Status   *int64
	ParentId *int64
	Sort     string
	Page     int
	Limit    int
}

func (m *LdapMenusModel) GetMenuListByParam(ctx context.Context, req GetMenuListByParamReq) (resp []LdapMenus, total int64, err error) {
	db := m.db.WithContext(ctx).Model(&LdapMenus{})
	if len(req.Ids) > 0 {
		db = db.Where("id in(?)", req.Ids)
	}
	if req.Name != "" {
		db = db.Where("name like ?", "%"+req.Name+"%")
	}
	if req.Title != "" {
		db = db.Where("title like ?", "%"+req.Title+"%")
	}
	if req.Name != "" {
		db = db.Where("path like ?", "%"+req.Path+"%")
	}
	if req.Status != nil {
		db = db.Where("status = ?", req.Status)
	}
	if req.ParentId != nil {
		db = db.Where("parent_id = ?", req.ParentId)
	}
	if req.Sort != "" {
		db = db.Order(req.Sort)
	}
	if err = db.Count(&total).Error; err != nil {
		return nil, 0, err
	}
	offset := (req.Page - 1) * req.Limit
	err = db.Offset(offset).Limit(req.Limit).Find(&resp).Error
	return resp, total, err
}