123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- package api
- import (
- "box-cost/db/model"
- "box-cost/db/repo"
- "box-cost/log"
- "errors"
- "fmt"
- "time"
- "github.com/gin-gonic/gin"
- "github.com/xuri/excelize/v2"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- )
- // 生产计划管理
- func ProductPlan(r *GinRouter) {
- // 创建生产计划
- r.POST("/plan/create", CreateProductPlan)
- // 获取生产计划详情
- r.GET("/plan/detail/:id", GetProductPlan)
- // 获取生产计划列表
- r.GET("/plan/list", GetProductPlans)
- // 更新生产计划
- r.POST("/plan/update", UpdateProductPlan)
- // 删除生产计划
- r.POST("/plan/delete/:id", DelProductPlan)
- // 下载部件打印单
- r.GET("/bill/plan/download", DownLoadPlan)
- // 生产成本表
- r.GET("/plan/cost/download", DownLoadPlanCost)
- }
- type SupplierPlanCost struct {
- *model.ProductPlan
- SupplierId string
- }
- func DownLoadPlanCost(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- _planId := c.Query("id")
- supplierId := c.Query("supplierId")
- planId, err := primitive.ObjectIDFromHex(_planId)
- if err != nil {
- return nil, errors.New("planId错误")
- }
- supplierPlanCost := &SupplierPlanCost{}
- plan := model.ProductPlan{}
- found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionProductPlan,
- Query: repo.Map{"_id": planId},
- }, &plan)
- if !found || err != nil {
- return nil, errors.New("数据未找到")
- }
- supplierPlanCost.ProductPlan = &plan
- supplierPlanCost.SupplierId = supplierId
- f := excelize.NewFile()
- index := f.NewSheet("Sheet1")
- f.SetActiveSheet(index)
- f.SetDefaultFont("宋体")
- planCostExcel := NewPlanCostExcel(f)
- planCostExcel.Title = fmt.Sprintf("生产成本表(%s)%d盒", plan.Name, plan.Total)
- planCostExcel.Content = supplierPlanCost
- planCostExcel.Draws()
- c.Header("Content-Type", "application/octet-stream")
- c.Header("Content-Disposition", "attachment; filename="+"planCost.xlsx")
- c.Header("Content-Transfer-Encoding", "binary")
- err = f.Write(c.Writer)
- if err != nil {
- return nil, err
- }
- return nil, nil
- }
- func DownLoadPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- _planId := c.Query("id")
- compId := c.Query("compId")
- planId, err := primitive.ObjectIDFromHex(_planId)
- if err != nil {
- return nil, errors.New("planId错误")
- }
- plan := model.ProductPlan{}
- found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionProductPlan,
- Query: repo.Map{"_id": planId},
- }, &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("宋体")
- companyName := getCompanyName(apictx)
- offset := 0
- 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.Title = fmt.Sprintf("%s原材料采购单", companyName)
- //设置对应的数据
- purchaseExcel.Offset = offset
- purchaseExcel.Draws()
- 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 {
- produceExcel := NewProduceBill(f)
- produceExcel.Content = &produce
- produceExcel.Title = fmt.Sprintf("%s加工单", companyName)
- //设置对应的数据
- produceExcel.Offset = offset
- produceExcel.Draws()
- 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
- }
- // 创建生产计划
- 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("数据未找到")
- }
- billStates := map[string]string{}
- if len(plan.Process) > 0 {
- for _, pp := range plan.Process {
- ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionBillPurchase, Query: repo.Map{"_id": pp.BillId}, Project: []string{"status"}})
- if ok {
- billStates[pp.BillId] = state["status"].(string)
- }
- }
- }
- if plan.Pack != nil && plan.Pack.Components != nil {
- for _, comp := range plan.Pack.Components {
- if comp.Mats != nil {
- for _, mat := range comp.Mats {
- if len(mat.BillId) > 0 {
- ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionBillPurchase, Query: repo.Map{"_id": mat.BillId}, Project: []string{"status"}})
- if ok {
- billStates[mat.BillId] = state["status"].(string)
- }
- }
- if mat.Crafts != nil {
- for _, craft := range mat.Crafts {
- if len(craft.BillId) > 0 {
- ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionBillProduce, Query: repo.Map{"_id": craft.BillId}, Project: []string{"status"}})
- if ok {
- billStates[craft.BillId] = state["status"].(string)
- }
- }
- }
- }
- }
- }
- }
- }
- return map[string]interface{}{
- "plan": plan,
- "billStates": billStates,
- }, nil
- }
- // 获取生产计划列表
- func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- page, size, query := UtilQueryPageSize(c)
- if _packId, ok := query["packId"]; ok {
- packId, _ := primitive.ObjectIDFromHex(_packId.(string))
- query["pack._id"] = packId
- delete(query, "packId")
- }
- option := &repo.PageSearchOptions{
- CollectName: repo.CollectionProductPlan,
- Query: query,
- Page: page,
- Size: size,
- Sort: bson.M{"createTime": -1},
- Project: []string{"_id", "thumbnail", "name", "updateTime", "createTime", "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)
- }
|