report.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package service
  2. import (
  3. "context"
  4. dao "dashoo.cn/micro/app/dao/contract"
  5. "dashoo.cn/micro/app/model/contract"
  6. "dashoo.cn/micro/app/service"
  7. "dashoo.cn/opms_libary/micro_srv"
  8. "dashoo.cn/opms_libary/myerrors"
  9. "fmt"
  10. "github.com/gogf/gf/frame/g"
  11. )
  12. type ContractReportService struct {
  13. Dao *dao.CtrContractDao
  14. *service.ContextService
  15. }
  16. func NewContractReportService(ctx context.Context) (svc *ContractReportService, err error) {
  17. tenant, err := micro_srv.GetTenant(ctx)
  18. if err != nil {
  19. err = myerrors.TipsError(fmt.Sprintf("获取租户码异常:%s", err.Error()))
  20. return nil, err //fmt.Errorf("获取租户码异常:%s", err.Error())
  21. }
  22. svc = new(ContractReportService)
  23. if svc.ContextService, err = svc.Init(ctx); err != nil {
  24. return nil, err
  25. }
  26. svc.Dao = dao.NewCtrContractDao(tenant)
  27. return svc, nil
  28. }
  29. type CollectionNumData struct {
  30. contract.CtrContractCollection
  31. InchargeId int `orm:"incharge_id" json:"inchargeId"` // 负责人ID
  32. InchargeName string `orm:"incharge_name" json:"inchargeName"` // 负责人(销售工程师)
  33. ProductLine string `orm:"product_line" json:"productLine"` // 产品线
  34. Total float64 `orm:"total"` // 合计
  35. }
  36. // QueryCollectionNum 业绩指标-回款金额
  37. func (s *ContractReportService) QueryCollectionNum(date string) (interface{}, error) {
  38. // 获取数据
  39. var collectionInfos []*CollectionNumData
  40. err := s.Dao.DB.Model("ctr_contract_collection a").InnerJoin("ctr_contract b", "a.contract_id=b.id").Where(fmt.Sprintf("collection_datetime LIKE '%v%%'", date)).Fields("a.*,b.incharge_id,b.incharge_name,b.product_line,SUM(a.collection_amount) total").Group("incharge_id, product_line").Order("incharge_id ASC, product_line ASC").Scan(&collectionInfos)
  41. if err != nil {
  42. return nil, err
  43. }
  44. // 获取销售数据
  45. userList, err := service.GetUserListByRoleCode(s.Ctx, []string{"SalesEngineer", "ProductLineManager"}, 1000)
  46. if err != nil {
  47. return nil, err
  48. }
  49. // BIOBANK CELLSOP LIMS+基因 其他
  50. header, data := make([]g.Map, 0), make([]g.Map, 0)
  51. header = append(header, g.Map{"prop": "userName", "label": "销售工程师"}, g.Map{"prop": "bio", "label": "BIOBANK"}, g.Map{"prop": "cell", "label": "CELLSOP"}, g.Map{"prop": "lims", "label": "LIMS+基因"}, g.Map{"prop": "other", "label": "其他"}, g.Map{"prop": "total", "label": "合计"})
  52. user2Collections := make(map[int][]*CollectionNumData, 0)
  53. total := float64(0)
  54. for _, col := range collectionInfos {
  55. if _, ok := user2Collections[col.InchargeId]; !ok {
  56. user2Collections[col.InchargeId] = make([]*CollectionNumData, 0)
  57. }
  58. user2Collections[col.InchargeId] = append(user2Collections[col.InchargeId], col)
  59. total += col.Total
  60. }
  61. // 统计数据
  62. for _, user := range userList {
  63. bio, cell, lims, other, tot := float64(0), float64(0), float64(0), float64(0), float64(0)
  64. if len(user2Collections[user["Id"].(int)]) > 0 {
  65. for _, col := range user2Collections[user["Id"].(int)] {
  66. tot += col.Total
  67. if col.ProductLine == "10" {
  68. bio += col.Total
  69. } else if col.ProductLine == "20" {
  70. cell += col.Total
  71. } else if col.ProductLine == "30" {
  72. lims += col.Total
  73. } else {
  74. other += col.Total
  75. }
  76. }
  77. }
  78. data = append(data, g.Map{
  79. "userName": user["NickName"],
  80. "bio": fmt.Sprintf("%.2f", bio),
  81. "cell": fmt.Sprintf("%.2f", cell),
  82. "lims": fmt.Sprintf("%.2f", lims),
  83. "other": fmt.Sprintf("%.2f", other),
  84. "total": fmt.Sprintf("%.2f", tot),
  85. })
  86. }
  87. data = append(data, g.Map{
  88. "userName": "",
  89. "bio": "",
  90. "cell": "",
  91. "lims": "",
  92. "other": "",
  93. "total": fmt.Sprintf("总计:%.2f元", total),
  94. })
  95. return g.Map{"header": header, "data": data}, nil
  96. }
  97. // QueryContractNum 业绩指标-签约合同金额
  98. func (s *ContractReportService) QueryContractNum(date string) (interface{}, error) {
  99. type ContractNumData struct {
  100. contract.CtrContract
  101. Total float64 `orm:"total"` // 合计
  102. }
  103. // 获取数据
  104. var contractInfos []*ContractNumData
  105. err := s.Dao.Where(fmt.Sprintf("contract_sign_time LIKE '%v%%'", date)).Fields("ctr_contract.*,SUM(contract_amount) total").Group("incharge_id, product_line").Order("incharge_id ASC, product_line ASC").Scan(&contractInfos)
  106. if err != nil {
  107. return nil, err
  108. }
  109. // 获取销售数据
  110. userList, err := service.GetUserListByRoleCode(s.Ctx, []string{"SalesEngineer", "ProductLineManager"}, 1000)
  111. if err != nil {
  112. return nil, err
  113. }
  114. // BIOBANK CELLSOP LIMS+基因 其他
  115. header, data := make([]g.Map, 0), make([]g.Map, 0)
  116. header = append(header, g.Map{"prop": "userName", "label": "销售工程师"}, g.Map{"prop": "bio", "label": "BIOBANK"}, g.Map{"prop": "cell", "label": "CELLSOP"}, g.Map{"prop": "lims", "label": "LIMS+基因"}, g.Map{"prop": "other", "label": "其他"}, g.Map{"prop": "total", "label": "合计"})
  117. user2Collections := make(map[int][]*ContractNumData, 0)
  118. total := float64(0)
  119. for _, col := range contractInfos {
  120. if _, ok := user2Collections[col.InchargeId]; !ok {
  121. user2Collections[col.InchargeId] = make([]*ContractNumData, 0)
  122. }
  123. user2Collections[col.InchargeId] = append(user2Collections[col.InchargeId], col)
  124. total += col.Total
  125. }
  126. // 统计数据
  127. for _, user := range userList {
  128. bio, cell, lims, other, tot := float64(0), float64(0), float64(0), float64(0), float64(0)
  129. if len(user2Collections[user["Id"].(int)]) > 0 {
  130. for _, col := range user2Collections[user["Id"].(int)] {
  131. tot += col.Total
  132. if col.ProductLine == "10" {
  133. bio += col.Total
  134. } else if col.ProductLine == "20" {
  135. cell += col.Total
  136. } else if col.ProductLine == "30" {
  137. lims += col.Total
  138. } else {
  139. other += col.Total
  140. }
  141. }
  142. }
  143. data = append(data, g.Map{
  144. "userName": user["NickName"],
  145. "bio": fmt.Sprintf("%.2f", bio),
  146. "cell": fmt.Sprintf("%.2f", cell),
  147. "lims": fmt.Sprintf("%.2f", lims),
  148. "other": fmt.Sprintf("%.2f", other),
  149. "total": fmt.Sprintf("%.2f", tot),
  150. })
  151. }
  152. data = append(data, g.Map{
  153. "userName": "",
  154. "bio": "",
  155. "cell": "",
  156. "lims": "",
  157. "other": "",
  158. "total": fmt.Sprintf("总计:%.2f元", total),
  159. })
  160. return g.Map{"header": header, "data": data}, nil
  161. }