report.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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("proj_business.sale_id='%v'", s.CxtUser.Id)
  39. }
  40. // 获取数据 (储备转C,项目来源为 400热线 10、展会信息获取 50)不记作新增数据
  41. // opn_type 项目升级
  42. var businessInfos []*BusinessNumData
  43. err := s.Dao.LeftJoin("(SELECT * FROM proj_business_dynamics WHERE opn_content LIKE '%\"origNboType\":\"50\"%' AND opn_type='40' GROUP BY proj_business_dynamics.bus_id) b", "proj_business.Id=b.bus_id").Where(fmt.Sprintf("proj_business.created_time LIKE '%v%%' AND proj_business.appro_status='30' AND proj_business.nbo_source<>'10' AND proj_business.nbo_source<>'50' AND proj_business.nbo_type<>'50' AND b.id IS NULL", date)).Where(where).Fields("proj_business.*,COUNT(1) total").Group("proj_business.sale_id, proj_business.product_line").Order("proj_business.sale_id ASC, proj_business.product_line ASC").Scan(&businessInfos)
  44. if err != nil {
  45. return nil, err
  46. }
  47. // 获取销售数据
  48. userList, err := service.GetUserListByRoleCode(s.Ctx, []string{"SalesEngineer"}, 1000)
  49. if err != nil {
  50. return nil, err
  51. }
  52. // 权限限制(销售工程师看自己的)
  53. if service.StringsContains(s.CxtUser.Roles, "SalesEngineer") {
  54. userList = []map[string]interface{}{
  55. {
  56. "Id": s.CxtUser.Id,
  57. "NickName": s.CxtUser.NickName,
  58. },
  59. }
  60. }
  61. // BIOBANK CELLSOP LIMS+基因 其他
  62. header, data := make([]g.Map, 0), make([]g.Map, 0)
  63. 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": "合计"})
  64. user2Business := make(map[int][]*BusinessNumData, 0)
  65. total := float64(0)
  66. for _, col := range businessInfos {
  67. if _, ok := user2Business[col.SaleId]; !ok {
  68. user2Business[col.SaleId] = make([]*BusinessNumData, 0)
  69. }
  70. user2Business[col.SaleId] = append(user2Business[col.SaleId], col)
  71. total += col.Total
  72. }
  73. // 统计数据
  74. for _, user := range userList {
  75. bio, cell, lims, other, tot := float64(0), float64(0), float64(0), float64(0), float64(0)
  76. if len(user2Business[user["Id"].(int)]) > 0 {
  77. for _, col := range user2Business[user["Id"].(int)] {
  78. tot += col.Total
  79. if col.ProductLine == "10" {
  80. bio += col.Total
  81. } else if col.ProductLine == "20" {
  82. cell += col.Total
  83. } else if col.ProductLine == "30" {
  84. lims += col.Total
  85. } else {
  86. other += col.Total
  87. }
  88. }
  89. }
  90. data = append(data, g.Map{
  91. "userName": user["NickName"],
  92. "bio": fmt.Sprintf("%v", bio),
  93. "cell": fmt.Sprintf("%v", cell),
  94. "lims": fmt.Sprintf("%v", lims),
  95. "other": fmt.Sprintf("%v", other),
  96. "total": fmt.Sprintf("%v", tot),
  97. })
  98. }
  99. data = append(data, g.Map{
  100. "userName": "",
  101. "bio": "",
  102. "cell": "",
  103. "lims": "",
  104. "other": "",
  105. "total": fmt.Sprintf("总计:%v个", total),
  106. })
  107. return g.Map{"header": header, "data": data}, nil
  108. }
  109. // QueryBusinessTransformNum 业绩指标-转化项目项目
  110. func (s *BusinessReportService) QueryBusinessTransformNum(date string) (interface{}, error) {
  111. type BusinessNumData struct {
  112. model.ProjBusiness
  113. Total float64 `orm:"total"` // 合计
  114. }
  115. where := ""
  116. // 权限限制(销售工程师看自己的)
  117. if service.StringsContains(s.CxtUser.Roles, "SalesEngineer") {
  118. where = fmt.Sprintf(" AND proj_business.sale_id='%v'", s.CxtUser.Id)
  119. }
  120. // 获取数据 去掉储备升级而来的数据
  121. // opn_type 项目升级
  122. var businessInfos1 []*BusinessNumData
  123. var businessInfos2 []*BusinessNumData
  124. err := s.Dao.DB.Model(fmt.Sprintf("(SELECT proj_business.* FROM proj_business INNER JOIN proj_business_dynamics ON proj_business.id=proj_business_dynamics.bus_id WHERE proj_business_dynamics.created_time LIKE '%v%%' AND proj_business.nbo_type<>'50' AND proj_business.appro_status='30' AND proj_business_dynamics.opn_type='40' AND proj_business.deleted_time IS NULL AND proj_business_dynamics.deleted_time IS NULL %v) a", date, where)).LeftJoin("(SELECT * FROM proj_business_dynamics WHERE opn_content LIKE '%\"origNboType\":\"50\"%' AND opn_type='40' AND proj_business_dynamics.deleted_time IS NULL GROUP BY proj_business_dynamics.bus_id) b", "a.id=b.bus_id").Unscoped().Where("b.id IS NULL").Fields("a.*,COUNT(1) total").Group("a.sale_id, a.product_line").Order("a.sale_id ASC, a.product_line ASC").Scan(&businessInfos1)
  125. if err != nil {
  126. return nil, err
  127. }
  128. err = s.Dao.DB.Model(fmt.Sprintf("(SELECT proj_business.* FROM proj_business INNER JOIN ((SELECT ctr_contract.* FROM ctr_contract INNER JOIN (SELECT nbo_id,MIN(contract_sign_time) contract_sign_time FROM ctr_contract WHERE ctr_contract.appro_status='30' AND ctr_contract.deleted_time IS NULL GROUP BY nbo_id) t ON ctr_contract.nbo_id=t.nbo_id AND ctr_contract.contract_sign_time=t.contract_sign_time WHERE ctr_contract.appro_status='30' AND ctr_contract.deleted_time IS NULL GROUP BY ctr_contract.id) c) ON proj_business.id=c.nbo_id WHERE c.contract_sign_time LIKE '%v%%' AND proj_business.nbo_type<>'50' AND proj_business.appro_status='30' AND proj_business.deleted_time IS NULL %v GROUP BY proj_business.id) a", date, where)).LeftJoin("(SELECT * FROM proj_business_dynamics WHERE opn_content LIKE '%\"origNboType\":\"50\"%' AND opn_type='40' AND proj_business_dynamics.deleted_time IS NULL GROUP BY proj_business_dynamics.bus_id) b", "a.id=b.bus_id").Unscoped().Where("b.id IS NULL").Fields("a.*,COUNT(1) total").Group("a.sale_id, a.product_line").Order("a.sale_id ASC, a.product_line ASC").Scan(&businessInfos2)
  129. if err != nil {
  130. return nil, err
  131. }
  132. // 获取销售数据
  133. userList, err := service.GetUserListByRoleCode(s.Ctx, []string{"SalesEngineer"}, 1000)
  134. if err != nil {
  135. return nil, err
  136. }
  137. // 权限限制(销售工程师看自己的)
  138. if service.StringsContains(s.CxtUser.Roles, "SalesEngineer") {
  139. userList = []map[string]interface{}{
  140. {
  141. "Id": s.CxtUser.Id,
  142. "NickName": s.CxtUser.NickName,
  143. },
  144. }
  145. }
  146. // BIOBANK CELLSOP LIMS+基因 其他
  147. header, data := make([]g.Map, 0), make([]g.Map, 0)
  148. 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": "合计"})
  149. user2Business := make(map[int][]*BusinessNumData, 0)
  150. total := float64(0)
  151. for _, col := range businessInfos1 {
  152. if _, ok := user2Business[col.SaleId]; !ok {
  153. user2Business[col.SaleId] = make([]*BusinessNumData, 0)
  154. }
  155. user2Business[col.SaleId] = append(user2Business[col.SaleId], col)
  156. total += col.Total
  157. }
  158. for _, col := range businessInfos2 {
  159. if _, ok := user2Business[col.SaleId]; !ok {
  160. user2Business[col.SaleId] = make([]*BusinessNumData, 0)
  161. }
  162. user2Business[col.SaleId] = append(user2Business[col.SaleId], col)
  163. total += col.Total
  164. }
  165. // 统计数据
  166. for _, user := range userList {
  167. bio, cell, lims, other, tot := float64(0), float64(0), float64(0), float64(0), float64(0)
  168. if len(user2Business[user["Id"].(int)]) > 0 {
  169. for _, col := range user2Business[user["Id"].(int)] {
  170. tot += col.Total
  171. if col.ProductLine == "10" {
  172. bio += col.Total
  173. } else if col.ProductLine == "20" {
  174. cell += col.Total
  175. } else if col.ProductLine == "30" {
  176. lims += col.Total
  177. } else {
  178. other += col.Total
  179. }
  180. }
  181. }
  182. data = append(data, g.Map{
  183. "userName": user["NickName"],
  184. "bio": fmt.Sprintf("%v", bio),
  185. "cell": fmt.Sprintf("%v", cell),
  186. "lims": fmt.Sprintf("%v", lims),
  187. "other": fmt.Sprintf("%v", other),
  188. "total": fmt.Sprintf("%v", tot),
  189. })
  190. }
  191. data = append(data, g.Map{
  192. "userName": "",
  193. "bio": "",
  194. "cell": "",
  195. "lims": "",
  196. "other": "",
  197. "total": fmt.Sprintf("总计:%v个", total),
  198. })
  199. return g.Map{"header": header, "data": data}, nil
  200. }