59eb34976a
iotlcm-admin-svr:管理后台服务。 - Go module:iotlcm → git.5iot.com/iotlcm-public/iotlcm-admin-svr(729 文件) - 凭据与地址脱敏:JWT signingKey(含代码里的 MustGet 默认值)、 MySQL DSN、Redis 口令、告警邮件发件人邮箱全部改为占位; 私网 IP → 文档地址段;库名 xiujie_admin → iotlcm - 真实手机号/邮箱 → 合法 11 位编造号 / example.com - 移除内部资产:CI 触发器、过时 DDL(sql/db_iotlcm.sql)、 私有依赖 go get 行、明文 registry 登录口令 - 启动 banner 统一 IOTLCM 方块字 + 社区惯例署名 - Dockerfile 改用公共基础镜像 - 拼错的 RADEME.md → README.md,README 重写 - 需要真实数据库的用例改为可用 IOTLCM_TEST_WITH_DB=1 显式启用 - 补 Apache-2.0 LICENSE,LF 行尾 + .gitattributes 数据库 schema 不随本仓发布(生产 DDL 已过时),README 已说明。
478 lines
14 KiB
Go
478 lines
14 KiB
Go
// Package gen
|
|
// @Link https://github.com/xiujiecn/xiu-admin
|
|
// @Copyright Copyright (c) 2026 LiXiujie
|
|
// @Author Lxj <li@xiujie.cn>
|
|
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
|
// @AutoGenerate Version
|
|
package iot
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/consts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/dao"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/contexts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/xgorm/handler"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/xgorm/hook"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model/entity"
|
|
"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"
|
|
"strings"
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/os/gctx"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
)
|
|
|
|
type sIotTOrder struct{}
|
|
|
|
const (
|
|
orderPerspectiveSell = "sell"
|
|
orderExportPageSize = 2000
|
|
)
|
|
|
|
func NewIotTOrder() *sIotTOrder {
|
|
return &sIotTOrder{}
|
|
}
|
|
|
|
func init() {
|
|
service.RegisterIotTOrder(NewIotTOrder())
|
|
}
|
|
|
|
// Model 订单主表ORM模型
|
|
func (s *sIotTOrder) 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.TOrder.Ctx(ctx), option...)
|
|
}
|
|
|
|
func currentOrderAgentID(ctx context.Context) (int64, error) {
|
|
deptID := contexts.GetDeptId(ctx)
|
|
if deptID <= 0 {
|
|
return 0, gerror.New("当前用户未关联组织")
|
|
}
|
|
agentID := handler.GetParentCompanyDeptId(ctx, deptID)
|
|
if agentID <= 0 {
|
|
return 0, gerror.New("当前用户未关联代理商企业")
|
|
}
|
|
return agentID, nil
|
|
}
|
|
|
|
func (s *sIotTOrder) businessModel(ctx context.Context) *gdb.Model {
|
|
return s.Model(ctx, &handler.Option{
|
|
FilterTenant: true,
|
|
FilterAuth: false,
|
|
})
|
|
}
|
|
|
|
// List 获取订单主表列表
|
|
func (s *sIotTOrder) List(ctx context.Context, in *model.TOrderListParam) (list []*model.TOrderListModel, totalCount int, err error) {
|
|
defer func() {
|
|
if err == nil {
|
|
err = s.enrichOrderList(ctx, list)
|
|
}
|
|
}()
|
|
mod := s.businessModel(ctx)
|
|
orderTable := dao.TOrder.Table()
|
|
deptTable := dao.SysDept.Table()
|
|
customerDeptTable := "customer_dept"
|
|
agentDeptTable := "agent_dept"
|
|
|
|
// 字段过滤
|
|
mod = mod.Fields(orderTable + ".*")
|
|
mod = mod.Fields(
|
|
customerDeptTable+".dept_name AS customerName",
|
|
agentDeptTable+".dept_name AS agentName",
|
|
)
|
|
|
|
agentID, err := currentOrderAgentID(ctx)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
mod = mod.Where(dao.TOrder.Columns().CustomerAgentId, agentID)
|
|
|
|
// 查询主键ID
|
|
if in.Id > 0 {
|
|
mod = mod.Where(dao.TOrder.Columns().Id, in.Id)
|
|
}
|
|
|
|
if orderSn := strings.TrimSpace(in.OrderSn); orderSn != "" {
|
|
mod = mod.WhereLike(dao.TOrder.Columns().OrderSn, "%"+orderSn+"%")
|
|
}
|
|
|
|
// 查询订单状态 枚举
|
|
if in.OrderStatus != nil {
|
|
mod = mod.Where(dao.TOrder.Columns().OrderStatus, *in.OrderStatus)
|
|
}
|
|
if in.OrderType != nil {
|
|
mod = mod.Where(dao.TOrder.Columns().OrderType, *in.OrderType)
|
|
}
|
|
if in.PayType != nil {
|
|
mod = mod.Where(dao.TOrder.Columns().PayType, *in.PayType)
|
|
}
|
|
|
|
// 查询配送状态 枚举
|
|
if in.ShippingStatus != nil {
|
|
mod = mod.Where(dao.TOrder.Columns().ShippingStatus, *in.ShippingStatus)
|
|
}
|
|
|
|
// 查询退货状态 枚举
|
|
if in.ReturnStatus != nil {
|
|
mod = mod.Where(dao.TOrder.Columns().ReturnStatus, *in.ReturnStatus)
|
|
}
|
|
|
|
// 查询支付状态 枚举
|
|
if in.PayStatus != nil {
|
|
mod = mod.Where(dao.TOrder.Columns().PayStatus, *in.PayStatus)
|
|
}
|
|
|
|
// 查询分销分成状态 枚举
|
|
if in.DistributionStatus != nil {
|
|
mod = mod.Where(dao.TOrder.Columns().DistributionStatus, *in.DistributionStatus)
|
|
}
|
|
|
|
// 查询创建时间
|
|
if len(in.CreatedAt) == 2 {
|
|
mod = mod.WhereBetween(dao.TOrder.Columns().CreatedAt, in.CreatedAt[0], in.CreatedAt[1].EndOfDay())
|
|
}
|
|
mod = mod.LeftJoin(
|
|
deptTable+" AS "+customerDeptTable,
|
|
orderTable+"."+dao.TOrder.Columns().CustomerAgentId+"="+customerDeptTable+"."+dao.SysDept.Columns().DeptId,
|
|
)
|
|
mod = mod.LeftJoin(
|
|
deptTable+" AS "+agentDeptTable,
|
|
orderTable+"."+dao.TOrder.Columns().AgentId+"="+agentDeptTable+"."+dao.SysDept.Columns().DeptId,
|
|
)
|
|
|
|
// 分页
|
|
mod = mod.Page(in.Page, in.PageSize)
|
|
|
|
// 排序
|
|
mod = mod.OrderDesc(dao.TOrder.Columns().Id)
|
|
|
|
// 操作人摘要信息
|
|
mod = mod.Hook(hook.MemberSummary)
|
|
|
|
// 查询数据
|
|
if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
|
|
err = gerror.Wrap(err, "获取订单主表列表失败,请稍后重试!")
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// SellList 获取销售订单列表
|
|
func (s *sIotTOrder) SellList(ctx context.Context, in *model.TOrderListParam) (list []*model.TOrderListModel, totalCount int, err error) {
|
|
defer func() {
|
|
if err == nil {
|
|
err = s.enrichOrderList(ctx, list)
|
|
}
|
|
}()
|
|
mod := s.businessModel(ctx)
|
|
orderTable := dao.TOrder.Table()
|
|
deptTable := dao.SysDept.Table()
|
|
customerDeptTable := "customer_dept"
|
|
agentDeptTable := "agent_dept"
|
|
|
|
// 字段过滤
|
|
mod = mod.Fields(orderTable + ".*")
|
|
mod = mod.Fields(
|
|
customerDeptTable+".dept_name AS customerName",
|
|
agentDeptTable+".dept_name AS agentName",
|
|
)
|
|
|
|
agentID, err := currentOrderAgentID(ctx)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
mod = mod.Where(dao.TOrder.Columns().AgentId, agentID)
|
|
|
|
// 查询主键ID
|
|
if in.Id > 0 {
|
|
mod = mod.Where(dao.TOrder.Columns().Id, in.Id)
|
|
}
|
|
|
|
if orderSn := strings.TrimSpace(in.OrderSn); orderSn != "" {
|
|
mod = mod.WhereLike(dao.TOrder.Columns().OrderSn, "%"+orderSn+"%")
|
|
}
|
|
|
|
// 查询订单状态 枚举
|
|
if in.OrderStatus != nil {
|
|
mod = mod.Where(dao.TOrder.Columns().OrderStatus, *in.OrderStatus)
|
|
}
|
|
if in.OrderType != nil {
|
|
mod = mod.Where(dao.TOrder.Columns().OrderType, *in.OrderType)
|
|
}
|
|
if in.PayType != nil {
|
|
mod = mod.Where(dao.TOrder.Columns().PayType, *in.PayType)
|
|
}
|
|
|
|
// 查询配送状态 枚举
|
|
if in.ShippingStatus != nil {
|
|
mod = mod.Where(dao.TOrder.Columns().ShippingStatus, *in.ShippingStatus)
|
|
}
|
|
|
|
// 查询退货状态 枚举
|
|
if in.ReturnStatus != nil {
|
|
mod = mod.Where(dao.TOrder.Columns().ReturnStatus, *in.ReturnStatus)
|
|
}
|
|
|
|
// 查询支付状态 枚举
|
|
if in.PayStatus != nil {
|
|
mod = mod.Where(dao.TOrder.Columns().PayStatus, *in.PayStatus)
|
|
}
|
|
|
|
// 查询分销分成状态 枚举
|
|
if in.DistributionStatus != nil {
|
|
mod = mod.Where(dao.TOrder.Columns().DistributionStatus, *in.DistributionStatus)
|
|
}
|
|
|
|
// 查询创建时间
|
|
if len(in.CreatedAt) == 2 {
|
|
mod = mod.WhereBetween(dao.TOrder.Columns().CreatedAt, in.CreatedAt[0], in.CreatedAt[1].EndOfDay())
|
|
|
|
}
|
|
mod = mod.LeftJoin(
|
|
deptTable+" AS "+customerDeptTable,
|
|
orderTable+"."+dao.TOrder.Columns().CustomerAgentId+"="+customerDeptTable+"."+dao.SysDept.Columns().DeptId,
|
|
)
|
|
mod = mod.LeftJoin(
|
|
deptTable+" AS "+agentDeptTable,
|
|
orderTable+"."+dao.TOrder.Columns().AgentId+"="+agentDeptTable+"."+dao.SysDept.Columns().DeptId,
|
|
)
|
|
|
|
// 分页
|
|
mod = mod.Page(in.Page, in.PageSize)
|
|
|
|
// 排序
|
|
mod = mod.OrderDesc(dao.TOrder.Columns().Id)
|
|
|
|
// 操作人摘要信息
|
|
mod = mod.Hook(hook.MemberSummary)
|
|
|
|
// 查询数据
|
|
if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
|
|
err = gerror.Wrap(err, "获取销售订单列表失败,请稍后重试!")
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// Export 导出订单主表
|
|
func (s *sIotTOrder) enrichOrderList(ctx context.Context, list []*model.TOrderListModel) error {
|
|
orderIDs := make([]int64, 0, len(list))
|
|
byID := make(map[int64]*model.TOrderListModel, len(list))
|
|
for _, order := range list {
|
|
if order == nil || order.Id <= 0 {
|
|
continue
|
|
}
|
|
orderIDs = append(orderIDs, order.Id)
|
|
byID[order.Id] = order
|
|
}
|
|
if len(orderIDs) == 0 {
|
|
return nil
|
|
}
|
|
var items []*entity.TOrderItem
|
|
if err := dao.TOrderItem.Ctx(ctx).
|
|
WhereIn(dao.TOrderItem.Columns().OrderId, orderIDs).
|
|
OrderAsc(dao.TOrderItem.Columns().ItemId).
|
|
Scan(&items); err != nil {
|
|
return gerror.Wrap(err, "load order item summaries failed")
|
|
}
|
|
for _, item := range items {
|
|
order := byID[item.OrderId]
|
|
if order == nil {
|
|
continue
|
|
}
|
|
view := orderItemView(item)
|
|
normalizeOrderItemProduct(order.OrderType, view)
|
|
order.Quantity += view.Quantity
|
|
if order.ProductName == "" {
|
|
order.ProductName = view.ProductName
|
|
order.ProductSn = view.ProductSn
|
|
order.UnitPrice = view.Price
|
|
order.ServiceBeginTime = view.ServiceBeginTime
|
|
order.ServiceEndTime = view.ServiceEndTime
|
|
order.BusinessBatchNo = view.BusinessBatchNo
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func normalizeOrderItemProduct(orderType int, view *model.TOrderItemViewModel) {
|
|
if view == nil || (view.ProductName != "余额充值" && view.ProductSn != "BALANCE_RECHARGE") {
|
|
return
|
|
}
|
|
switch orderType {
|
|
case consts.OrderTypeCardRenew:
|
|
view.ProductName = "卡续费"
|
|
case consts.OrderTypeCardChange:
|
|
view.ProductName = "卡变更"
|
|
case consts.OrderTypeCardSubscribe:
|
|
view.ProductName = "卡订购"
|
|
default:
|
|
return
|
|
}
|
|
view.ProductSn = ""
|
|
}
|
|
|
|
func orderItemView(item *entity.TOrderItem) *model.TOrderItemViewModel {
|
|
view := &model.TOrderItemViewModel{
|
|
ItemId: item.ItemId,
|
|
Price: item.Price,
|
|
Quantity: item.Quantity,
|
|
ProductName: item.ProductName,
|
|
ProductSn: item.ProductSn,
|
|
ServiceBeginTime: item.ServiceBeginTime,
|
|
ServiceEndTime: item.ServiceEndTime,
|
|
}
|
|
if item.SkuData != "" {
|
|
sku := gjson.New(item.SkuData)
|
|
packageName := sku.Get("0.skuValue").String()
|
|
packageCode := sku.Get("0.packageCode").String()
|
|
if packageName != "" && (view.ProductName == "余额充值" || view.ProductName == "") {
|
|
view.ProductName = packageName
|
|
}
|
|
if packageCode != "" && (view.ProductSn == "BALANCE_RECHARGE" || view.ProductSn == "") {
|
|
view.ProductSn = packageCode
|
|
}
|
|
}
|
|
if item.ExtraSkuData == "" {
|
|
return view
|
|
}
|
|
extra := gjson.New(item.ExtraSkuData)
|
|
view.BusinessBatchNo = extra.Get("businessBatchNo").String()
|
|
view.SourceBatchNo = extra.Get("sourceBatchNo").String()
|
|
view.EffectiveMode = extra.Get("effectiveMode").String()
|
|
view.DurationValue = extra.Get("durationValue").Int()
|
|
view.DurationUnit = extra.Get("durationUnit").String()
|
|
return view
|
|
}
|
|
|
|
func (s *sIotTOrder) Export(ctx context.Context, in *model.TOrderListParam) (err error) {
|
|
query := *in
|
|
query.Page = 1
|
|
query.PageSize = orderExportPageSize
|
|
list := make([]*model.TOrderListModel, 0)
|
|
totalCount := 0
|
|
for {
|
|
var (
|
|
pageList []*model.TOrderListModel
|
|
pageTotal int
|
|
)
|
|
if query.Perspective == orderPerspectiveSell {
|
|
pageList, pageTotal, err = s.SellList(ctx, &query)
|
|
} else {
|
|
pageList, pageTotal, err = s.List(ctx, &query)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
totalCount = pageTotal
|
|
list = append(list, pageList...)
|
|
if len(pageList) == 0 || len(list) >= totalCount {
|
|
break
|
|
}
|
|
query.Page++
|
|
}
|
|
|
|
// 字段的排序是依据tags的字段顺序,如果你不想使用默认的排序方式,可以直接定义 tags = []string{"字段名称", "字段名称2", ...}
|
|
tags, err := convert.GetEntityDescTags(model.TOrderExportModel{})
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
var (
|
|
fileName = "导出订单主表-" + gctx.CtxId(ctx)
|
|
sheetName = fmt.Sprintf("筛选结果共%v行", totalCount)
|
|
exports []model.TOrderExportModel
|
|
)
|
|
|
|
if err = gconv.Scan(list, &exports); err != nil {
|
|
return
|
|
}
|
|
|
|
err = excel.ExportByStructs(ctx, tags, exports, fileName, sheetName)
|
|
return
|
|
}
|
|
|
|
// Edit 订单由业务流程生成,不允许后台手工修改。
|
|
func (s *sIotTOrder) Edit(ctx context.Context, in *model.TOrderEditParam) (err error) {
|
|
return gerror.New("订单由业务流程生成,不支持手工新增或修改")
|
|
}
|
|
|
|
// Delete 订单是业务审计记录,不允许后台手工删除。
|
|
func (s *sIotTOrder) Delete(ctx context.Context, in *model.TOrderDeleteParam) (err error) {
|
|
return gerror.New("订单属于业务审计记录,不支持删除")
|
|
}
|
|
|
|
// View 获取订单主表指定信息
|
|
func (s *sIotTOrder) View(ctx context.Context, in *model.TOrderViewParam) (res *model.TOrderViewModel, err error) {
|
|
defer func() {
|
|
if err == nil && res != nil {
|
|
err = s.enrichOrderView(ctx, res)
|
|
}
|
|
}()
|
|
agentID, err := currentOrderAgentID(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
columns := dao.TOrder.Columns()
|
|
if err = s.businessModel(ctx).
|
|
WherePri(in.Id).
|
|
Where("("+columns.CustomerAgentId+" = ? OR "+columns.AgentId+" = ?)", agentID, agentID).
|
|
Hook(hook.MemberSummary).
|
|
Scan(&res); err != nil {
|
|
err = gerror.Wrap(err, "获取订单主表信息,请稍后重试!")
|
|
return
|
|
}
|
|
if res == nil || res.Id <= 0 {
|
|
return nil, gerror.New("订单不存在或无权访问")
|
|
}
|
|
return
|
|
}
|
|
|
|
func (s *sIotTOrder) enrichOrderView(ctx context.Context, order *model.TOrderViewModel) error {
|
|
if order.Id <= 0 {
|
|
return gerror.New("订单不存在或无权访问")
|
|
}
|
|
deptIDs := []int64{order.CustomerAgentId, order.AgentId}
|
|
var depts []*entity.SysDept
|
|
if err := dao.SysDept.Ctx(ctx).
|
|
Fields(dao.SysDept.Columns().DeptId, dao.SysDept.Columns().DeptName).
|
|
WhereIn(dao.SysDept.Columns().DeptId, deptIDs).
|
|
Scan(&depts); err != nil {
|
|
return gerror.Wrap(err, "加载订单组织信息失败")
|
|
}
|
|
for _, dept := range depts {
|
|
if dept.DeptId == order.CustomerAgentId {
|
|
order.CustomerName = dept.DeptName
|
|
}
|
|
if dept.DeptId == order.AgentId {
|
|
order.AgentName = dept.DeptName
|
|
}
|
|
}
|
|
var items []*entity.TOrderItem
|
|
if err := dao.TOrderItem.Ctx(ctx).
|
|
Where(dao.TOrderItem.Columns().OrderId, order.Id).
|
|
OrderAsc(dao.TOrderItem.Columns().ItemId).
|
|
Scan(&items); err != nil {
|
|
return gerror.Wrap(err, "加载订单明细失败")
|
|
}
|
|
order.Items = make([]*model.TOrderItemViewModel, 0, len(items))
|
|
for _, item := range items {
|
|
view := orderItemView(item)
|
|
normalizeOrderItemProduct(order.OrderType, view)
|
|
order.Items = append(order.Items, view)
|
|
}
|
|
return nil
|
|
}
|