animeic 2 years ago
parent
commit
0e5dd9190e

+ 124 - 0
boxcost/api/bill.go

@@ -0,0 +1,124 @@
+package api
+
+import (
+	"box-cost/db/model"
+	"box-cost/db/repo"
+	"box-cost/log"
+	"errors"
+	"fmt"
+	"time"
+
+	"github.com/gin-gonic/gin"
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+// 单据管理
+func Bill(r *GinRouter) {
+
+	// 创建单据
+	r.POST("/bill", CreateBill)
+
+	// 获取单据详情
+	r.GET("/bill/:id", GetBill)
+
+	// 获取单据列表
+	r.GET("/bills", GetBills)
+
+	// 更新单据
+	r.POST("/bill/update", UpdateBill)
+
+	// 删除单据
+	r.POST("/bill/delete/:id", DelBill)
+}
+
+// 创建单据
+func CreateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	var bill model.Bill
+
+	err := c.ShouldBindJSON(&bill)
+	if err != nil {
+		fmt.Println(err)
+		return nil, errors.New("参数错误!")
+	}
+	ctx := apictx.CreateRepoCtx()
+
+	if bill.PackId.Hex() == "" {
+		return nil, errors.New("包装产品id为空")
+	}
+	if bill.PlanId.Hex() == "" {
+		return nil, errors.New("生产计划id为空")
+	}
+	if bill.Type == "" {
+		return nil, errors.New("类型为空")
+	}
+
+	bill.Status = "created"
+	bill.CreateTime = time.Now()
+	bill.UpdateTime = time.Now()
+
+	result, err := repo.RepoAddDoc(ctx, repo.CollectionBill, &bill)
+	return result, err
+}
+
+// 获取单据信息
+func GetBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	billId := c.Param("id")
+	id, err := primitive.ObjectIDFromHex(billId)
+	if err != nil {
+		return nil, errors.New("非法id")
+	}
+	var bill model.Bill
+	option := &repo.DocSearchOptions{
+		CollectName: repo.CollectionBill,
+		Query:       repo.Map{"_id": id},
+	}
+
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
+	if !found || err != nil {
+		log.Info(err)
+		return nil, errors.New("数据未找到")
+	}
+
+	return bill, nil
+}
+
+// 获取单据列表
+func GetBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	page, size, query := UtilQueryPageSize(c)
+
+	option := &repo.PageSearchOptions{
+		CollectName: repo.CollectionBill,
+		Query:       query,
+		Page:        page,
+		Size:        size,
+		Sort:        bson.M{"createTime": -1},
+	}
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
+}
+
+// 更新单据
+func UpdateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var bill model.Bill
+	err := c.ShouldBindJSON(&bill)
+	if err != nil {
+		return nil, errors.New("参数错误")
+	}
+	if bill.Id.Hex() == "" {
+		return nil, errors.New("id的为空")
+	}
+	bill.UpdateTime = time.Now()
+	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBill, bill.Id.Hex(), &bill)
+}
+
+// 删除单据
+func DelBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	billId := c.Param("id")
+	if billId == "" {
+		return nil, errors.New("id为空")
+	}
+
+	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBill, billId)
+}

+ 117 - 0
boxcost/api/craft.go

@@ -0,0 +1,117 @@
+package api
+
+import (
+	"box-cost/db/model"
+	"box-cost/db/repo"
+	"box-cost/log"
+	"errors"
+	"fmt"
+	"time"
+
+	"github.com/gin-gonic/gin"
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+// 工艺管理
+func Craft(r *GinRouter) {
+
+	// 创建工艺
+	r.POST("/craft", CreateCraft)
+
+	// 获取工艺详情
+	r.GET("/craft/:id", GetCraft)
+
+	// 获取工艺列表
+	r.GET("/crafts", GetCrafts)
+
+	// 更新工艺
+	r.POST("/craft/update", UpdateCraft)
+
+	// 删除工艺
+	r.POST("/craft/delete/:id", DelCraft)
+}
+
+// 创建工艺
+func CreateCraft(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	var craft model.Craft
+
+	err := c.ShouldBindJSON(&craft)
+	if err != nil {
+		fmt.Println(err)
+		return nil, errors.New("参数错误!")
+	}
+	ctx := apictx.CreateRepoCtx()
+
+	if craft.Name == "" {
+		return nil, errors.New("工艺名为空")
+	}
+
+	craft.CreateTime = time.Now()
+	craft.UpdateTime = time.Now()
+
+	result, err := repo.RepoAddDoc(ctx, repo.CollectionCraft, &craft)
+	return result, err
+}
+
+// 获取工艺信息
+func GetCraft(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	craftId := c.Param("id")
+	id, err := primitive.ObjectIDFromHex(craftId)
+	if err != nil {
+		return nil, errors.New("非法id")
+	}
+	var craft model.Craft
+	option := &repo.DocSearchOptions{
+		CollectName: repo.CollectionCraft,
+		Query:       repo.Map{"_id": id},
+	}
+
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &craft)
+	if !found || err != nil {
+		log.Info(err)
+		return nil, errors.New("数据未找到")
+	}
+
+	return craft, nil
+}
+
+// 获取工艺列表
+func GetCrafts(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	page, size, query := UtilQueryPageSize(c)
+
+	option := &repo.PageSearchOptions{
+		CollectName: repo.CollectionCraft,
+		Query:       query,
+		Page:        page,
+		Size:        size,
+		Sort:        bson.M{"createTime": -1},
+	}
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
+}
+
+// 更新工艺
+func UpdateCraft(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var craft model.Craft
+	err := c.ShouldBindJSON(&craft)
+	if err != nil {
+		return nil, errors.New("参数错误")
+	}
+	if craft.Id.Hex() == "" {
+		return nil, errors.New("id的为空")
+	}
+	craft.UpdateTime = time.Now()
+	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionCraft, craft.Id.Hex(), &craft)
+}
+
+// 删除工艺
+func DelCraft(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	craftId := c.Param("id")
+	if craftId == "" {
+		return nil, errors.New("id为空")
+	}
+
+	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionCraft, craftId)
+}

+ 100 - 0
boxcost/api/material.go

@@ -0,0 +1,100 @@
+package api
+
+import (
+	"box-cost/db/model"
+	"box-cost/db/repo"
+	"box-cost/log"
+	"errors"
+	"time"
+
+	"github.com/gin-gonic/gin"
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+// 材料管理
+// 不提供修改
+func Material(r *GinRouter) {
+
+	// 新增材料
+	r.POST("/material", CreateMaterial)
+
+	// 获取材料信息
+	r.GET("/material/:id", GetMaterial)
+
+	// 获取材料列表
+	r.GET("/materials", GetMaterials)
+
+	r.POST("/material/delete/:id", DelMaterial)
+}
+
+func CreateMaterial(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	var material model.Material
+
+	err := c.ShouldBindJSON(&material)
+	if err != nil {
+		return nil, errors.New("参数错误!")
+	}
+	ctx := apictx.CreateRepoCtx()
+
+	if material.Name == "" {
+		return nil, errors.New("材料名为空")
+	}
+	if material.Type == "" {
+		return nil, errors.New("材料类型为空")
+	}
+
+	material.CreateTime = time.Now()
+	material.UpdateTime = time.Now()
+
+	result, err := repo.RepoAddDoc(ctx, repo.CollectionMaterial, &material)
+	return result, err
+}
+
+// 获取材料信息
+func GetMaterial(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	materialId := c.Param("id")
+	id, err := primitive.ObjectIDFromHex(materialId)
+	if err != nil {
+		return nil, errors.New("非法id")
+	}
+	var material model.Material
+	option := &repo.DocSearchOptions{
+		CollectName: repo.CollectionMaterial,
+		Query:       repo.Map{"_id": id},
+	}
+
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &material)
+	if !found || err != nil {
+		log.Info(err)
+		return nil, errors.New("数据未找到")
+	}
+
+	return material, nil
+}
+
+// 获取材料列表
+func GetMaterials(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	page, size, query := UtilQueryPageSize(c)
+
+	option := &repo.PageSearchOptions{
+		CollectName: repo.CollectionMaterial,
+		Query:       query,
+		Page:        page,
+		Size:        size,
+		Sort:        bson.M{"createTime": -1},
+	}
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
+}
+
+// 删除材料
+func DelMaterial(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	materialId := c.Param("id")
+	if materialId == "" {
+		return nil, errors.New("id为空")
+	}
+
+	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionMaterial, materialId)
+}

+ 142 - 0
boxcost/api/pack.go

@@ -0,0 +1,142 @@
+package api
+
+import (
+	"box-cost/db/model"
+	"box-cost/db/repo"
+	"box-cost/log"
+	"errors"
+	"fmt"
+	"time"
+
+	"github.com/gin-gonic/gin"
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+// 包装管理
+func Pack(r *GinRouter) {
+
+	// 创建包装
+	r.POST("/pack", CreatePack)
+
+	// 获取包装详情
+	r.GET("/pack/:id", GetPack)
+
+	// 获取包装列表
+	r.GET("/packs", GetPacks)
+
+	// 更新包装
+	r.POST("/pack/update", UpdatePack)
+
+	// 删除包装
+	r.POST("/pack/delete/:id", DelPack)
+}
+
+// 创建包装
+func CreatePack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	var pack model.Pack
+
+	err := c.ShouldBindJSON(&pack)
+	if err != nil {
+		fmt.Println(err)
+		return nil, errors.New("参数错误!")
+	}
+	ctx := apictx.CreateRepoCtx()
+
+	if pack.Name == "" {
+		return nil, errors.New("包装名为空")
+	}
+	// 创建时生产 CompCounts,packComponent Id,mat。id craft id
+	pack.CompCounts = len(pack.Components)
+
+	// 生成id
+	// components id
+	if pack.CompCounts > 0 {
+		for _, v := range pack.Components {
+			v.Id = primitive.NewObjectID()
+			// mats id
+			if len(v.Mats) > 0 {
+				for _, v := range v.Mats {
+					v.Id = primitive.NewObjectID()
+					// crafts id
+					if len(v.Crafts) > 0 {
+						for _, v := range v.Crafts {
+							v.Id = primitive.NewObjectID()
+
+						}
+					}
+				}
+			}
+
+		}
+	}
+
+	pack.CreateTime = time.Now()
+	pack.UpdateTime = time.Now()
+
+	result, err := repo.RepoAddDoc(ctx, repo.CollectionPack, &pack)
+	return result, err
+}
+
+// 获取包装信息
+func GetPack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	packId := c.Param("id")
+	id, err := primitive.ObjectIDFromHex(packId)
+	if err != nil {
+		return nil, errors.New("非法id")
+	}
+	var pack model.Pack
+	option := &repo.DocSearchOptions{
+		CollectName: repo.CollectionPack,
+		Query:       repo.Map{"_id": id},
+	}
+
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &pack)
+	if !found || err != nil {
+		log.Info(err)
+		return nil, errors.New("数据未找到")
+	}
+
+	return pack, nil
+}
+
+// 获取包装列表
+func GetPacks(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	page, size, query := UtilQueryPageSize(c)
+
+	option := &repo.PageSearchOptions{
+		CollectName: repo.CollectionPack,
+		Query:       query,
+		Page:        page,
+		Size:        size,
+		Sort:        bson.M{"createTime": -1},
+		Project:     []string{"_id", "name", "thumbnail", "compCounts", "designer", "updateTime"},
+	}
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
+}
+
+// 更新包装
+func UpdatePack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var pack model.Pack
+	err := c.ShouldBindJSON(&pack)
+	if err != nil {
+		return nil, errors.New("参数错误")
+	}
+	if pack.Id.Hex() == "" {
+		return nil, errors.New("id的为空")
+	}
+	pack.UpdateTime = time.Now()
+	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionPack, pack.Id.Hex(), &pack)
+}
+
+// 删除包装
+func DelPack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	packId := c.Param("id")
+	if packId == "" {
+		return nil, errors.New("id为空")
+	}
+
+	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionPack, packId)
+}

+ 122 - 0
boxcost/api/plan.go

@@ -0,0 +1,122 @@
+package api
+
+import (
+	"box-cost/db/model"
+	"box-cost/db/repo"
+	"box-cost/log"
+	"errors"
+	"fmt"
+	"time"
+
+	"github.com/gin-gonic/gin"
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+// 生产计划管理
+func ProductPlan(r *GinRouter) {
+
+	// 创建生产计划
+	r.POST("/plan", CreateProductPlan)
+
+	// 获取生产计划详情
+	r.GET("/plan/:id", GetProductPlan)
+
+	// 获取生产计划列表
+	r.GET("/plans", GetProductPlans)
+
+	// 更新生产计划
+	r.POST("/plan/update", UpdateProductPlan)
+
+	// 删除生产计划
+	r.POST("/plan/delete/:id", DelProductPlan)
+}
+
+// 创建生产计划
+func CreateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	var plan model.ProductPlan
+
+	err := c.ShouldBindJSON(&plan)
+	if err != nil {
+		fmt.Println(err)
+		return nil, errors.New("参数错误!")
+	}
+	ctx := apictx.CreateRepoCtx()
+
+	if plan.Name == "" {
+		return nil, errors.New("生产计划名为空")
+	}
+	if plan.Total == 0 {
+		return nil, errors.New("生产计划数应不为0")
+	}
+
+	plan.Status = "process" // 进行中
+	plan.CreateTime = time.Now()
+	plan.UpdateTime = time.Now()
+
+	result, err := repo.RepoAddDoc(ctx, repo.CollectionProductPlan, &plan)
+	return result, err
+}
+
+// 获取生产计划信息
+func GetProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	planId := c.Param("id")
+	id, err := primitive.ObjectIDFromHex(planId)
+	if err != nil {
+		return nil, errors.New("非法id")
+	}
+	var plan model.ProductPlan
+	option := &repo.DocSearchOptions{
+		CollectName: repo.CollectionProductPlan,
+		Query:       repo.Map{"_id": id},
+	}
+
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &plan)
+	if !found || err != nil {
+		log.Info(err)
+		return nil, errors.New("数据未找到")
+	}
+
+	return plan, nil
+}
+
+// 获取生产计划列表
+func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	page, size, query := UtilQueryPageSize(c)
+
+	option := &repo.PageSearchOptions{
+		CollectName: repo.CollectionProductPlan,
+		Query:       query,
+		Page:        page,
+		Size:        size,
+		Sort:        bson.M{"createTime": -1},
+		Project:     []string{"_id", "thumbnail", "name", "updateTime", "createUser", "total", "totalPrice", "status"},
+	}
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
+}
+
+// 更新生产计划
+func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var plan model.ProductPlan
+	err := c.ShouldBindJSON(&plan)
+	if err != nil {
+		return nil, errors.New("参数错误")
+	}
+	if plan.Id.Hex() == "" {
+		return nil, errors.New("id的为空")
+	}
+	plan.UpdateTime = time.Now()
+	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
+}
+
+// 删除生产计划
+func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	planId := c.Param("id")
+	if planId == "" {
+		return nil, errors.New("id为空")
+	}
+
+	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
+}

+ 117 - 0
boxcost/api/prcess.go

@@ -0,0 +1,117 @@
+package api
+
+import (
+	"box-cost/db/model"
+	"box-cost/db/repo"
+	"box-cost/log"
+	"errors"
+	"fmt"
+	"time"
+
+	"github.com/gin-gonic/gin"
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+// 工序管理
+func Process(r *GinRouter) {
+
+	// 创建工序
+	r.POST("/process", CreateProcess)
+
+	// 获取工序详情
+	r.GET("/process/:id", GetProcess)
+
+	// 获取工序列表
+	r.GET("/process/list", GetProcesss)
+
+	// 更新工序
+	r.POST("/process/update", UpdateProcess)
+
+	// 删除工序
+	r.POST("/process/delete/:id", DelProcess)
+}
+
+// 创建工序
+func CreateProcess(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	var process model.Process
+
+	err := c.ShouldBindJSON(&process)
+	if err != nil {
+		fmt.Println(err)
+		return nil, errors.New("参数错误!")
+	}
+	ctx := apictx.CreateRepoCtx()
+
+	if process.Name == "" {
+		return nil, errors.New("工序名为空")
+	}
+
+	process.CreateTime = time.Now()
+	process.UpdateTime = time.Now()
+
+	result, err := repo.RepoAddDoc(ctx, repo.CollectionProcess, &process)
+	return result, err
+}
+
+// 获取工序详情
+func GetProcess(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	processId := c.Param("id")
+	id, err := primitive.ObjectIDFromHex(processId)
+	if err != nil {
+		return nil, errors.New("非法id")
+	}
+	var process model.Process
+	option := &repo.DocSearchOptions{
+		CollectName: repo.CollectionProcess,
+		Query:       repo.Map{"_id": id},
+	}
+
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &process)
+	if !found || err != nil {
+		log.Info(err)
+		return nil, errors.New("数据未找到")
+	}
+
+	return process, nil
+}
+
+// 获取工序列表
+func GetProcesss(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	page, size, query := UtilQueryPageSize(c)
+
+	option := &repo.PageSearchOptions{
+		CollectName: repo.CollectionProcess,
+		Query:       query,
+		Page:        page,
+		Size:        size,
+		Sort:        bson.M{"createTime": -1},
+	}
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
+}
+
+// 更新工序
+func UpdateProcess(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var process model.Process
+	err := c.ShouldBindJSON(&process)
+	if err != nil {
+		return nil, errors.New("参数错误")
+	}
+	if process.Id.Hex() == "" {
+		return nil, errors.New("id的为空")
+	}
+	process.UpdateTime = time.Now()
+	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProcess, process.Id.Hex(), &process)
+}
+
+// 删除工序
+func DelProcess(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	processId := c.Param("id")
+	if processId == "" {
+		return nil, errors.New("id为空")
+	}
+
+	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProcess, processId)
+}

+ 23 - 0
boxcost/api/router.go

@@ -13,6 +13,29 @@ func RegRouters(svc *Service) {
 	boxcost.group.Use(Logger())
 	boxcost.GET("/printr", Printr)
 
+	// 材料管理
+	Material(boxcost)
+
+	// 工艺管理
+	Craft(boxcost)
+
+	// 工序管理
+	Process(boxcost)
+
+	// 供应商管理
+	Supplier(boxcost)
+
+	// 供应商价格管理
+	SupplierPrice(boxcost)
+
+	// 包装管理
+	Pack(boxcost)
+
+	// 生产计划管理
+	ProductPlan(boxcost)
+
+	// 单据管理
+	Bill(boxcost)
 }
 
 func Logger() gin.HandlerFunc {

+ 123 - 0
boxcost/api/supplier-price.go

@@ -0,0 +1,123 @@
+package api
+
+import (
+	"box-cost/db/model"
+	"box-cost/db/repo"
+	"box-cost/log"
+	"errors"
+	"fmt"
+	"time"
+
+	"github.com/gin-gonic/gin"
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+// 供应商价格管理
+func SupplierPrice(r *GinRouter) {
+
+	// 创建供应商价格
+	r.POST("/supplierPrice", CreateSupplierPrice)
+
+	// 获取供应商价格详情
+	r.GET("/supplierPrice/:id", GetSupplierPrice)
+
+	// 获取供应商价格列表
+	r.GET("/supplierPrices", GetSupplierPrices)
+
+	// 更新供应商价格
+	r.POST("/supplierPrice/update", UpdateSupplierPrice)
+
+	// 删除供应商价格
+	r.POST("/supplierPrice/delete/:id", DelSupplierPrice)
+}
+
+// 创建供应商价格
+func CreateSupplierPrice(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	var supplierprice model.SupplierPrice
+
+	err := c.ShouldBindJSON(&supplierprice)
+	if err != nil {
+		fmt.Println(err)
+		return nil, errors.New("参数错误!")
+	}
+	ctx := apictx.CreateRepoCtx()
+
+	if supplierprice.SupplierId.Hex() == "" {
+		return nil, errors.New("供应商id为空")
+	}
+	if supplierprice.ProductId.Hex() == "" {
+		return nil, errors.New("供应产品id为空")
+	}
+	if supplierprice.PriceStrategy == nil {
+		return nil, errors.New("供应商价格策略为空")
+	}
+
+	supplierprice.CreateTime = time.Now()
+	supplierprice.UpdateTime = time.Now()
+
+	result, err := repo.RepoAddDoc(ctx, repo.CollectionSupplierPrice, &supplierprice)
+	return result, err
+}
+
+// 获取供应商价格信息
+func GetSupplierPrice(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	supplierpriceId := c.Param("id")
+	id, err := primitive.ObjectIDFromHex(supplierpriceId)
+	if err != nil {
+		return nil, errors.New("非法id")
+	}
+	var supplierprice model.SupplierPrice
+	option := &repo.DocSearchOptions{
+		CollectName: repo.CollectionSupplierPrice,
+		Query:       repo.Map{"_id": id},
+	}
+
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &supplierprice)
+	if !found || err != nil {
+		log.Info(err)
+		return nil, errors.New("数据未找到")
+	}
+
+	return supplierprice, nil
+}
+
+// 获取供应商价格列表
+func GetSupplierPrices(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	page, size, query := UtilQueryPageSize(c)
+
+	option := &repo.PageSearchOptions{
+		CollectName: repo.CollectionSupplierPrice,
+		Query:       query,
+		Page:        page,
+		Size:        size,
+		Sort:        bson.M{"createTime": -1},
+	}
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
+}
+
+// 更新供应商价格
+func UpdateSupplierPrice(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var supplierprice model.SupplierPrice
+	err := c.ShouldBindJSON(&supplierprice)
+	if err != nil {
+		return nil, errors.New("参数错误")
+	}
+	if supplierprice.Id.Hex() == "" {
+		return nil, errors.New("id的为空")
+	}
+	supplierprice.UpdateTime = time.Now()
+	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionSupplierPrice, supplierprice.Id.Hex(), &supplierprice)
+}
+
+// 删除供应商价格
+func DelSupplierPrice(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	supplierpriceId := c.Param("id")
+	if supplierpriceId == "" {
+		return nil, errors.New("id为空")
+	}
+
+	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionSupplierPrice, supplierpriceId)
+}

+ 123 - 0
boxcost/api/supplier.go

@@ -0,0 +1,123 @@
+package api
+
+import (
+	"box-cost/db/model"
+	"box-cost/db/repo"
+	"box-cost/log"
+	"errors"
+	"fmt"
+	"time"
+
+	"github.com/gin-gonic/gin"
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+// 供应商管理
+func Supplier(r *GinRouter) {
+
+	// 创建供应商
+	r.POST("/supplier", CreateSupplier)
+
+	// 获取供应商详情
+	r.GET("/supplier/:id", GetSupplier)
+
+	// 获取供应商列表
+	r.GET("/suppliers", GetSuppliers)
+
+	// 更新供应商
+	r.POST("/supplier/update", UpdateSupplier)
+
+	// 删除供应商
+	r.POST("/supplier/delete/:id", DelSupplier)
+}
+
+// 创建供应商
+func CreateSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	var supplier model.Supplier
+
+	err := c.ShouldBindJSON(&supplier)
+	if err != nil {
+		fmt.Println(err)
+		return nil, errors.New("参数错误!")
+	}
+	ctx := apictx.CreateRepoCtx()
+
+	if supplier.Name == "" {
+		return nil, errors.New("供应商名为空")
+	}
+	if supplier.Address == "" {
+		return nil, errors.New("供应商地址为空")
+	}
+	if supplier.Phone == "" {
+		return nil, errors.New("供应商联系电话为空")
+	}
+
+	supplier.CreateTime = time.Now()
+	supplier.UpdateTime = time.Now()
+
+	result, err := repo.RepoAddDoc(ctx, repo.CollectionSupplier, &supplier)
+	return result, err
+}
+
+// 获取供应商信息
+func GetSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	supplierId := c.Param("id")
+	id, err := primitive.ObjectIDFromHex(supplierId)
+	if err != nil {
+		return nil, errors.New("非法id")
+	}
+	var supplier model.Supplier
+	option := &repo.DocSearchOptions{
+		CollectName: repo.CollectionSupplier,
+		Query:       repo.Map{"_id": id},
+	}
+
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &supplier)
+	if !found || err != nil {
+		log.Info(err)
+		return nil, errors.New("数据未找到")
+	}
+
+	return supplier, nil
+}
+
+// 获取供应商列表
+func GetSuppliers(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	page, size, query := UtilQueryPageSize(c)
+
+	option := &repo.PageSearchOptions{
+		CollectName: repo.CollectionSupplier,
+		Query:       query,
+		Page:        page,
+		Size:        size,
+		Sort:        bson.M{"createTime": -1},
+	}
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
+}
+
+// 更新供应商
+func UpdateSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var supplier model.Supplier
+	err := c.ShouldBindJSON(&supplier)
+	if err != nil {
+		return nil, errors.New("参数错误")
+	}
+	if supplier.Id.Hex() == "" {
+		return nil, errors.New("id的为空")
+	}
+	supplier.UpdateTime = time.Now()
+	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionSupplier, supplier.Id.Hex(), &supplier)
+}
+
+// 删除供应商
+func DelSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	supplierId := c.Param("id")
+	if supplierId == "" {
+		return nil, errors.New("id为空")
+	}
+
+	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionSupplier, supplierId)
+}

+ 6 - 0
boxcost/boxcost.log

@@ -0,0 +1,6 @@
+{"level":"info","timestamp":"2022-10-25 11:33:14","message":"[<nil>]","service_name":"boxcost"}
+{"level":"info","timestamp":"2022-10-25 11:44:56","message":"[<nil>]","service_name":"boxcost"}
+{"level":"info","timestamp":"2022-10-25 11:45:41","message":"[<nil>]","service_name":"boxcost"}
+{"level":"info","timestamp":"2022-10-25 11:48:50","message":"[<nil>]","service_name":"boxcost"}
+{"level":"info","timestamp":"2022-10-25 18:21:40","message":"[<nil>]","service_name":"boxcost"}
+{"level":"info","timestamp":"2022-10-26 11:16:11","message":"[<nil>]","service_name":"boxcost"}

+ 16 - 14
boxcost/db/model/bill.go

@@ -1,20 +1,22 @@
 package model
 
-import "go.mongodb.org/mongo-driver/bson/primitive"
+import (
+	"time"
 
-type MatBill struct {
-	Id          primitive.ObjectID
-	PackId      primitive.ObjectID
-	PlanId      primitive.ObjectID
-	ProductUnit []*ProductUnit //多个工序
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
 
-	State string //created complete
+type Bill struct {
+	Id     primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
+	PackId primitive.ObjectID `bson:"packId,omitempty" json:"packId"`
+	PlanId primitive.ObjectID `bson:"planId,omitempty" json:"planId"`
+	// mat craft
+	Type string `bson:"type,omitempty" json:"type"`
+	//多个工序
+	ProductUnit []*ProductUnit `bson:"productUnit,omitempty" json:"productUnit"`
 
-}
-
-type CraftBill struct {
-	Id          primitive.ObjectID
-	PackId      primitive.ObjectID
-	PlanId      primitive.ObjectID
-	ProductUnit []*ProductUnit //多个工序
+	//created complete
+	Status     string    `bson:"status,omitempty" json:"status"`
+	CreateTime time.Time `bson:"createTime,omitempty" json:"createTime"`
+	UpdateTime time.Time `bson:"updateTime,omitempty" json:"updateTime"`
 }

+ 2 - 2
boxcost/db/model/craft.go

@@ -11,8 +11,8 @@ type Craft struct {
 	Id   primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
 	Name string             `bson:"name,omitempty" json:"name"`
 	// 单位
-	Unit  string `bson:"unit,omitempty" json:"unit"`
-	Price int    `bson:"price,omitempty" json:"price"`
+	Unit  string  `bson:"unit,omitempty" json:"unit"`
+	Price float64 `bson:"price,omitempty" json:"price"`
 	// 质量要求
 	Quality string `bson:"quality,omitempty" json:"quality"`
 	// 备注

+ 9 - 9
boxcost/db/model/pack.go

@@ -14,11 +14,11 @@ type Pack struct {
 	// 部件数量
 	CompCounts int `bson:"compCounts,omitempty" json:"compCounts"`
 	// 设计师
-	Designer   string    `bson:"designer,omitempty" json:"designer"`
-	CreateTime time.Time `bson:"createTime,omitempty" json:"createTime"`
-	UpdateTime time.Time `bson:"updateTime,omitempty" json:"updateTime"`
-
+	Designer string `bson:"designer,omitempty" json:"designer"`
+	// 组成包装的部件
 	Components []*PackComponent `bson:"components,omitempty" json:"components"`
+	CreateTime time.Time        `bson:"createTime,omitempty" json:"createTime"`
+	UpdateTime time.Time        `bson:"updateTime,omitempty" json:"updateTime"`
 }
 
 type PackComponent struct {
@@ -30,12 +30,12 @@ type PackComponent struct {
 	Uv string `bson:"uv,omitempty" json:"uv"`
 
 	//拼版尺寸
-	UvSize string
+	UvSize string `bson:"uvSize,omitempty" json:"uvSize"`
 
 	//所有材料
-	Mats []*PackComponentMat
+	Mats []*PackComponentMat `bson:"mats,omitempty" json:"mats"`
 
-	Remark string
+	Remark string `bson:"remark,omitempty" json:"remark"`
 }
 
 type PackComponentMat struct {
@@ -43,7 +43,7 @@ type PackComponentMat struct {
 	MatId primitive.ObjectID `bson:"matId,omitempty" json:"matId"`
 
 	//所有工艺
-	Crafts []*PackComponentMatCraft
+	Crafts []*PackComponentMatCraft `bson:"crafts,omitempty" json:"crafts"`
 }
 
 type PackComponentMatCraft struct {
@@ -51,5 +51,5 @@ type PackComponentMatCraft struct {
 	CraftId primitive.ObjectID `bson:"craftId,omitempty" json:"craftId"`
 
 	//工艺尺寸
-	Size string
+	Size string `bson:"size,omitempty" json:"size"`
 }

+ 49 - 33
boxcost/db/model/plan.go

@@ -9,68 +9,84 @@ import (
 // 生产计划
 type ProductPlan struct {
 	Id   primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
-	Pack *Pack              `bson:"pack,omitempty" json:"pack"`
-
-	Thumbnail  string
-	Name       string
-	CreateUser string
+	Name string             `bson:"name,omitempty" json:"name"`
+	// 包装
+	Pack       *Pack  `bson:"pack,omitempty" json:"pack"`
+	Thumbnail  string `bson:"thumbnail,omitempty" json:"thumbnail"`
+	CreateUser string `bson:"createUser,omitempty" json:"createUser"`
 	//生产数量
-	Total int
+	Total int `bson:"total,omitempty" json:"total"`
 
 	//状态
-	State string
+	Status string `bson:"status,omitempty" json:"status"`
 
 	//总价
-	TotalPrice float64
-
-	UpdateTime time.Time
-	CreateTime time.Time
-
+	TotalPrice float64 `bson:"totalPrice,omitempty" json:"totalPrice"`
 	//所有包装部件的工序
-	Components []*PackComp
+	Components []*PackComp `bson:"components,omitempty" json:"components"`
+	UpdateTime time.Time   `bson:"updteTime,omitempty" json:"updateTime"`
+	CreateTime time.Time   `bson:"createTime,omitempty" json:"createTime"`
 }
 
 type PackComp struct {
-	Id    primitive.ObjectID //包装部件I
-	Steps []*ProductUnit     //生产的工序列表
+	//包装部件Id
+	Id    primitive.ObjectID `bson:"id,omitempty" json:"id"`
+	Steps []*ProductUnit     `bson:"steps,omitempty" json:"steps"`
+	//生产的工序列表
 }
 
 // 工序单位
 type ProductUnit struct {
-	Id     string //工序Id
-	Type   string //mat craft process
-	TypeId string //type类型对应的Id
+	//工序Id
+	Id string `bson:"id,omitempty" json:"id"`
+
+	//mat craft process
+	Type string `bson:"type,omitempty" json:"type"`
+
+	//type类型对应的Id
+	TypeId string `bson:"typeId,omitempty" json:"typeId"`
 
-	CraftNorm  *CraftNorm //生产工艺的规格要求
-	MatNorm    *MatNorm
-	PlanCount  float64 //生产数量
-	FinalCount float64 //最终数量
+	//生产工艺的规格要求
+	CraftNorm *CraftNorm `bson:"craftNorm,omitempty" json:"craftNorm"`
+	MatNorm   *MatNorm   `bson:"matNorm,omitempty" json:"matNorm"`
+
+	//生产数量
+	PlanCount float64 `bson:"planCount,omitempty" json:"planCount"`
+
+	//最终数量
+	FinalCount float64 `bson:"finalCount,omitempty" json:"finalCount"`
+
+	//总价
+	TotalPrice float64 `bson:"totalPrice,omitempty" json:"totalPrice"`
 
-	TotalPrice   float64   //总价
-	DeliveryTime time.Time //交货时间
+	//交货时间
+	DeliveryTime time.Time `bson:"deliveryTime,omitempty" json:"deliveryTime"`
 
-	Price float64 //单价
+	//单价
+	Price float64 `bson:"price,omitempty" json:"price"`
 
 	//供应商
-	SupplierId primitive.ObjectID
+	SupplierId primitive.ObjectID `bson:"supplierId,omitempty" json:"supplierId"`
 
 	//计价
-	PriceStrategy *PriceStrategy
+	PriceStrategy *PriceStrategy `bson:"priceStrategy,omitempty" json:"priceStrategy"`
 
 	//单据Id
-	BillId primitive.ObjectID
+	BillId primitive.ObjectID `bson:"billId,omitempty" json:"billId"`
 }
 
 type MatNorm struct {
-	PaperWidth  string //下纸尺寸
-	PaperHeight string //下纸尺寸
+	//下纸尺寸
+	PaperWidth  string `bson:"paperWidth,omitempty" json:"paperWidth"`
+	PaperHeight string `bson:"paperHeight,omitempty" json:"paperHeight"`
 }
 
 type CraftNorm struct {
-	PrintWidth string //印刷尺寸
-	PrintHeigt string //印刷尺寸
+	//印刷尺寸
+	PrintWidth  string `bson:"printWidth,omitempty" json:"printWidth"`
+	PrintHeight string `bson:"printHeight,omitempty" json:"printHeight"`
 }
 
 type ProcessNorm struct {
-	Norm string
+	Norm string `bson:"norm,omitempty" json:"norm"`
 }

+ 17 - 0
boxcost/db/model/supplier-price.go

@@ -0,0 +1,17 @@
+package model
+
+import (
+	"time"
+
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+// 供应商价格
+type SupplierPrice struct {
+	Id            primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
+	SupplierId    primitive.ObjectID `bson:"supplierId,omitempty" json:"supplierId"`
+	ProductId     primitive.ObjectID `bson:"productId,omitempty" json:"productId"`
+	PriceStrategy *PriceStrategy     `bson:"priceStrategy,omitempty" json:"priceStrategy"`
+	CreateTime    time.Time          `bson:"createTime,omitempty" json:"createTime"`
+	UpdateTime    time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
+}

+ 5 - 13
boxcost/db/model/supplier.go

@@ -17,17 +17,9 @@ type Supplier struct {
 }
 
 type PriceStrategy struct {
-	Type   string //same
-	Param1 float64
-	Param2 float64
-	Param3 float64
-}
-
-// 供应商价格
-type SupplierPrice struct {
-	Id         primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
-	SupplierId primitive.ObjectID `bson:"supplierId,omitempty" json:"supplierId"`
-	ProductId  primitive.ObjectID `bson:"productId,omitempty" json:"productId"`
-	PriceType  *PriceStrategy     `bson:"priceType,omitempty" json:"priceType"`
-	CreateTime time.Time          `bson:"createTime,omitempty" json:"createTime"`
+	Type string `bson:"type,omitempty" json:"type"`
+	//same
+	Param1 float64 `bson:"param1,omitempty" json:"param1"`
+	Param2 float64 `bson:"param2,omitempty" json:"param2"`
+	Param3 float64 `bson:"param3,omitempty" json:"param3"`
 }

+ 74 - 0
boxcost/db/model/tmp.json

@@ -0,0 +1,74 @@
+{
+    "name":"喜乐1001",
+    "pack":{
+        "_id":"6357b30dc5a84c8f3b963023",
+        "name":"乐喜",
+        "thumbnail":"aaa.png",
+        "compCounts":1,
+        "designer":"李爽",
+        "components":[
+            {
+                "id":"6357b30dc5a84c8f3b963020",
+                "name":"上盖纸面",
+                "thumbnail":"bbb.png",
+                "uv":"ccc.png",
+                "uvSize":"300*300",
+                "mats":[
+                    {
+                        "id":"6357b30dc5a84c8f3b963021",
+                        "matId":"63575b91a178c8443cfd302e",
+                        "crafts":[
+                            {
+                                "id":"6357b30dc5a84c8f3b963022",
+                                "craftId":"63575b91a178c8443cfd302e",
+                                "size":"230*230"
+                            }
+                        ]
+                    }
+                ],
+                "remark":"xxxx"
+            }
+        ],
+        "createTime":"2022-10-25T09:57:33.473Z",
+        "updateTime":"2022-10-25T10:00:32.926Z"
+    },
+    "thumbnail":"xxxx.png",
+    "createUser":"李爽",
+    "total":4,
+    "status":"process",
+    "totalPrice":100,
+    "components":[
+        {
+            "id":"6357b30dc5a84c8f3b963020",
+            "steps":[
+                {
+                    "type":"mat",
+                    "typeId":"63575b91a178c8443cfd302e",
+                    "craftNorm":{
+                        "printHeight":"120cm",
+                        "printWidth":"50cm"
+                    },
+                    "matNorm":{
+                        "paperHeight":"150cm",
+                        "paperWidth":"80cm"
+                    },
+                    "processNorm":{
+                        "norm":"120*120"
+                    },
+                    "planCount":200,
+                    "finalCount":185,
+                    "totalPrice":600,
+                    "price":2.8,
+                    "supplierId":"6357600d1afd488cc53a899f",
+                    "priceStrategy":{
+                        "type": "same",
+                        "param1": 0.9,
+                        "param2": 0.9,
+                        "param3": 0.9
+                    },
+                    "billId":"6357600d1afd488cc53a899f"
+                }
+            ]
+        }
+    ]
+}

+ 8 - 2
boxcost/db/repo/repo.go

@@ -18,8 +18,14 @@ type RepoSession struct {
 }
 
 const (
-	CollectionDatabase   = "database"
-	CollectionAssetCount = "assetcount"
+	CollectionMaterial      = "material"
+	CollectionCraft         = "craft"
+	CollectionProcess       = "process"
+	CollectionSupplier      = "supplier"
+	CollectionSupplierPrice = "supplier-price"
+	CollectionPack          = "pack"
+	CollectionProductPlan   = "product-plan"
+	CollectionBill          = "bill"
 )
 
 type Map map[string]interface{}

+ 15 - 13
boxcost/utils/uitls.go

@@ -1,17 +1,19 @@
 package utils
 
-func OrderIdRange(OrderId int64) (int64, int64) {
-	minOrderId := OrderId //后面有几个零
-	var maxStep int64 = 1
-	currValue := minOrderId
-	for {
-		if currValue%10 == 0 {
-			currValue = currValue / 10
-			maxStep = maxStep * 10
-		} else {
-			break
-		}
+import (
+	"math/rand"
+	"strings"
+	"time"
+)
+
+func RandString() string {
+	rand.Seed(time.Now().UnixNano())
+	chars := []rune("abcdefghijklmnopqrstuvwxyz" +
+		"0123456789")
+	length := 24
+	var b strings.Builder
+	for i := 0; i < length; i++ {
+		b.WriteRune(chars[rand.Intn(len(chars))])
 	}
-	maxOrderId := minOrderId + maxStep - 1
-	return minOrderId, maxOrderId
+	return b.String()
 }