plan.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. billIds := make([]string, 0)
  102. for _, stage := range curComp.Stages {
  103. billId, _ := primitive.ObjectIDFromHex(stage.BillId)
  104. if !billId.IsZero() {
  105. billIds = append(billIds, fmt.Sprintf("%d_%s", stage.BillType, stage.BillId))
  106. }
  107. }
  108. // 去重单据号
  109. typeBillIds := removeDuplicationSort(billIds)
  110. if len(typeBillIds) < 1 {
  111. return nil, errors.New("未找到单据信息")
  112. }
  113. f := excelize.NewFile()
  114. index := f.NewSheet("Sheet1")
  115. f.SetActiveSheet(index)
  116. f.SetDefaultFont("宋体")
  117. companyName := getCompanyName(apictx)
  118. row := 0
  119. for _, tId := range typeBillIds {
  120. tidArr := strings.Split(tId, "_")
  121. var billExcel IExcel
  122. // 采购
  123. billId, _ := primitive.ObjectIDFromHex(tidArr[1])
  124. if tidArr[0] == "1" {
  125. purchase := model.PurchaseBill{}
  126. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  127. CollectName: repo.CollectionBillPurchase,
  128. Query: repo.Map{"_id": billId},
  129. }, &purchase)
  130. if found {
  131. billExcel = NewPurchaseBill(f)
  132. if purchase.Reviewed == 1 {
  133. if len(purchase.SignUsers) > 0 {
  134. signs := []*model.Signature{}
  135. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  136. CollectName: repo.CollectionSignature,
  137. Query: repo.Map{"_id": bson.M{"$in": purchase.SignUsers}},
  138. Sort: bson.M{"sort": 1},
  139. }, &signs)
  140. billExcel.SetSignatures(signs)
  141. }
  142. }
  143. billExcel.SetContent(&purchase)
  144. billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
  145. }
  146. }
  147. // 工艺
  148. if tidArr[0] == "2" {
  149. produce := model.ProduceBill{}
  150. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  151. CollectName: repo.CollectionBillProduce,
  152. Query: repo.Map{"_id": billId},
  153. }, &produce)
  154. if found {
  155. billExcel = NewProduceBill(f)
  156. if produce.Reviewed == 1 {
  157. if len(produce.SignUsers) > 0 {
  158. signs := []*model.Signature{}
  159. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  160. CollectName: repo.CollectionSignature,
  161. Query: repo.Map{"_id": bson.M{"$in": produce.SignUsers}},
  162. Sort: bson.M{"sort": 1},
  163. }, &signs)
  164. billExcel.SetSignatures(signs)
  165. }
  166. }
  167. billExcel.SetContent(&produce)
  168. billExcel.SetTitle(fmt.Sprintf("%s加工单", companyName))
  169. }
  170. }
  171. // 成品采购
  172. if tidArr[0] == "3" {
  173. product := model.ProductBill{}
  174. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  175. CollectName: repo.CollectionBillProduct,
  176. Query: repo.Map{"_id": billId},
  177. }, &product)
  178. if found {
  179. billExcel = NewProductBill(f)
  180. if product.Reviewed == 1 {
  181. if len(product.SignUsers) > 0 {
  182. signs := []*model.Signature{}
  183. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  184. CollectName: repo.CollectionSignature,
  185. Query: repo.Map{"_id": bson.M{"$in": product.SignUsers}},
  186. Sort: bson.M{"sort": 1},
  187. }, &signs)
  188. billExcel.SetSignatures(signs)
  189. }
  190. }
  191. billExcel.SetContent(&product)
  192. billExcel.SetTitle(companyName)
  193. }
  194. }
  195. if billExcel == nil {
  196. continue
  197. }
  198. billExcel.SetRow(row)
  199. billExcel.Draws()
  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. if plan.Name == "" {
  220. return nil, errors.New("生产计划名为空")
  221. }
  222. if plan.Total == 0 {
  223. return nil, errors.New("生产计划数应不为0")
  224. }
  225. plan.Status = "process" // 进行中
  226. plan.CreateTime = time.Now()
  227. plan.UpdateTime = time.Now()
  228. result, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, &plan)
  229. return result, err
  230. }
  231. // 获取生产计划信息
  232. func GetProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  233. planId := c.Param("id")
  234. id, err := primitive.ObjectIDFromHex(planId)
  235. if err != nil {
  236. return nil, errors.New("非法id")
  237. }
  238. var plan model.ProductPlan
  239. option := &repo.DocSearchOptions{
  240. CollectName: repo.CollectionProductPlan,
  241. Query: repo.Map{"_id": id},
  242. }
  243. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &plan)
  244. if !found || err != nil {
  245. log.Info(err)
  246. return nil, errors.New("数据未找到")
  247. }
  248. billStates := map[string]string{}
  249. if plan.Pack != nil && plan.Pack.Components != nil {
  250. for _, comp := range plan.Pack.Components {
  251. if comp.Stages != nil {
  252. for _, stage := range comp.Stages {
  253. if len(stage.BillId) > 0 {
  254. collectName := ""
  255. // 材料
  256. if stage.BillType == 1 {
  257. collectName = repo.CollectionBillPurchase
  258. }
  259. // 工艺
  260. if stage.BillType == 2 {
  261. collectName = repo.CollectionBillProduce
  262. }
  263. // 成品
  264. if stage.BillType == 3 {
  265. collectName = repo.CollectionBillProduct
  266. }
  267. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: collectName, Query: repo.Map{"_id": stage.BillId}, Project: []string{"status"}})
  268. if ok {
  269. billStates[stage.BillId] = state["status"].(string)
  270. }
  271. }
  272. }
  273. }
  274. }
  275. }
  276. return map[string]interface{}{
  277. "plan": plan,
  278. "billStates": billStates,
  279. }, nil
  280. }
  281. // 获取生产计划列表
  282. func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  283. page, size, query := UtilQueryPageSize(c)
  284. if _packId, ok := query["packId"]; ok {
  285. packId, _ := primitive.ObjectIDFromHex(_packId.(string))
  286. query["pack._id"] = packId
  287. delete(query, "packId")
  288. }
  289. option := &repo.PageSearchOptions{
  290. CollectName: repo.CollectionProductPlan,
  291. Query: query,
  292. Page: page,
  293. Size: size,
  294. Sort: bson.M{"createTime": -1},
  295. Project: []string{"_id", "thumbnail", "name", "updateTime", "createTime", "createUser", "total", "totalPrice", "status"},
  296. }
  297. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  298. }
  299. // 更新生产计划
  300. func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  301. var plan model.ProductPlan
  302. err := c.ShouldBindJSON(&plan)
  303. if err != nil {
  304. return nil, errors.New("参数错误")
  305. }
  306. if plan.Id.Hex() == "" {
  307. return nil, errors.New("id的为空")
  308. }
  309. plan.UpdateTime = time.Now()
  310. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
  311. }
  312. // 删除生产计划
  313. func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  314. planId := c.Param("id")
  315. if planId == "" {
  316. return nil, errors.New("id为空")
  317. }
  318. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
  319. }