157 lines
4.8 KiB
Go
157 lines
4.8 KiB
Go
// Package iot
|
|
// @Link https://github.com/xiujiecn/xiu-admin
|
|
// @Copyright Copyright (c) 2026 LiXiujie
|
|
// @Author Lxj <xxx@example.com>
|
|
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
|
// @AutoGenerate Version
|
|
package common
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/dao"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/xgorm/handler"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/service"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/utility/convert"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/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 sIotTDivisionCode struct{}
|
|
|
|
func NewIotTDivisionCode() *sIotTDivisionCode {
|
|
return &sIotTDivisionCode{}
|
|
}
|
|
|
|
func init() {
|
|
service.RegisterIotTDivisionCode(NewIotTDivisionCode())
|
|
}
|
|
|
|
// Model 村级行政区划表ORM模型
|
|
func (s *sIotTDivisionCode) Model(ctx context.Context, option ...*handler.Option) *gdb.Model {
|
|
if len(option) == 0 {
|
|
option = append(option, &handler.Option{
|
|
FilterTenant: true,
|
|
FilterAuth: true,
|
|
})
|
|
}
|
|
return handler.Model(dao.TDivisionCode.Ctx(ctx), option...)
|
|
}
|
|
|
|
// List 获取村级行政区划表列表
|
|
func (s *sIotTDivisionCode) List(ctx context.Context, in *model.TDivisionCodeListParam) (list []*model.TDivisionCodeListModel, totalCount int, err error) {
|
|
mod := s.Model(ctx)
|
|
|
|
// 根据父级代码查询子级列表
|
|
if in.RegionParent != "" {
|
|
mod = mod.Where(dao.TDivisionCode.Columns().RegionParent, in.RegionParent)
|
|
}
|
|
|
|
// 根据名称查询
|
|
if in.RegionName != "" {
|
|
mod = mod.WhereLike(dao.TDivisionCode.Columns().RegionName, "%"+in.RegionName+"%")
|
|
}
|
|
|
|
// 根据代码查询
|
|
if in.RegionCode != "" {
|
|
mod = mod.Where(dao.TDivisionCode.Columns().RegionCode, in.RegionCode)
|
|
}
|
|
|
|
// 根据等级查询
|
|
if in.RegionLevel > 0 {
|
|
mod = mod.Where(dao.TDivisionCode.Columns().RegionLevel, in.RegionLevel)
|
|
}
|
|
|
|
// 字段过滤
|
|
mod = mod.Fields(model.TDivisionCodeListModel{})
|
|
|
|
// 分页
|
|
mod = mod.Page(in.Page, in.PageSize)
|
|
|
|
// 排序
|
|
mod = mod.OrderDesc(dao.TDivisionCode.Columns().RegionId)
|
|
|
|
// 查询数据
|
|
if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
|
|
err = gerror.Wrap(err, "获取村级行政区划表列表失败,请稍后重试!")
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// Export 导出村级行政区划表
|
|
func (s *sIotTDivisionCode) Export(ctx context.Context, in *model.TDivisionCodeListParam) (err error) {
|
|
list, totalCount, err := s.List(ctx, in)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// 字段的排序是依据tags的字段顺序,如果你不想使用默认的排序方式,可以直接定义 tags = []string{"字段名称", "字段名称2", ...}
|
|
tags, err := convert.GetEntityDescTags(model.TDivisionCodeExportModel{})
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
var (
|
|
fileName = "导出村级行政区划表-" + gctx.CtxId(ctx)
|
|
sheetName = fmt.Sprintf("索引条件共%v行,共%v页,当前导出是第%v页,本页共%v行", totalCount, (totalCount+in.PageSize-1)/in.PageSize, in.Page, len(list))
|
|
exports []model.TDivisionCodeExportModel
|
|
)
|
|
|
|
if err = gconv.Scan(list, &exports); err != nil {
|
|
return
|
|
}
|
|
|
|
err = excel.ExportByStructs(ctx, tags, exports, fileName, sheetName)
|
|
return
|
|
}
|
|
|
|
// Edit 修改/新增村级行政区划表
|
|
func (s *sIotTDivisionCode) Edit(ctx context.Context, in *model.TDivisionCodeEditParam) (err error) {
|
|
return g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
|
|
|
// 修改
|
|
if in.RegionId > 0 {
|
|
if _, err = s.Model(ctx).
|
|
Fields(model.TDivisionCodeUpdateFields{}).
|
|
WherePri(in.RegionId).Data(in).Update(); err != nil {
|
|
err = gerror.Wrap(err, "修改村级行政区划表失败,请稍后重试!")
|
|
}
|
|
return
|
|
}
|
|
|
|
// 新增
|
|
if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).
|
|
Fields(model.TDivisionCodeInsertFields{}).
|
|
Data(in).Insert(); err != nil {
|
|
err = gerror.Wrap(err, "新增村级行政区划表失败,请稍后重试!")
|
|
}
|
|
return
|
|
})
|
|
}
|
|
|
|
// Delete 删除村级行政区划表
|
|
func (s *sIotTDivisionCode) Delete(ctx context.Context, in *model.TDivisionCodeDeleteParam) (err error) {
|
|
|
|
if _, err = s.Model(ctx).WherePri(in.RegionId).Unscoped().Delete(); err != nil {
|
|
err = gerror.Wrap(err, "删除村级行政区划表失败,请稍后重试!")
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// View 获取村级行政区划表指定信息
|
|
func (s *sIotTDivisionCode) View(ctx context.Context, in *model.TDivisionCodeViewParam) (res *model.TDivisionCodeViewModel, err error) {
|
|
if err = s.Model(ctx).WherePri(in.RegionId).Scan(&res); err != nil {
|
|
err = gerror.Wrap(err, "获取村级行政区划表信息,请稍后重试!")
|
|
return
|
|
}
|
|
return
|
|
}
|