|
@@ -9,6 +9,7 @@ import (
|
|
|
"time"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
+ "github.com/xuri/excelize/v2"
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
)
|
|
@@ -30,6 +31,101 @@ func ProductPlan(r *GinRouter) {
|
|
|
|
|
|
// 删除生产计划
|
|
|
r.POST("/plan/delete/:id", DelProductPlan)
|
|
|
+
|
|
|
+ // 下载部件打印单
|
|
|
+ r.GET("/bill/plan/download", DownLoadPlan)
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func DownLoadPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+ _packId := c.Query("id")
|
|
|
+ compId := c.Query("compId")
|
|
|
+ packId, err := primitive.ObjectIDFromHex(_packId)
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.New("packId错误")
|
|
|
+ }
|
|
|
+ plan := model.ProductPlan{}
|
|
|
+ found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
+ CollectName: repo.CollectionProductPlan,
|
|
|
+ Query: repo.Map{"pack._id": packId},
|
|
|
+ }, &plan)
|
|
|
+ if !found || err != nil {
|
|
|
+ return nil, errors.New("数据未找到")
|
|
|
+ }
|
|
|
+ // 获取部件单据
|
|
|
+ curComp := &model.PackComponent{}
|
|
|
+ for _, comp := range plan.Pack.Components {
|
|
|
+ if comp.Id == compId {
|
|
|
+ curComp = comp
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if curComp.Id == "" {
|
|
|
+ return nil, errors.New("该组件不存在")
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取bill
|
|
|
+ if len(curComp.Mats) == 0 {
|
|
|
+ return nil, errors.New("该组件数据不存在")
|
|
|
+ }
|
|
|
+ f := excelize.NewFile()
|
|
|
+ // Create a new sheet.
|
|
|
+ index := f.NewSheet("Sheet1")
|
|
|
+ f.SetActiveSheet(index)
|
|
|
+ f.SetDefaultFont("宋体")
|
|
|
+
|
|
|
+ //
|
|
|
+ for _, mat := range curComp.Mats {
|
|
|
+ // 采购单
|
|
|
+ _purchaseId := mat.BillId
|
|
|
+ purchaseId, err := primitive.ObjectIDFromHex(_purchaseId)
|
|
|
+ if err == nil {
|
|
|
+ purchase := model.PurchaseBill{}
|
|
|
+ found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
+ CollectName: repo.CollectionBillPurchase,
|
|
|
+ Query: repo.Map{"_id": purchaseId},
|
|
|
+ }, &purchase)
|
|
|
+ if found {
|
|
|
+ purchaseExcel := NewPurchaseBill(f)
|
|
|
+ purchaseExcel.Content = &purchase
|
|
|
+ //设置对应的数据
|
|
|
+ purchaseExcel.Draws()
|
|
|
+ purchaseExcel.Offset += 15
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(mat.Crafts) > 0 {
|
|
|
+ for _, carft := range mat.Crafts {
|
|
|
+ // 加工单
|
|
|
+ _produceId := carft.BillId
|
|
|
+ produceId, err := primitive.ObjectIDFromHex(_produceId)
|
|
|
+ if err == nil {
|
|
|
+ produce := model.ProduceBill{}
|
|
|
+ found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
+ CollectName: repo.CollectionBillProduce,
|
|
|
+ Query: repo.Map{"_id": produceId},
|
|
|
+ }, &produce)
|
|
|
+ if found {
|
|
|
+ billExcel := NewProduceBill(f)
|
|
|
+ billExcel.Content = &produce
|
|
|
+ //设置对应的数据
|
|
|
+ billExcel.Draws()
|
|
|
+ billExcel.Offset += 15
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ c.Header("Content-Type", "application/octet-stream")
|
|
|
+ c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
|
|
|
+ c.Header("Content-Transfer-Encoding", "binary")
|
|
|
+
|
|
|
+ err = f.Write(c.Writer)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil, nil
|
|
|
}
|
|
|
|
|
|
// 创建生产计划
|