report.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. where := ""
  39. // 权限限制(销售工程师看自己的)
  40. if service.StringsContains(s.CxtUser.Roles, "SalesEngineer") {
  41. where = fmt.Sprintf("b.incharge_id='%v'", s.CxtUser.Id)
  42. }
  43. // 获取数据
  44. var collectionInfos []*CollectionNumData
  45. 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)).Where(where).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)
  46. if err != nil {
  47. return nil, err
  48. }
  49. // 获取销售数据
  50. userList, err := service.GetUserListByRoleCode(s.Ctx, []string{"SalesEngineer"}, 1000)
  51. if err != nil {
  52. return nil, err
  53. }
  54. // 权限限制(销售工程师看自己的)
  55. if service.StringsContains(s.CxtUser.Roles, "SalesEngineer") {
  56. userList = []map[string]interface{}{
  57. {
  58. "Id": s.CxtUser.Id,
  59. "NickName": s.CxtUser.NickName,
  60. },
  61. }
  62. }
  63. // BIOBANK CELLSOP LIMS+基因 其他
  64. header, data := make([]g.Map, 0), make([]g.Map, 0)
  65. 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": "合计"})
  66. user2Collections := make(map[int][]*CollectionNumData, 0)
  67. total := float64(0)
  68. for _, col := range collectionInfos {
  69. if _, ok := user2Collections[col.InchargeId]; !ok {
  70. user2Collections[col.InchargeId] = make([]*CollectionNumData, 0)
  71. }
  72. user2Collections[col.InchargeId] = append(user2Collections[col.InchargeId], col)
  73. total += col.Total
  74. }
  75. // 统计数据
  76. for _, user := range userList {
  77. bio, cell, lims, other, tot := float64(0), float64(0), float64(0), float64(0), float64(0)
  78. if len(user2Collections[user["Id"].(int)]) > 0 {
  79. for _, col := range user2Collections[user["Id"].(int)] {
  80. tot += col.Total
  81. if col.ProductLine == "10" {
  82. bio += col.Total
  83. } else if col.ProductLine == "20" {
  84. cell += col.Total
  85. } else if col.ProductLine == "30" {
  86. lims += col.Total
  87. } else {
  88. other += col.Total
  89. }
  90. }
  91. }
  92. data = append(data, g.Map{
  93. "userName": user["NickName"],
  94. "bio": fmt.Sprintf("%.2f", bio),
  95. "cell": fmt.Sprintf("%.2f", cell),
  96. "lims": fmt.Sprintf("%.2f", lims),
  97. "other": fmt.Sprintf("%.2f", other),
  98. "total": fmt.Sprintf("%.2f", tot),
  99. })
  100. }
  101. data = append(data, g.Map{
  102. "userName": "",
  103. "bio": "",
  104. "cell": "",
  105. "lims": "",
  106. "other": "",
  107. "total": fmt.Sprintf("总计:%.2f元", total),
  108. })
  109. return g.Map{"header": header, "data": data}, nil
  110. }
  111. // QueryContractNum 业绩指标-签约合同金额
  112. func (s *ContractReportService) QueryContractNum(date string) (interface{}, error) {
  113. type ContractNumData struct {
  114. contract.CtrContract
  115. Total float64 `orm:"total"` // 合计
  116. }
  117. where := ""
  118. // 权限限制(销售工程师看自己的)
  119. if service.StringsContains(s.CxtUser.Roles, "SalesEngineer") {
  120. where = fmt.Sprintf("incharge_id='%v'", s.CxtUser.Id)
  121. }
  122. // 获取数据
  123. var contractInfos []*ContractNumData
  124. err := s.Dao.Where(fmt.Sprintf("contract_sign_time LIKE '%v%%' AND appro_status='30'", date)).Where(where).Fields("ctr_contract.*,SUM(contract_amount) total").Group("incharge_id, product_line").Order("incharge_id ASC, product_line ASC").Scan(&contractInfos)
  125. if err != nil {
  126. return nil, err
  127. }
  128. // 获取销售数据
  129. userList, err := service.GetUserListByRoleCode(s.Ctx, []string{"SalesEngineer"}, 1000)
  130. if err != nil {
  131. return nil, err
  132. }
  133. // 权限限制(销售工程师看自己的)
  134. if service.StringsContains(s.CxtUser.Roles, "SalesEngineer") {
  135. userList = []map[string]interface{}{
  136. {
  137. "Id": s.CxtUser.Id,
  138. "NickName": s.CxtUser.NickName,
  139. },
  140. }
  141. }
  142. // BIOBANK CELLSOP LIMS+基因 其他
  143. header, data := make([]g.Map, 0), make([]g.Map, 0)
  144. 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": "合计"})
  145. user2Contracts := make(map[int][]*ContractNumData, 0)
  146. total := float64(0)
  147. for _, col := range contractInfos {
  148. if _, ok := user2Contracts[col.InchargeId]; !ok {
  149. user2Contracts[col.InchargeId] = make([]*ContractNumData, 0)
  150. }
  151. user2Contracts[col.InchargeId] = append(user2Contracts[col.InchargeId], col)
  152. total += col.Total
  153. }
  154. // 统计数据
  155. for _, user := range userList {
  156. bio, cell, lims, other, tot := float64(0), float64(0), float64(0), float64(0), float64(0)
  157. if len(user2Contracts[user["Id"].(int)]) > 0 {
  158. for _, col := range user2Contracts[user["Id"].(int)] {
  159. tot += col.Total
  160. if col.ProductLine == "10" {
  161. bio += col.Total
  162. } else if col.ProductLine == "20" {
  163. cell += col.Total
  164. } else if col.ProductLine == "30" {
  165. lims += col.Total
  166. } else {
  167. other += col.Total
  168. }
  169. }
  170. }
  171. data = append(data, g.Map{
  172. "userName": user["NickName"],
  173. "bio": fmt.Sprintf("%.2f", bio),
  174. "cell": fmt.Sprintf("%.2f", cell),
  175. "lims": fmt.Sprintf("%.2f", lims),
  176. "other": fmt.Sprintf("%.2f", other),
  177. "total": fmt.Sprintf("%.2f", tot),
  178. })
  179. }
  180. data = append(data, g.Map{
  181. "userName": "",
  182. "bio": "",
  183. "cell": "",
  184. "lims": "",
  185. "other": "",
  186. "total": fmt.Sprintf("总计:%.2f元", total),
  187. })
  188. return g.Map{"header": header, "data": data}, nil
  189. }