rai_data_summary.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package statistic
  2. import (
  3. "fmt"
  4. "hongze/hz_crm_api/controllers"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/company"
  7. "hongze/hz_crm_api/models/statistic_report"
  8. "hongze/hz_crm_api/models/system"
  9. "hongze/hz_crm_api/utils"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // 权益数据汇总
  15. // StatisticRaiDataSummaryController 权益数据汇总基类
  16. type StatisticRaiDataSummaryController struct {
  17. controllers.BaseAuthController
  18. }
  19. // MergeCompanyList
  20. // @Title 权益数据汇总
  21. // @Description 权益数据汇总统计列表接口
  22. // @Param AdminId query string true "开拓组销售id,多个用英文逗号隔开,空字符串为全部"
  23. // @Param ServiceAdminId query string true "服务组销售id,多个用英文逗号隔开,空字符串为全部"
  24. // @Param StartYear query int false "开始日期(年份)"
  25. // @Param EndYear query int false "结束日期(年份)"
  26. // @Param DataType query string false "报表类型,枚举值:`季度`,`年度`,`半年度`"
  27. // @Param DevelopButton query int false "开拓组开关,枚举值:1:开启,0:关闭 ,默认关闭"
  28. // @Param ServerButton query int false "服务组开关,枚举值:1:开启,0:关闭 ,默认关闭"
  29. // @Param ContractButtonType query string false "开关类型,:`新签`,`续约`,`收入` 多个用英文逗号隔开, "
  30. // @Param IsExport query bool false "是否导出excel,默认是false"
  31. // @Success 200 {object} statistic_report.RaiDataSummaryListResp
  32. // @router /rai_data_summary/list [get]
  33. func (this *StatisticRaiDataSummaryController) RaiDataSummaryList() {
  34. br := new(models.BaseResponse).Init()
  35. defer func() {
  36. this.Data["json"] = br
  37. this.ServeJSON()
  38. }()
  39. sysUser := this.SysUser
  40. if sysUser == nil {
  41. br.Msg = "请登录"
  42. br.ErrMsg = "请登录,SysUser Is Empty"
  43. br.Ret = 408
  44. return
  45. }
  46. //adminId := this.GetString("AdminId")
  47. dataType := this.GetString("DataType")
  48. adminId := this.GetString("AdminId")
  49. serviceAdminId := this.GetString("ServiceAdminId")
  50. startYear, _ := this.GetInt("StartYear")
  51. endYear, _ := this.GetInt("EndYear")
  52. developButton, _ := this.GetInt("DevelopButton")
  53. serverButton, _ := this.GetInt("ServerButton")
  54. dataTypeArr := []string{}
  55. var dateIntervalArr []statistic_report.DateInterval //时间区间
  56. if dataType == "季度" {
  57. dataTypeArr = []string{"Q1", "Q2", "Q3", "Q4"}
  58. dateIntervalArr = []statistic_report.DateInterval{statistic_report.DateInterval{StartDate: "01-01", EndDate: "03-31"}, statistic_report.DateInterval{StartDate: "04-01", EndDate: "06-30"}, statistic_report.DateInterval{StartDate: "07-01", EndDate: "09-30"}, statistic_report.DateInterval{StartDate: "10-01", EndDate: "12-31"}}
  59. } else if dataType == "半年度" {
  60. dataTypeArr = []string{"H1", "H2"}
  61. dateIntervalArr = []statistic_report.DateInterval{statistic_report.DateInterval{StartDate: "01-01", EndDate: "06-30"}, statistic_report.DateInterval{StartDate: "07-01", EndDate: "12-31"}}
  62. } else {
  63. dataTypeArr = []string{""}
  64. dateIntervalArr = []statistic_report.DateInterval{statistic_report.DateInterval{StartDate: "01-01", EndDate: "12-31"}}
  65. }
  66. for _, v := range dateIntervalArr {
  67. fmt.Println(v)
  68. }
  69. condition := " AND role_type_code IN ('rai_seller','rai_group') AND enabled = 1 AND rai_enabled = 1"
  70. var pars []interface{}
  71. if adminId != "" {
  72. serviceAdminId = ""
  73. condition += " AND admin_id IN (" + adminId + ") "
  74. }
  75. if serviceAdminId != "" {
  76. condition += " AND admin_id IN (" + serviceAdminId + ") "
  77. }
  78. if developButton == 1 && serverButton == 0 && adminId == "" && serviceAdminId == "" { //开拓组筛选条件
  79. condition += " AND real_name NOT LIKE '%6%' "
  80. }
  81. if developButton == 0 && serverButton == 1 && adminId == "" && serviceAdminId == "" { //服务组筛选条件
  82. condition += " AND real_name LIKE '%6%' "
  83. }
  84. sellerList, err := system.GetSysUserItemsOrderByCreated(condition, pars)
  85. if err != nil {
  86. br.Msg = "获取管理账号失败"
  87. br.ErrMsg = "获取管理账号失败,Err:" + err.Error()
  88. return
  89. }
  90. //拼接起始时间查询
  91. startData := fmt.Sprintf("%d-01-01", startYear)
  92. endData := fmt.Sprintf("%d-12-31", endYear)
  93. var conditionRai string
  94. var parsRai []interface{}
  95. conditionRai = " AND a.product_id = 2 AND a.status = 1 AND a.start_date >= ? AND a.start_date <= ? "
  96. parsRai = append(parsRai, startData, endData)
  97. listRaiData, err := statistic_report.GetRaiDataSummaryList(conditionRai, parsRai)
  98. if err != nil {
  99. br.Msg = "获取数据信息失败"
  100. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  101. return
  102. }
  103. var conditionConfirm string
  104. var parsConfirm []interface{}
  105. mapNoRenewedcompanyContractIds := make(map[int]bool) //已经确定未续约的合同ID
  106. conditionConfirm = " AND company_ascribe_id != 9 "
  107. companyConfirmList, err := company.GetCompanyContractNoRenewedAscribeList(conditionConfirm, parsConfirm, 0, 0)
  108. if err != nil && err.Error() != utils.ErrNoRow() {
  109. br.Msg = "获取失败"
  110. br.ErrMsg = "获取失败,GetCompanyNoRenewedAscribeList Err:" + err.Error()
  111. return
  112. }
  113. for _, v := range companyConfirmList {
  114. mapNoRenewedcompanyContractIds[v.CompanyContractId] = true
  115. }
  116. mapNewContractMoney := make(map[string]float64) // 新签合同(金额)
  117. mapNewContractNum := make(map[string]int) // 新签合同(数量)
  118. mapExpiredContractMoney := make(map[string]float64) // 到期合同(金额)
  119. mapExpiredContractNum := make(map[string]int) // 到期合同(数量)
  120. mapRenewedContractMoney := make(map[string]float64) // 续约合同(金额)
  121. mapRenewedContractNum := make(map[string]int) // 续约合同(数量)
  122. confirmedNoRenewalContractMoney := make(map[string]float64) // 确认不续约合同(金额)
  123. confirmedNoRenewalContractNum := make(map[string]int) // 确认不续约合同(数量)
  124. for _, v := range listRaiData {
  125. startDateTime := utils.StrDateToDate(v.StartDate)
  126. monthNum := startDateTime.Month()
  127. yearStr := strconv.Itoa(startDateTime.Year())
  128. if dataType == "季度" {
  129. if monthNum < 4 {
  130. yearStr += "Q1"
  131. } else if monthNum > 3 && monthNum < 7 {
  132. yearStr += "Q2"
  133. } else if monthNum > 6 && monthNum < 10 {
  134. yearStr += "Q3"
  135. } else if monthNum > 9 {
  136. yearStr += "Q4"
  137. }
  138. } else if dataType == "半年度" {
  139. if monthNum < 7 {
  140. yearStr += "H1"
  141. } else {
  142. yearStr += "H2"
  143. }
  144. }
  145. keyMap := fmt.Sprint(yearStr, "_", v.SellerIdLast)
  146. if v.RaiContractType == "新签合同" {
  147. mapNewContractMoney[keyMap] += v.Money
  148. mapNewContractNum[keyMap]++
  149. } else if v.RaiContractType == "续约合同" {
  150. mapRenewedContractMoney[keyMap] += v.Money
  151. mapRenewedContractNum[keyMap]++
  152. }
  153. if startDateTime.Before(time.Now().AddDate(0, 0, -1)) { //到期合同数据
  154. mapExpiredContractMoney[keyMap] += v.Money
  155. mapExpiredContractNum[keyMap]++
  156. }
  157. if mapNoRenewedcompanyContractIds[v.CompanyContractId] { // 确认不续约合同
  158. confirmedNoRenewalContractMoney[keyMap] += v.Money
  159. confirmedNoRenewalContractNum[keyMap]++
  160. }
  161. }
  162. //fmt.Println(time.Now().AddDate(0, 0, -1))
  163. //return
  164. var sellerDevelop []*system.AdminItem // 开拓组销售
  165. var sellerService []*system.AdminItem // 服务组销售
  166. for _, v := range sellerList {
  167. if strings.Contains(v.RealName, "6") {
  168. sellerService = append(sellerService, v)
  169. } else {
  170. sellerDevelop = append(sellerDevelop, v)
  171. }
  172. }
  173. resp := new(statistic_report.RaiDataSummaryListResp)
  174. var items []*statistic_report.RaiDataSummaryResp
  175. for i := startYear; i <= endYear; i++ {
  176. //if len(dataTypeArr) > 0 {
  177. for _, Dv := range dataTypeArr {
  178. item := new(statistic_report.RaiDataSummaryResp)
  179. item.DataType = fmt.Sprint(i, Dv)
  180. for _, vS := range sellerDevelop {
  181. keyMap := fmt.Sprint(item.DataType, "_", vS.AdminId)
  182. sellerItem := new(statistic_report.RaiDataSummaryDetail)
  183. sellerItem.SellerName = vS.RealName
  184. sellerItem.AddTrialCount = "新增试用-(数据)"
  185. sellerItem.NewContractData = fmt.Sprint(utils.SubFloatToString(mapNewContractMoney[keyMap], 2), " / ", mapNewContractNum[keyMap]) // 新签合同(金额/数量)-(数据)
  186. sellerItem.ExpiredContractData = fmt.Sprint(utils.SubFloatToString(mapExpiredContractMoney[keyMap], 2), " / ", mapExpiredContractNum[keyMap]) //"到期合同(金额/数量)-(数据)"
  187. sellerItem.RenewedContractData = fmt.Sprint(utils.SubFloatToString(mapRenewedContractMoney[keyMap], 2), " / ", mapRenewedContractNum[keyMap]) // "续约合同(金额/数量)-(数据)"
  188. var renewalRateMoey string
  189. var renewalRateNum string
  190. if mapRenewedContractMoney[keyMap] == 0 || mapExpiredContractMoney[keyMap] == 0 {
  191. renewalRateMoey = "0%"
  192. } else {
  193. renewalRateMoey = utils.SubFloatToString(mapRenewedContractMoney[keyMap]/mapExpiredContractMoney[keyMap]*100, 2) + "%"
  194. }
  195. if mapRenewedContractNum[keyMap] == 0 || mapExpiredContractNum[keyMap] == 0 {
  196. renewalRateNum = "0%"
  197. } else {
  198. renewalRateNum = utils.SubFloatToString(float64(mapRenewedContractNum[keyMap])/float64(mapExpiredContractNum[keyMap])*100, 2) + "%"
  199. }
  200. sellerItem.RenewalRateData = fmt.Sprint(renewalRateMoey, " / ", renewalRateNum) //"续约率(金额/数量)-(数据)"
  201. sellerItem.ConfirmedNoRenewalContractData = fmt.Sprint(utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMap], 2), " / ", confirmedNoRenewalContractNum[keyMap]) //"确认不续约合同(金额/数量)-(数据)"
  202. var confirmNonRenewalRateMoey string
  203. var confirmNonRenewalRateNum string
  204. if confirmedNoRenewalContractMoney[keyMap] == 0 || mapExpiredContractMoney[keyMap] == 0 {
  205. confirmNonRenewalRateMoey = "0%"
  206. } else {
  207. confirmNonRenewalRateMoey = utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMap]/mapExpiredContractMoney[keyMap]*100, 2) + "%"
  208. }
  209. if confirmedNoRenewalContractNum[keyMap] == 0 || mapExpiredContractNum[keyMap] == 0 {
  210. confirmNonRenewalRateNum = "0%"
  211. } else {
  212. confirmNonRenewalRateNum = utils.SubFloatToString(float64(confirmedNoRenewalContractNum[keyMap])/float64(mapExpiredContractNum[keyMap])*100, 2) + "%"
  213. }
  214. sellerItem.ConfirmNonRenewalRateData = fmt.Sprint(confirmNonRenewalRateMoey, " / ", confirmNonRenewalRateNum) //确认不续约率(金额/数量)-(数据)"
  215. sellerItem.SignedClientCount = "签约客户数量-(数据)"
  216. sellerItem.AverageRevenueCount = "客单价-(数据)"
  217. sellerItem.InvoiceAmountCount = "开票金额-(数据)"
  218. sellerItem.PaymentReceivedCount = "到款金额-(数据)"
  219. sellerItem.UnpaidRatioCount = "未到款比例-(数据)"
  220. sellerItem.NewCustomerInvoicingCount = "新客开票-(数据)"
  221. sellerItem.NewCustomerPaymentsReceivedCount = "新客到款-(数据)"
  222. item.DataList = append(item.DataList, sellerItem)
  223. }
  224. if len(sellerDevelop) > 0 {
  225. sellerItem := new(statistic_report.RaiDataSummaryDetail)
  226. sellerItem.SellerName = "开拓组合计"
  227. item.DataList = append(item.DataList, sellerItem)
  228. }
  229. for _, vS := range sellerService {
  230. sellerItem := new(statistic_report.RaiDataSummaryDetail)
  231. sellerItem.SellerName = vS.RealName
  232. sellerItem.AddTrialCount = "新增试用-(数据)"
  233. sellerItem.NewContractData = "新签合同(金额/数量)-(数据)"
  234. sellerItem.ExpiredContractData = "到期合同(金额/数量)-(数据)"
  235. sellerItem.RenewedContractData = "续约合同(金额/数量)-(数据)"
  236. sellerItem.RenewalRateData = "续约率(金额/数量)-(数据)"
  237. sellerItem.ConfirmedNoRenewalContractData = "确认不续约合同(金额/数量)-(数据)"
  238. sellerItem.ConfirmNonRenewalRateData = "确认不续约率(金额/数量)-(数据)"
  239. sellerItem.SignedClientCount = "签约客户数量-(数据)"
  240. sellerItem.AverageRevenueCount = "客单价-(数据)"
  241. sellerItem.InvoiceAmountCount = "开票金额-(数据)"
  242. sellerItem.PaymentReceivedCount = "到款金额-(数据)"
  243. sellerItem.UnpaidRatioCount = "未到款比例-(数据)"
  244. sellerItem.NewCustomerInvoicingCount = "新客开票-(数据)"
  245. sellerItem.NewCustomerPaymentsReceivedCount = "新客到款-(数据)"
  246. item.DataList = append(item.DataList, sellerItem)
  247. }
  248. if len(sellerService) > 0 {
  249. sellerItem := new(statistic_report.RaiDataSummaryDetail)
  250. sellerItem.SellerName = "服务组合计"
  251. item.DataList = append(item.DataList, sellerItem)
  252. }
  253. items = append(items, item)
  254. }
  255. }
  256. resp.List = items
  257. br.Ret = 200
  258. br.Success = true
  259. br.Msg = "获取成功"
  260. br.Data = resp
  261. }