123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- package api
- import (
- "box-cost/db/model"
- "box-cost/db/repo"
- "box-cost/log"
- "errors"
- "fmt"
- "regexp"
- "strings"
- "time"
- "github.com/gin-gonic/gin"
- "github.com/xuri/excelize/v2"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- )
- type PlanIds struct {
- Ids []primitive.ObjectID `json:"ids"`
- }
- var PlanSearchFields = []string{"_id", "name", "createUser", "total", "status", "totalPrice", "updateTime", "createTime"}
- func PlanTrack(r *GinRouter) {
-
- r.POSTJWT("/planTrack/create", CreatePlanTrack)
-
- r.GETJWT("/planTrack/detail/:id", GetPlanTrack)
-
- r.GETJWT("/planTrack/list", GetPlanTracks)
-
- r.POSTJWT("/planTrack/update", UpdatePlanTrack)
- r.POSTJWT("/planTrack/delete/:id", DelPlanTrack)
- }
- func CreatePlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var PlanTrack model.PlanTrack
- err := c.ShouldBindJSON(&PlanTrack)
- if err != nil {
- return nil, errors.New("参数错误!")
- }
- ctx := apictx.CreateRepoCtx()
- if len(PlanTrack.Name) < 1 {
- return nil, errors.New("计划追踪名为空")
- }
- if len(PlanTrack.PlanIds) < 1 {
- return nil, errors.New("计划追踪id为空")
- }
- PlanTrack.CreateTime = time.Now()
- PlanTrack.UpdateTime = time.Now()
- PlanTrack.UserId, _ = primitive.ObjectIDFromHex(apictx.User.ID)
- result, err := repo.RepoAddDoc(ctx, repo.CollectionPlanTrack, &PlanTrack)
- return result, err
- }
- func GetPlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- PlanTrackId := c.Param("id")
- id, err := primitive.ObjectIDFromHex(PlanTrackId)
- if err != nil {
- return nil, errors.New("非法id")
- }
- var PlanTrack model.PlanTrack
- found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionPlanTrack,
- Query: repo.Map{"_id": id},
- }, &PlanTrack)
- if !found || err != nil {
- log.Info(err)
- return nil, errors.New("数据未找到")
- }
- userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
-
- userInfo, err := getUserById(apictx, userId)
- if err != nil {
- return nil, errors.New("用户信息获取失败")
- }
- PlanTrack.UserInfo = userInfo
-
- for _, planId := range PlanTrack.PlanIds {
- plan := &model.ProductPlan{}
- _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionProductPlan,
- Query: repo.Map{"_id": planId},
- Project: PlanSearchFields,
- }, plan)
- if err != nil {
- log.Info(err)
- continue
- }
- PlanTrack.Plans = append(PlanTrack.Plans, plan)
- }
- return PlanTrack, nil
- }
- func GetPlanTracks(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- page, size, query := UtilQueryPageSize(c)
- if _name, ok := query["name"]; ok {
- delete(query, "name")
- query["name"] = bson.M{"$regex": _name.(string)}
- }
- option := &repo.PageSearchOptions{
- CollectName: repo.CollectionPlanTrack,
- Query: query,
- Page: page,
- Size: size,
- Sort: bson.M{"createTime": -1},
- }
- return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
- }
- func UpdatePlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var pt model.PlanTrack
- err := c.ShouldBindJSON(&pt)
- if err != nil {
- return nil, errors.New("参数错误")
- }
- if pt.Id == primitive.NilObjectID {
- return nil, errors.New("id的为空")
- }
- pt.UpdateTime = time.Now()
- return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionPlanTrack, pt.Id.Hex(), &pt, &repo.RecordLogReq{
- Path: c.Request.URL.Path,
- TargetId: pt.Id.Hex(),
- })
- }
- func DelPlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- PlanTrackId := c.Param("id")
- if PlanTrackId == "" {
- return nil, errors.New("id为空")
- }
- return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionPlanTrack, PlanTrackId)
- }
- func DownloadPlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var form PlanIds
- err := c.ShouldBindJSON(&form)
- if err != nil {
- return nil, err
- }
- if len(form.Ids) == 0 {
- return nil, errors.New("ids为空")
- }
- plans := []*model.ProductPlan{}
- for _, objId := range form.Ids {
- fmt.Println(objId.Hex())
- plan := &model.ProductPlan{}
- _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionProductPlan,
- Query: repo.Map{"_id": objId},
- }, plan)
- if err != nil {
- fmt.Println(err)
- log.Info(err)
- continue
- }
- plans = append(plans, plan)
- }
- f := excelize.NewFile()
- index := f.NewSheet("Sheet1")
- f.SetActiveSheet(index)
- f.SetDefaultFont("宋体")
- planTrackExcel := NewPlanProcessTrackExcel(f)
- planStatusInfos, err := handlPlanStatus(apictx, plans)
- if err != nil {
- return nil, err
- }
- planTrackExcel.Content = planStatusInfos
-
- planTrackExcel.Draws()
- date := time.Now().Format("2006年01月02日_150405")
- fileName := fmt.Sprintf("礼盒加工追踪表_%s.xlsx", date)
- c.Header("Content-Type", "application/octet-stream")
- c.Header("Content-Disposition", "attachment; filename="+fileName)
- c.Header("Content-Transfer-Encoding", "binary")
- err = f.Write(c.Writer)
- if err != nil {
- return nil, err
- }
- return nil, nil
- }
- const (
- EXCEL_TMPLATE_FILE = "tmplate/tmplate.xlsx"
- CELL_BACKGROUND = "808080"
- )
- func MatchString(targetStr string, regexPattern string) bool {
-
- re := regexp.MustCompile(regexPattern)
-
- return re.MatchString(targetStr)
- }
- type PlanStatusInfo struct {
- PlanName string `json:"planName"`
- PlanUnit string `json:"planUnit"`
- PlanCount int `json:"planCount"`
- PlanRowStart int `json:"planRowStart"`
- PlanRowEnd int `json:"planRowEnd"`
- PlanCompStatus []map[string]string `json:"planCompStatus"`
- }
- func handlPlanStatus(apictx *ApiSession, plans []*model.ProductPlan) ([]*PlanStatusInfo, error) {
- row := 5
- planStatusInfos := make([]*PlanStatusInfo, 0)
- planCompStatus := []map[string]string{}
- for _, plan := range plans {
- startRow := row
- for _, comp := range plan.Pack.Components {
-
-
- compStatus := map[string]string{
- "部件": " ",
- "行数": "5",
-
- "下单": " ",
-
- "纸张": " ",
-
- "印刷": " ",
-
- "覆膜": " ",
-
- "烫金": " ",
-
- "丝印": " ",
-
- "对裱": " ",
-
- "压纹": " ",
-
- "裱瓦": " ",
-
- "模切": " ",
-
- "粘盒": " ",
-
- "组装": " ",
- "交货": " ",
- }
- fmt.Println(plan.Name)
- fmt.Println(comp.Name)
- fmt.Println(row)
- fmt.Println("------------------------------------")
- compStatus["部件"] = comp.Name
- compStatus["行数"] = fmt.Sprintf("%d", row)
- row++
-
- seen := make(map[string]bool)
- tbills := make([]string, 0, len(comp.Stages))
-
-
-
-
-
-
- for _, stage := range comp.Stages {
- if len(stage.BillId) > 0 {
- value := fmt.Sprintf("%d_%s", stage.BillType, stage.BillId)
- if _, ok := seen[value]; !ok {
-
- seen[value] = true
-
- tbills = append(tbills, value)
- }
- } else {
-
- for k := range compStatus {
- if k == "下单" || k == "交货" || k == "部件" || k == "行数" {
- continue
- }
-
- if stage.Type == 1 {
- compStatus["纸张"] = CELL_BACKGROUND
- }
-
- if MatchString(stage.Name, k) {
- compStatus[k] = CELL_BACKGROUND
- }
- }
- }
- }
-
-
- if len(tbills) == 0 {
-
- planCompStatus = append(planCompStatus, compStatus)
- continue
- }
-
-
- lastBillType := tbills[len(tbills)-1]
- for _, billType := range tbills {
- compStatus["下单"] = "√"
- bt := strings.Split(billType, "_")[0]
- billId, _ := primitive.ObjectIDFromHex(strings.Split(billType, "_")[1])
- if bt == "1" {
-
- bill := &model.PurchaseBill{}
- _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionBillPurchase,
- Query: repo.Map{"_id": billId},
- }, bill)
- if err != nil {
- fmt.Printf("billId:%s---%s", billId.Hex(), err.Error())
- return nil, errors.New("采购单不存在")
- }
-
-
-
-
-
-
- compStatus["纸张"] = "〇"
- if bill.Status == "complete" {
- compStatus["纸张"] = "√"
- }
-
-
-
- if lastBillType == billType {
- for _, paper := range bill.Paper {
- compStatus["交货"] = fmt.Sprintf("%d", paper.ConfirmCount)
- }
- }
- }
- if bt == "2" {
-
- bill := &model.ProduceBill{}
- _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionBillProduce,
- Query: repo.Map{"_id": billId},
- }, bill)
- if err != nil {
- fmt.Printf("billId:%s---%s", billId.Hex(), err.Error())
- return nil, errors.New("工艺单不存在")
- }
- for _, produce := range bill.Produces {
- for k := range compStatus {
- if k == "下单" || k == "纸张" || k == "交货" || k == "部件" || k == "行数" {
- continue
- }
- if MatchString(produce.Name, k) {
- compStatus[k] = "〇"
- if bill.Status == "complete" {
- compStatus[k] = "√"
- }
- }
-
-
-
-
-
-
- compStatus["交货"] = fmt.Sprintf("%d", produce.ConfirmCount)
- }
- }
- }
-
- if bt == "3" {
-
- bill := &model.ProductBill{}
- _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionBillProduct,
- Query: repo.Map{"_id": billId},
- }, bill)
- if err != nil {
- fmt.Printf("billId:%s---%s", billId.Hex(), err.Error())
- return nil, errors.New("成品单不存在")
- }
-
-
-
-
-
- if lastBillType == billType {
- for _, product := range bill.Products {
- compStatus["交货"] = fmt.Sprintf("%d", product.ConfirmCount)
- }
- }
- }
- }
- planCompStatus = append(planCompStatus, compStatus)
- }
-
-
-
- planStatusInfos = append(planStatusInfos, &PlanStatusInfo{
- PlanName: plan.Name,
-
- PlanUnit: "",
- PlanCount: plan.Total,
- PlanRowStart: startRow,
- PlanRowEnd: row - 1,
- PlanCompStatus: planCompStatus,
- })
- }
- return planStatusInfos, nil
- }
|