rai_data_summary.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. package statistic
  2. import (
  3. "fmt"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "hongze/hz_crm_api/controllers"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/models/company"
  8. "hongze/hz_crm_api/models/fms"
  9. "hongze/hz_crm_api/models/statistic_report"
  10. "hongze/hz_crm_api/models/system"
  11. cygxService "hongze/hz_crm_api/services/cygx"
  12. "hongze/hz_crm_api/utils"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. // 权益数据汇总
  18. // StatisticRaiDataSummaryController 权益数据汇总基类
  19. type StatisticRaiDataSummaryController struct {
  20. controllers.BaseAuthController
  21. }
  22. // MergeCompanyList
  23. // @Title 权益数据汇总
  24. // @Description 权益数据汇总统计列表接口
  25. // @Param AdminId query string true "开拓组销售id,多个用英文逗号隔开,空字符串为全部"
  26. // @Param ServiceAdminId query string true "服务组销售id,多个用英文逗号隔开,空字符串为全部"
  27. // @Param StartYear query int false "开始日期(年份)"
  28. // @Param EndYear query int false "结束日期(年份)"
  29. // @Param DataType query string false "报表类型,枚举值:`季度`,`年度`,`半年度`"
  30. // @Param DevelopButton query int false "开拓组开关,枚举值:1:开启,0:关闭 ,默认关闭"
  31. // @Param ServerButton query int false "服务组开关,枚举值:1:开启,0:关闭 ,默认关闭"
  32. // @Param ContractButtonType query string false "开关类型,:`新签`,`续约`,`收入` 多个用英文逗号隔开, "
  33. // @Param IsExport query bool false "是否导出excel,默认是false"
  34. // @Success 200 {object} statistic_report.RaiDataSummaryListResp
  35. // @router /rai_data_summary/list [get]
  36. func (this *StatisticRaiDataSummaryController) RaiDataSummaryList() {
  37. br := new(models.BaseResponse).Init()
  38. defer func() {
  39. this.Data["json"] = br
  40. this.ServeJSON()
  41. }()
  42. sysUser := this.SysUser
  43. if sysUser == nil {
  44. br.Msg = "请登录"
  45. br.ErrMsg = "请登录,SysUser Is Empty"
  46. br.Ret = 408
  47. return
  48. }
  49. //adminId := this.GetString("AdminId")
  50. dataType := this.GetString("DataType")
  51. adminId := this.GetString("AdminId")
  52. serviceAdminId := this.GetString("ServiceAdminId")
  53. startYear, _ := this.GetInt("StartYear")
  54. endYear, _ := this.GetInt("EndYear")
  55. developButton, _ := this.GetInt("DevelopButton")
  56. serverButton, _ := this.GetInt("ServerButton")
  57. dataTypeArr := []string{}
  58. //var dateIntervalArr []statistic_report.DateInterval //时间区间
  59. if dataType == "季度" {
  60. dataTypeArr = []string{"Q1", "Q2", "Q3", "Q4"}
  61. } else if dataType == "半年度" {
  62. dataTypeArr = []string{"H1", "H2"}
  63. } else {
  64. dataTypeArr = []string{""}
  65. }
  66. condition := " AND role_type_code IN ('rai_seller','rai_group') AND enabled = 1 AND rai_enabled = 1 "
  67. var pars []interface{}
  68. if adminId != "" {
  69. serviceAdminId = ""
  70. condition += " AND admin_id IN (" + adminId + ") "
  71. }
  72. if serviceAdminId != "" {
  73. condition += " AND admin_id IN (" + serviceAdminId + ") "
  74. }
  75. if developButton == 1 && serverButton == 0 && adminId == "" && serviceAdminId == "" { //开拓组筛选条件
  76. condition += " AND real_name NOT LIKE '%6%' "
  77. }
  78. if developButton == 0 && serverButton == 1 && adminId == "" && serviceAdminId == "" { //服务组筛选条件
  79. condition += " AND real_name LIKE '%6%' "
  80. }
  81. sellerList, err := system.GetSysUserItemsOrderByCreated(condition, pars)
  82. if err != nil {
  83. br.Msg = "获取管理账号失败"
  84. br.ErrMsg = "获取管理账号失败,Err:" + err.Error()
  85. return
  86. }
  87. var sellerDevelop []*system.AdminItem // 开拓组销售
  88. var sellerService []*system.AdminItem // 服务组销售
  89. var sellerDevelopIds = make(map[int]bool) // 开拓组销售的map
  90. for _, v := range sellerList {
  91. if strings.Contains(v.RealName, "6") {
  92. sellerService = append(sellerService, v)
  93. } else {
  94. sellerDevelop = append(sellerDevelop, v)
  95. sellerDevelopIds[v.AdminId] = true
  96. }
  97. }
  98. //拼接起始时间查询
  99. startDate := fmt.Sprintf("%d-01-01", startYear)
  100. endDate := fmt.Sprintf("%d-12-31", endYear)
  101. var conditionRai string
  102. var parsRai []interface{}
  103. conditionRai = " AND a.product_id = 2 AND a.status = 1 AND a.start_date >= ? AND a.start_date <= ? "
  104. parsRai = append(parsRai, startDate, endDate)
  105. listRaiData, err := statistic_report.GetRaiDataSummaryList(conditionRai, parsRai)
  106. if err != nil {
  107. br.Msg = "获取数据信息失败"
  108. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  109. return
  110. }
  111. var conditionConfirm string
  112. var parsConfirm []interface{}
  113. mapNoRenewedcompanyContractIds := make(map[int]bool) //已经确定未续约的合同ID
  114. conditionConfirm = " AND company_ascribe_id != 9 "
  115. companyConfirmList, err := company.GetCompanyContractNoRenewedAscribeList(conditionConfirm, parsConfirm, 0, 0)
  116. if err != nil && err.Error() != utils.ErrNoRow() {
  117. br.Msg = "获取失败"
  118. br.ErrMsg = "获取失败,GetCompanyNoRenewedAscribeList Err:" + err.Error()
  119. return
  120. }
  121. for _, v := range companyConfirmList {
  122. mapNoRenewedcompanyContractIds[v.CompanyContractId] = true
  123. }
  124. mapAddTrialNum := make(map[string]float64) // 新增试用-(数据)
  125. mapNewContractMoney := make(map[string]float64) // 新签合同(金额)
  126. mapNewContractNum := make(map[string]int) // 新签合同(数量)
  127. mapExpiredContractMoney := make(map[string]float64) // 到期合同(金额)
  128. mapExpiredContractNum := make(map[string]int) // 到期合同(数量)
  129. mapRenewedContractMoney := make(map[string]float64) // 续约合同(金额)
  130. mapRenewedContractNum := make(map[string]int) // 续约合同(数量)
  131. confirmedNoRenewalContractMoney := make(map[string]float64) // 确认不续约合同(金额)
  132. confirmedNoRenewalContractNum := make(map[string]int) // 确认不续约合同(数量)
  133. mapSignedClientNum := make(map[string]int) // 签约客户(数量)
  134. mapSignedClientMoney := make(map[string]float64) // 签约客户(金额)
  135. mapInvoiceAmountMoney := make(map[string]float64) // 财务系统开票金额(金额)
  136. mapPaymentAmountMoney := make(map[string]float64) // 财务系统到款金额(金额)
  137. mapNewCustomerInvoicingMoney := make(map[string]float64) // 财务系统新客开票金额(金额)
  138. mapNewCustomerPaymentsReceivedMoney := make(map[string]float64) // 财务系统新客到款金额(金额)
  139. //mapAddTrialNumTotal := make(map[string]float64) // 新增试用-(数据)_总计
  140. //mapNewContractMoneyTotal := make(map[string]float64) // 新签合同(金额)_总计
  141. //mapNewContractNumTotal := make(map[string]int) // 新签合同(数量)_总计
  142. //mapExpiredContractMoneyTotal := make(map[string]float64) // 到期合同(金额)_总计
  143. //mapExpiredContractNumTotal := make(map[string]int) // 到期合同(数量)_总计
  144. //mapRenewedContractMoneyTotal := make(map[string]float64) // 续约合同(金额)_总计
  145. //mapRenewedContractNumTotal := make(map[string]int) // 续约合同(数量)_总计
  146. //confirmedNoRenewalContractMoneyTotal := make(map[string]float64) // 确认不续约合同(金额)_总计
  147. //confirmedNoRenewalContractNumTotal := make(map[string]int) // 确认不续约合同(数量)_总计
  148. //mapSignedClientNumTotal := make(map[string]int) // 签约客户(数量)_总计
  149. //mapSignedClientMoneyTotal := make(map[string]float64) // 签约客户(金额)_总计
  150. //mapInvoiceAmountMoneyTotal := make(map[string]float64) // 财务系统开票金额(金额)_总计
  151. //mapPaymentAmountMoneyTotal := make(map[string]float64) // 财务系统到款金额(金额)_总计
  152. //mapNewCustomerInvoicingMoneyTotal := make(map[string]float64) // 财务系统新客开票金额(金额)_总计
  153. //mapNewCustomerPaymentsReceivedMoneyTotal := make(map[string]float64) // 财务系统新客到款金额(金额)_总计
  154. var keyMap string
  155. var keyMapTtoal string
  156. var keyMapTtoalServer string
  157. for _, v := range listRaiData {
  158. startDateTime := utils.StrDateToDate(v.StartDate)
  159. monthNum := startDateTime.Month()
  160. yearStr := strconv.Itoa(startDateTime.Year())
  161. if dataType == "季度" {
  162. if monthNum < 4 {
  163. yearStr += "Q1"
  164. } else if monthNum > 3 && monthNum < 7 {
  165. yearStr += "Q2"
  166. } else if monthNum > 6 && monthNum < 10 {
  167. yearStr += "Q3"
  168. } else if monthNum > 9 {
  169. yearStr += "Q4"
  170. }
  171. } else if dataType == "半年度" {
  172. if monthNum < 7 {
  173. yearStr += "H1"
  174. } else {
  175. yearStr += "H2"
  176. }
  177. }
  178. keyMap = fmt.Sprint(yearStr, "_", v.SellerIdLast)
  179. if sellerDevelopIds[v.SellerIdLast] == true {
  180. keyMapTtoal = fmt.Sprint(yearStr, "_Develop")
  181. } else {
  182. keyMapTtoal = fmt.Sprint(yearStr, "_Service")
  183. }
  184. if v.RaiContractType == "新签合同" {
  185. mapNewContractMoney[keyMap] += v.Money
  186. mapNewContractNum[keyMap]++
  187. mapNewContractMoney[keyMapTtoal] += v.Money
  188. mapNewContractNum[keyMapTtoal]++
  189. } else if v.RaiContractType == "续约合同" {
  190. mapRenewedContractMoney[keyMap] += v.Money
  191. mapRenewedContractNum[keyMap]++
  192. mapRenewedContractMoney[keyMapTtoal] += v.Money
  193. mapRenewedContractNum[keyMapTtoal]++
  194. }
  195. if startDateTime.Before(time.Now().AddDate(0, 0, -1)) { //到期合同数据
  196. mapExpiredContractMoney[keyMap] += v.Money
  197. mapExpiredContractNum[keyMap]++
  198. mapExpiredContractMoney[keyMapTtoal] += v.Money
  199. mapExpiredContractNum[keyMapTtoal]++
  200. }
  201. if mapNoRenewedcompanyContractIds[v.CompanyContractId] { // 确认不续约合同
  202. confirmedNoRenewalContractMoney[keyMap] += v.Money
  203. confirmedNoRenewalContractNum[keyMap]++
  204. confirmedNoRenewalContractMoney[keyMapTtoal] += v.Money
  205. confirmedNoRenewalContractNum[keyMapTtoal]++
  206. }
  207. mapSignedClientMoney[keyMap] += v.Money
  208. mapSignedClientNum[keyMap]++
  209. mapSignedClientMoney[keyMapTtoal] += v.Money
  210. mapSignedClientNum[keyMapTtoal]++
  211. }
  212. listFmsData, err := fms.GetContractRegisterListByStartDate(startDate, endDate)
  213. if err != nil {
  214. br.Msg = "获取数据信息失败"
  215. br.ErrMsg = "获取财务系统数据信息失败,Err:" + err.Error()
  216. return
  217. }
  218. for _, v := range listFmsData {
  219. startDateTime := v.StartDate
  220. monthNum := startDateTime.Month()
  221. yearStr := strconv.Itoa(startDateTime.Year())
  222. if dataType == "季度" {
  223. if monthNum < 4 {
  224. yearStr += "Q1"
  225. } else if monthNum > 3 && monthNum < 7 {
  226. yearStr += "Q2"
  227. } else if monthNum > 6 && monthNum < 10 {
  228. yearStr += "Q3"
  229. } else if monthNum > 9 {
  230. yearStr += "Q4"
  231. }
  232. } else if dataType == "半年度" {
  233. if monthNum < 7 {
  234. yearStr += "H1"
  235. } else {
  236. yearStr += "H2"
  237. }
  238. }
  239. keyMap := fmt.Sprint(yearStr, "_", v.RaiSellerId)
  240. if sellerDevelopIds[v.RaiSellerId] == true {
  241. keyMapTtoal = fmt.Sprint(yearStr, "_Develop")
  242. } else {
  243. keyMapTtoal = fmt.Sprint(yearStr, "_Service")
  244. }
  245. mapInvoiceAmountMoney[keyMap] += v.InvoicedAmount
  246. mapPaymentAmountMoney[keyMap] += v.PaymentAmount
  247. mapInvoiceAmountMoney[keyMapTtoal] += v.InvoicedAmount
  248. mapPaymentAmountMoney[keyMapTtoal] += v.PaymentAmount
  249. if v.ContractType == 1 {
  250. mapNewCustomerInvoicingMoney[keyMapTtoal] += v.InvoicedAmount
  251. mapNewCustomerPaymentsReceivedMoney[keyMapTtoal] += v.PaymentAmount
  252. }
  253. }
  254. conditionTry := ` AND a.create_time >= ? AND a.create_time <= ? AND a.operation in ( "add","receive","apply_receive" ) AND a.sys_user_id IN ( SELECT admin_id FROM admin WHERE role_type_code IN ( 'rai_seller', 'rai_group' , 'rai_admin') ) `
  255. var parsTry []interface{}
  256. parsTry = append(parsTry, startDate, endDate)
  257. //列表页数据
  258. tryList, err := models.GetIncrementalCompanyListByOperationRecordRai(conditionTry, parsTry, 0, 9999)
  259. if err != nil {
  260. br.Msg = "获取失败"
  261. br.ErrMsg = "获取失败,Err:" + err.Error()
  262. return
  263. }
  264. for _, v := range tryList {
  265. startDateTime := utils.StrTimeToTime(v.CreateTime)
  266. monthNum := startDateTime.Month()
  267. yearStr := strconv.Itoa(startDateTime.Year())
  268. if dataType == "季度" {
  269. if monthNum < 4 {
  270. yearStr += "Q1"
  271. } else if monthNum > 3 && monthNum < 7 {
  272. yearStr += "Q2"
  273. } else if monthNum > 6 && monthNum < 10 {
  274. yearStr += "Q3"
  275. } else if monthNum > 9 {
  276. yearStr += "Q4"
  277. }
  278. } else if dataType == "半年度" {
  279. if monthNum < 7 {
  280. yearStr += "H1"
  281. } else {
  282. yearStr += "H2"
  283. }
  284. }
  285. keyMap := fmt.Sprint(yearStr, "_", v.SysUserId)
  286. mapAddTrialNum[keyMap]++
  287. //var keyMapTtoal string
  288. sysUserId, _ := strconv.Atoi(v.SysUserId)
  289. if sellerDevelopIds[sysUserId] == true {
  290. keyMapTtoal = fmt.Sprint(yearStr, "_Develop")
  291. } else {
  292. keyMapTtoal = fmt.Sprint(yearStr, "_Service")
  293. }
  294. mapAddTrialNum[keyMapTtoal]++
  295. }
  296. resp := new(statistic_report.RaiDataSummaryListResp)
  297. var items []*statistic_report.RaiDataSummaryResp
  298. for i := startYear; i <= endYear; i++ {
  299. //if len(dataTypeArr) > 0 {
  300. for _, Dv := range dataTypeArr {
  301. item := new(statistic_report.RaiDataSummaryResp)
  302. item.DataType = fmt.Sprint(i, Dv)
  303. keyMapTtoal = fmt.Sprint(item.DataType, "_Develop")
  304. keyMapTtoalServer = fmt.Sprint(item.DataType, "_Server")
  305. for _, vS := range sellerDevelop {
  306. keyMap = fmt.Sprint(item.DataType, "_", vS.AdminId)
  307. sellerItem := new(statistic_report.RaiDataSummaryDetail)
  308. sellerItem.SellerId = vS.AdminId
  309. sellerItem.SellerName = vS.RealName
  310. sellerItem.AddTrialCount = fmt.Sprint(mapAddTrialNum[keyMap])
  311. sellerItem.NewContractData = fmt.Sprint(utils.SubFloatToString(mapNewContractMoney[keyMap], 2), " / ", mapNewContractNum[keyMap]) // 新签合同(金额/数量)-(数据)
  312. sellerItem.ExpiredContractData = fmt.Sprint(utils.SubFloatToString(mapExpiredContractMoney[keyMap], 2), " / ", mapExpiredContractNum[keyMap]) //"到期合同(金额/数量)-(数据)"
  313. sellerItem.RenewedContractData = fmt.Sprint(utils.SubFloatToString(mapRenewedContractMoney[keyMap], 2), " / ", mapRenewedContractNum[keyMap]) // "续约合同(金额/数量)-(数据)"
  314. var renewalRateMoey string
  315. var renewalRateNum string
  316. if mapRenewedContractMoney[keyMap] == 0 || mapExpiredContractMoney[keyMap] == 0 {
  317. renewalRateMoey = "0%"
  318. } else {
  319. renewalRateMoey = utils.SubFloatToString(mapRenewedContractMoney[keyMap]/mapExpiredContractMoney[keyMap]*100, 2) + "%"
  320. }
  321. if mapRenewedContractNum[keyMap] == 0 || mapExpiredContractNum[keyMap] == 0 {
  322. renewalRateNum = "0%"
  323. } else {
  324. renewalRateNum = utils.SubFloatToString(float64(mapRenewedContractNum[keyMap])/float64(mapExpiredContractNum[keyMap])*100, 2) + "%"
  325. }
  326. sellerItem.RenewalRateData = fmt.Sprint(renewalRateMoey, " / ", renewalRateNum) //"续约率(金额/数量)-(数据)"
  327. sellerItem.ConfirmedNoRenewalContractData = fmt.Sprint(utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMap], 2), " / ", confirmedNoRenewalContractNum[keyMap]) //"确认不续约合同(金额/数量)-(数据)"
  328. var confirmNonRenewalRateMoey string
  329. var confirmNonRenewalRateNum string
  330. if confirmedNoRenewalContractMoney[keyMap] == 0 || mapExpiredContractMoney[keyMap] == 0 {
  331. confirmNonRenewalRateMoey = "0%"
  332. } else {
  333. confirmNonRenewalRateMoey = utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMap]/mapExpiredContractMoney[keyMap]*100, 2) + "%"
  334. }
  335. if confirmedNoRenewalContractNum[keyMap] == 0 || mapExpiredContractNum[keyMap] == 0 {
  336. confirmNonRenewalRateNum = "0%"
  337. } else {
  338. confirmNonRenewalRateNum = utils.SubFloatToString(float64(confirmedNoRenewalContractNum[keyMap])/float64(mapExpiredContractNum[keyMap])*100, 2) + "%"
  339. }
  340. sellerItem.ConfirmNonRenewalRateData = fmt.Sprint(confirmNonRenewalRateMoey, " / ", confirmNonRenewalRateNum) //确认不续约率(金额/数量)-(数据)"
  341. sellerItem.SignedClientCount = fmt.Sprint(mapSignedClientNum[keyMap]) // 签约客户数量
  342. if mapSignedClientNum[keyMap] == 0 || mapSignedClientMoney[keyMap] == 0 {
  343. sellerItem.AverageRevenueCount = "0"
  344. } else {
  345. sellerItem.AverageRevenueCount = utils.SubFloatToString(mapSignedClientMoney[keyMap]/float64(mapSignedClientNum[keyMap]), 2) //客单价
  346. }
  347. sellerItem.InvoiceAmountCount = utils.SubFloatToString(mapInvoiceAmountMoney[keyMap], 2) //"开票金额-(数据)"
  348. sellerItem.PaymentReceivedCount = utils.SubFloatToString(mapPaymentAmountMoney[keyMap], 2) //"开票金额-(数据)"
  349. if mapInvoiceAmountMoney[keyMap] == 0 || mapPaymentAmountMoney[keyMap] == 0 {
  350. sellerItem.UnpaidRatioCount = "0%"
  351. } else {
  352. sellerItem.UnpaidRatioCount = utils.SubFloatToString((mapInvoiceAmountMoney[keyMap]-mapPaymentAmountMoney[keyMap])/mapInvoiceAmountMoney[keyMap]*100, 2) + "%" //"未到款比例-(数据)"
  353. }
  354. sellerItem.NewCustomerInvoicingCount = utils.SubFloatToString(mapNewCustomerInvoicingMoney[keyMap], 2) // "新客开票-(数据)"
  355. sellerItem.NewCustomerPaymentsReceivedCount = utils.SubFloatToString(mapNewCustomerPaymentsReceivedMoney[keyMap], 2) // "新客到款-(数据)"
  356. item.DataList = append(item.DataList, sellerItem)
  357. }
  358. if len(sellerDevelop) > 0 {
  359. sellerItem := new(statistic_report.RaiDataSummaryDetail)
  360. sellerItem.SellerName = "开拓组合计"
  361. sellerItem.AddTrialCount = fmt.Sprint(mapAddTrialNum[keyMapTtoal])
  362. sellerItem.NewContractData = fmt.Sprint(utils.SubFloatToString(mapNewContractMoney[keyMapTtoal], 2), " / ", mapNewContractNum[keyMapTtoal]) // 新签合同(金额/数量)-(数据)
  363. sellerItem.ExpiredContractData = fmt.Sprint(utils.SubFloatToString(mapExpiredContractMoney[keyMapTtoal], 2), " / ", mapExpiredContractNum[keyMapTtoal]) //"到期合同(金额/数量)-(数据)"
  364. sellerItem.RenewedContractData = fmt.Sprint(utils.SubFloatToString(mapRenewedContractMoney[keyMapTtoal], 2), " / ", mapRenewedContractNum[keyMapTtoal]) // "续约合同(金额/数量)-(数据)"
  365. var renewalRateMoey string
  366. var renewalRateNum string
  367. if mapRenewedContractMoney[keyMapTtoal] == 0 || mapExpiredContractMoney[keyMapTtoal] == 0 {
  368. renewalRateMoey = "0%"
  369. } else {
  370. renewalRateMoey = utils.SubFloatToString(mapRenewedContractMoney[keyMapTtoal]/mapExpiredContractMoney[keyMapTtoal]*100, 2) + "%"
  371. }
  372. if mapRenewedContractNum[keyMapTtoal] == 0 || mapExpiredContractNum[keyMapTtoal] == 0 {
  373. renewalRateNum = "0%"
  374. } else {
  375. renewalRateNum = utils.SubFloatToString(float64(mapRenewedContractNum[keyMapTtoal])/float64(mapExpiredContractNum[keyMapTtoal])*100, 2) + "%"
  376. }
  377. sellerItem.RenewalRateData = fmt.Sprint(renewalRateMoey, " / ", renewalRateNum) //"续约率(金额/数量)-(数据)"
  378. sellerItem.ConfirmedNoRenewalContractData = fmt.Sprint(utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMapTtoal], 2), " / ", confirmedNoRenewalContractNum[keyMapTtoal]) //"确认不续约合同(金额/数量)-(数据)"
  379. var confirmNonRenewalRateMoey string
  380. var confirmNonRenewalRateNum string
  381. if confirmedNoRenewalContractMoney[keyMapTtoal] == 0 || mapExpiredContractMoney[keyMapTtoal] == 0 {
  382. confirmNonRenewalRateMoey = "0%"
  383. } else {
  384. confirmNonRenewalRateMoey = utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMapTtoal]/mapExpiredContractMoney[keyMapTtoal]*100, 2) + "%"
  385. }
  386. if confirmedNoRenewalContractNum[keyMapTtoal] == 0 || mapExpiredContractNum[keyMapTtoal] == 0 {
  387. confirmNonRenewalRateNum = "0%"
  388. } else {
  389. confirmNonRenewalRateNum = utils.SubFloatToString(float64(confirmedNoRenewalContractNum[keyMapTtoal])/float64(mapExpiredContractNum[keyMapTtoal])*100, 2) + "%"
  390. }
  391. sellerItem.ConfirmNonRenewalRateData = fmt.Sprint(confirmNonRenewalRateMoey, " / ", confirmNonRenewalRateNum) //确认不续约率(金额/数量)-(数据)"
  392. sellerItem.SignedClientCount = fmt.Sprint(mapSignedClientNum[keyMapTtoal]) // 签约客户数量
  393. if mapSignedClientNum[keyMapTtoal] == 0 || mapSignedClientMoney[keyMapTtoal] == 0 {
  394. sellerItem.AverageRevenueCount = "0"
  395. } else {
  396. sellerItem.AverageRevenueCount = utils.SubFloatToString(mapSignedClientMoney[keyMapTtoal]/float64(mapSignedClientNum[keyMapTtoal]), 2) //客单价
  397. }
  398. sellerItem.InvoiceAmountCount = utils.SubFloatToString(mapInvoiceAmountMoney[keyMapTtoal], 2) //"开票金额-(数据)"
  399. sellerItem.PaymentReceivedCount = utils.SubFloatToString(mapPaymentAmountMoney[keyMapTtoal], 2) //"开票金额-(数据)"
  400. if mapInvoiceAmountMoney[keyMapTtoal] == 0 || mapPaymentAmountMoney[keyMapTtoal] == 0 {
  401. sellerItem.UnpaidRatioCount = "0%"
  402. } else {
  403. sellerItem.UnpaidRatioCount = utils.SubFloatToString((mapInvoiceAmountMoney[keyMapTtoal]-mapPaymentAmountMoney[keyMapTtoal])/mapInvoiceAmountMoney[keyMapTtoal]*100, 2) + "%" //"未到款比例-(数据)"
  404. }
  405. sellerItem.NewCustomerInvoicingCount = utils.SubFloatToString(mapNewCustomerInvoicingMoney[keyMapTtoal], 2) // "新客开票-(数据)"
  406. sellerItem.NewCustomerPaymentsReceivedCount = utils.SubFloatToString(mapNewCustomerPaymentsReceivedMoney[keyMapTtoal], 2) // "新客到款-(数据)"
  407. item.DataList = append(item.DataList, sellerItem)
  408. }
  409. for _, vS := range sellerService {
  410. sellerItem := new(statistic_report.RaiDataSummaryDetail)
  411. sellerItem.SellerId = vS.AdminId
  412. sellerItem.SellerName = vS.RealName
  413. sellerItem.AddTrialCount = fmt.Sprint(mapAddTrialNum[keyMap])
  414. sellerItem.NewContractData = fmt.Sprint(utils.SubFloatToString(mapNewContractMoney[keyMap], 2), " / ", mapNewContractNum[keyMap]) // 新签合同(金额/数量)-(数据)
  415. sellerItem.ExpiredContractData = fmt.Sprint(utils.SubFloatToString(mapExpiredContractMoney[keyMap], 2), " / ", mapExpiredContractNum[keyMap]) //"到期合同(金额/数量)-(数据)"
  416. sellerItem.RenewedContractData = fmt.Sprint(utils.SubFloatToString(mapRenewedContractMoney[keyMap], 2), " / ", mapRenewedContractNum[keyMap]) // "续约合同(金额/数量)-(数据)"
  417. var renewalRateMoey string
  418. var renewalRateNum string
  419. if mapRenewedContractMoney[keyMap] == 0 || mapExpiredContractMoney[keyMap] == 0 {
  420. renewalRateMoey = "0%"
  421. } else {
  422. renewalRateMoey = utils.SubFloatToString(mapRenewedContractMoney[keyMap]/mapExpiredContractMoney[keyMap]*100, 2) + "%"
  423. }
  424. if mapRenewedContractNum[keyMap] == 0 || mapExpiredContractNum[keyMap] == 0 {
  425. renewalRateNum = "0%"
  426. } else {
  427. renewalRateNum = utils.SubFloatToString(float64(mapRenewedContractNum[keyMap])/float64(mapExpiredContractNum[keyMap])*100, 2) + "%"
  428. }
  429. sellerItem.RenewalRateData = fmt.Sprint(renewalRateMoey, " / ", renewalRateNum) //"续约率(金额/数量)-(数据)"
  430. sellerItem.ConfirmedNoRenewalContractData = fmt.Sprint(utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMap], 2), " / ", confirmedNoRenewalContractNum[keyMap]) //"确认不续约合同(金额/数量)-(数据)"
  431. var confirmNonRenewalRateMoey string
  432. var confirmNonRenewalRateNum string
  433. if confirmedNoRenewalContractMoney[keyMap] == 0 || mapExpiredContractMoney[keyMap] == 0 {
  434. confirmNonRenewalRateMoey = "0%"
  435. } else {
  436. confirmNonRenewalRateMoey = utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMap]/mapExpiredContractMoney[keyMap]*100, 2) + "%"
  437. }
  438. if confirmedNoRenewalContractNum[keyMap] == 0 || mapExpiredContractNum[keyMap] == 0 {
  439. confirmNonRenewalRateNum = "0%"
  440. } else {
  441. confirmNonRenewalRateNum = utils.SubFloatToString(float64(confirmedNoRenewalContractNum[keyMap])/float64(mapExpiredContractNum[keyMap])*100, 2) + "%"
  442. }
  443. sellerItem.ConfirmNonRenewalRateData = fmt.Sprint(confirmNonRenewalRateMoey, " / ", confirmNonRenewalRateNum) //确认不续约率(金额/数量)-(数据)"
  444. sellerItem.SignedClientCount = fmt.Sprint(mapSignedClientNum[keyMap]) // 签约客户数量
  445. if mapSignedClientNum[keyMap] == 0 || mapSignedClientMoney[keyMap] == 0 {
  446. sellerItem.AverageRevenueCount = "0"
  447. } else {
  448. sellerItem.AverageRevenueCount = utils.SubFloatToString(mapSignedClientMoney[keyMap]/float64(mapSignedClientNum[keyMap]), 2) //客单价
  449. }
  450. sellerItem.InvoiceAmountCount = utils.SubFloatToString(mapInvoiceAmountMoney[keyMap], 2) //"开票金额-(数据)"
  451. sellerItem.PaymentReceivedCount = utils.SubFloatToString(mapPaymentAmountMoney[keyMap], 2) //"开票金额-(数据)"
  452. if mapInvoiceAmountMoney[keyMap] == 0 || mapPaymentAmountMoney[keyMap] == 0 {
  453. sellerItem.UnpaidRatioCount = "0%"
  454. } else {
  455. sellerItem.UnpaidRatioCount = utils.SubFloatToString((mapInvoiceAmountMoney[keyMap]-mapPaymentAmountMoney[keyMap])/mapInvoiceAmountMoney[keyMap]*100, 2) + "%" //"未到款比例-(数据)"
  456. }
  457. sellerItem.NewCustomerInvoicingCount = utils.SubFloatToString(mapNewCustomerInvoicingMoney[keyMap], 2) // "新客开票-(数据)"
  458. sellerItem.NewCustomerPaymentsReceivedCount = utils.SubFloatToString(mapNewCustomerPaymentsReceivedMoney[keyMap], 2) // "新客到款-(数据)"
  459. item.DataList = append(item.DataList, sellerItem)
  460. }
  461. if len(sellerService) > 0 {
  462. sellerItem := new(statistic_report.RaiDataSummaryDetail)
  463. sellerItem.SellerName = "服务组合计"
  464. sellerItem.AddTrialCount = fmt.Sprint(mapAddTrialNum[keyMapTtoalServer])
  465. sellerItem.NewContractData = fmt.Sprint(utils.SubFloatToString(mapNewContractMoney[keyMapTtoalServer], 2), " / ", mapNewContractNum[keyMapTtoalServer]) // 新签合同(金额/数量)-(数据)
  466. sellerItem.ExpiredContractData = fmt.Sprint(utils.SubFloatToString(mapExpiredContractMoney[keyMapTtoalServer], 2), " / ", mapExpiredContractNum[keyMapTtoalServer]) //"到期合同(金额/数量)-(数据)"
  467. sellerItem.RenewedContractData = fmt.Sprint(utils.SubFloatToString(mapRenewedContractMoney[keyMapTtoalServer], 2), " / ", mapRenewedContractNum[keyMapTtoalServer]) // "续约合同(金额/数量)-(数据)"
  468. var renewalRateMoey string
  469. var renewalRateNum string
  470. if mapRenewedContractMoney[keyMapTtoalServer] == 0 || mapExpiredContractMoney[keyMapTtoalServer] == 0 {
  471. renewalRateMoey = "0%"
  472. } else {
  473. renewalRateMoey = utils.SubFloatToString(mapRenewedContractMoney[keyMapTtoalServer]/mapExpiredContractMoney[keyMapTtoalServer]*100, 2) + "%"
  474. }
  475. if mapRenewedContractNum[keyMapTtoalServer] == 0 || mapExpiredContractNum[keyMapTtoalServer] == 0 {
  476. renewalRateNum = "0%"
  477. } else {
  478. renewalRateNum = utils.SubFloatToString(float64(mapRenewedContractNum[keyMapTtoalServer])/float64(mapExpiredContractNum[keyMapTtoalServer])*100, 2) + "%"
  479. }
  480. sellerItem.RenewalRateData = fmt.Sprint(renewalRateMoey, " / ", renewalRateNum) //"续约率(金额/数量)-(数据)"
  481. sellerItem.ConfirmedNoRenewalContractData = fmt.Sprint(utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMapTtoalServer], 2), " / ", confirmedNoRenewalContractNum[keyMapTtoalServer]) //"确认不续约合同(金额/数量)-(数据)"
  482. var confirmNonRenewalRateMoey string
  483. var confirmNonRenewalRateNum string
  484. if confirmedNoRenewalContractMoney[keyMapTtoalServer] == 0 || mapExpiredContractMoney[keyMapTtoalServer] == 0 {
  485. confirmNonRenewalRateMoey = "0%"
  486. } else {
  487. confirmNonRenewalRateMoey = utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMapTtoalServer]/mapExpiredContractMoney[keyMapTtoalServer]*100, 2) + "%"
  488. }
  489. if confirmedNoRenewalContractNum[keyMapTtoalServer] == 0 || mapExpiredContractNum[keyMapTtoalServer] == 0 {
  490. confirmNonRenewalRateNum = "0%"
  491. } else {
  492. confirmNonRenewalRateNum = utils.SubFloatToString(float64(confirmedNoRenewalContractNum[keyMapTtoalServer])/float64(mapExpiredContractNum[keyMapTtoalServer])*100, 2) + "%"
  493. }
  494. sellerItem.ConfirmNonRenewalRateData = fmt.Sprint(confirmNonRenewalRateMoey, " / ", confirmNonRenewalRateNum) //确认不续约率(金额/数量)-(数据)"
  495. sellerItem.SignedClientCount = fmt.Sprint(mapSignedClientNum[keyMapTtoalServer]) // 签约客户数量
  496. if mapSignedClientNum[keyMapTtoalServer] == 0 || mapSignedClientMoney[keyMapTtoalServer] == 0 {
  497. sellerItem.AverageRevenueCount = "0"
  498. } else {
  499. sellerItem.AverageRevenueCount = utils.SubFloatToString(mapSignedClientMoney[keyMapTtoalServer]/float64(mapSignedClientNum[keyMapTtoalServer]), 2) //客单价
  500. }
  501. sellerItem.InvoiceAmountCount = utils.SubFloatToString(mapInvoiceAmountMoney[keyMapTtoalServer], 2) //"开票金额-(数据)"
  502. sellerItem.PaymentReceivedCount = utils.SubFloatToString(mapPaymentAmountMoney[keyMapTtoalServer], 2) //"开票金额-(数据)"
  503. if mapInvoiceAmountMoney[keyMapTtoalServer] == 0 || mapPaymentAmountMoney[keyMapTtoalServer] == 0 {
  504. sellerItem.UnpaidRatioCount = "0%"
  505. } else {
  506. sellerItem.UnpaidRatioCount = utils.SubFloatToString((mapInvoiceAmountMoney[keyMapTtoalServer]-mapPaymentAmountMoney[keyMapTtoalServer])/mapInvoiceAmountMoney[keyMapTtoalServer]*100, 2) + "%" //"未到款比例-(数据)"
  507. }
  508. sellerItem.NewCustomerInvoicingCount = utils.SubFloatToString(mapNewCustomerInvoicingMoney[keyMapTtoalServer], 2) // "新客开票-(数据)"
  509. sellerItem.NewCustomerPaymentsReceivedCount = utils.SubFloatToString(mapNewCustomerPaymentsReceivedMoney[keyMapTtoalServer], 2) // "新客到款-(数据)"
  510. item.DataList = append(item.DataList, sellerItem)
  511. }
  512. items = append(items, item)
  513. }
  514. }
  515. resp.List = items
  516. br.Ret = 200
  517. br.Success = true
  518. br.Msg = "获取成功"
  519. br.Data = resp
  520. }
  521. // MergeCompanyList
  522. // @Title 权益数据汇总弹窗详情
  523. // @Description 权益数据汇总弹窗详情接口
  524. // @Param SellerId query int true "销售ID"
  525. // @Param DataType query string false "报表类型,枚举值:`季度`,`年度`,`半年度`"
  526. // @Param PopupType query string false "弹窗数据类型,枚举值:"
  527. // @Param PageSize query int true "每页数据条数"
  528. // @Param CurrentIndex query int true "当前页页码,从1开始"
  529. // @Success 200 {object} statistic_report.RaiDataSummaryPopupTypeResp
  530. // @router /rai_data_summary/detail [get]
  531. func (this *StatisticRaiDataSummaryController) RaiDataSummaryDetail() {
  532. br := new(models.BaseResponse).Init()
  533. defer func() {
  534. this.Data["json"] = br
  535. this.ServeJSON()
  536. }()
  537. sysUser := this.SysUser
  538. if sysUser == nil {
  539. br.Msg = "请登录"
  540. br.ErrMsg = "请登录,SysUser Is Empty"
  541. br.Ret = 408
  542. return
  543. }
  544. pageSize, _ := this.GetInt("PageSize")
  545. currentIndex, _ := this.GetInt("CurrentIndex")
  546. dataType := this.GetString("DataType")
  547. sellerId, _ := this.GetInt("SellerId")
  548. popupType := this.GetString("PopupType")
  549. var startSize int
  550. if pageSize <= 0 {
  551. pageSize = utils.PageSize20
  552. }
  553. if currentIndex <= 0 {
  554. currentIndex = 1
  555. }
  556. startSize = utils.StartIndex(currentIndex, pageSize)
  557. var startDate string
  558. var endDate string
  559. year := (dataType[:4])
  560. if strings.Contains(dataType, "Q1") {
  561. startDate = year + "-01-01"
  562. endDate = year + "-03-31"
  563. } else if strings.Contains(dataType, "Q2") {
  564. startDate = year + "-04-01"
  565. endDate = year + "-06-30"
  566. } else if strings.Contains(dataType, "Q3") {
  567. startDate = year + "-07-01"
  568. endDate = year + "-09-30"
  569. } else if strings.Contains(dataType, "Q4") {
  570. startDate = year + "-10-01"
  571. endDate = year + "-12-31"
  572. } else if strings.Contains(dataType, "H1") {
  573. startDate = year + "-01-01"
  574. endDate = year + "-06-31"
  575. } else if strings.Contains(dataType, "H2") {
  576. startDate = year + "-07-01"
  577. endDate = year + "-12-31"
  578. } else {
  579. startDate = year + "-01-01"
  580. endDate = year + "-12-31"
  581. }
  582. fmt.Println(startDate)
  583. fmt.Println(endDate)
  584. resp := new(statistic_report.RaiDataSummaryPopupTypeResp)
  585. var listResp []*statistic_report.RaiDataSummaryDetailResp
  586. var trialTotal int
  587. var condition string
  588. var pars []interface{}
  589. switch popupType {
  590. case "新增试用":
  591. var parsTry []interface{}
  592. var conditionTry string
  593. if sellerId > 0 {
  594. conditionTry += ` AND a.sys_user_id = ? `
  595. parsTry = append(parsTry, sellerId)
  596. }
  597. conditionTry += ` AND a.create_time >= ? AND a.create_time <= ? AND a.operation in ( "add","receive","apply_receive" ) AND a.sys_user_id IN ( SELECT admin_id FROM admin WHERE role_type_code IN ( 'rai_seller', 'rai_group' , 'rai_admin') ) `
  598. parsTry = append(parsTry, startDate, endDate)
  599. total, err := models.GetIncrementalCompanyCountByOperationRecordRai(conditionTry, parsTry)
  600. if err != nil {
  601. br.Msg = "获取失败"
  602. br.ErrMsg = "获取新增试用客户数量失败,Err:" + err.Error()
  603. return
  604. }
  605. trialTotal = total
  606. //列表页数据
  607. tryList, err := models.GetIncrementalCompanyListByOperationRecordRai(conditionTry, parsTry, startSize, pageSize)
  608. if err != nil {
  609. br.Msg = "获取失败"
  610. br.ErrMsg = "获取失败,Err:" + err.Error()
  611. return
  612. }
  613. if len(tryList) > 0 {
  614. for _, v := range tryList {
  615. item := new(statistic_report.RaiDataSummaryDetailResp)
  616. v.SellerName = v.SysRealName
  617. if v.Operation == "add" {
  618. item.AddType = "新建"
  619. } else if v.Operation == "receive" || v.Operation == "apply_receive" {
  620. item.AddType = "领取"
  621. }
  622. item.CompanyName = v.CompanyName
  623. item.SellerName = v.SellerName
  624. item.CreateTime = v.CreateTime
  625. listResp = append(listResp, item)
  626. }
  627. }
  628. case "新签合同":
  629. if sellerId > 0 {
  630. condition += ` AND a.seller_id_init = ? `
  631. pars = append(pars, sellerId)
  632. }
  633. condition += ` AND a.status = 1 AND a.start_date >= ? AND a.start_date <= ? `
  634. pars = append(pars, startDate, endDate)
  635. condition += ` AND a.rai_contract_type = ? `
  636. pars = append(pars, "新签合同")
  637. total, err := company.GetIncrementalNewCompanyProductMergeCount(condition, pars)
  638. if err != nil && err.Error() != utils.ErrNoRow() {
  639. br.Msg = "获取失败"
  640. br.ErrMsg = "获取失败,Err:" + err.Error()
  641. return
  642. }
  643. trialTotal = total
  644. //列表页数据
  645. tmpList, err := models.GetIncrementalCompanyMergeList(condition, pars, startSize, pageSize)
  646. if err != nil {
  647. br.Msg = "获取失败"
  648. br.ErrMsg = "获取失败,Err:" + err.Error()
  649. return
  650. }
  651. if len(tmpList) > 0 {
  652. var companyContractIds []int
  653. for _, v := range tmpList {
  654. companyContractIds = append(companyContractIds, v.CompanyContractId)
  655. }
  656. //合并合同所对应的权限
  657. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  658. if err != nil {
  659. br.Msg = "获取失败"
  660. br.ErrMsg = "获取失败,Err:" + err.Error()
  661. return
  662. }
  663. for _, v := range tmpList {
  664. item := new(statistic_report.RaiDataSummaryDetailResp)
  665. item.CompanyName = v.CompanyName
  666. item.SellerName = v.SellerNameInit
  667. item.StartDate = v.StartDate
  668. item.EndDate = v.EndDate
  669. item.Money = v.Money
  670. item.PermissionName = mappermissionName[v.CompanyContractId]
  671. listResp = append(listResp, item)
  672. }
  673. }
  674. }
  675. if len(listResp) == 0 {
  676. listResp = make([]*statistic_report.RaiDataSummaryDetailResp, 0)
  677. }
  678. resp.List = listResp
  679. page := paging.GetPaging(currentIndex, pageSize, trialTotal)
  680. resp.Paging = page
  681. br.Ret = 200
  682. br.Success = true
  683. br.Msg = "获取成功"
  684. br.Data = resp
  685. }