report.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package home
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "dashoo.cn/micro/app/model/home"
  7. "dashoo.cn/micro/app/service"
  8. contractService "dashoo.cn/micro/app/service/contract"
  9. "dashoo.cn/opms_libary/micro_srv"
  10. "github.com/gogf/gf/database/gdb"
  11. "github.com/gogf/gf/os/gtime"
  12. "github.com/gogf/gf/util/gconv"
  13. )
  14. // getPersonalContractReportData 获取个人数据
  15. // dataType:collection为回款;其他为合同
  16. func getPersonalContractReportData(ctx context.Context, dataType string, params *map[string]interface{}) (*home.ReportData, error) {
  17. var reportData home.ReportData
  18. targetMap := make(map[int]gdb.Record, 0)
  19. realMap := make(map[int][]*gdb.Record, 0)
  20. targetField := "sales_target"
  21. realField := "contract_amount"
  22. year := gtime.Now().Format("Y")
  23. if params != nil && (*params)["year"] != nil {
  24. year = gconv.String((*params)["year"])
  25. }
  26. // 统计字段
  27. if dataType == "COLLECTION" {
  28. targetField = "collection_target"
  29. realField = "collection_amount"
  30. }
  31. srv, err := contractService.NewCtrContractService(ctx)
  32. if err != nil {
  33. return nil, err
  34. }
  35. // 获取用户信息
  36. userInfo, err := micro_srv.GetUserInfo(ctx)
  37. if err != nil {
  38. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  39. }
  40. targets, err := srv.Dao.DB.Model("rpt_sales_target").Where("sale_user_id", userInfo.Id).Order("monthly ASC").FindAll()
  41. if err != nil && err != sql.ErrNoRows {
  42. return nil, err
  43. }
  44. for index, target := range targets {
  45. targetMap[target["monthly"].Int()] = targets[index]
  46. }
  47. // 统计12个月份的数据
  48. // 统计目标值
  49. for index := 1; index <= 12; index++ {
  50. reportData.XData = append(reportData.XData, fmt.Sprintf("%v月", index))
  51. if target, ok := targetMap[index]; ok {
  52. reportData.YDataTarget = append(reportData.YDataTarget, target[targetField].Float64())
  53. } else {
  54. reportData.YDataTarget = append(reportData.YDataTarget, 0)
  55. }
  56. }
  57. // 统计合同值
  58. contractModel := srv.Dao.DB.Model("ctr_contract").Where("ctr_contract.incharge_id", userInfo.Id)
  59. if dataType != "COLLECTION" {
  60. contracts, err := contractModel.Where("ctr_contract.contract_start_time LIKE ?", year+"-%").Order("ctr_contract.contract_start_time ASC").FindAll()
  61. if err != nil && err != sql.ErrNoRows {
  62. return nil, err
  63. }
  64. for index, contract := range contracts {
  65. realMap[contract["contract_start_time"].GTime().Month()] = append(realMap[contract["contract_start_time"].GTime().Month()], &contracts[index])
  66. }
  67. } else {
  68. // 回款数据统计
  69. 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()
  70. if err != nil && err != sql.ErrNoRows {
  71. return nil, err
  72. }
  73. for index, collection := range collections {
  74. realMap[collection["collection_datetime"].GTime().Month()] = append(realMap[collection["collection_datetime"].GTime().Month()], &collections[index])
  75. }
  76. }
  77. // 统计12个月份的数据
  78. // 统计实际值
  79. for index := 1; index <= 12; index++ {
  80. if realData, ok := realMap[index]; ok {
  81. realAmount := float64(0)
  82. for _, data := range realData {
  83. realAmount += (*data)[realField].Float64()
  84. }
  85. reportData.YDataReal = append(reportData.YDataReal, realAmount)
  86. } else {
  87. reportData.YDataReal = append(reportData.YDataReal, 0)
  88. }
  89. if reportData.YDataTarget[index-1] == 0 {
  90. reportData.PercentData = append(reportData.PercentData, 0)
  91. } else {
  92. reportData.PercentData = append(reportData.PercentData, reportData.YDataReal[index-1]*100/reportData.YDataTarget[index-1])
  93. }
  94. }
  95. return &reportData, nil
  96. }
  97. // getCompanyContractReportData 获取总部数据
  98. // dataType:collection为回款;其他为合同
  99. func getCompanyContractReportData(ctx context.Context, dataType string, params *map[string]interface{}) (*home.ReportData, error) {
  100. var reportData home.ReportData
  101. realMap := make(map[int]gdb.Record, 0)
  102. targetField := "sales_target"
  103. realField := "contract_amount"
  104. dateWhere1 := "" // 查询条件
  105. dateWhere2 := "" // 查询条件
  106. dateWhere3 := ""
  107. date := gtime.Now()
  108. if params != nil && (*params)["date"] != nil {
  109. date = gconv.GTime((*params)["date"])
  110. }
  111. dateWhere1 = fmt.Sprintf("ctr_contract.contract_start_time LIKE '%v'", date.Format("Y")+"-%")
  112. dateWhere2 = fmt.Sprintf("ctr_contract_collection.collection_datetime LIKE '%v'", date.Format("Y")+"-%")
  113. if params != nil && (*params)["searchType"] != nil {
  114. searchType := gconv.String((*params)["searchType"])
  115. if searchType == "month" {
  116. dateWhere1 = fmt.Sprintf("ctr_contract.contract_start_time LIKE '%v'", date.Format("Y-m")+"-%")
  117. dateWhere2 = fmt.Sprintf("ctr_contract_collection.collection_datetime LIKE '%v'", date.Format("Y-m")+"-%")
  118. dateWhere3 = fmt.Sprintf("monthly=%v", date.Month())
  119. } else if searchType == "quarter" {
  120. dateWhere1 = getQuarterWhere(date, "ctr_contract.contract_start_time")
  121. dateWhere2 = getQuarterWhere(date, "ctr_contract_collection.collection_datetime")
  122. dateWhere3 = getQuarterMonthWhere(date, "monthly")
  123. }
  124. }
  125. // 统计字段
  126. if dataType == "COLLECTION" {
  127. targetField = "collection_target"
  128. realField = "collection_amount"
  129. }
  130. srv, err := contractService.NewCtrContractService(ctx)
  131. if err != nil {
  132. return nil, err
  133. }
  134. 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()
  135. if err != nil && err != sql.ErrNoRows {
  136. return nil, err
  137. }
  138. // 统计合同值
  139. contractModel := srv.Dao.DB.Model("ctr_contract").Group("ctr_contract.incharge_id").Order("ctr_contract.incharge_id ASC")
  140. if dataType != "COLLECTION" {
  141. contracts, err := contractModel.Where(dateWhere1).Fields("ctr_contract.incharge_id, ctr_contract.incharge_name, SUM(ctr_contract.contract_amount) contract_amount").FindAll()
  142. if err != nil && err != sql.ErrNoRows {
  143. return nil, err
  144. }
  145. for index, contract := range contracts {
  146. realMap[contract["incharge_id"].Int()] = contracts[index]
  147. }
  148. } else {
  149. // 回款数据统计
  150. 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()
  151. if err != nil && err != sql.ErrNoRows {
  152. return nil, err
  153. }
  154. for index, collection := range collections {
  155. realMap[collection["incharge_id"].Int()] = collections[index]
  156. }
  157. }
  158. // 统计目标值和实际值
  159. for index, target := range targets {
  160. reportData.XData = append(reportData.XData, target["sale_user_name"].String())
  161. reportData.YDataTarget = append(reportData.YDataTarget, target[targetField].Float64())
  162. if realData, ok := realMap[target["sale_user_id"].Int()]; ok {
  163. reportData.YDataReal = append(reportData.YDataReal, realData[realField].Float64())
  164. } else {
  165. reportData.YDataReal = append(reportData.YDataReal, 0)
  166. }
  167. if reportData.YDataTarget[index] == 0 {
  168. reportData.PercentData = append(reportData.PercentData, 0)
  169. } else {
  170. reportData.PercentData = append(reportData.PercentData, reportData.YDataReal[index]*100/reportData.YDataTarget[index])
  171. }
  172. }
  173. return &reportData, nil
  174. }
  175. func getQuarterGoalReportData(ctx context.Context, dataType string, params *map[string]interface{}) (*home.ReportData, error) {
  176. if params == nil {
  177. return nil, fmt.Errorf("请输入年度")
  178. }
  179. p := *params
  180. year := gconv.Int(p["year"])
  181. quarter := gconv.Int(p["quarter"])
  182. if year == 0 {
  183. return nil, fmt.Errorf("请输入年度")
  184. }
  185. srv, err := contractService.NewCtrContractService(ctx)
  186. if err != nil {
  187. return nil, err
  188. }
  189. goal := map[string]float64{}
  190. goaldao := srv.GoalDao.Where("year = ?", year).Where("goal_type = ?", dataType)
  191. if quarter != 0 {
  192. goaldao = goaldao.Where("quarter = ?", quarter)
  193. } else {
  194. goaldao = goaldao.Where("quarter = 1 || quarter = 2 || quarter = 3 || quarter = 4")
  195. }
  196. goalent, err := goaldao.All()
  197. if err != nil {
  198. return nil, err
  199. }
  200. for _, ent := range goalent {
  201. goal[ent.ProductLine] += ent.Amount
  202. }
  203. got := map[string]float64{}
  204. ctrdao := srv.Dao.Where("year(created_time) = ?", year)
  205. if quarter != 0 {
  206. if quarter == 1 {
  207. ctrdao = ctrdao.Where("month(created_time) in (1,2,3)")
  208. }
  209. if quarter == 2 {
  210. ctrdao = ctrdao.Where("month(created_time) in (4,5,6)")
  211. }
  212. if quarter == 3 {
  213. ctrdao = ctrdao.Where("month(created_time) in (7,8,9)")
  214. }
  215. if quarter == 4 {
  216. ctrdao = ctrdao.Where("month(created_time) in (10,11,12)")
  217. }
  218. }
  219. ctrent, err := ctrdao.All()
  220. if err != nil {
  221. return nil, err
  222. }
  223. for _, ent := range ctrent {
  224. if dataType == "10" {
  225. got[ent.ProductLine] += ent.ContractAmount
  226. }
  227. if dataType == "20" {
  228. got[ent.ProductLine] += ent.CollectedAmount
  229. }
  230. }
  231. productLine, err := service.GetDictDataByType(ctx, "sys_product_line")
  232. if err != nil {
  233. return nil, err
  234. }
  235. productLineCode := []string{}
  236. productLineName := []string{}
  237. for k, v := range productLine {
  238. productLineCode = append(productLineCode, k)
  239. productLineName = append(productLineName, v)
  240. }
  241. var reportData home.ReportData
  242. for _, c := range productLineCode {
  243. reportData.XData = append(reportData.XData, productLine[c])
  244. reportData.YDataTarget = append(reportData.YDataTarget, goal[c])
  245. reportData.YDataReal = append(reportData.YDataReal, got[c]/10000)
  246. }
  247. return &reportData, nil
  248. }
  249. func getQuarterWhere(date *gtime.Time, field string) string {
  250. if date.Month() >= 1 && date.Month() <= 3 {
  251. 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")
  252. } else if date.Month() >= 4 && date.Month() <= 6 {
  253. 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")
  254. } else if date.Month() >= 7 && date.Month() <= 9 {
  255. 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")
  256. } else if date.Month() >= 10 && date.Month() <= 12 {
  257. 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")
  258. }
  259. return ""
  260. }
  261. func getQuarterMonthWhere(date *gtime.Time, field string) string {
  262. if date.Month() >= 1 && date.Month() <= 3 {
  263. return fmt.Sprintf("%v >= %v AND %v < %v", field, 1, field, 4)
  264. } else if date.Month() >= 4 && date.Month() <= 6 {
  265. return fmt.Sprintf("%v >= %v AND %v < %v", field, 4, field, 7)
  266. } else if date.Month() >= 7 && date.Month() <= 9 {
  267. return fmt.Sprintf("%v >= %v AND %v < %v", field, 7, field, 10)
  268. } else if date.Month() >= 10 && date.Month() <= 12 {
  269. return fmt.Sprintf("%v >= %v AND %v <= %v", field, 10, field, 12)
  270. }
  271. return ""
  272. }