summary.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. flag := false
  84. for _, plan := range plans {
  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. if flag {
  119. continue
  120. }
  121. }
  122. return planSimples, nil
  123. }
  124. // /summary/download?planIds=id1,id2&supplierId=xxx
  125. func SummaryDownload(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  126. // 获取planIds supplierId
  127. _planIds := c.Query("planIds") // id1,id2...
  128. _supplierId := c.Query("supplierId") // 供应商id
  129. planIds := strings.Split(_planIds, ",")
  130. supplierId, _ := primitive.ObjectIDFromHex(_supplierId)
  131. // 获取详情
  132. if len(planIds) < 1 {
  133. return nil, errors.New("数据不存在")
  134. }
  135. summaryPlans := []*PlanSummary{}
  136. for _, _planId := range planIds {
  137. id, _ := primitive.ObjectIDFromHex(_planId)
  138. if id.IsZero() {
  139. return nil, errors.New("非法id")
  140. }
  141. plan := &model.ProductPlan{}
  142. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  143. CollectName: repo.CollectionProductPlan,
  144. Query: repo.Map{"_id": id},
  145. }, plan)
  146. if !found || err != nil {
  147. continue
  148. }
  149. summaryPlan := GetPlanStatus(plan, apictx)
  150. summaryPlans = append(summaryPlans, summaryPlan)
  151. }
  152. if len(summaryPlans) < 1 {
  153. return nil, errors.New("数据不存在")
  154. }
  155. // 实例化表格,传入数据
  156. f := excelize.NewFile()
  157. index := f.NewSheet("Sheet1")
  158. f.SetActiveSheet(index)
  159. f.SetDefaultFont("宋体")
  160. planSummaryExcel := NewPlanSummaryExcel(f)
  161. planSummaryExcel.Content = &SupplierPlanSummary{
  162. Plans: summaryPlans,
  163. SupplierId: supplierId,
  164. ApiSession: apictx,
  165. }
  166. // 绘制表格
  167. planSummaryExcel.Draws()
  168. c.Header("Content-Type", "application/octet-stream")
  169. c.Header("Content-Disposition", "attachment; filename="+"planSummary.xlsx")
  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. // /summary/sample/download?planIds=id1,id2&supplierId=xxx
  178. func SummarySampleDownload(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  179. // 获取planIds supplierId
  180. _planIds := c.Query("planIds") // id1,id2...
  181. _supplierId := c.Query("supplierId") // 供应商id
  182. planIds := strings.Split(_planIds, ",")
  183. supplierId, _ := primitive.ObjectIDFromHex(_supplierId)
  184. // 获取详情
  185. if len(planIds) < 1 {
  186. return nil, errors.New("数据不存在")
  187. }
  188. summaryPlans := []*PlanSummary{}
  189. for _, _planId := range planIds {
  190. id, _ := primitive.ObjectIDFromHex(_planId)
  191. if id.IsZero() {
  192. return nil, errors.New("非法id")
  193. }
  194. plan := &model.ProductPlan{}
  195. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  196. CollectName: repo.CollectionProductPlan,
  197. Query: repo.Map{"_id": id},
  198. }, plan)
  199. if !found || err != nil {
  200. continue
  201. }
  202. summaryPlan := GetPlanStatus(plan, apictx)
  203. summaryPlans = append(summaryPlans, summaryPlan)
  204. }
  205. if len(summaryPlans) < 1 {
  206. return nil, errors.New("数据不存在")
  207. }
  208. // 实例化表格,传入数据
  209. f := excelize.NewFile()
  210. index := f.NewSheet("Sheet1")
  211. f.SetActiveSheet(index)
  212. f.SetDefaultFont("宋体")
  213. planSummaryExcel := NewSummarySampleExcel(f)
  214. planSummaryExcel.Content = &SupplierPlanSummary{
  215. Plans: summaryPlans,
  216. SupplierId: supplierId,
  217. }
  218. // 绘制表格
  219. planSummaryExcel.Draws()
  220. c.Header("Content-Type", "application/octet-stream")
  221. c.Header("Content-Disposition", "attachment; filename="+"planSummary.xlsx")
  222. c.Header("Content-Transfer-Encoding", "binary")
  223. err := f.Write(c.Writer)
  224. if err != nil {
  225. return nil, err
  226. }
  227. return nil, nil
  228. }
  229. func GetPlanStatus(plan *model.ProductPlan, apictx *ApiSession) *PlanSummary {
  230. billStates := map[string]string{}
  231. billSerialNumber := map[string]string{}
  232. billSendTo := map[string]string{}
  233. billIsSend := map[string]bool{}
  234. billReviewed := map[string]int32{}
  235. billIsAck := map[string]bool{}
  236. billCreateTimes := map[string]time.Time{}
  237. if plan.Pack != nil && plan.Pack.Components != nil {
  238. for _, comp := range plan.Pack.Components {
  239. if comp.Stages != nil {
  240. for _, stage := range comp.Stages {
  241. if len(stage.BillId) > 0 {
  242. collectName := ""
  243. // 材料
  244. if stage.BillType == 1 {
  245. collectName = repo.CollectionBillPurchase
  246. }
  247. // 工艺
  248. if stage.BillType == 2 {
  249. collectName = repo.CollectionBillProduce
  250. }
  251. // 成品
  252. if stage.BillType == 3 {
  253. collectName = repo.CollectionBillProduct
  254. }
  255. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  256. CollectName: collectName,
  257. Query: repo.Map{"_id": stage.BillId},
  258. Project: []string{"status", "isSend", "reviewed", "isAck", "serialNumber", "sendTo", "remark", "createTime"}})
  259. if ok {
  260. billStates[stage.BillId] = state["status"].(string)
  261. billSerialNumber[stage.BillId] = state["serialNumber"].(string)
  262. if v, ok := state["sendTo"]; ok {
  263. billSendTo[stage.BillId] = v.(string)
  264. }
  265. if v, ok := state["isSend"]; ok {
  266. billIsSend[stage.BillId] = v.(bool)
  267. } else {
  268. billIsSend[stage.BillId] = false
  269. }
  270. if v, ok := state["reviewed"]; ok {
  271. billReviewed[stage.BillId] = v.(int32)
  272. } else {
  273. billReviewed[stage.BillId] = -1
  274. }
  275. if v, ok := state["isAck"]; ok {
  276. billIsAck[stage.BillId] = v.(bool)
  277. } else {
  278. billIsAck[stage.BillId] = false
  279. }
  280. if v, ok := state["createTime"]; ok {
  281. billCreateTimes[stage.BillId] = v.(primitive.DateTime).Time()
  282. }
  283. }
  284. }
  285. }
  286. }
  287. }
  288. }
  289. return &PlanSummary{
  290. Plan: plan,
  291. IsSend: billIsSend,
  292. IsAck: billIsAck,
  293. Reviewed: billReviewed,
  294. State: billStates,
  295. CreateTimes: billCreateTimes,
  296. SerialNumber: billSerialNumber,
  297. SendTo: billSendTo,
  298. }
  299. }