123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- 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 Bill(r *GinRouter) {
- // 创建单据
- r.POST("/bill/purchase/create", CreateBill)
- // 获取单据详情
- r.GET("/bill/purchase/detail/:id", GetBill)
- // 获取单据列表
- r.GET("/bill/purchase/list", GetBills)
- // 获取单据列表
- r.GET("/bill/purchase/download", DownLoadBills)
- // 更新单据
- r.POST("/bill/purchase/update", UpdateBill)
- // 删除单据
- r.POST("/bill/purchase/delete/:id", DelBill)
- }
- type MatBillReq struct {
- Bill *model.PurchaseBill
- CompIndex *int
- MatIndex *int
- //MatKey string //components.0.mats.0.billId
- }
- // 创建单据
- func CreateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- req := &model.PurchaseBill{}
- err := c.ShouldBindJSON(req)
- if err != nil {
- fmt.Println(err)
- return nil, errors.New("参数错误")
- }
- ctx := apictx.CreateRepoCtx()
- bill := req
- 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.SerialNumber, err = generateSerial(apictx, bill.Type)
- if err != nil {
- return nil, err
- }
- bill.Status = "created"
- bill.CreateTime = time.Now()
- bill.UpdateTime = time.Now()
- return repo.RepoAddDoc(ctx, repo.CollectionBillPurchase, &bill)
- }
- // 获取单据信息
- 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.PurchaseBill
- option := &repo.DocSearchOptions{
- CollectName: repo.CollectionBillPurchase,
- 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)
- if query["packId"] != nil {
- query["packId"], _ = primitive.ObjectIDFromHex(query["packId"].(string))
- }
- if query["planId"] != nil {
- query["planId"], _ = primitive.ObjectIDFromHex(query["planId"].(string))
- }
- option := &repo.PageSearchOptions{
- CollectName: repo.CollectionBillPurchase,
- Query: query,
- Page: page,
- Size: size,
- Sort: bson.M{"createTime": -1},
- }
- return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
- }
- func DownLoadBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- billId := c.Query("id")
- isPdf := c.Query("isPdf")
- if len(billId) < 1 {
- return nil, fmt.Errorf("id不能为空")
- }
- id, err := primitive.ObjectIDFromHex(billId)
- if err != nil {
- return nil, errors.New("非法id")
- }
- var bill model.PurchaseBill
- option := &repo.DocSearchOptions{
- CollectName: repo.CollectionBillPurchase,
- Query: repo.Map{"_id": id},
- }
- found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
- if !found || err != nil {
- log.Info(err)
- return nil, errors.New("数据未找到")
- }
- f := excelize.NewFile()
- // Create a new sheet.
- index := f.NewSheet("Sheet1")
- f.SetActiveSheet(index)
- f.SetDefaultFont("宋体")
- billExcel := NewPurchaseBill(f)
- billExcel.Content = &bill
- info := model.Setting{}
- repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: "infos",
- }, &info)
- billExcel.Title = fmt.Sprintf("%s原材料采购单", info.CompanyName)
- //设置对应的数据
- billExcel.Draws()
- // 下载为pdf
- if isPdf == "true" {
- buf, _ := f.WriteToBuffer()
- res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
- if err != nil {
- return nil, errors.New("转化pdf失败")
- }
- c.Header("Content-Type", "application/octet-stream")
- c.Header("Content-Disposition", "attachment; filename="+"bill.pdf")
- c.Header("Content-Transfer-Encoding", "binary")
- err = res.Write(c.Writer)
- if err != nil {
- return nil, err
- }
- return nil, nil
- }
- // 下载为execl
- 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 UpdateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var bill model.PurchaseBill
- err := c.ShouldBindJSON(&bill)
- if err != nil {
- return nil, errors.New("参数错误")
- }
- if bill.Id.Hex() == "" {
- return nil, errors.New("id的为空")
- }
- billType, err := searchBillTypeById(apictx, repo.CollectionBillPurchase, bill.Id)
- if err != nil {
- return nil, err
- }
- // 如果更改类型
- if billType != bill.Type {
- bill.SerialNumber, err = generateSerial(apictx, bill.Type)
- if err != nil {
- return nil, err
- }
- }
- bill.UpdateTime = time.Now()
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, 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.CollectionBillPurchase, billId)
- }
|