plan-process-track.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. UserId: apictx.User.ID,
  119. TargetId: pt.Id.Hex(),
  120. })
  121. }
  122. // 删除计划追踪
  123. func DelPlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  124. PlanTrackId := c.Param("id")
  125. if PlanTrackId == "" {
  126. return nil, errors.New("id为空")
  127. }
  128. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionPlanTrack, PlanTrackId)
  129. }
  130. func DownloadPlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  131. var form PlanIds
  132. err := c.ShouldBindJSON(&form)
  133. if err != nil {
  134. return nil, err
  135. }
  136. if len(form.Ids) == 0 {
  137. return nil, errors.New("ids为空")
  138. }
  139. plans := []*model.ProductPlan{}
  140. for _, objId := range form.Ids {
  141. fmt.Println(objId.Hex())
  142. plan := &model.ProductPlan{}
  143. _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  144. CollectName: repo.CollectionProductPlan,
  145. Query: repo.Map{"_id": objId},
  146. }, plan)
  147. if err != nil {
  148. fmt.Println(err)
  149. log.Info(err)
  150. continue
  151. }
  152. plans = append(plans, plan)
  153. }
  154. f := excelize.NewFile()
  155. index := f.NewSheet("Sheet1")
  156. f.SetActiveSheet(index)
  157. f.SetDefaultFont("宋体")
  158. planTrackExcel := NewPlanProcessTrackExcel(f)
  159. planStatusInfos, err := handlPlanStatus(apictx, plans)
  160. if err != nil {
  161. return nil, err
  162. }
  163. planTrackExcel.Content = planStatusInfos
  164. //设置对应的数据
  165. planTrackExcel.Draws()
  166. date := time.Now().Format("2006年01月02日_150405")
  167. fileName := fmt.Sprintf("礼盒加工追踪表_%s.xlsx", date)
  168. c.Header("Content-Type", "application/octet-stream")
  169. c.Header("Content-Disposition", "attachment; filename="+fileName)
  170. c.Header("Content-Transfer-Encoding", "binary")
  171. err = f.Write(c.Writer)
  172. if err != nil {
  173. return nil, err
  174. }
  175. return nil, nil
  176. }
  177. const (
  178. EXCEL_TMPLATE_FILE = "tmplate/tmplate.xlsx"
  179. CELL_BACKGROUND = "808080"
  180. )
  181. var needChangeCol = map[string]string{
  182. "部件": "E",
  183. "下单": "F",
  184. "纸张": "G",
  185. "印刷": "H",
  186. "覆膜": "I",
  187. "烫金": "J",
  188. "丝印": "K",
  189. "对裱": "L",
  190. "压纹": "M",
  191. "裱瓦": "N",
  192. "模切": "O",
  193. "粘盒": "P",
  194. "组装": "Q",
  195. "交货": "R",
  196. }
  197. func MatchString(targetStr string, regexPattern string) bool {
  198. // 编译正则表达式
  199. re := regexp.MustCompile(regexPattern)
  200. // 检查目标字符串是否包含匹配的子串
  201. return re.MatchString(targetStr)
  202. }
  203. type PlanStatusInfo struct {
  204. PlanName string `json:"planName"`
  205. PlanUnit string `json:"planUnit"`
  206. PlanCount int `json:"planCount"`
  207. PlanRowStart int `json:"planRowStart"`
  208. PlanRowEnd int `json:"planRowEnd"`
  209. PlanCompStatus []map[string]string `json:"planCompStatus"`
  210. }
  211. func handlPlanStatus(apictx *ApiSession, plans []*model.ProductPlan) ([]*PlanStatusInfo, error) {
  212. row := 5
  213. planStatusInfos := make([]*PlanStatusInfo, 0)
  214. planCompStatus := []map[string]string{}
  215. for _, plan := range plans {
  216. startRow := row
  217. for _, comp := range plan.Pack.Components {
  218. // ""代表该部件没有该工艺 "〇"代表正在进行的工艺
  219. // "808080"背景颜色代表部件所含未进行工艺 √代表已完成工序
  220. compStatus := map[string]string{
  221. "部件": " ",
  222. "行数": "5",
  223. // 部件中只要有一个订单就说明下单了
  224. "下单": " ",
  225. // 采购单中type为纸张 订单对应状态为完成
  226. "纸张": " ",
  227. // 遍历工艺单,工序中包含印刷
  228. "印刷": " ",
  229. // 遍历工艺单,工序中包含覆膜
  230. "覆膜": " ",
  231. // 遍历工艺单,工序中包含烫金
  232. "烫金": " ",
  233. // 遍历工艺单,工序中包含丝印
  234. "丝印": " ",
  235. // 遍历工艺单,工序中包含对裱
  236. "对裱": " ",
  237. // 遍历工艺单,工序中包含压纹
  238. "压纹": " ",
  239. // 遍历工艺单,工序中包含裱瓦
  240. "裱瓦": " ",
  241. // 遍历工艺单,工序中包含模切
  242. "模切": " ",
  243. // 遍历工艺单,工序中包含粘盒
  244. "粘盒": " ",
  245. // 遍历工艺单,工序中包含组装
  246. "组装": " ",
  247. "交货": " ",
  248. }
  249. fmt.Println(plan.Name)
  250. fmt.Println(comp.Name)
  251. fmt.Println(row)
  252. fmt.Println("------------------------------------")
  253. compStatus["部件"] = comp.Name
  254. compStatus["行数"] = fmt.Sprintf("%d", row)
  255. row++
  256. // 去重获取所有订单
  257. seen := make(map[string]bool)
  258. tbills := make([]string, 0, len(comp.Stages)) // 结果数组,容量初始化为原数组的长度
  259. // ???:1 最后一个工序没下单
  260. // isBill := true
  261. // lastStage := comp.Stages[len(comp.Stages)-1]
  262. // if len(lastStage.BillId) < 24 {
  263. // isBill = false
  264. // }
  265. for _, stage := range comp.Stages {
  266. if len(stage.BillId) > 0 {
  267. value := fmt.Sprintf("%d_%s", stage.BillType, stage.BillId)
  268. if _, ok := seen[value]; !ok {
  269. // 标记为已出现
  270. seen[value] = true
  271. // 添加到结果数组
  272. tbills = append(tbills, value)
  273. }
  274. } else {
  275. // 产品中填写了这个工序但是没有下单 黑色背景
  276. for k := range compStatus {
  277. if k == "下单" || k == "交货" || k == "部件" || k == "行数" {
  278. continue
  279. }
  280. // 纸张 只要是采购单就设置纸张状态
  281. if stage.Type == 1 {
  282. compStatus["纸张"] = CELL_BACKGROUND
  283. }
  284. // 匹配工艺关键字 匹配到就设置状态为黑背景
  285. if MatchString(stage.Name, k) {
  286. compStatus[k] = CELL_BACKGROUND
  287. }
  288. }
  289. }
  290. }
  291. // fmt.Println(tbills)
  292. // 如果tbills为空,说明该部件没有订单
  293. if len(tbills) == 0 {
  294. // 该部件没有订单,跳过
  295. planCompStatus = append(planCompStatus, compStatus)
  296. continue
  297. }
  298. // 查询数据库获取bill详细信息
  299. // 最后工序订单号
  300. lastBillType := tbills[len(tbills)-1]
  301. for _, billType := range tbills {
  302. compStatus["下单"] = "√"
  303. bt := strings.Split(billType, "_")[0]
  304. billId, _ := primitive.ObjectIDFromHex(strings.Split(billType, "_")[1])
  305. if bt == "1" {
  306. // 查询采购单
  307. bill := &model.PurchaseBill{}
  308. _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  309. CollectName: repo.CollectionBillPurchase,
  310. Query: repo.Map{"_id": billId},
  311. }, bill)
  312. if err != nil {
  313. fmt.Printf("billId:%s---%s", billId.Hex(), err.Error())
  314. return nil, errors.New("采购单不存在")
  315. }
  316. // if bill.Type == "纸张" {
  317. // compStatus["纸张"] = "〇"
  318. // if bill.Status == "complete" {
  319. // compStatus["纸张"] = "√"
  320. // }
  321. // }
  322. compStatus["纸张"] = "〇"
  323. if bill.Status == "complete" {
  324. compStatus["纸张"] = "√"
  325. }
  326. // if !isBill {
  327. // continue
  328. // }
  329. if lastBillType == billType {
  330. for _, paper := range bill.Paper {
  331. compStatus["交货"] = fmt.Sprintf("%d", paper.ConfirmCount)
  332. }
  333. }
  334. }
  335. if bt == "2" {
  336. // 查询工艺单
  337. bill := &model.ProduceBill{}
  338. _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  339. CollectName: repo.CollectionBillProduce,
  340. Query: repo.Map{"_id": billId},
  341. }, bill)
  342. if err != nil {
  343. fmt.Printf("billId:%s---%s", billId.Hex(), err.Error())
  344. return nil, errors.New("工艺单不存在")
  345. }
  346. for _, produce := range bill.Produces {
  347. for k := range compStatus {
  348. if k == "下单" || k == "纸张" || k == "交货" || k == "部件" || k == "行数" {
  349. continue
  350. }
  351. if MatchString(produce.Name, k) {
  352. compStatus[k] = "〇"
  353. if bill.Status == "complete" {
  354. compStatus[k] = "√"
  355. }
  356. }
  357. // 直接赋值,如果这个订单是最后一个,则状态覆盖
  358. // ???思考:如果最后一个工序没有生成订单的话,是按最后一个工序还是最后一个订单?这里是最后一个订单
  359. // ???:1
  360. // if !isBill {
  361. // continue
  362. // }
  363. compStatus["交货"] = fmt.Sprintf("%d", produce.ConfirmCount)
  364. }
  365. }
  366. }
  367. // 暂时没有状态标定
  368. if bt == "3" {
  369. // 查询成品单
  370. bill := &model.ProductBill{}
  371. _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  372. CollectName: repo.CollectionBillProduct,
  373. Query: repo.Map{"_id": billId},
  374. }, bill)
  375. if err != nil {
  376. fmt.Printf("billId:%s---%s", billId.Hex(), err.Error())
  377. return nil, errors.New("成品单不存在")
  378. }
  379. // fmt.Println(bill)
  380. // ?? 这里需不需要 影响正确数据吗?
  381. // if !isBill {
  382. // continue
  383. // }
  384. if lastBillType == billType {
  385. for _, product := range bill.Products {
  386. compStatus["交货"] = fmt.Sprintf("%d", product.ConfirmCount)
  387. }
  388. }
  389. }
  390. }
  391. planCompStatus = append(planCompStatus, compStatus)
  392. }
  393. // fmt.Println(plan.Name)
  394. // fmt.Println("rowstart:", startRow)
  395. // fmt.Println("rowend:", row-1)
  396. planStatusInfos = append(planStatusInfos, &PlanStatusInfo{
  397. PlanName: plan.Name,
  398. // !这个规格好像没有
  399. PlanUnit: "",
  400. PlanCount: plan.Total,
  401. PlanRowStart: startRow,
  402. PlanRowEnd: row - 1,
  403. PlanCompStatus: planCompStatus,
  404. })
  405. }
  406. return planStatusInfos, nil
  407. }