You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
4.3 KiB
136 lines
4.3 KiB
package console
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"yunyan/pb"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// ============================ 代工厂域 (oem_factory) ============================
|
|
//
|
|
// 代工厂 = 「这批货由哪家厂生产组装」,与品牌商(brand,「挂谁的牌子卖」)是两件事,故独立成表。
|
|
// 生产批次(production_batch.factoryid)引用本表:在「MAC生成管理」发码时选厂,落在批次台账上、
|
|
// 不写进每条授权码——换厂是批次级的事,授权码里再存一份只会在改厂时产生两处不一致。
|
|
|
|
// sortOemFactories 统一排序:sort 小的在前,相同再按 id。
|
|
func sortOemFactories(list []*OemFactory) {
|
|
sort.SliceStable(list, func(i, j int) bool {
|
|
if list[i].Sort != list[j].Sort {
|
|
return list[i].Sort < list[j].Sort
|
|
}
|
|
return list[i].Id < list[j].Id
|
|
})
|
|
}
|
|
|
|
func (this *serverComp) getOemFactories(c *gin.Context, sys *appConn) {
|
|
models, err := dvOemFactories(sys)
|
|
if err != nil {
|
|
writeErr(c, pb.ErrorCode_DBError, err.Error())
|
|
return
|
|
}
|
|
sortOemFactories(models)
|
|
writeOK(c, gin.H{"factories": models})
|
|
}
|
|
|
|
func (this *serverComp) addOemFactory(c *gin.Context, sys *appConn) {
|
|
var req struct {
|
|
Factory *OemFactory `json:"factory"`
|
|
}
|
|
_ = c.ShouldBindJSON(&req)
|
|
if req.Factory == nil || strings.TrimSpace(req.Factory.Name) == "" {
|
|
writeErr(c, pb.ErrorCode_ReqParameterError, "工厂名称必填")
|
|
return
|
|
}
|
|
req.Factory.Name = strings.TrimSpace(req.Factory.Name)
|
|
if msg := checkOemFactoryNameUnique(sys, req.Factory.Name, 0); msg != "" {
|
|
writeErr(c, pb.ErrorCode_ReqParameterError, msg)
|
|
return
|
|
}
|
|
req.Factory.Id = 0 // id 由自增分配
|
|
now := time.Now().Unix()
|
|
req.Factory.Createtime, req.Factory.Updatetime = now, now
|
|
if err := dvAddOemFactory(sys, req.Factory); err != nil {
|
|
writeErr(c, pb.ErrorCode_DBError, err.Error())
|
|
return
|
|
}
|
|
writeOK(c, gin.H{"factory": req.Factory})
|
|
}
|
|
|
|
func (this *serverComp) updateOemFactory(c *gin.Context, sys *appConn) {
|
|
var req struct {
|
|
Factory *OemFactory `json:"factory"`
|
|
}
|
|
_ = c.ShouldBindJSON(&req)
|
|
if req.Factory == nil || req.Factory.Id == 0 {
|
|
writeErr(c, pb.ErrorCode_ReqParameterError, "factory id is required")
|
|
return
|
|
}
|
|
if strings.TrimSpace(req.Factory.Name) == "" {
|
|
writeErr(c, pb.ErrorCode_ReqParameterError, "工厂名称必填")
|
|
return
|
|
}
|
|
req.Factory.Name = strings.TrimSpace(req.Factory.Name)
|
|
old, err := dvOemFactory(sys, req.Factory.Id)
|
|
if err != nil || old == nil || old.Id == 0 {
|
|
writeErr(c, pb.ErrorCode_ReqParameterError, "工厂不存在")
|
|
return
|
|
}
|
|
if msg := checkOemFactoryNameUnique(sys, req.Factory.Name, req.Factory.Id); msg != "" {
|
|
writeErr(c, pb.ErrorCode_ReqParameterError, msg)
|
|
return
|
|
}
|
|
req.Factory.Createtime = old.Createtime // 建档时间不可改
|
|
req.Factory.Updatetime = time.Now().Unix()
|
|
if err := dvSaveOemFactory(sys, req.Factory); err != nil {
|
|
writeErr(c, pb.ErrorCode_DBError, err.Error())
|
|
return
|
|
}
|
|
writeOK(c, gin.H{"factory": req.Factory})
|
|
}
|
|
|
|
func (this *serverComp) delOemFactory(c *gin.Context, sys *appConn) {
|
|
var req struct {
|
|
Id uint32 `json:"id"`
|
|
}
|
|
_ = c.ShouldBindJSON(&req)
|
|
if req.Id == 0 {
|
|
writeErr(c, pb.ErrorCode_ReqParameterError, "id is required")
|
|
return
|
|
}
|
|
// 生产批次引用了工厂就不许删:factoryid 是裸整数列、没有外键,删掉后那些批次的工厂
|
|
// 会变成查不到名字的孤儿数值,而批次台账是生产追溯的依据,不能出现指不到人的记录。
|
|
n, err := dvCountBatchesByFactory(sys, req.Id)
|
|
if err != nil {
|
|
writeErr(c, pb.ErrorCode_DBError, err.Error())
|
|
return
|
|
}
|
|
if n > 0 {
|
|
writeErr(c, pb.ErrorCode_ReqParameterError,
|
|
fmt.Sprintf("该工厂已关联 %d 个生产批次,不能删除(批次台账要能追溯到厂)。不再合作请改成「停用」,停用后不再出现在发码时的工厂下拉里", n))
|
|
return
|
|
}
|
|
if err := dvDelOemFactory(sys, req.Id); err != nil {
|
|
writeErr(c, pb.ErrorCode_DBError, err.Error())
|
|
return
|
|
}
|
|
writeOK(c, gin.H{"id": req.Id})
|
|
}
|
|
|
|
// checkOemFactoryNameUnique 同名校验(排除自己)。返回空串表示通过。
|
|
func checkOemFactoryNameUnique(sys *appConn, name string, selfId uint32) string {
|
|
list, err := dvOemFactories(sys)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
for _, f := range list {
|
|
if f.Id != selfId && strings.EqualFold(strings.TrimSpace(f.Name), name) {
|
|
return fmt.Sprintf("工厂「%s」已存在", name)
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|