// Package sys
// @Link  https://github.com/bufanyun/hotgo
// @Copyright  Copyright (c) 2025 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License  https://github.com/bufanyun/hotgo/blob/master/LICENSE
// @AutoGenerate Version 2.17.8
package sys

import (
	"context"
	"fmt"
	"hotgo/internal/dao"
	"hotgo/internal/library/hgorm/handler"
	"hotgo/internal/model/input/form"
	"hotgo/internal/model/input/sysin"
	"hotgo/internal/service"
	"hotgo/utility/convert"
	"hotgo/utility/excel"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/util/gconv"
)

type sSysUsers struct{}

func NewSysUsers() *sSysUsers {
	return &sSysUsers{}
}

func init() {
	service.RegisterSysUsers(NewSysUsers())
}

// Model 前台用户表ORM模型
func (s *sSysUsers) Model(ctx context.Context, option ...*handler.Option) *gdb.Model {
	return handler.Model(dao.Users.Ctx(ctx), option...)
}

// List 获取前台用户表列表
func (s *sSysUsers) List(ctx context.Context, in *sysin.UsersListInp) (list []*sysin.UsersListModel, totalCount int, err error) {
	mod := s.Model(ctx)

	// 字段过滤
	mod = mod.Fields(sysin.UsersListModel{})

	// 查询id
	if in.Id > 0 {
		mod = mod.Where(dao.Users.Columns().Id, in.Id)
	}

	// 查询用户状态
	if in.Status > 0 {
		mod = mod.Where(dao.Users.Columns().Status, in.Status)
	}

	// 查询注册时间
	if len(in.CreatedAt) == 2 {
		mod = mod.WhereBetween(dao.Users.Columns().CreatedAt, in.CreatedAt[0], in.CreatedAt[1])
	}

	// 分页
	mod = mod.Page(in.Page, in.PerPage)

	// 排序
	mod = mod.OrderDesc(dao.Users.Columns().Id)

	// 查询数据
	if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
		err = gerror.Wrap(err, "获取前台用户表列表失败，请稍后重试！")
		return
	}
	return
}

// Export 导出前台用户表
func (s *sSysUsers) Export(ctx context.Context, in *sysin.UsersListInp) (err error) {
	list, totalCount, err := s.List(ctx, in)
	if err != nil {
		return
	}

	// 字段的排序是依据tags的字段顺序，如果你不想使用默认的排序方式，可以直接定义 tags = []string{"字段名称", "字段名称2", ...}
	tags, err := convert.GetEntityDescTags(sysin.UsersExportModel{})
	if err != nil {
		return
	}

	var (
		fileName  = "导出前台用户表-" + gctx.CtxId(ctx)
		sheetName = fmt.Sprintf("索引条件共%v行,共%v页,当前导出是第%v页,本页共%v行", totalCount, form.CalPageCount(totalCount, in.PerPage), in.Page, len(list))
		exports   []sysin.UsersExportModel
	)

	if err = gconv.Scan(list, &exports); err != nil {
		return
	}

	err = excel.ExportByStructs(ctx, tags, exports, fileName, sheetName)
	return
}

// Edit 修改/新增前台用户表
func (s *sSysUsers) Edit(ctx context.Context, in *sysin.UsersEditInp) (err error) {
	return g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {

		// 修改
		if in.Id > 0 {
			if _, err = s.Model(ctx).
				Fields(sysin.UsersUpdateFields{}).
				WherePri(in.Id).Data(in).Update(); err != nil {
				err = gerror.Wrap(err, "修改前台用户表失败，请稍后重试！")
			}
			return
		}

		// 新增
		if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).
			Fields(sysin.UsersInsertFields{}).
			Data(in).OmitEmptyData().Insert(); err != nil {
			err = gerror.Wrap(err, "新增前台用户表失败，请稍后重试！")
		}
		return
	})
}

// Delete 删除前台用户表
func (s *sSysUsers) Delete(ctx context.Context, in *sysin.UsersDeleteInp) (err error) {

	if _, err = s.Model(ctx).WherePri(in.Id).Unscoped().Delete(); err != nil {
		err = gerror.Wrap(err, "删除前台用户表失败，请稍后重试！")
		return
	}
	return
}

// View 获取前台用户表指定信息
func (s *sSysUsers) View(ctx context.Context, in *sysin.UsersViewInp) (res *sysin.UsersViewModel, err error) {
	if err = s.Model(ctx).WherePri(in.Id).Scan(&res); err != nil {
		err = gerror.Wrap(err, "获取前台用户表信息，请稍后重试！")
		return
	}
	return
}

// Status 更新前台用户表状态
func (s *sSysUsers) Status(ctx context.Context, in *sysin.UsersStatusInp) (err error) {
	if _, err = s.Model(ctx).WherePri(in.Id).Data(g.Map{
		dao.Users.Columns().Status: in.Status,
	}).Update(); err != nil {
		err = gerror.Wrap(err, "更新前台用户表状态失败，请稍后重试！")
		return
	}
	return
}