report.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. "github.com/gogf/gf/os/gtime"
  12. )
  13. type ContractReportService struct {
  14. Dao *dao.CtrContractDao
  15. *service.ContextService
  16. }
  17. func NewContractReportService(ctx context.Context) (svc *ContractReportService, err error) {
  18. tenant, err := micro_srv.GetTenant(ctx)
  19. if err != nil {
  20. err = myerrors.TipsError(fmt.Sprintf("获取租户码异常:%s", err.Error()))
  21. return nil, err //fmt.Errorf("获取租户码异常:%s", err.Error())
  22. }
  23. svc = new(ContractReportService)
  24. if svc.ContextService, err = svc.Init(ctx); err != nil {
  25. return nil, err
  26. }
  27. svc.Dao = dao.NewCtrContractDao(tenant)
  28. return svc, nil
  29. }
  30. type CollectionNumData struct {
  31. contract.CtrContractCollection
  32. InchargeId int `orm:"incharge_id" json:"inchargeId"` // 负责人ID
  33. InchargeName string `orm:"incharge_name" json:"inchargeName"` // 负责人(销售工程师)
  34. ProductLine string `orm:"product_line" json:"productLine"` // 产品线
  35. Total float64 `orm:"total"` // 合计
  36. }
  37. // QueryCollectionNum 业绩指标-回款金额
  38. func (s *ContractReportService) QueryCollectionNum(date string) (interface{}, error) {
  39. where := ""
  40. // 权限限制(销售工程师看自己的)
  41. if service.StringsContains(s.CxtUser.Roles, "SalesEngineer") {
  42. where = fmt.Sprintf("b.incharge_id='%v'", s.CxtUser.Id)
  43. }
  44. // 获取数据
  45. var collectionInfos []*CollectionNumData
  46. 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)
  47. if err != nil {
  48. return nil, err
  49. }
  50. // 获取销售数据
  51. userList, err := service.GetUserListByRoleCode(s.Ctx, []string{"SalesEngineer"}, 1000)
  52. if err != nil {
  53. return nil, err
  54. }
  55. // 权限限制(销售工程师看自己的)
  56. if service.StringsContains(s.CxtUser.Roles, "SalesEngineer") {
  57. userList = []map[string]interface{}{
  58. {
  59. "Id": s.CxtUser.Id,
  60. "NickName": s.CxtUser.NickName,
  61. },
  62. }
  63. }
  64. // BIOBANK CELLSOP LIMS+基因 其他
  65. header, data := make([]g.Map, 0), make([]g.Map, 0)
  66. 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": "合计"})
  67. user2Collections := make(map[int][]*CollectionNumData, 0)
  68. total := float64(0)
  69. for _, col := range collectionInfos {
  70. if _, ok := user2Collections[col.InchargeId]; !ok {
  71. user2Collections[col.InchargeId] = make([]*CollectionNumData, 0)
  72. }
  73. user2Collections[col.InchargeId] = append(user2Collections[col.InchargeId], col)
  74. total += col.Total
  75. }
  76. // 统计数据
  77. for _, user := range userList {
  78. bio, cell, lims, other, tot := float64(0), float64(0), float64(0), float64(0), float64(0)
  79. if len(user2Collections[user["Id"].(int)]) > 0 {
  80. for _, col := range user2Collections[user["Id"].(int)] {
  81. tot += col.Total
  82. if col.ProductLine == "10" {
  83. bio += col.Total
  84. } else if col.ProductLine == "20" {
  85. cell += col.Total
  86. } else if col.ProductLine == "30" {
  87. lims += col.Total
  88. } else {
  89. other += col.Total
  90. }
  91. }
  92. }
  93. data = append(data, g.Map{
  94. "userName": user["NickName"],
  95. "bio": fmt.Sprintf("%.2f", bio),
  96. "cell": fmt.Sprintf("%.2f", cell),
  97. "lims": fmt.Sprintf("%.2f", lims),
  98. "other": fmt.Sprintf("%.2f", other),
  99. "total": fmt.Sprintf("%.2f", tot),
  100. })
  101. }
  102. data = append(data, g.Map{
  103. "userName": "",
  104. "bio": "",
  105. "cell": "",
  106. "lims": "",
  107. "other": "",
  108. "total": fmt.Sprintf("总计:%.2f元", total),
  109. })
  110. return g.Map{"header": header, "data": data}, nil
  111. }
  112. // QueryContractNum 业绩指标-签约合同金额
  113. func (s *ContractReportService) QueryContractNum(date string) (interface{}, error) {
  114. type ContractNumData struct {
  115. contract.CtrContract
  116. Total float64 `orm:"total"` // 合计
  117. }
  118. where := ""
  119. // 权限限制(销售工程师看自己的)
  120. if service.StringsContains(s.CxtUser.Roles, "SalesEngineer") {
  121. where = fmt.Sprintf("incharge_id='%v'", s.CxtUser.Id)
  122. }
  123. // 获取数据
  124. var contractInfos []*ContractNumData
  125. 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)
  126. if err != nil {
  127. return nil, err
  128. }
  129. // 获取销售数据
  130. userList, err := service.GetUserListByRoleCode(s.Ctx, []string{"SalesEngineer"}, 1000)
  131. if err != nil {
  132. return nil, err
  133. }
  134. // 权限限制(销售工程师看自己的)
  135. if service.StringsContains(s.CxtUser.Roles, "SalesEngineer") {
  136. userList = []map[string]interface{}{
  137. {
  138. "Id": s.CxtUser.Id,
  139. "NickName": s.CxtUser.NickName,
  140. },
  141. }
  142. }
  143. // BIOBANK CELLSOP LIMS+基因 其他
  144. header, data := make([]g.Map, 0), make([]g.Map, 0)
  145. 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": "合计"})
  146. user2Contracts := make(map[int][]*ContractNumData, 0)
  147. total := float64(0)
  148. for _, col := range contractInfos {
  149. if _, ok := user2Contracts[col.InchargeId]; !ok {
  150. user2Contracts[col.InchargeId] = make([]*ContractNumData, 0)
  151. }
  152. user2Contracts[col.InchargeId] = append(user2Contracts[col.InchargeId], col)
  153. total += col.Total
  154. }
  155. // 统计数据
  156. for _, user := range userList {
  157. bio, cell, lims, other, tot := float64(0), float64(0), float64(0), float64(0), float64(0)
  158. if len(user2Contracts[user["Id"].(int)]) > 0 {
  159. for _, col := range user2Contracts[user["Id"].(int)] {
  160. tot += col.Total
  161. if col.ProductLine == "10" {
  162. bio += col.Total
  163. } else if col.ProductLine == "20" {
  164. cell += col.Total
  165. } else if col.ProductLine == "30" {
  166. lims += col.Total
  167. } else {
  168. other += col.Total
  169. }
  170. }
  171. }
  172. data = append(data, g.Map{
  173. "userName": user["NickName"],
  174. "bio": fmt.Sprintf("%.2f", bio),
  175. "cell": fmt.Sprintf("%.2f", cell),
  176. "lims": fmt.Sprintf("%.2f", lims),
  177. "other": fmt.Sprintf("%.2f", other),
  178. "total": fmt.Sprintf("%.2f", tot),
  179. })
  180. }
  181. data = append(data, g.Map{
  182. "userName": "",
  183. "bio": "",
  184. "cell": "",
  185. "lims": "",
  186. "other": "",
  187. "total": fmt.Sprintf("总计:%.2f元", total),
  188. })
  189. return g.Map{"header": header, "data": data}, nil
  190. }
  191. // QueryContractExpireNum 售后运维到期统计表
  192. func (s *ContractReportService) QueryContractExpireNum(date string) (interface{}, error) {
  193. if date != "" {
  194. date += "-01 00:00:00"
  195. date = gtime.NewFromStr(date).AddDate(1, 0, 0).Format("Y-m")
  196. }
  197. where := ""
  198. // 权限限制(销售工程师看自己的)
  199. if service.StringsContains(s.CxtUser.Roles, "SalesEngineer") {
  200. where = fmt.Sprintf("incharge_id='%v'", s.CxtUser.Id)
  201. }
  202. // 获取数据
  203. var contractInfos []*contract.CtrContract
  204. err := s.Dao.Where(fmt.Sprintf("contract_end_time LIKE '%v%%' AND appro_status='30'", date)).Where(where).Order("incharge_id ASC, contract_end_time ASC").Scan(&contractInfos)
  205. if err != nil {
  206. return nil, err
  207. }
  208. header, data := make([]g.Map, 0), make([]g.Map, 0)
  209. header = append(header, g.Map{"prop": "userName", "label": "销售工程师"}, g.Map{"prop": "code", "label": "合同编号"}, g.Map{"prop": "endTime", "label": "合同结束时间"}, g.Map{"prop": "cusName", "label": "客户名称"}, g.Map{"prop": "amount", "label": "合同总金额"})
  210. total := 0
  211. for _, info := range contractInfos {
  212. data = append(data, g.Map{
  213. "userName": info.InchargeName,
  214. "code": info.ContractCode,
  215. "endTime": info.ContractEndTime.Format("Y-m-d"),
  216. "cusName": info.CustName,
  217. "amount": fmt.Sprintf("总计:%.2f元", info.ContractAmount),
  218. })
  219. total += 1
  220. }
  221. data = append(data, g.Map{
  222. "userName": "",
  223. "code": "",
  224. "endTime": "",
  225. "cusName": "",
  226. "amount": fmt.Sprintf("总计:%v", total),
  227. })
  228. return g.Map{"header": header, "data": data}, nil
  229. }