2026-08-28 15:06:01 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2026-09-04 14:18:53 +08:00
|
|
|
"strconv"
|
2026-09-03 14:20:37 +08:00
|
|
|
"time"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
|
|
|
|
"bj_power_wms/ent"
|
|
|
|
|
"bj_power_wms/ent/inbounddetail"
|
|
|
|
|
"bj_power_wms/ent/inboundorder"
|
2026-09-02 08:21:11 +08:00
|
|
|
"bj_power_wms/ent/inventory"
|
2026-08-28 15:06:01 +08:00
|
|
|
"bj_power_wms/ent/material"
|
|
|
|
|
"bj_power_wms/internal/svc"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// createInboundHandler 入库
|
|
|
|
|
// 结构件: { inboundType, materialCode, batchNo, quantity, zoneCode, operator }
|
2026-09-10 16:59:15 +08:00
|
|
|
// 电气件: { inboundType, materialCode, snList: [sn...], zoneCode, operator }
|
2026-09-02 08:21:11 +08:00
|
|
|
//
|
|
|
|
|
// 事务保证:入库单 + 库存行(inventory) + 入库明细 三者要么全成功,要么全回滚,杜绝半成品状态。
|
2026-08-28 15:06:01 +08:00
|
|
|
func createInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var req struct {
|
|
|
|
|
InboundType string `json:"inboundType"` // purchase/semi/finished/return
|
|
|
|
|
MaterialCode string `json:"materialCode"`
|
|
|
|
|
BatchNo string `json:"batchNo"`
|
|
|
|
|
Quantity int `json:"quantity"`
|
|
|
|
|
SnList []string `json:"snList"`
|
|
|
|
|
ZoneCode string `json:"zoneCode"`
|
|
|
|
|
Operator string `json:"operator"`
|
|
|
|
|
ProductionDate string `json:"productionDate"`
|
|
|
|
|
Supplier string `json:"supplier"`
|
|
|
|
|
Remark string `json:"remark"`
|
2026-09-10 16:59:15 +08:00
|
|
|
InspectionNo string `json:"inspectionNo"`
|
2026-08-28 15:06:01 +08:00
|
|
|
}
|
|
|
|
|
if err := parseJSON(r, &req); err != nil {
|
|
|
|
|
fail(w, http.StatusBadRequest, "参数错误: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if req.MaterialCode == "" {
|
2026-08-29 15:30:25 +08:00
|
|
|
fail(w, http.StatusBadRequest, "物料编码必填")
|
2026-08-28 15:06:01 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-09-01 13:49:44 +08:00
|
|
|
if req.ZoneCode == "" {
|
2026-09-10 16:59:15 +08:00
|
|
|
fail(w, http.StatusBadRequest, "区域必填,请先选择入库区域")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if req.InspectionNo == "" {
|
|
|
|
|
fail(w, http.StatusBadRequest, "检测单号必填,请填写入库检报告单号")
|
|
|
|
|
return
|
2026-09-01 13:49:44 +08:00
|
|
|
}
|
2026-08-28 15:06:01 +08:00
|
|
|
|
|
|
|
|
// 物料档案
|
|
|
|
|
m, err := ctx.EntClient.Material.Query().
|
|
|
|
|
Where(material.CodeEQ(req.MaterialCode)).
|
|
|
|
|
Only(ctx0())
|
|
|
|
|
if err != nil {
|
|
|
|
|
fail(w, http.StatusBadRequest, "物料不存在: "+req.MaterialCode)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inboundNo := "IB" + uuid.NewString()[:12]
|
|
|
|
|
|
2026-09-02 08:21:11 +08:00
|
|
|
// 开启事务:入库单 + 库存行 + 明细 原子提交
|
|
|
|
|
tx, err := ctx.EntClient.Tx(ctx0())
|
|
|
|
|
if err != nil {
|
|
|
|
|
fail(w, http.StatusInternalServerError, "开启事务失败: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
committed := false
|
|
|
|
|
defer func() {
|
|
|
|
|
if !committed {
|
|
|
|
|
_ = tx.Rollback()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
ib, err := tx.InboundOrder.Create().
|
2026-08-28 15:06:01 +08:00
|
|
|
SetInboundNo(inboundNo).
|
|
|
|
|
SetNillableInboundType(strPtr(req.InboundType)).
|
|
|
|
|
SetMaterialCode(req.MaterialCode).
|
|
|
|
|
SetNillableMaterialName(strPtr(m.Name)).
|
|
|
|
|
SetManageMode(m.ManageMode).
|
|
|
|
|
SetNillableZoneCode(strPtr(req.ZoneCode)).
|
|
|
|
|
SetQuantity(0).
|
|
|
|
|
SetNillableOperator(strPtr(req.Operator)).
|
|
|
|
|
SetNillableRemark(strPtr(req.Remark)).
|
2026-09-10 16:59:15 +08:00
|
|
|
SetInspectionNo(req.InspectionNo).
|
2026-08-28 15:06:01 +08:00
|
|
|
Save(ctx0())
|
|
|
|
|
if err != nil {
|
|
|
|
|
fail(w, http.StatusInternalServerError, "创建入库单失败: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
totalQty := 0
|
|
|
|
|
|
|
|
|
|
// 结构件(批次管理)
|
|
|
|
|
if m.ManageMode == 1 {
|
2026-09-01 13:49:44 +08:00
|
|
|
// 结构件走批次入库,不允许携带 SN 清单
|
|
|
|
|
if len(req.SnList) > 0 {
|
|
|
|
|
fail(w, http.StatusBadRequest, "该物料为结构件(按批次管理),不应提交 SN 清单,请使用『结构件入库』")
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-08-28 15:06:01 +08:00
|
|
|
batchNo := req.BatchNo
|
|
|
|
|
if batchNo == "" {
|
|
|
|
|
batchNo = genBatchNo(req.MaterialCode)
|
|
|
|
|
}
|
|
|
|
|
qty := req.Quantity
|
|
|
|
|
if qty <= 0 {
|
2026-09-01 13:49:44 +08:00
|
|
|
fail(w, http.StatusBadRequest, "数量必填且大于 0,请输入本次入库数量")
|
2026-08-28 15:06:01 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 同批次追加(多批到货追加)
|
2026-09-02 08:21:11 +08:00
|
|
|
existing, e := tx.Inventory.Query().
|
|
|
|
|
Where(inventory.ManageModeEQ(1), inventory.BatchNoEQ(batchNo)).Only(ctx0())
|
|
|
|
|
if e == nil {
|
|
|
|
|
if _, err = tx.Inventory.UpdateOneID(existing.ID).
|
|
|
|
|
AddQuantity(qty).
|
2026-08-28 15:06:01 +08:00
|
|
|
SetNillableSupplier(strPtr(req.Supplier)).
|
2026-09-02 08:21:11 +08:00
|
|
|
Save(ctx0()); err != nil {
|
|
|
|
|
fail(w, http.StatusInternalServerError, "追加批次失败: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-08-28 15:06:01 +08:00
|
|
|
} else {
|
2026-09-02 08:21:11 +08:00
|
|
|
if _, err = tx.Inventory.Create().
|
|
|
|
|
SetManageMode(1).
|
2026-08-28 15:06:01 +08:00
|
|
|
SetMaterialCode(req.MaterialCode).
|
|
|
|
|
SetNillableMaterialName(strPtr(m.Name)).
|
2026-09-02 08:21:11 +08:00
|
|
|
SetBatchNo(batchNo).
|
|
|
|
|
SetQuantity(qty).SetLockedQty(0).
|
2026-08-28 15:06:01 +08:00
|
|
|
SetNillableProductionDate(strPtr(req.ProductionDate)).
|
|
|
|
|
SetNillableSupplier(strPtr(req.Supplier)).
|
|
|
|
|
SetQualityStatus("未检").
|
|
|
|
|
SetNillableZoneCode(strPtr(req.ZoneCode)).
|
2026-09-02 08:21:11 +08:00
|
|
|
SetStatus("在库").
|
|
|
|
|
SetInboundNo(inboundNo).
|
|
|
|
|
Save(ctx0()); err != nil {
|
|
|
|
|
fail(w, http.StatusInternalServerError, "创建库存行失败: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-08-28 15:06:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 明细
|
2026-09-02 08:21:11 +08:00
|
|
|
if _, err = tx.InboundDetail.Create().
|
2026-08-28 15:06:01 +08:00
|
|
|
SetInboundNo(inboundNo).
|
|
|
|
|
SetBatchNo(batchNo).
|
|
|
|
|
SetMaterialCode(req.MaterialCode).
|
|
|
|
|
SetQuantity(qty).
|
2026-09-02 08:21:11 +08:00
|
|
|
Save(ctx0()); err != nil {
|
|
|
|
|
fail(w, http.StatusInternalServerError, "创建入库明细失败: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-08-28 15:06:01 +08:00
|
|
|
totalQty = qty
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 16:59:15 +08:00
|
|
|
// 电气件(SN 管理)
|
2026-08-28 15:06:01 +08:00
|
|
|
if m.ManageMode == 2 {
|
|
|
|
|
if len(req.SnList) == 0 {
|
2026-09-10 16:59:15 +08:00
|
|
|
fail(w, http.StatusBadRequest, "该物料为电气件(按 SN 管理),请至少录入 1 条序列号(SN)")
|
2026-08-28 15:06:01 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for _, sn := range req.SnList {
|
|
|
|
|
// 查重
|
2026-09-02 08:21:11 +08:00
|
|
|
exists, _ := tx.Inventory.Query().
|
|
|
|
|
Where(inventory.ManageModeEQ(2), inventory.SnCodeEQ(sn)).
|
2026-08-28 15:06:01 +08:00
|
|
|
Exist(ctx0())
|
|
|
|
|
if exists {
|
|
|
|
|
fail(w, http.StatusConflict, "SN 已存在: "+sn)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-09-02 08:21:11 +08:00
|
|
|
if _, err = tx.Inventory.Create().
|
|
|
|
|
SetManageMode(2).
|
2026-08-28 15:06:01 +08:00
|
|
|
SetMaterialCode(req.MaterialCode).
|
2026-09-02 08:21:11 +08:00
|
|
|
SetNillableMaterialName(strPtr(m.Name)).
|
|
|
|
|
SetSnCode(sn).
|
|
|
|
|
SetQuantity(1).SetLockedQty(0).
|
2026-08-28 15:06:01 +08:00
|
|
|
SetQualityStatus("未检").
|
2026-09-02 08:21:11 +08:00
|
|
|
SetNillableZoneCode(strPtr(req.ZoneCode)).
|
|
|
|
|
SetStatus("在库").
|
|
|
|
|
SetInboundNo(inboundNo).
|
|
|
|
|
Save(ctx0()); err != nil {
|
|
|
|
|
fail(w, http.StatusInternalServerError, "创建 SN 库存行失败: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if _, err = tx.InboundDetail.Create().
|
2026-08-28 15:06:01 +08:00
|
|
|
SetInboundNo(inboundNo).
|
|
|
|
|
SetSnCode(sn).
|
|
|
|
|
SetMaterialCode(req.MaterialCode).
|
|
|
|
|
SetQuantity(1).
|
2026-09-02 08:21:11 +08:00
|
|
|
Save(ctx0()); err != nil {
|
|
|
|
|
fail(w, http.StatusInternalServerError, "创建入库明细失败: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-08-28 15:06:01 +08:00
|
|
|
}
|
|
|
|
|
totalQty = len(req.SnList)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新入库单数量
|
2026-09-02 08:21:11 +08:00
|
|
|
if _, err = tx.InboundOrder.UpdateOneID(ib.ID).
|
|
|
|
|
SetQuantity(totalQty).Save(ctx0()); err != nil {
|
|
|
|
|
fail(w, http.StatusInternalServerError, "更新入库单数量失败: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = tx.Commit(); err != nil {
|
|
|
|
|
fail(w, http.StatusInternalServerError, "提交事务失败: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
committed = true
|
2026-08-28 15:06:01 +08:00
|
|
|
|
2026-09-04 14:18:53 +08:00
|
|
|
ctx.EventLog.Write(ctx0(), "inbound.create", r.Header.Get("X-Username"), "inbound", inboundNo,
|
|
|
|
|
"入库 "+req.MaterialCode+" 数量 "+strconv.Itoa(totalQty), map[string]any{
|
|
|
|
|
"inboundType": req.InboundType, "quantity": totalQty, "snCount": len(req.SnList)})
|
2026-08-28 15:06:01 +08:00
|
|
|
logx.Infof("inbound: no=%s material=%s qty=%d by=%s", inboundNo, req.MaterialCode, totalQty, req.Operator)
|
|
|
|
|
ok(w, map[string]any{
|
|
|
|
|
"inboundNo": inboundNo,
|
|
|
|
|
"quantity": totalQty,
|
|
|
|
|
"batchNo": req.BatchNo,
|
|
|
|
|
"snCount": len(req.SnList),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// excelInboundHandler 实现见 excel.go
|
|
|
|
|
|
2026-09-03 14:20:37 +08:00
|
|
|
// inboundRow 列表/导出统一结构:入库单 + 明细总数。
|
2026-09-10 16:59:15 +08:00
|
|
|
// 注意:明细(批次/SN)不在此内嵌——电气件单可能上万条 SN,
|
2026-09-03 14:20:37 +08:00
|
|
|
// 列表查询若一次性加载全部明细会撑爆内存,故只带总数,明细按需分页加载。
|
|
|
|
|
type inboundRow struct {
|
|
|
|
|
*ent.InboundOrder
|
|
|
|
|
DetailCount int `json:"detailCount"`
|
|
|
|
|
}
|
2026-08-28 15:06:01 +08:00
|
|
|
|
2026-09-03 14:20:37 +08:00
|
|
|
// applyInboundFilters 统一列表与导出的筛选条件
|
2026-09-10 16:59:15 +08:00
|
|
|
// 结构件(manageMode=1)与电气件(manageMode=2)同主表(inbound_order),按 manageMode 区分类型
|
2026-09-03 14:20:37 +08:00
|
|
|
func applyInboundFilters(q *ent.InboundOrderQuery, r *http.Request) *ent.InboundOrderQuery {
|
|
|
|
|
if v := r.URL.Query().Get("inboundNo"); v != "" {
|
2026-09-08 11:44:04 +08:00
|
|
|
q = q.Where(inboundorder.InboundNoContainsFold(v))
|
2026-09-03 14:20:37 +08:00
|
|
|
}
|
|
|
|
|
if v := r.URL.Query().Get("materialCode"); v != "" {
|
2026-09-08 11:44:04 +08:00
|
|
|
q = q.Where(inboundorder.MaterialCodeContainsFold(v))
|
2026-09-03 14:20:37 +08:00
|
|
|
}
|
|
|
|
|
if v := r.URL.Query().Get("manageMode"); v != "" {
|
|
|
|
|
if m := atoi(v, 0); m != 0 {
|
|
|
|
|
q = q.Where(inboundorder.ManageModeEQ(m))
|
2026-08-28 15:06:01 +08:00
|
|
|
}
|
2026-09-03 14:20:37 +08:00
|
|
|
}
|
|
|
|
|
if v := r.URL.Query().Get("inboundType"); v != "" {
|
|
|
|
|
q = q.Where(inboundorder.InboundTypeEQ(v))
|
|
|
|
|
}
|
2026-09-08 13:43:08 +08:00
|
|
|
if v := r.URL.Query().Get("materialName"); v != "" {
|
|
|
|
|
// 2026-09-08 用户明确:禁止"关键字"多字段混搜,各字段独立筛选
|
|
|
|
|
q = q.Where(inboundorder.MaterialNameContainsFold(v))
|
2026-09-03 14:20:37 +08:00
|
|
|
}
|
|
|
|
|
if v := r.URL.Query().Get("startDate"); v != "" {
|
|
|
|
|
if t, err := time.ParseInLocation("2006-01-02", v, time.Local); err == nil {
|
|
|
|
|
q = q.Where(inboundorder.CreatedAtGTE(t.Unix()))
|
2026-08-28 15:06:01 +08:00
|
|
|
}
|
2026-09-03 14:20:37 +08:00
|
|
|
}
|
|
|
|
|
if v := r.URL.Query().Get("endDate"); v != "" {
|
|
|
|
|
if t, err := time.ParseInLocation("2006-01-02", v, time.Local); err == nil {
|
|
|
|
|
// 含当天全天
|
|
|
|
|
q = q.Where(inboundorder.CreatedAtLTE(t.Add(24 * time.Hour).Unix()))
|
2026-08-28 15:06:01 +08:00
|
|
|
}
|
2026-09-03 14:20:37 +08:00
|
|
|
}
|
|
|
|
|
return q
|
|
|
|
|
}
|
2026-08-28 15:06:01 +08:00
|
|
|
|
2026-09-03 14:20:37 +08:00
|
|
|
// buildInboundRows 仅组装入库单 + 各单明细总数(count,代价极低,不加载明细内容)
|
|
|
|
|
func buildInboundRows(ctx *svc.ServiceContext, list []*ent.InboundOrder) []inboundRow {
|
|
|
|
|
rows := make([]inboundRow, 0, len(list))
|
|
|
|
|
for _, ib := range list {
|
|
|
|
|
cnt, _ := ctx.EntClient.InboundDetail.Query().
|
|
|
|
|
Where(inbounddetail.InboundNoEQ(ib.InboundNo)).Count(ctx0())
|
|
|
|
|
rows = append(rows, inboundRow{ib, cnt})
|
|
|
|
|
}
|
|
|
|
|
return rows
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// inboundDetailsHandler 入库单明细分页查询(批次/SN)。
|
|
|
|
|
// 单独接口、按需加载、分页返回——避免一次拉取上万条 SN 撑爆内存/前端。
|
|
|
|
|
// 参数:inboundNo(必填) page pageSize(默认 50)
|
|
|
|
|
func inboundDetailsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
inboundNo := r.URL.Query().Get("inboundNo")
|
|
|
|
|
if inboundNo == "" {
|
|
|
|
|
fail(w, http.StatusBadRequest, "inboundNo 必填")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
page := atoi(r.URL.Query().Get("page"), 1)
|
|
|
|
|
pageSize := atoi(r.URL.Query().Get("pageSize"), 50)
|
|
|
|
|
if page < 1 {
|
|
|
|
|
page = 1
|
|
|
|
|
}
|
|
|
|
|
if pageSize < 1 || pageSize > 500 {
|
|
|
|
|
pageSize = 50
|
|
|
|
|
}
|
|
|
|
|
q := ctx.EntClient.InboundDetail.Query().
|
|
|
|
|
Where(inbounddetail.InboundNoEQ(inboundNo))
|
2026-08-28 15:06:01 +08:00
|
|
|
total, err := q.Count(ctx0())
|
|
|
|
|
if err != nil {
|
|
|
|
|
fail(w, http.StatusInternalServerError, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-09-03 18:13:59 +08:00
|
|
|
list, err := q.Order(ent.Desc("created_at"), ent.Desc("id")).
|
2026-08-28 15:06:01 +08:00
|
|
|
Offset((page - 1) * pageSize).Limit(pageSize).All(ctx0())
|
|
|
|
|
if err != nil {
|
|
|
|
|
fail(w, http.StatusInternalServerError, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-09-03 14:20:37 +08:00
|
|
|
ok(w, map[string]any{
|
|
|
|
|
"total": total,
|
|
|
|
|
"list": list,
|
|
|
|
|
"page": page,
|
|
|
|
|
"pageSize": pageSize,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// queryInboundHandler 入库单查询(分页列表)
|
2026-09-10 16:59:15 +08:00
|
|
|
// 结构件/电气件同主表,默认返回全部;支持多条件筛选。
|
2026-09-03 14:20:37 +08:00
|
|
|
func queryInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
page := atoi(r.URL.Query().Get("page"), 1)
|
|
|
|
|
pageSize := atoi(r.URL.Query().Get("pageSize"), 20)
|
|
|
|
|
if page < 1 {
|
|
|
|
|
page = 1
|
|
|
|
|
}
|
|
|
|
|
if pageSize < 1 {
|
|
|
|
|
pageSize = 20
|
|
|
|
|
}
|
|
|
|
|
q := ctx.EntClient.InboundOrder.Query()
|
|
|
|
|
q = applyInboundFilters(q, r)
|
2026-08-28 15:06:01 +08:00
|
|
|
|
2026-09-03 14:20:37 +08:00
|
|
|
total, err := q.Count(ctx0())
|
|
|
|
|
if err != nil {
|
|
|
|
|
fail(w, http.StatusInternalServerError, err.Error())
|
|
|
|
|
return
|
2026-08-28 15:06:01 +08:00
|
|
|
}
|
2026-09-03 18:13:59 +08:00
|
|
|
list, err := q.Order(ent.Desc("created_at"), ent.Desc("id")).
|
2026-09-03 14:20:37 +08:00
|
|
|
Offset((page - 1) * pageSize).Limit(pageSize).All(ctx0())
|
|
|
|
|
if err != nil {
|
|
|
|
|
fail(w, http.StatusInternalServerError, err.Error())
|
|
|
|
|
return
|
2026-08-28 15:06:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ok(w, map[string]any{
|
|
|
|
|
"total": total,
|
2026-09-03 14:20:37 +08:00
|
|
|
"list": buildInboundRows(ctx, list),
|
2026-08-28 15:06:01 +08:00
|
|
|
"page": page,
|
|
|
|
|
"pageSize": pageSize,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-03 14:20:37 +08:00
|
|
|
// exportInboundHandler 入库单导出:按筛选条件返回全部(不分页),前端转 CSV,
|
|
|
|
|
// 与 /api/outbound/export 保持一致。
|
|
|
|
|
func exportInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2026-09-08 11:44:04 +08:00
|
|
|
if _, _, rerr := normalizeExportRange(r.URL.Query().Get("startDate"), r.URL.Query().Get("endDate")); rerr != nil {
|
|
|
|
|
fail(w, http.StatusBadRequest, rerr.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-09-03 14:20:37 +08:00
|
|
|
q := ctx.EntClient.InboundOrder.Query()
|
|
|
|
|
q = applyInboundFilters(q, r)
|
2026-09-03 18:13:59 +08:00
|
|
|
list, err := q.Order(ent.Desc("created_at"), ent.Desc("id")).All(ctx0())
|
2026-09-03 14:20:37 +08:00
|
|
|
if err != nil {
|
|
|
|
|
fail(w, http.StatusInternalServerError, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
rows := buildInboundRows(ctx, list)
|
|
|
|
|
// xlsx 导出:格式化 = xlsx → 直接生成二进制文件;否则保持 JSON 兼容
|
|
|
|
|
if r.URL.Query().Get("format") == "xlsx" {
|
2026-09-10 16:59:15 +08:00
|
|
|
headers := []string{"入库单号", "物料编码", "物料名称", "类型", "数量", "区域", "操作人", "备注", "检测单号", "明细条数", "入库时间"}
|
2026-09-03 14:20:37 +08:00
|
|
|
matrix := make([][]any, 0, len(rows))
|
|
|
|
|
for _, row := range rows {
|
|
|
|
|
ib := row.InboundOrder
|
|
|
|
|
matrix = append(matrix, []any{
|
|
|
|
|
ib.InboundNo, ib.MaterialCode, ib.MaterialName,
|
|
|
|
|
manageModeLabel(ib.ManageMode), ib.Quantity, ib.ZoneCode,
|
|
|
|
|
ib.Operator, ib.Remark, row.DetailCount,
|
2026-09-10 16:59:15 +08:00
|
|
|
ib.InspectionNo,
|
2026-09-03 14:20:37 +08:00
|
|
|
unixFmt(ib.CreatedAt),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
sendExcel(w, xlsxFilename("入库管理"), headers, matrix)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ok(w, map[string]any{"total": len(rows), "list": rows})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-28 15:06:01 +08:00
|
|
|
// genBatchNo 生成批次号:日期 + 自增
|
|
|
|
|
func genBatchNo(materialCode string) string {
|
|
|
|
|
// TODO: 使用数据库序列或 Redis INCR 保证自增唯一
|
|
|
|
|
// 暂用时间戳 + 随机后缀
|
|
|
|
|
now := nowStr()
|
|
|
|
|
return "B" + now + materialCodeSuffix(materialCode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func nowStr() string {
|
|
|
|
|
// YYYYMMDDHHMMSS
|
|
|
|
|
const layout = "20060102150405"
|
|
|
|
|
return timeNow().Format(layout)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func materialCodeSuffix(code string) string {
|
|
|
|
|
if len(code) > 4 {
|
|
|
|
|
code = code[len(code)-4:]
|
|
|
|
|
}
|
|
|
|
|
return code
|
|
|
|
|
}
|