chore: 并入 iotlcm-admin-svr 历史到 admin-svr/(原提交 49818c6)
This commit is contained in:
@@ -0,0 +1,461 @@
|
||||
// Package gen
|
||||
// @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 iot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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/exportcenter"
|
||||
"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"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// Export 导出订单(异步:提交任务后到下载中心取文件)
|
||||
func (s *sIotTOrder) Export(ctx context.Context, in *model.TOrderListParam) (err error) {
|
||||
_, err = exportcenter.Submit(ctx, &exportcenter.Request{
|
||||
BizKey: "tOrder",
|
||||
BizName: "订单",
|
||||
PermCode: "cpm:iot:tOrder:export",
|
||||
QueryParam: in,
|
||||
Tags: func() ([]string, error) {
|
||||
return convert.GetEntityDescTags(model.TOrderListModel{})
|
||||
},
|
||||
Fetch: func(ctx context.Context, page, pageSize int) (rows []interface{}, total int, err error) {
|
||||
query := *in
|
||||
query.Page = page
|
||||
query.PageSize = pageSize
|
||||
var list []*model.TOrderListModel
|
||||
var count int
|
||||
if query.Perspective == orderPerspectiveSell {
|
||||
list, count, err = s.SellList(ctx, &query)
|
||||
} else {
|
||||
list, count, err = s.List(ctx, &query)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
rows = make([]interface{}, 0, len(list))
|
||||
for _, item := range list {
|
||||
rows = append(rows, item)
|
||||
}
|
||||
return rows, count, nil
|
||||
},
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
Reference in New Issue
Block a user