plan-process-track.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "box-cost/log"
  6. "errors"
  7. "fmt"
  8. "regexp"
  9. "strings"
  10. "time"
  11. "github.com/gin-gonic/gin"
  12. "github.com/xuri/excelize/v2"
  13. "go.mongodb.org/mongo-driver/bson"
  14. "go.mongodb.org/mongo-driver/bson/primitive"
  15. )
  16. type PlanIds struct {
  17. Ids []primitive.ObjectID `json:"ids"`
  18. }
  19. var PlanSearchFields = []string{"_id", "name", "createUser", "total", "status", "totalPrice", "updateTime", "createTime"}
  20. // 计划追踪管理
  21. func PlanTrack(r *GinRouter) {
  22. // 新增计划追踪
  23. r.POSTJWT("/planTrack/create", CreatePlanTrack)
  24. // 获取计划追踪信息
  25. r.GETJWT("/planTrack/detail/:id", GetPlanTrack)
  26. // 获取计划追踪列表
  27. r.GETJWT("/planTrack/list", GetPlanTracks)
  28. // 计划追踪列表
  29. r.POSTJWT("/planTrack/update", UpdatePlanTrack)
  30. r.POSTJWT("/planTrack/delete/:id", DelPlanTrack)
  31. }
  32. func CreatePlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  33. var PlanTrack model.PlanTrack
  34. err := c.ShouldBindJSON(&PlanTrack)
  35. if err != nil {
  36. return nil, errors.New("参数错误!")
  37. }
  38. ctx := apictx.CreateRepoCtx()
  39. if len(PlanTrack.Name) < 1 {
  40. return nil, errors.New("计划追踪名为空")
  41. }
  42. if len(PlanTrack.PlanIds) < 1 {
  43. return nil, errors.New("计划追踪id为空")
  44. }
  45. PlanTrack.CreateTime = time.Now()
  46. PlanTrack.UpdateTime = time.Now()
  47. PlanTrack.UserId, _ = primitive.ObjectIDFromHex(apictx.User.ID)
  48. result, err := repo.RepoAddDoc(ctx, repo.CollectionPlanTrack, &PlanTrack)
  49. return result, err
  50. }
  51. // 获取计划追踪信息
  52. func GetPlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  53. PlanTrackId := c.Param("id")
  54. id, err := primitive.ObjectIDFromHex(PlanTrackId)
  55. if err != nil {
  56. return nil, errors.New("非法id")
  57. }
  58. var PlanTrack model.PlanTrack
  59. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  60. CollectName: repo.CollectionPlanTrack,
  61. Query: repo.Map{"_id": id},
  62. }, &PlanTrack)
  63. if !found || err != nil {
  64. log.Info(err)
  65. return nil, errors.New("数据未找到")
  66. }
  67. userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
  68. // 获取用户信息
  69. userInfo, err := getUserById(apictx, userId)
  70. if err != nil {
  71. return nil, errors.New("用户信息获取失败")
  72. }
  73. PlanTrack.UserInfo = userInfo
  74. // 查询包含计划的详细信息
  75. for _, planId := range PlanTrack.PlanIds {
  76. plan := &model.ProductPlan{}
  77. _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  78. CollectName: repo.CollectionProductPlan,
  79. Query: repo.Map{"_id": planId},
  80. Project: PlanSearchFields,
  81. }, plan)
  82. if err != nil {
  83. log.Info(err)
  84. continue
  85. }
  86. PlanTrack.Plans = append(PlanTrack.Plans, plan)
  87. }
  88. return PlanTrack, nil
  89. }
  90. // 获取计划追踪列表
  91. func GetPlanTracks(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  92. page, size, query := UtilQueryPageSize(c)
  93. if _name, ok := query["name"]; ok {
  94. delete(query, "name")
  95. query["name"] = bson.M{"$regex": _name.(string)}
  96. }
  97. option := &repo.PageSearchOptions{
  98. CollectName: repo.CollectionPlanTrack,
  99. Query: query,
  100. Page: page,
  101. Size: size,
  102. Sort: bson.M{"createTime": -1},
  103. }
  104. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  105. }
  106. func UpdatePlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  107. var pt model.PlanTrack
  108. err := c.ShouldBindJSON(&pt)
  109. if err != nil {
  110. return nil, errors.New("参数错误")
  111. }
  112. if pt.Id == primitive.NilObjectID {
  113. return nil, errors.New("id的为空")
  114. }
  115. pt.UpdateTime = time.Now()
  116. return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionPlanTrack, pt.Id.Hex(), &pt, &repo.RecordLogReq{
  117. Path: c.Request.URL.Path,
  118. TargetId: pt.Id.Hex(),
  119. })
  120. }
  121. // 删除计划追踪
  122. func DelPlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  123. PlanTrackId := c.Param("id")
  124. if PlanTrackId == "" {
  125. return nil, errors.New("id为空")
  126. }
  127. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionPlanTrack, PlanTrackId)
  128. }
  129. func DownloadPlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  130. var form PlanIds
  131. err := c.ShouldBindJSON(&form)
  132. if err != nil {
  133. return nil, err
  134. }
  135. if len(form.Ids) == 0 {
  136. return nil, errors.New("ids为空")
  137. }
  138. plans := []*model.ProductPlan{}
  139. for _, objId := range form.Ids {
  140. fmt.Println(objId.Hex())
  141. plan := &model.ProductPlan{}
  142. _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  143. CollectName: repo.CollectionProductPlan,
  144. Query: repo.Map{"_id": objId},
  145. }, plan)
  146. if err != nil {
  147. fmt.Println(err)
  148. log.Info(err)
  149. continue
  150. }
  151. plans = append(plans, plan)
  152. }
  153. f := excelize.NewFile()
  154. index := f.NewSheet("Sheet1")
  155. f.SetActiveSheet(index)
  156. f.SetDefaultFont("宋体")
  157. planTrackExcel := NewPlanProcessTrackExcel(f)
  158. planStatusInfos, err := handlPlanStatus(apictx, plans)
  159. if err != nil {
  160. return nil, err
  161. }
  162. planTrackExcel.Content = planStatusInfos
  163. //设置对应的数据
  164. planTrackExcel.Draws()
  165. date := time.Now().Format("2006年01月02日_150405")
  166. fileName := fmt.Sprintf("礼盒加工追踪表_%s.xlsx", date)
  167. c.Header("Content-Type", "application/octet-stream")
  168. c.Header("Content-Disposition", "attachment; filename="+fileName)
  169. c.Header("Content-Transfer-Encoding", "binary")
  170. err = f.Write(c.Writer)
  171. if err != nil {
  172. return nil, err
  173. }
  174. return nil, nil
  175. }
  176. const (
  177. EXCEL_TMPLATE_FILE = "tmplate/tmplate.xlsx"
  178. CELL_BACKGROUND = "808080"
  179. )
  180. // var needChangeCol = map[string]string{
  181. // "部件": "E",
  182. // "下单": "F",
  183. // "纸张": "G",
  184. // "印刷": "H",
  185. // "覆膜": "I",
  186. // "烫金": "J",
  187. // "丝印": "K",
  188. // "对裱": "L",
  189. // "压纹": "M",
  190. // "裱瓦": "N",
  191. // "模切": "O",
  192. // "粘盒": "P",
  193. // "组装": "Q",
  194. // "交货": "R",
  195. // }
  196. func MatchString(targetStr string, regexPattern string) bool {
  197. // 编译正则表达式
  198. re := regexp.MustCompile(regexPattern)
  199. // 检查目标字符串是否包含匹配的子串
  200. return re.MatchString(targetStr)
  201. }
  202. type PlanStatusInfo struct {
  203. PlanName string `json:"planName"`
  204. PlanUnit string `json:"planUnit"`
  205. PlanCount int `json:"planCount"`
  206. PlanRowStart int `json:"planRowStart"`
  207. PlanRowEnd int `json:"planRowEnd"`
  208. PlanCompStatus []map[string]string `json:"planCompStatus"`
  209. }
  210. func handlPlanStatus(apictx *ApiSession, plans []*model.ProductPlan) ([]*PlanStatusInfo, error) {
  211. row := 5
  212. planStatusInfos := make([]*PlanStatusInfo, 0)
  213. planCompStatus := []map[string]string{}
  214. for _, plan := range plans {
  215. startRow := row
  216. for _, comp := range plan.Pack.Components {
  217. // ""代表该部件没有该工艺 "〇"代表正在进行的工艺
  218. // "808080"背景颜色代表部件所含未进行工艺 √代表已完成工序
  219. compStatus := map[string]string{
  220. "部件": " ",
  221. "行数": "5",
  222. // 部件中只要有一个订单就说明下单了
  223. "下单": " ",
  224. // 采购单中type为纸张 订单对应状态为完成
  225. "纸张": " ",
  226. // 遍历工艺单,工序中包含印刷
  227. "印刷": " ",
  228. // 遍历工艺单,工序中包含覆膜
  229. "覆膜": " ",
  230. // 遍历工艺单,工序中包含烫金
  231. "烫金": " ",
  232. // 遍历工艺单,工序中包含丝印
  233. "丝印": " ",
  234. // 遍历工艺单,工序中包含对裱
  235. "对裱": " ",
  236. // 遍历工艺单,工序中包含压纹
  237. "压纹": " ",
  238. // 遍历工艺单,工序中包含裱瓦
  239. "裱瓦": " ",
  240. // 遍历工艺单,工序中包含模切
  241. "模切": " ",
  242. // 遍历工艺单,工序中包含粘盒
  243. "粘盒": " ",
  244. // 遍历工艺单,工序中包含组装
  245. "组装": " ",
  246. "交货": " ",
  247. }
  248. fmt.Println(plan.Name)
  249. fmt.Println(comp.Name)
  250. fmt.Println(row)
  251. fmt.Println("------------------------------------")
  252. compStatus["部件"] = comp.Name
  253. compStatus["行数"] = fmt.Sprintf("%d", row)
  254. row++
  255. // 去重获取所有订单
  256. seen := make(map[string]bool)
  257. tbills := make([]string, 0, len(comp.Stages)) // 结果数组,容量初始化为原数组的长度
  258. // ???:1 最后一个工序没下单
  259. // isBill := true
  260. // lastStage := comp.Stages[len(comp.Stages)-1]
  261. // if len(lastStage.BillId) < 24 {
  262. // isBill = false
  263. // }
  264. for _, stage := range comp.Stages {
  265. if len(stage.BillId) > 0 {
  266. value := fmt.Sprintf("%d_%s", stage.BillType, stage.BillId)
  267. if _, ok := seen[value]; !ok {
  268. // 标记为已出现
  269. seen[value] = true
  270. // 添加到结果数组
  271. tbills = append(tbills, value)
  272. }
  273. } else {
  274. // 产品中填写了这个工序但是没有下单 黑色背景
  275. for k := range compStatus {
  276. if k == "下单" || k == "交货" || k == "部件" || k == "行数" {
  277. continue
  278. }
  279. // 纸张 只要是采购单就设置纸张状态
  280. if stage.Type == 1 {
  281. compStatus["纸张"] = CELL_BACKGROUND
  282. }
  283. // 匹配工艺关键字 匹配到就设置状态为黑背景
  284. if MatchString(stage.Name, k) {
  285. compStatus[k] = CELL_BACKGROUND
  286. }
  287. }
  288. }
  289. }
  290. // fmt.Println(tbills)
  291. // 如果tbills为空,说明该部件没有订单
  292. if len(tbills) == 0 {
  293. // 该部件没有订单,跳过
  294. planCompStatus = append(planCompStatus, compStatus)
  295. continue
  296. }
  297. // 查询数据库获取bill详细信息
  298. // 最后工序订单号
  299. lastBillType := tbills[len(tbills)-1]
  300. for _, billType := range tbills {
  301. compStatus["下单"] = "√"
  302. bt := strings.Split(billType, "_")[0]
  303. billId, _ := primitive.ObjectIDFromHex(strings.Split(billType, "_")[1])
  304. if bt == "1" {
  305. // 查询采购单
  306. bill := &model.PurchaseBill{}
  307. _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  308. CollectName: repo.CollectionBillPurchase,
  309. Query: repo.Map{"_id": billId},
  310. }, bill)
  311. if err != nil {
  312. fmt.Printf("billId:%s---%s", billId.Hex(), err.Error())
  313. return nil, errors.New("采购单不存在")
  314. }
  315. // if bill.Type == "纸张" {
  316. // compStatus["纸张"] = "〇"
  317. // if bill.Status == "complete" {
  318. // compStatus["纸张"] = "√"
  319. // }
  320. // }
  321. compStatus["纸张"] = "〇"
  322. if bill.Status == "complete" {
  323. compStatus["纸张"] = "√"
  324. }
  325. // if !isBill {
  326. // continue
  327. // }
  328. if lastBillType == billType {
  329. for _, paper := range bill.Paper {
  330. compStatus["交货"] = fmt.Sprintf("%d", paper.ConfirmCount)
  331. }
  332. }
  333. }
  334. if bt == "2" {
  335. // 查询工艺单
  336. bill := &model.ProduceBill{}
  337. _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  338. CollectName: repo.CollectionBillProduce,
  339. Query: repo.Map{"_id": billId},
  340. }, bill)
  341. if err != nil {
  342. fmt.Printf("billId:%s---%s", billId.Hex(), err.Error())
  343. return nil, errors.New("工艺单不存在")
  344. }
  345. for _, produce := range bill.Produces {
  346. for k := range compStatus {
  347. if k == "下单" || k == "纸张" || k == "交货" || k == "部件" || k == "行数" {
  348. continue
  349. }
  350. if MatchString(produce.Name, k) {
  351. compStatus[k] = "〇"
  352. if bill.Status == "complete" {
  353. compStatus[k] = "√"
  354. }
  355. }
  356. // 直接赋值,如果这个订单是最后一个,则状态覆盖
  357. // ???思考:如果最后一个工序没有生成订单的话,是按最后一个工序还是最后一个订单?这里是最后一个订单
  358. // ???:1
  359. // if !isBill {
  360. // continue
  361. // }
  362. compStatus["交货"] = fmt.Sprintf("%d", produce.ConfirmCount)
  363. }
  364. }
  365. }
  366. // 暂时没有状态标定
  367. if bt == "3" {
  368. // 查询成品单
  369. bill := &model.ProductBill{}
  370. _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  371. CollectName: repo.CollectionBillProduct,
  372. Query: repo.Map{"_id": billId},
  373. }, bill)
  374. if err != nil {
  375. fmt.Printf("billId:%s---%s", billId.Hex(), err.Error())
  376. return nil, errors.New("成品单不存在")
  377. }
  378. // fmt.Println(bill)
  379. // ?? 这里需不需要 影响正确数据吗?
  380. // if !isBill {
  381. // continue
  382. // }
  383. if lastBillType == billType {
  384. for _, product := range bill.Products {
  385. compStatus["交货"] = fmt.Sprintf("%d", product.ConfirmCount)
  386. }
  387. }
  388. }
  389. }
  390. planCompStatus = append(planCompStatus, compStatus)
  391. }
  392. // fmt.Println(plan.Name)
  393. // fmt.Println("rowstart:", startRow)
  394. // fmt.Println("rowend:", row-1)
  395. planStatusInfos = append(planStatusInfos, &PlanStatusInfo{
  396. PlanName: plan.Name,
  397. // !这个规格好像没有
  398. PlanUnit: "",
  399. PlanCount: plan.Total,
  400. PlanRowStart: startRow,
  401. PlanRowEnd: row - 1,
  402. PlanCompStatus: planCompStatus,
  403. })
  404. }
  405. return planStatusInfos, nil
  406. }