plan.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "box-cost/log"
  6. "errors"
  7. "fmt"
  8. "strings"
  9. "time"
  10. "github.com/gin-gonic/gin"
  11. "github.com/xuri/excelize/v2"
  12. "go.mongodb.org/mongo-driver/bson"
  13. "go.mongodb.org/mongo-driver/bson/primitive"
  14. )
  15. // 生产计划管理
  16. func ProductPlan(r *GinRouter) {
  17. // 创建生产计划
  18. r.POST("/plan/create", CreateProductPlan)
  19. // 获取生产计划详情
  20. r.GET("/plan/detail/:id", GetProductPlan)
  21. // 获取生产计划列表
  22. r.GET("/plan/list", GetProductPlans)
  23. // 更新生产计划
  24. r.POST("/plan/update", UpdateProductPlan)
  25. // 删除生产计划
  26. r.POST("/plan/delete/:id", DelProductPlan)
  27. // 下载部件单据
  28. r.GET("/bill/plan/download", DownLoadCompBills)
  29. // 生产成本表
  30. r.GET("/plan/cost/download", DownLoadPlanCost)
  31. }
  32. type SupplierPlanCost struct {
  33. *model.ProductPlan
  34. SupplierId string
  35. }
  36. func DownLoadPlanCost(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  37. _planId := c.Query("id")
  38. supplierId := c.Query("supplierId")
  39. planId, _ := primitive.ObjectIDFromHex(_planId)
  40. if planId.IsZero() {
  41. return nil, errors.New("planId错误")
  42. }
  43. supplierPlanCost := &SupplierPlanCost{}
  44. plan := model.ProductPlan{}
  45. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  46. CollectName: repo.CollectionProductPlan,
  47. Query: repo.Map{"_id": planId},
  48. }, &plan)
  49. if !found || err != nil {
  50. return nil, errors.New("数据未找到")
  51. }
  52. supplierPlanCost.ProductPlan = &plan
  53. supplierPlanCost.SupplierId = supplierId
  54. f := excelize.NewFile()
  55. index := f.NewSheet("Sheet1")
  56. f.SetActiveSheet(index)
  57. f.SetDefaultFont("宋体")
  58. planCostExcel := NewPlanCostExcel(f)
  59. planCostExcel.Title = fmt.Sprintf("生产成本表(%s)%d盒", plan.Name, plan.Total)
  60. planCostExcel.Content = supplierPlanCost
  61. planCostExcel.Draws()
  62. c.Header("Content-Type", "application/octet-stream")
  63. c.Header("Content-Disposition", "attachment; filename="+"planCost.xlsx")
  64. c.Header("Content-Transfer-Encoding", "binary")
  65. err = f.Write(c.Writer)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return nil, nil
  70. }
  71. func DownLoadCompBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  72. _planId := c.Query("id")
  73. compId := c.Query("compId")
  74. planId, err := primitive.ObjectIDFromHex(_planId)
  75. if err != nil {
  76. return nil, errors.New("planId错误")
  77. }
  78. plan := model.ProductPlan{}
  79. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  80. CollectName: repo.CollectionProductPlan,
  81. Query: repo.Map{"_id": planId},
  82. }, &plan)
  83. if !found || err != nil {
  84. return nil, errors.New("数据未找到")
  85. }
  86. // 获取部件单据
  87. curComp := &model.PackComponent{}
  88. for _, comp := range plan.Pack.Components {
  89. if comp.Id == compId {
  90. curComp = comp
  91. }
  92. }
  93. if curComp.Id == "" {
  94. return nil, errors.New("该组件不存在")
  95. }
  96. // 获取bill
  97. if len(curComp.Stages) == 0 {
  98. return nil, errors.New("该组件数据不存在")
  99. }
  100. // 获取不同类型的单据id
  101. // set := make(map[int]map[primitive.ObjectID]struct{})
  102. billIds := make([]string, 0)
  103. for _, stage := range curComp.Stages {
  104. _billId := stage.BillId
  105. billId, _ := primitive.ObjectIDFromHex(_billId)
  106. if !billId.IsZero() {
  107. // set[stage.Type] = map[primitive.ObjectID]struct{}{billId: struct{}{}}
  108. billIds = append(billIds, fmt.Sprintf("%d_%s", stage.BillType, stage.BillId))
  109. }
  110. }
  111. typeBillIds := removeDuplicationSort(billIds)
  112. if len(typeBillIds) < 1 {
  113. return nil, errors.New("未找到单据信息")
  114. }
  115. f := excelize.NewFile()
  116. index := f.NewSheet("Sheet1")
  117. f.SetActiveSheet(index)
  118. f.SetDefaultFont("宋体")
  119. companyName := getCompanyName(apictx)
  120. row := 0
  121. for _, tId := range typeBillIds {
  122. tidArr := strings.Split(tId, "_")
  123. var billExcel IExcel
  124. // 采购
  125. billId, _ := primitive.ObjectIDFromHex(tidArr[1])
  126. if tidArr[0] == "1" {
  127. purchase := model.PurchaseBill{}
  128. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  129. CollectName: repo.CollectionBillPurchase,
  130. Query: repo.Map{"_id": billId},
  131. }, &purchase)
  132. if found {
  133. billExcel = NewPurchaseBill(f)
  134. if purchase.Reviewed == 1 {
  135. if len(purchase.SignUsers) > 0 {
  136. signs := []*model.Signature{}
  137. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  138. CollectName: repo.CollectionSignature,
  139. Query: repo.Map{"_id": bson.M{"$in": purchase.SignUsers}},
  140. Sort: bson.M{"sort": 1}, // 升序
  141. }, &signs)
  142. billExcel.SetSignatures(signs)
  143. }
  144. }
  145. billExcel.SetContent(&purchase)
  146. billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
  147. }
  148. }
  149. // 工艺
  150. if tidArr[0] == "2" {
  151. produce := model.ProduceBill{}
  152. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  153. CollectName: repo.CollectionBillProduce,
  154. Query: repo.Map{"_id": billId},
  155. }, &produce)
  156. if found {
  157. billExcel = NewProduceBill(f)
  158. if produce.Reviewed == 1 {
  159. if len(produce.SignUsers) > 0 {
  160. signs := []*model.Signature{}
  161. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  162. CollectName: repo.CollectionSignature,
  163. Query: repo.Map{"_id": bson.M{"$in": produce.SignUsers}},
  164. Sort: bson.M{"sort": 1}, // 升序
  165. }, &signs)
  166. billExcel.SetSignatures(signs)
  167. }
  168. }
  169. billExcel.SetContent(&produce)
  170. billExcel.SetTitle(fmt.Sprintf("%s加工单", companyName))
  171. }
  172. }
  173. // 成品采购
  174. if tidArr[0] == "3" {
  175. product := model.ProductBill{}
  176. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  177. CollectName: repo.CollectionBillProduct,
  178. Query: repo.Map{"_id": billId},
  179. }, &product)
  180. if found {
  181. billExcel = NewProduceBill(f)
  182. if product.Reviewed == 1 {
  183. if len(product.SignUsers) > 0 {
  184. signs := []*model.Signature{}
  185. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  186. CollectName: repo.CollectionSignature,
  187. Query: repo.Map{"_id": bson.M{"$in": product.SignUsers}},
  188. Sort: bson.M{"sort": 1}, // 升序
  189. }, &signs)
  190. billExcel.SetSignatures(signs)
  191. }
  192. }
  193. billExcel.SetContent(&product)
  194. billExcel.SetTitle(companyName)
  195. }
  196. }
  197. billExcel.SetRow(row)
  198. billExcel.Draws()
  199. fmt.Println(billExcel.GetRow())
  200. row = billExcel.GetRow() + 5
  201. }
  202. c.Header("Content-Type", "application/octet-stream")
  203. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  204. c.Header("Content-Transfer-Encoding", "binary")
  205. err = f.Write(c.Writer)
  206. if err != nil {
  207. return nil, err
  208. }
  209. return nil, nil
  210. }
  211. // 创建生产计划
  212. func CreateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  213. var plan model.ProductPlan
  214. err := c.ShouldBindJSON(&plan)
  215. if err != nil {
  216. fmt.Println(err)
  217. return nil, errors.New("参数错误!")
  218. }
  219. ctx := apictx.CreateRepoCtx()
  220. if plan.Name == "" {
  221. return nil, errors.New("生产计划名为空")
  222. }
  223. if plan.Total == 0 {
  224. return nil, errors.New("生产计划数应不为0")
  225. }
  226. plan.Status = "process" // 进行中
  227. plan.CreateTime = time.Now()
  228. plan.UpdateTime = time.Now()
  229. result, err := repo.RepoAddDoc(ctx, repo.CollectionProductPlan, &plan)
  230. return result, err
  231. }
  232. // 获取生产计划信息
  233. func GetProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  234. planId := c.Param("id")
  235. id, err := primitive.ObjectIDFromHex(planId)
  236. if err != nil {
  237. return nil, errors.New("非法id")
  238. }
  239. var plan model.ProductPlan
  240. option := &repo.DocSearchOptions{
  241. CollectName: repo.CollectionProductPlan,
  242. Query: repo.Map{"_id": id},
  243. }
  244. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &plan)
  245. if !found || err != nil {
  246. log.Info(err)
  247. return nil, errors.New("数据未找到")
  248. }
  249. billStates := map[string]string{}
  250. if plan.Pack != nil && plan.Pack.Components != nil {
  251. for _, comp := range plan.Pack.Components {
  252. if comp.Stages != nil {
  253. for _, stage := range comp.Stages {
  254. if len(stage.BillId) > 0 {
  255. collectName := ""
  256. // 材料
  257. if stage.BillType == 1 {
  258. collectName = repo.CollectionBillPurchase
  259. }
  260. // 工艺
  261. if stage.BillType == 2 {
  262. collectName = repo.CollectionBillProduce
  263. }
  264. // 成品
  265. if stage.BillType == 3 {
  266. collectName = repo.CollectionBillProduct
  267. }
  268. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: collectName, Query: repo.Map{"_id": stage.BillId}, Project: []string{"status"}})
  269. if ok {
  270. billStates[stage.BillId] = state["status"].(string)
  271. }
  272. }
  273. }
  274. }
  275. }
  276. }
  277. return map[string]interface{}{
  278. "plan": plan,
  279. "billStates": billStates,
  280. }, nil
  281. }
  282. // 获取生产计划列表
  283. func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  284. page, size, query := UtilQueryPageSize(c)
  285. if _packId, ok := query["packId"]; ok {
  286. packId, _ := primitive.ObjectIDFromHex(_packId.(string))
  287. query["pack._id"] = packId
  288. delete(query, "packId")
  289. }
  290. option := &repo.PageSearchOptions{
  291. CollectName: repo.CollectionProductPlan,
  292. Query: query,
  293. Page: page,
  294. Size: size,
  295. Sort: bson.M{"createTime": -1},
  296. Project: []string{"_id", "thumbnail", "name", "updateTime", "createTime", "createUser", "total", "totalPrice", "status"},
  297. }
  298. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  299. }
  300. // 更新生产计划
  301. func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  302. var plan model.ProductPlan
  303. err := c.ShouldBindJSON(&plan)
  304. if err != nil {
  305. return nil, errors.New("参数错误")
  306. }
  307. if plan.Id.Hex() == "" {
  308. return nil, errors.New("id的为空")
  309. }
  310. plan.UpdateTime = time.Now()
  311. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
  312. }
  313. // 删除生产计划
  314. func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  315. planId := c.Param("id")
  316. if planId == "" {
  317. return nil, errors.New("id为空")
  318. }
  319. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
  320. }