report.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package proj
  2. import (
  3. "context"
  4. dao "dashoo.cn/micro/app/dao/proj"
  5. model "dashoo.cn/micro/app/model/proj"
  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 BusinessReportService struct {
  13. Dao *dao.ProjBusinessDao
  14. *service.ContextService
  15. }
  16. func NewBusinessReportService(ctx context.Context) (svc *BusinessReportService, 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(BusinessReportService)
  23. if svc.ContextService, err = svc.Init(ctx); err != nil {
  24. return nil, err
  25. }
  26. svc.Dao = dao.NewProjBusinessDao(tenant)
  27. return svc, nil
  28. }
  29. // QueryBusinessNum 业绩指标-当月新增项目
  30. func (s *BusinessReportService) QueryBusinessNum(date string) (interface{}, error) {
  31. type BusinessNumData struct {
  32. model.ProjBusiness
  33. Total float64 `orm:"total"` // 合计
  34. }
  35. where := ""
  36. // 权限限制(销售工程师看自己的)
  37. if service.StringsContains(s.CxtUser.Roles, "SalesEngineer") {
  38. where = fmt.Sprintf("sale_id='%v'", s.CxtUser.Id)
  39. }
  40. // 获取数据 (储备转C,项目来源为 400热线 10、展会信息获取 50)不记作新增数据
  41. var businessInfos []*BusinessNumData
  42. err := s.Dao.Where(fmt.Sprintf("created_time LIKE '%v%%' AND appro_status='30' AND nbo_source<>'10' AND nbo_source<>'50'", date)).Where(where).Fields("proj_business.*,COUNT(1) total").Group("sale_id, product_line").Order("sale_id ASC, product_line ASC").Scan(&businessInfos)
  43. if err != nil {
  44. return nil, err
  45. }
  46. // 获取销售数据
  47. userList, err := service.GetUserListByRoleCode(s.Ctx, []string{"SalesEngineer"}, 1000)
  48. if err != nil {
  49. return nil, err
  50. }
  51. // 权限限制(销售工程师看自己的)
  52. if service.StringsContains(s.CxtUser.Roles, "SalesEngineer") {
  53. userList = []map[string]interface{}{
  54. {
  55. "Id": s.CxtUser.Id,
  56. "NickName": s.CxtUser.NickName,
  57. },
  58. }
  59. }
  60. // BIOBANK CELLSOP LIMS+基因 其他
  61. header, data := make([]g.Map, 0), make([]g.Map, 0)
  62. 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": "合计"})
  63. user2Business := make(map[int][]*BusinessNumData, 0)
  64. total := float64(0)
  65. for _, col := range businessInfos {
  66. if _, ok := user2Business[col.SaleId]; !ok {
  67. user2Business[col.SaleId] = make([]*BusinessNumData, 0)
  68. }
  69. user2Business[col.SaleId] = append(user2Business[col.SaleId], col)
  70. total += col.Total
  71. }
  72. // 统计数据
  73. for _, user := range userList {
  74. bio, cell, lims, other, tot := float64(0), float64(0), float64(0), float64(0), float64(0)
  75. if len(user2Business[user["Id"].(int)]) > 0 {
  76. for _, col := range user2Business[user["Id"].(int)] {
  77. tot += col.Total
  78. if col.ProductLine == "10" {
  79. bio += col.Total
  80. } else if col.ProductLine == "20" {
  81. cell += col.Total
  82. } else if col.ProductLine == "30" {
  83. lims += col.Total
  84. } else {
  85. other += col.Total
  86. }
  87. }
  88. }
  89. data = append(data, g.Map{
  90. "userName": user["NickName"],
  91. "bio": fmt.Sprintf("%v", bio),
  92. "cell": fmt.Sprintf("%v", cell),
  93. "lims": fmt.Sprintf("%v", lims),
  94. "other": fmt.Sprintf("%v", other),
  95. "total": fmt.Sprintf("%v", tot),
  96. })
  97. }
  98. data = append(data, g.Map{
  99. "userName": "",
  100. "bio": "",
  101. "cell": "",
  102. "lims": "",
  103. "other": "",
  104. "total": fmt.Sprintf("总计:%v个", total),
  105. })
  106. return g.Map{"header": header, "data": data}, nil
  107. }