report.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package home
  2. import (
  3. "context"
  4. "dashoo.cn/micro/app/model/home"
  5. contractService "dashoo.cn/micro/app/service/contract"
  6. "dashoo.cn/opms_libary/micro_srv"
  7. "database/sql"
  8. "fmt"
  9. "github.com/gogf/gf/database/gdb"
  10. "github.com/gogf/gf/os/gtime"
  11. "github.com/gogf/gf/util/gconv"
  12. )
  13. // getPersonalContractReportData 获取个人数据
  14. // dataType:collection为回款;其他为合同
  15. func getPersonalContractReportData(ctx context.Context, dataType string, params *map[string]interface{}) (*home.ReportData, error) {
  16. var reportData home.ReportData
  17. targetMap := make(map[int]gdb.Record, 0)
  18. realMap := make(map[int][]*gdb.Record, 0)
  19. targetField := "sales_target"
  20. realField := "contract_amount"
  21. year := gtime.Now().Format("Y")
  22. if params != nil && (*params)["year"] != nil {
  23. year = gconv.String((*params)["year"])
  24. }
  25. // 统计字段
  26. if dataType == "COLLECTION" {
  27. targetField = "collection_target"
  28. realField = "collection_amount"
  29. }
  30. srv, err := contractService.NewCtrContractService(ctx)
  31. if err != nil {
  32. return nil, err
  33. }
  34. // 获取用户信息
  35. userInfo, err := micro_srv.GetUserInfo(ctx)
  36. if err != nil {
  37. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  38. }
  39. targets, err := srv.Dao.DB.Model("rpt_sales_target").Where("sale_user_id", userInfo.Id).Order("monthly ASC").FindAll()
  40. if err != nil && err != sql.ErrNoRows {
  41. return nil, err
  42. }
  43. for index, target := range targets {
  44. targetMap[target["monthly"].Int()] = targets[index]
  45. }
  46. // 统计12个月份的数据
  47. // 统计目标值
  48. for index := 1; index <= 12; index++ {
  49. reportData.XData = append(reportData.XData, fmt.Sprintf("%v月", index))
  50. if target, ok := targetMap[index]; ok {
  51. reportData.YDataTarget = append(reportData.YDataTarget, target[targetField].Float64())
  52. } else {
  53. reportData.YDataTarget = append(reportData.YDataTarget, 0)
  54. }
  55. }
  56. // 统计合同值
  57. contractModel := srv.Dao.DB.Model("ctr_contract").Where("ctr_contract.incharge_id", userInfo.Id)
  58. if dataType != "COLLECTION" {
  59. contracts, err := contractModel.Where("ctr_contract.contract_start_time LIKE ?", year+"-%").Order("ctr_contract.contract_start_time ASC").FindAll()
  60. if err != nil && err != sql.ErrNoRows {
  61. return nil, err
  62. }
  63. for index, contract := range contracts {
  64. realMap[contract["contract_start_time"].GTime().Month()] = append(realMap[contract["contract_start_time"].GTime().Month()], &contracts[index])
  65. }
  66. } else {
  67. // 回款数据统计
  68. collections, err := contractModel.InnerJoin("ctr_contract_collection", "ctr_contract.id=ctr_contract_collection.contract_id").Where("ctr_contract_collection.appro_status", "20").Where("ctr_contract_collection.collection_datetime LIKE ?", year+"-%").Order("ctr_contract_collection.collection_datetime ASC").Fields("ctr_contract_collection.*").FindAll()
  69. if err != nil && err != sql.ErrNoRows {
  70. return nil, err
  71. }
  72. for index, collection := range collections {
  73. realMap[collection["collection_datetime"].GTime().Month()] = append(realMap[collection["collection_datetime"].GTime().Month()], &collections[index])
  74. }
  75. }
  76. // 统计12个月份的数据
  77. // 统计实际值
  78. for index := 1; index <= 12; index++ {
  79. if realData, ok := realMap[index]; ok {
  80. realAmount := float64(0)
  81. for _, data := range realData {
  82. realAmount += (*data)[realField].Float64()
  83. }
  84. reportData.YDataReal = append(reportData.YDataReal, realAmount)
  85. } else {
  86. reportData.YDataReal = append(reportData.YDataReal, 0)
  87. }
  88. if reportData.YDataTarget[index-1] == 0 {
  89. reportData.PercentData = append(reportData.PercentData, 0)
  90. } else {
  91. reportData.PercentData = append(reportData.PercentData, reportData.YDataReal[index-1]*100/reportData.YDataTarget[index-1])
  92. }
  93. }
  94. return &reportData, nil
  95. }
  96. // getCompanyContractReportData 获取总部数据
  97. // dataType:collection为回款;其他为合同
  98. func getCompanyContractReportData(ctx context.Context, dataType string, params *map[string]interface{}) (*home.ReportData, error) {
  99. var reportData home.ReportData
  100. realMap := make(map[int]gdb.Record, 0)
  101. targetField := "sales_target"
  102. realField := "contract_amount"
  103. dateWhere1 := "" // 查询条件
  104. dateWhere2 := "" // 查询条件
  105. dateWhere3 := ""
  106. date := gtime.Now()
  107. if params != nil && (*params)["date"] != nil {
  108. date = gconv.GTime((*params)["date"])
  109. }
  110. dateWhere1 = fmt.Sprintf("ctr_contract.contract_start_time LIKE '%v'", date.Format("Y")+"-%")
  111. dateWhere2 = fmt.Sprintf("ctr_contract_collection.collection_datetime LIKE '%v'", date.Format("Y")+"-%")
  112. if params != nil && (*params)["searchType"] != nil {
  113. searchType := gconv.String((*params)["searchType"])
  114. if searchType == "month" {
  115. dateWhere1 = fmt.Sprintf("ctr_contract.contract_start_time LIKE '%v'", date.Format("Y-m")+"-%")
  116. dateWhere2 = fmt.Sprintf("ctr_contract_collection.collection_datetime LIKE '%v'", date.Format("Y-m")+"-%")
  117. dateWhere3 = fmt.Sprintf("monthly=%v", date.Month())
  118. } else if searchType == "quarter" {
  119. dateWhere1 = getQuarterWhere(date, "ctr_contract.contract_start_time")
  120. dateWhere2 = getQuarterWhere(date, "ctr_contract_collection.collection_datetime")
  121. dateWhere3 = getQuarterMonthWhere(date, "monthly")
  122. }
  123. }
  124. // 统计字段
  125. if dataType == "COLLECTION" {
  126. targetField = "collection_target"
  127. realField = "collection_amount"
  128. }
  129. srv, err := contractService.NewCtrContractService(ctx)
  130. if err != nil {
  131. return nil, err
  132. }
  133. targets, err := srv.Dao.DB.Model("rpt_sales_target").Where(dateWhere3).Order("sale_user_id ASC").Group("sale_user_id").Fields(fmt.Sprintf("sale_user_id, sale_user_name, SUM(%v) %v", targetField, targetField)).FindAll()
  134. if err != nil && err != sql.ErrNoRows {
  135. return nil, err
  136. }
  137. // 统计合同值
  138. contractModel := srv.Dao.DB.Model("ctr_contract").Group("ctr_contract.incharge_id").Order("ctr_contract.incharge_id ASC")
  139. if dataType != "COLLECTION" {
  140. contracts, err := contractModel.Where(dateWhere1).Fields("ctr_contract.incharge_id, ctr_contract.incharge_name, SUM(ctr_contract.contract_amount) contract_amount").FindAll()
  141. if err != nil && err != sql.ErrNoRows {
  142. return nil, err
  143. }
  144. for index, contract := range contracts {
  145. realMap[contract["incharge_id"].Int()] = contracts[index]
  146. }
  147. } else {
  148. // 回款数据统计
  149. collections, err := contractModel.InnerJoin("ctr_contract_collection", "ctr_contract.id=ctr_contract_collection.contract_id").Where("ctr_contract_collection.appro_status", "20").Where(dateWhere2).Fields("ctr_contract.incharge_id, ctr_contract.incharge_name, SUM(ctr_contract_collection.collection_amount) collection_amount").FindAll()
  150. if err != nil && err != sql.ErrNoRows {
  151. return nil, err
  152. }
  153. for index, collection := range collections {
  154. realMap[collection["incharge_id"].Int()] = collections[index]
  155. }
  156. }
  157. // 统计目标值和实际值
  158. for index, target := range targets {
  159. reportData.XData = append(reportData.XData, target["sale_user_name"].String())
  160. reportData.YDataTarget = append(reportData.YDataTarget, target[targetField].Float64())
  161. if realData, ok := realMap[target["sale_user_id"].Int()]; ok {
  162. reportData.YDataReal = append(reportData.YDataReal, realData[realField].Float64())
  163. } else {
  164. reportData.YDataReal = append(reportData.YDataReal, 0)
  165. }
  166. if reportData.YDataTarget[index] == 0 {
  167. reportData.PercentData = append(reportData.PercentData, 0)
  168. } else {
  169. reportData.PercentData = append(reportData.PercentData, reportData.YDataReal[index]*100/reportData.YDataTarget[index])
  170. }
  171. }
  172. return &reportData, nil
  173. }
  174. func getQuarterWhere(date *gtime.Time, field string) string {
  175. if date.Month() >= 1 && date.Month() <= 3 {
  176. return fmt.Sprintf("%v >= '%v' AND %v < '%v'", field, date.Format("Y-")+"01-01 00:00:00", field, date.Format("Y-")+"04-01 00:00:00")
  177. } else if date.Month() >= 4 && date.Month() <= 6 {
  178. return fmt.Sprintf("%v >= '%v' AND %v < '%v'", field, date.Format("Y-")+"04-01 00:00:00", field, date.Format("Y-")+"07-01 00:00:00")
  179. } else if date.Month() >= 7 && date.Month() <= 9 {
  180. return fmt.Sprintf("%v >= '%v' AND %v < '%v'", field, date.Format("Y-")+"07-01 00:00:00", field, date.Format("Y-")+"10-01 00:00:00")
  181. } else if date.Month() >= 10 && date.Month() <= 12 {
  182. return fmt.Sprintf("%v >= '%v' AND %v <= '%v'", field, date.Format("Y-")+"10-01 00:00:00", field, date.Format("Y-")+"12-31 23:59:59")
  183. }
  184. return ""
  185. }
  186. func getQuarterMonthWhere(date *gtime.Time, field string) string {
  187. if date.Month() >= 1 && date.Month() <= 3 {
  188. return fmt.Sprintf("%v >= %v AND %v < %v", field, 1, field, 4)
  189. } else if date.Month() >= 4 && date.Month() <= 6 {
  190. return fmt.Sprintf("%v >= %v AND %v < %v", field, 4, field, 7)
  191. } else if date.Month() >= 7 && date.Month() <= 9 {
  192. return fmt.Sprintf("%v >= %v AND %v < %v", field, 7, field, 10)
  193. } else if date.Month() >= 10 && date.Month() <= 12 {
  194. return fmt.Sprintf("%v >= %v AND %v <= %v", field, 10, field, 12)
  195. }
  196. return ""
  197. }