summary.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "box-cost/log"
  6. "errors"
  7. "strings"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. "github.com/xuri/excelize/v2"
  11. "go.mongodb.org/mongo-driver/bson"
  12. "go.mongodb.org/mongo-driver/bson/primitive"
  13. )
  14. // 汇总
  15. func Summary(r *GinRouter) {
  16. // 下载详细汇总
  17. r.GET("/summary/download", SummaryDownload)
  18. // 下载简单汇总
  19. r.GET("/summary/sample/download", SummarySampleDownload)
  20. // !10.27 时间和供应商查询
  21. // summary/supplier/plan query={"supplierId":"xxx","timeRange":["xxx","xxx"]}
  22. // 返回planIds数组 []string
  23. r.GET("/summary/supplier/plan", SummarySupplierPlan)
  24. }
  25. type SupplierPlanSummary struct {
  26. Plans []*PlanSummary
  27. SupplierId primitive.ObjectID
  28. ApiSession *ApiSession
  29. }
  30. type PlanSummary struct {
  31. Plan *model.ProductPlan
  32. IsSend map[string]bool
  33. IsAck map[string]bool
  34. Reviewed map[string]int32
  35. State map[string]string
  36. CreateTimes map[string]time.Time
  37. SerialNumber map[string]string
  38. SendTo map[string]string
  39. }
  40. type PlanSimple struct {
  41. Id primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
  42. Name string `bson:"name,omitempty" json:"name"`
  43. CreateUser string `bson:"createUser,omitempty" json:"createUser"`
  44. //生产数量
  45. Total int `bson:"total,omitempty" json:"total"`
  46. //状态
  47. Status string `bson:"status,omitempty" json:"status"`
  48. //总价
  49. TotalPrice float64 `bson:"totalPrice,omitempty" json:"totalPrice"`
  50. CreateTime time.Time `bson:"createTime,omitempty" json:"createTime"`
  51. }
  52. func SummarySupplierPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  53. _, _, query := UtilQueryPageSize(c)
  54. supplierId := primitive.NilObjectID
  55. if _supplierId, ok := query["supplierId"]; ok {
  56. supplierId, _ = primitive.ObjectIDFromHex(_supplierId.(string))
  57. delete(query, "supplierId")
  58. }
  59. if _timeRange, ok := query["timeRange"]; ok {
  60. timeRange, _ := _timeRange.([]interface{})
  61. if len(timeRange) == 2 {
  62. start, end := getTimeRange(timeRange[0].(string), timeRange[1].(string))
  63. query["createTime"] = bson.M{"$gte": start, "$lte": end}
  64. }
  65. delete(query, "timeRange")
  66. }
  67. // 遍历判断包含供应商的数据,返回planIds
  68. plans := []*model.ProductPlan{}
  69. err := repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  70. CollectName: repo.CollectionProductPlan,
  71. Query: query,
  72. Sort: bson.M{"createTime": -1},
  73. }, &plans)
  74. if err != nil {
  75. log.Error(err)
  76. return nil, errors.New("查询数据错误!")
  77. }
  78. planSimples := []*PlanSimple{}
  79. if len(plans) < 1 {
  80. return planSimples, nil
  81. }
  82. // 遍历
  83. for _, plan := range plans {
  84. flag := false
  85. ps := &PlanSimple{
  86. Id: plan.Id,
  87. Name: plan.Name,
  88. CreateUser: plan.CreateUser,
  89. Total: plan.Total,
  90. Status: plan.Status,
  91. TotalPrice: plan.TotalPrice,
  92. CreateTime: plan.CreateTime,
  93. }
  94. // 选供应商
  95. if !supplierId.IsZero() {
  96. for _, comp := range plan.Pack.Components {
  97. if comp.Id == "" || len(comp.Stages) == 0 {
  98. continue
  99. }
  100. for _, stage := range comp.Stages {
  101. // 只要任意一个stage中存在选中的供应商,那么这个计划包含这个供应商,这时应该跳出来
  102. if stage.SupplierInfo == nil {
  103. continue
  104. }
  105. if stage.SupplierInfo.Id == supplierId {
  106. planSimples = append(planSimples, ps)
  107. flag = true
  108. break
  109. }
  110. }
  111. if flag {
  112. break
  113. }
  114. }
  115. } else {
  116. planSimples = append(planSimples, ps)
  117. }
  118. }
  119. return planSimples, nil
  120. }
  121. // /summary/download?planIds=id1,id2&supplierId=xxx
  122. func SummaryDownload(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  123. // 获取planIds supplierId
  124. _planIds := c.Query("planIds") // id1,id2...
  125. _supplierId := c.Query("supplierId") // 供应商id
  126. planIds := strings.Split(_planIds, ",")
  127. supplierId, _ := primitive.ObjectIDFromHex(_supplierId)
  128. // 获取详情
  129. if len(planIds) < 1 {
  130. return nil, errors.New("数据不存在")
  131. }
  132. summaryPlans := []*PlanSummary{}
  133. for _, _planId := range planIds {
  134. id, _ := primitive.ObjectIDFromHex(_planId)
  135. if id.IsZero() {
  136. return nil, errors.New("非法id")
  137. }
  138. plan := &model.ProductPlan{}
  139. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  140. CollectName: repo.CollectionProductPlan,
  141. Query: repo.Map{"_id": id},
  142. }, plan)
  143. if !found || err != nil {
  144. continue
  145. }
  146. summaryPlan := GetPlanStatus(plan, apictx)
  147. summaryPlans = append(summaryPlans, summaryPlan)
  148. }
  149. if len(summaryPlans) < 1 {
  150. return nil, errors.New("数据不存在")
  151. }
  152. // 实例化表格,传入数据
  153. f := excelize.NewFile()
  154. index := f.NewSheet("Sheet1")
  155. f.SetActiveSheet(index)
  156. f.SetDefaultFont("宋体")
  157. planSummaryExcel := NewPlanSummaryExcel(f)
  158. planSummaryExcel.Content = &SupplierPlanSummary{
  159. Plans: summaryPlans,
  160. SupplierId: supplierId,
  161. ApiSession: apictx,
  162. }
  163. // 绘制表格
  164. planSummaryExcel.Draws()
  165. c.Header("Content-Type", "application/octet-stream")
  166. c.Header("Content-Disposition", "attachment; filename="+"planSummary.xlsx")
  167. c.Header("Content-Transfer-Encoding", "binary")
  168. err := f.Write(c.Writer)
  169. if err != nil {
  170. return nil, err
  171. }
  172. return nil, nil
  173. }
  174. // /summary/sample/download?planIds=id1,id2&supplierId=xxx
  175. func SummarySampleDownload(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  176. // 获取planIds supplierId
  177. _planIds := c.Query("planIds") // id1,id2...
  178. _supplierId := c.Query("supplierId") // 供应商id
  179. planIds := strings.Split(_planIds, ",")
  180. supplierId, _ := primitive.ObjectIDFromHex(_supplierId)
  181. // 获取详情
  182. if len(planIds) < 1 {
  183. return nil, errors.New("数据不存在")
  184. }
  185. summaryPlans := []*PlanSummary{}
  186. for _, _planId := range planIds {
  187. id, _ := primitive.ObjectIDFromHex(_planId)
  188. if id.IsZero() {
  189. return nil, errors.New("非法id")
  190. }
  191. plan := &model.ProductPlan{}
  192. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  193. CollectName: repo.CollectionProductPlan,
  194. Query: repo.Map{"_id": id},
  195. }, plan)
  196. if !found || err != nil {
  197. continue
  198. }
  199. summaryPlan := GetPlanStatus(plan, apictx)
  200. summaryPlans = append(summaryPlans, summaryPlan)
  201. }
  202. if len(summaryPlans) < 1 {
  203. return nil, errors.New("数据不存在")
  204. }
  205. // 实例化表格,传入数据
  206. f := excelize.NewFile()
  207. index := f.NewSheet("Sheet1")
  208. f.SetActiveSheet(index)
  209. f.SetDefaultFont("宋体")
  210. planSummaryExcel := NewSummarySampleExcel(f)
  211. planSummaryExcel.Content = &SupplierPlanSummary{
  212. Plans: summaryPlans,
  213. SupplierId: supplierId,
  214. }
  215. // 绘制表格
  216. planSummaryExcel.Draws()
  217. c.Header("Content-Type", "application/octet-stream")
  218. c.Header("Content-Disposition", "attachment; filename="+"planSummary.xlsx")
  219. c.Header("Content-Transfer-Encoding", "binary")
  220. err := f.Write(c.Writer)
  221. if err != nil {
  222. return nil, err
  223. }
  224. return nil, nil
  225. }
  226. func GetPlanStatus(plan *model.ProductPlan, apictx *ApiSession) *PlanSummary {
  227. billStates := map[string]string{}
  228. billSerialNumber := map[string]string{}
  229. billSendTo := map[string]string{}
  230. billIsSend := map[string]bool{}
  231. billReviewed := map[string]int32{}
  232. billIsAck := map[string]bool{}
  233. billCreateTimes := map[string]time.Time{}
  234. if plan.Pack != nil && plan.Pack.Components != nil {
  235. for _, comp := range plan.Pack.Components {
  236. if comp.Stages != nil {
  237. for _, stage := range comp.Stages {
  238. if len(stage.BillId) > 0 {
  239. collectName := ""
  240. // 材料
  241. if stage.BillType == 1 {
  242. collectName = repo.CollectionBillPurchase
  243. }
  244. // 工艺
  245. if stage.BillType == 2 {
  246. collectName = repo.CollectionBillProduce
  247. }
  248. // 成品
  249. if stage.BillType == 3 {
  250. collectName = repo.CollectionBillProduct
  251. }
  252. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  253. CollectName: collectName,
  254. Query: repo.Map{"_id": stage.BillId},
  255. Project: []string{"status", "isSend", "reviewed", "isAck", "serialNumber", "sendTo", "remark", "createTime"}})
  256. if ok {
  257. billStates[stage.BillId] = state["status"].(string)
  258. billSerialNumber[stage.BillId] = state["serialNumber"].(string)
  259. if v, ok := state["sendTo"]; ok {
  260. billSendTo[stage.BillId] = v.(string)
  261. }
  262. if v, ok := state["isSend"]; ok {
  263. billIsSend[stage.BillId] = v.(bool)
  264. } else {
  265. billIsSend[stage.BillId] = false
  266. }
  267. if v, ok := state["reviewed"]; ok {
  268. billReviewed[stage.BillId] = v.(int32)
  269. } else {
  270. billReviewed[stage.BillId] = -1
  271. }
  272. if v, ok := state["isAck"]; ok {
  273. billIsAck[stage.BillId] = v.(bool)
  274. } else {
  275. billIsAck[stage.BillId] = false
  276. }
  277. if v, ok := state["createTime"]; ok {
  278. billCreateTimes[stage.BillId] = v.(primitive.DateTime).Time()
  279. }
  280. }
  281. }
  282. }
  283. }
  284. }
  285. }
  286. return &PlanSummary{
  287. Plan: plan,
  288. IsSend: billIsSend,
  289. IsAck: billIsAck,
  290. Reviewed: billReviewed,
  291. State: billStates,
  292. CreateTimes: billCreateTimes,
  293. SerialNumber: billSerialNumber,
  294. SendTo: billSendTo,
  295. }
  296. }