rai_data_summary.go 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  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. "hongze/hz_crm_api/services"
  12. cygxService "hongze/hz_crm_api/services/cygx"
  13. "hongze/hz_crm_api/utils"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // 权益数据汇总
  19. // StatisticRaiDataSummaryController 权益数据汇总基类
  20. type StatisticRaiDataSummaryController struct {
  21. controllers.BaseAuthController
  22. }
  23. // MergeCompanyList
  24. // @Title 权益数据汇总
  25. // @Description 权益数据汇总统计列表接口
  26. // @Param AdminId query string true "开拓组销售id,多个用英文逗号隔开,空字符串为全部"
  27. // @Param ServiceAdminId query string true "服务组销售id,多个用英文逗号隔开,空字符串为全部"
  28. // @Param StartYear query int false "开始日期(年份)"
  29. // @Param EndYear query int false "结束日期(年份)"
  30. // @Param DataType query string false "报表类型,枚举值:`季度`,`年度`,`半年度`"
  31. // @Param DevelopButton query int false "开拓组开关,枚举值:1:开启,0:关闭 ,默认关闭"
  32. // @Param ServerButton query int false "服务组开关,枚举值:1:开启,0:关闭 ,默认关闭"
  33. // @Param ContractButtonType query string false "开关类型,:`新签`,`续约`,`收入` 多个用英文逗号隔开, "
  34. // @Param IsExport query bool false "是否导出excel,默认是false"
  35. // @Success 200 {object} statistic_report.RaiDataSummaryListResp
  36. // @router /rai_data_summary/list [get]
  37. func (this *StatisticRaiDataSummaryController) RaiDataSummaryList() {
  38. br := new(models.BaseResponse).Init()
  39. defer func() {
  40. this.Data["json"] = br
  41. this.ServeJSON()
  42. }()
  43. sysUser := this.SysUser
  44. if sysUser == nil {
  45. br.Msg = "请登录"
  46. br.ErrMsg = "请登录,SysUser Is Empty"
  47. br.Ret = 408
  48. return
  49. }
  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.GetBool("DevelopButton")
  56. serverButton, _ := this.GetBool("ServerButton")
  57. dataTypeArr := []string{}
  58. if dataType == "季度" {
  59. dataTypeArr = []string{"Q1", "Q2", "Q3", "Q4"}
  60. } else if dataType == "半年度" {
  61. dataTypeArr = []string{"H1", "H2"}
  62. } else {
  63. dataTypeArr = []string{""}
  64. }
  65. condition := " AND role_type_code IN ('rai_seller','rai_group') AND enabled = 1 AND rai_enabled = 1 "
  66. var pars []interface{}
  67. if adminId != "" {
  68. serviceAdminId = ""
  69. condition += " AND admin_id IN (" + adminId + ") "
  70. }
  71. if serviceAdminId != "" {
  72. condition += " AND admin_id IN (" + serviceAdminId + ") "
  73. }
  74. if developButton && !serverButton && adminId == "" && serviceAdminId == "" { //开拓组筛选条件
  75. condition += " AND real_name NOT LIKE '%6%' "
  76. }
  77. if !developButton && serverButton && adminId == "" && serviceAdminId == "" { //服务组筛选条件
  78. condition += " AND real_name LIKE '%6%' "
  79. }
  80. sellerList, err := system.GetSysUserItemsOrderByCreated(condition, pars)
  81. if err != nil {
  82. br.Msg = "获取管理账号失败"
  83. br.ErrMsg = "获取管理账号失败,Err:" + err.Error()
  84. return
  85. }
  86. var sellerDevelop []*system.AdminItem // 开拓组销售
  87. var sellerService []*system.AdminItem // 服务组销售
  88. var sellerDevelopIds = make(map[int]bool) // 开拓组销售的map
  89. mapsellerId := 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. mapsellerId[v.AdminId] = true
  98. }
  99. //拼接起始时间查询
  100. startDate := fmt.Sprintf("%d-01-01", startYear)
  101. endDate := fmt.Sprintf("%d-12-31", endYear)
  102. //新签部分的数据
  103. var conditionRai string
  104. var parsRai []interface{}
  105. conditionRai = " AND a.product_id = 2 AND a.status = 1 AND a.start_date >= ? AND a.start_date <= ? AND a.rai_contract_type = '新签合同' "
  106. parsRai = append(parsRai, startDate, endDate)
  107. listRaiData, err := statistic_report.GetRaiDataSummaryList(conditionRai, parsRai)
  108. if err != nil {
  109. br.Msg = "获取数据信息失败"
  110. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  111. return
  112. }
  113. var companyIds []int
  114. for _, v := range listRaiData {
  115. companyIds = append(companyIds, v.CompanyId)
  116. }
  117. companyIds = append(companyIds, 0)
  118. //续约部分的数据
  119. var conditionInherit string
  120. var parsInherit []interface{}
  121. conditionInherit = ` AND a.product_id = 2 AND a.status = 1 AND a.inherit_end_date >= ? AND a.inherit_end_date <= ? AND a.rai_contract_type = '续约合同' AND a.company_id NOT IN (` + utils.GetOrmInReplace(len(companyIds)) + `) `
  122. parsInherit = append(parsInherit, startDate, endDate, companyIds)
  123. listInheritData, err := statistic_report.GetRaiDataSummaryInheritList(conditionInherit, parsInherit)
  124. if err != nil {
  125. br.Msg = "获取数据信息失败"
  126. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  127. return
  128. }
  129. //确认不续约、到期合同部分的数据
  130. var conditionEnd string
  131. var parsEnd []interface{}
  132. conditionEnd = " AND a.product_id = 2 AND a.status = 1 AND a.end_date >= ? AND a.end_date <= ? AND a.end_date <= ? "
  133. parsEnd = append(parsEnd, startDate, endDate, time.Now().Format(utils.FormatDate))
  134. listEndData, err := statistic_report.GetRaiDataSummaryList(conditionEnd, parsEnd)
  135. if err != nil {
  136. br.Msg = "获取数据信息失败"
  137. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  138. return
  139. }
  140. var conditionConfirm string
  141. var parsConfirm []interface{}
  142. mapNoRenewedcompanyContractIds := make(map[int]bool) //已经确定未续约的合同ID
  143. conditionConfirm = " AND company_ascribe_id != 9 "
  144. companyConfirmList, err := company.GetCompanyContractNoRenewedAscribeList(conditionConfirm, parsConfirm, 0, 0)
  145. if err != nil && err.Error() != utils.ErrNoRow() {
  146. br.Msg = "获取失败"
  147. br.ErrMsg = "获取失败,GetCompanyNoRenewedAscribeList Err:" + err.Error()
  148. return
  149. }
  150. for _, v := range companyConfirmList {
  151. mapNoRenewedcompanyContractIds[v.CompanyContractId] = true
  152. }
  153. mapAddTrialNum := make(map[string]float64) // 新增试用-(数据)
  154. mapNewContractMoney := make(map[string]float64) // 新签合同(金额)
  155. mapNewContractNum := make(map[string]int) // 新签合同(数量)
  156. mapExpiredContractMoney := make(map[string]float64) // 到期合同(金额)
  157. mapExpiredContractNum := make(map[string]int) // 到期合同(数量)
  158. mapExpiredContractCompanyNum := make(map[string]int) // 到期公司(数量)
  159. mapRenewedContractMoney := make(map[string]float64) // 续约合同(金额)
  160. mapRenewedContractNum := make(map[string]int) // 续约合同(数量)
  161. mapRenewedContractCompanyNum := make(map[string]int) // 续约公司(数量)
  162. confirmedNoRenewalContractMoney := make(map[string]float64) // 确认不续约合同(金额)
  163. confirmedNoRenewalContractNum := make(map[string]int) // 确认不续约合同(数量)
  164. mapSignedClientNum := make(map[string]int) // 签约客户(数量)
  165. mapSignedClientMoney := make(map[string]float64) // 签约客户(金额)
  166. mapInvoiceAmountMoney := make(map[string]float64) // 财务系统开票金额(金额)
  167. mapPaymentAmountMoney := make(map[string]float64) // 财务系统到款金额(金额)
  168. mapNewCustomerInvoicingMoney := make(map[string]float64) // 财务系统新客开票金额(金额)
  169. mapNewCustomerPaymentsReceivedMoney := make(map[string]float64) // 财务系统新客到款金额(金额)
  170. var keyMap string
  171. var keyMapTtoal string
  172. var keyMapTtoalServer string
  173. var keyMapCompany string
  174. //var keyMapCompanyNo string
  175. var keySigned string
  176. mapCompanyData := make(map[string]bool)
  177. //新签部分的数据
  178. for _, v := range listRaiData {
  179. if !mapsellerId[v.SellerIdLast] {
  180. continue
  181. }
  182. startDateTime := utils.StrDateToDate(v.StartDate)
  183. monthNum := startDateTime.Month()
  184. yearStr := strconv.Itoa(startDateTime.Year())
  185. if dataType == "季度" {
  186. if monthNum < 4 {
  187. yearStr += "Q1"
  188. } else if monthNum > 3 && monthNum < 7 {
  189. yearStr += "Q2"
  190. } else if monthNum > 6 && monthNum < 10 {
  191. yearStr += "Q3"
  192. } else if monthNum > 9 {
  193. yearStr += "Q4"
  194. }
  195. } else if dataType == "半年度" {
  196. if monthNum < 7 {
  197. yearStr += "H1"
  198. } else {
  199. yearStr += "H2"
  200. }
  201. }
  202. keyMap = fmt.Sprint(yearStr, "_", v.SellerIdLast)
  203. keySigned = fmt.Sprint(yearStr, "_", "_CID_", v.CompanyId, "_SID_", v.SellerIdLast)
  204. if sellerDevelopIds[v.SellerIdLast] == true {
  205. keyMapTtoal = fmt.Sprint(yearStr, "_Develop")
  206. } else {
  207. keyMapTtoal = fmt.Sprint(yearStr, "_Service")
  208. }
  209. if v.RaiContractType == "新签合同" {
  210. mapNewContractMoney[keyMap] += v.Money
  211. mapNewContractNum[keyMap]++
  212. mapNewContractMoney[keyMapTtoal] += v.Money
  213. mapNewContractNum[keyMapTtoal]++
  214. }
  215. mapSignedClientMoney[keyMap] += v.Money
  216. mapSignedClientMoney[keyMapTtoal] += v.Money
  217. if !mapCompanyData[keySigned] && v.RaiContractType == "新签合同" {
  218. mapSignedClientNum[keyMap]++
  219. mapSignedClientNum[keyMapTtoal]++
  220. mapCompanyData[keySigned] = true
  221. }
  222. }
  223. //fmt.Println(mapSignedClientNum)
  224. //续约部分的数据
  225. mapKeyMapCompanyData := make(map[string]bool)
  226. for _, v := range listInheritData {
  227. if !mapsellerId[v.SellerIdLast] {
  228. continue
  229. }
  230. startDateTime := utils.StrDateToDate(v.InheritEndDate)
  231. monthNum := startDateTime.Month()
  232. yearStr := strconv.Itoa(startDateTime.Year())
  233. if dataType == "季度" {
  234. if monthNum < 4 {
  235. yearStr += "Q1"
  236. } else if monthNum > 3 && monthNum < 7 {
  237. yearStr += "Q2"
  238. } else if monthNum > 6 && monthNum < 10 {
  239. yearStr += "Q3"
  240. } else if monthNum > 9 {
  241. yearStr += "Q4"
  242. }
  243. } else if dataType == "半年度" {
  244. if monthNum < 7 {
  245. yearStr += "H1"
  246. } else {
  247. yearStr += "H2"
  248. }
  249. }
  250. keyMap = fmt.Sprint(yearStr, "_", v.SellerIdLast)
  251. keyMapCompany = fmt.Sprint(yearStr, "_", v.SellerIdLast, "_CID_", v.CompanyId)
  252. if sellerDevelopIds[v.SellerIdLast] == true {
  253. keyMapTtoal = fmt.Sprint(yearStr, "_Develop")
  254. } else {
  255. keyMapTtoal = fmt.Sprint(yearStr, "_Service")
  256. }
  257. //续约合同 数据
  258. mapRenewedContractMoney[keyMap] += v.Money
  259. mapRenewedContractMoney[keyMapTtoal] += v.Money
  260. //一家公司同一个时间纬度,只统计一次
  261. //if !mapKeyMapCompanyData[keyMapCompany] {
  262. mapRenewedContractNum[keyMap]++
  263. mapRenewedContractNum[keyMapTtoal]++
  264. //mapKeyMapCompanyData[keyMapCompany] = true
  265. //}
  266. if !mapKeyMapCompanyData[keyMapCompany] {
  267. mapRenewedContractCompanyNum[keyMap]++
  268. mapRenewedContractCompanyNum[keyMapTtoal]++
  269. mapKeyMapCompanyData[keyMapCompany] = true
  270. }
  271. if !mapCompanyData[keySigned] && v.RaiContractType == "续约合同" {
  272. mapSignedClientNum[keyMap]++
  273. mapSignedClientNum[keyMapTtoal]++
  274. mapCompanyData[keySigned] = true
  275. }
  276. }
  277. //确认不续约、到期合同部分的数据
  278. mapKeyMapCompanyEndData := make(map[string]bool)
  279. //mapKeyMapCompanyNoData := make(map[string]bool)
  280. for _, v := range listEndData {
  281. if !mapsellerId[v.SellerIdLast] {
  282. continue
  283. }
  284. startDateTime := utils.StrDateToDate(v.EndDate)
  285. monthNum := startDateTime.Month()
  286. yearStr := strconv.Itoa(startDateTime.Year())
  287. if dataType == "季度" {
  288. if monthNum < 4 {
  289. yearStr += "Q1"
  290. } else if monthNum > 3 && monthNum < 7 {
  291. yearStr += "Q2"
  292. } else if monthNum > 6 && monthNum < 10 {
  293. yearStr += "Q3"
  294. } else if monthNum > 9 {
  295. yearStr += "Q4"
  296. }
  297. } else if dataType == "半年度" {
  298. if monthNum < 7 {
  299. yearStr += "H1"
  300. } else {
  301. yearStr += "H2"
  302. }
  303. }
  304. keyMap = fmt.Sprint(yearStr, "_", v.SellerIdLast)
  305. keyMapCompany = fmt.Sprint(yearStr, "_", v.SellerIdLast, "_CID_", v.CompanyId)
  306. //keyMapCompanyNo = fmt.Sprint(yearStr, "_", v.SellerIdLast, "_CID_NO", v.CompanyId)
  307. if sellerDevelopIds[v.SellerIdLast] == true {
  308. keyMapTtoal = fmt.Sprint(yearStr, "_Develop")
  309. } else {
  310. keyMapTtoal = fmt.Sprint(yearStr, "_Service")
  311. }
  312. //到期合同数据
  313. mapExpiredContractMoney[keyMap] += v.Money
  314. mapExpiredContractMoney[keyMapTtoal] += v.Money
  315. //一家公司同一个时间纬度,只统计一次
  316. //if !mapKeyMapCompanyEndData[keyMapCompany] {
  317. mapExpiredContractNum[keyMap]++
  318. mapExpiredContractNum[keyMapTtoal]++
  319. //mapKeyMapCompanyEndData[keyMapCompany] = true
  320. //}
  321. //一家公司同一个时间纬度,只统计一次
  322. if !mapKeyMapCompanyEndData[keyMapCompany] {
  323. mapExpiredContractCompanyNum[keyMap]++
  324. mapExpiredContractCompanyNum[keyMapTtoal]++
  325. mapKeyMapCompanyEndData[keyMapCompany] = true
  326. }
  327. if mapNoRenewedcompanyContractIds[v.CompanyContractId] { // 确认不续约合同
  328. confirmedNoRenewalContractMoney[keyMap] += v.Money
  329. confirmedNoRenewalContractMoney[keyMapTtoal] += v.Money
  330. //一家公司同一个时间纬度,只统计一次
  331. //if !mapKeyMapCompanyNoData[keyMapCompanyNo] {
  332. confirmedNoRenewalContractNum[keyMap]++
  333. confirmedNoRenewalContractNum[keyMapTtoal]++
  334. //mapKeyMapCompanyNoData[keyMapCompanyNo] = true
  335. //}
  336. }
  337. }
  338. listFmsData, err := fms.GetContractRegisterListByStartDate(startDate, endDate)
  339. if err != nil {
  340. br.Msg = "获取数据信息失败"
  341. br.ErrMsg = "获取财务系统数据信息失败,Err:" + err.Error()
  342. return
  343. }
  344. for _, v := range listFmsData {
  345. if !mapsellerId[v.RaiSellerId] {
  346. continue
  347. }
  348. startDateTime := v.StartDate
  349. monthNum := startDateTime.Month()
  350. yearStr := strconv.Itoa(startDateTime.Year())
  351. if dataType == "季度" {
  352. if monthNum < 4 {
  353. yearStr += "Q1"
  354. } else if monthNum > 3 && monthNum < 7 {
  355. yearStr += "Q2"
  356. } else if monthNum > 6 && monthNum < 10 {
  357. yearStr += "Q3"
  358. } else if monthNum > 9 {
  359. yearStr += "Q4"
  360. }
  361. } else if dataType == "半年度" {
  362. if monthNum < 7 {
  363. yearStr += "H1"
  364. } else {
  365. yearStr += "H2"
  366. }
  367. }
  368. keyMap = fmt.Sprint(yearStr, "_", v.RaiSellerId)
  369. if sellerDevelopIds[v.RaiSellerId] == true {
  370. keyMapTtoal = fmt.Sprint(yearStr, "_Develop")
  371. } else {
  372. keyMapTtoal = fmt.Sprint(yearStr, "_Service")
  373. }
  374. mapInvoiceAmountMoney[keyMap] += v.InvoicedAmount
  375. mapPaymentAmountMoney[keyMap] += v.PaymentAmount
  376. mapInvoiceAmountMoney[keyMapTtoal] += v.InvoicedAmount
  377. mapPaymentAmountMoney[keyMapTtoal] += v.PaymentAmount
  378. if v.ContractType == 1 {
  379. mapNewCustomerInvoicingMoney[keyMap] += v.InvoicedAmount
  380. mapNewCustomerPaymentsReceivedMoney[keyMap] += v.PaymentAmount
  381. mapNewCustomerInvoicingMoney[keyMapTtoal] += v.InvoicedAmount
  382. mapNewCustomerPaymentsReceivedMoney[keyMapTtoal] += v.PaymentAmount
  383. }
  384. }
  385. 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') ) `
  386. var parsTry []interface{}
  387. parsTry = append(parsTry, startDate, endDate)
  388. //列表页数据
  389. tryList, err := models.GetIncrementalCompanyListByOperationRecordRai(conditionTry, parsTry, 0, 9999)
  390. if err != nil {
  391. br.Msg = "获取失败"
  392. br.ErrMsg = "获取失败,Err:" + err.Error()
  393. return
  394. }
  395. for _, v := range tryList {
  396. startDateTime := utils.StrTimeToTime(v.CreateTime)
  397. monthNum := startDateTime.Month()
  398. yearStr := strconv.Itoa(startDateTime.Year())
  399. if dataType == "季度" {
  400. if monthNum < 4 {
  401. yearStr += "Q1"
  402. } else if monthNum > 3 && monthNum < 7 {
  403. yearStr += "Q2"
  404. } else if monthNum > 6 && monthNum < 10 {
  405. yearStr += "Q3"
  406. } else if monthNum > 9 {
  407. yearStr += "Q4"
  408. }
  409. } else if dataType == "半年度" {
  410. if monthNum < 7 {
  411. yearStr += "H1"
  412. } else {
  413. yearStr += "H2"
  414. }
  415. }
  416. keyMap = fmt.Sprint(yearStr, "_", v.SysUserId)
  417. mapAddTrialNum[keyMap]++
  418. //var keyMapTtoal string
  419. sysUserId, _ := strconv.Atoi(v.SysUserId)
  420. if sellerDevelopIds[sysUserId] == true {
  421. keyMapTtoal = fmt.Sprint(yearStr, "_Develop")
  422. } else {
  423. keyMapTtoal = fmt.Sprint(yearStr, "_Service")
  424. }
  425. mapAddTrialNum[keyMapTtoal]++
  426. }
  427. resp := new(statistic_report.RaiDataSummaryListResp)
  428. var items []*statistic_report.RaiDataSummaryResp
  429. for i := startYear; i <= endYear; i++ {
  430. //if len(dataTypeArr) > 0 {
  431. for _, Dv := range dataTypeArr {
  432. item := new(statistic_report.RaiDataSummaryResp)
  433. item.DataType = fmt.Sprint(i, Dv)
  434. keyMapTtoal = fmt.Sprint(item.DataType, "_Develop")
  435. keyMapTtoalServer = fmt.Sprint(item.DataType, "_Server")
  436. var sellerIds []string
  437. var sellerServiceIds []string
  438. for _, vS := range sellerDevelop {
  439. keyMap = fmt.Sprint(item.DataType, "_", vS.AdminId)
  440. sellerItem := new(statistic_report.RaiDataSummaryDetail)
  441. sellerItem.SellerId = strconv.Itoa(vS.AdminId)
  442. sellerIds = append(sellerIds, sellerItem.SellerId)
  443. sellerItem.SellerName = vS.RealName
  444. sellerItem.AddTrialCount = fmt.Sprint(mapAddTrialNum[keyMap])
  445. sellerItem.NewContractData = fmt.Sprint(utils.SubFloatToString(mapNewContractMoney[keyMap], 2), " / ", mapNewContractNum[keyMap]) // 新签合同(金额/数量)-(数据)
  446. sellerItem.ExpiredContractData = fmt.Sprint(utils.SubFloatToString(mapExpiredContractMoney[keyMap], 2), " / ", mapExpiredContractNum[keyMap]) //"到期合同(金额/数量)-(数据)"
  447. sellerItem.RenewedContractData = fmt.Sprint(utils.SubFloatToString(mapRenewedContractMoney[keyMap], 2), " / ", mapRenewedContractNum[keyMap]) // "续约合同(金额/数量)-(数据)"
  448. var renewalRateMoey string
  449. var renewalRateNum string
  450. if mapRenewedContractMoney[keyMap] == 0 || mapExpiredContractMoney[keyMap] == 0 {
  451. renewalRateMoey = "0%"
  452. } else {
  453. renewalRateMoey = utils.SubFloatToString(mapRenewedContractMoney[keyMap]/mapExpiredContractMoney[keyMap]*100, 2) + "%"
  454. }
  455. if mapRenewedContractCompanyNum[keyMap] == 0 || mapExpiredContractCompanyNum[keyMap] == 0 {
  456. renewalRateNum = "0%"
  457. } else {
  458. renewalRateNum = utils.SubFloatToString(float64(mapRenewedContractCompanyNum[keyMap])/float64(mapExpiredContractCompanyNum[keyMap])*100, 2) + "%"
  459. }
  460. sellerItem.RenewalRateData = fmt.Sprint(renewalRateMoey, " / ", renewalRateNum) //"续约率(金额/数量)-(数据)"
  461. sellerItem.ConfirmedNoRenewalContractData = fmt.Sprint(utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMap], 2), " / ", confirmedNoRenewalContractNum[keyMap]) //"确认不续约合同(金额/数量)-(数据)"
  462. var confirmNonRenewalRateMoey string
  463. var confirmNonRenewalRateNum string
  464. if confirmedNoRenewalContractMoney[keyMap] == 0 || mapExpiredContractMoney[keyMap] == 0 {
  465. confirmNonRenewalRateMoey = "0%"
  466. } else {
  467. confirmNonRenewalRateMoey = utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMap]/mapExpiredContractMoney[keyMap]*100, 2) + "%"
  468. }
  469. if confirmedNoRenewalContractNum[keyMap] == 0 || mapExpiredContractCompanyNum[keyMap] == 0 {
  470. confirmNonRenewalRateNum = "0%"
  471. } else {
  472. confirmNonRenewalRateNum = utils.SubFloatToString(float64(confirmedNoRenewalContractNum[keyMap])/float64(mapExpiredContractCompanyNum[keyMap])*100, 2) + "%"
  473. }
  474. sellerItem.ConfirmNonRenewalRateData = fmt.Sprint(confirmNonRenewalRateMoey, " / ", confirmNonRenewalRateNum) //确认不续约率(金额/数量)-(数据)"
  475. sellerItem.SignedClientCount = fmt.Sprint(mapSignedClientNum[keyMap]) // 签约客户数量
  476. if mapSignedClientNum[keyMap] == 0 || mapSignedClientMoney[keyMap] == 0 {
  477. sellerItem.AverageRevenueCount = "0"
  478. } else {
  479. sellerItem.AverageRevenueCount = utils.SubFloatToString(mapSignedClientMoney[keyMap]/float64(mapSignedClientNum[keyMap]), 2) //客单价
  480. }
  481. sellerItem.InvoiceAmountCount = utils.SubFloatToString(mapInvoiceAmountMoney[keyMap], 2) //"开票金额-(数据)"
  482. sellerItem.PaymentReceivedCount = utils.SubFloatToString(mapPaymentAmountMoney[keyMap], 2) //"开票金额-(数据)"
  483. if mapInvoiceAmountMoney[keyMap] == 0 || mapPaymentAmountMoney[keyMap] == 0 {
  484. sellerItem.UnpaidRatioCount = "0%"
  485. } else {
  486. sellerItem.UnpaidRatioCount = utils.SubFloatToString((mapInvoiceAmountMoney[keyMap]-mapPaymentAmountMoney[keyMap])/mapInvoiceAmountMoney[keyMap]*100, 2) + "%" //"未到款比例-(数据)"
  487. }
  488. sellerItem.NewCustomerInvoicingCount = utils.SubFloatToString(mapNewCustomerInvoicingMoney[keyMap], 2) // "新客开票-(数据)"
  489. sellerItem.NewCustomerPaymentsReceivedCount = utils.SubFloatToString(mapNewCustomerPaymentsReceivedMoney[keyMap], 2) // "新客到款-(数据)"
  490. if developButton || serverButton || adminId != "" {
  491. item.DataList = append(item.DataList, sellerItem)
  492. }
  493. }
  494. if len(sellerDevelop) > 0 {
  495. sellerItem := new(statistic_report.RaiDataSummaryDetail)
  496. sellerItem.SellerId = strings.Join(sellerIds, ",")
  497. sellerItem.SellerName = "开拓组合计"
  498. sellerItem.AddTrialCount = fmt.Sprint(mapAddTrialNum[keyMapTtoal])
  499. sellerItem.NewContractData = fmt.Sprint(utils.SubFloatToString(mapNewContractMoney[keyMapTtoal], 2), " / ", mapNewContractNum[keyMapTtoal]) // 新签合同(金额/数量)-(数据)
  500. sellerItem.ExpiredContractData = fmt.Sprint(utils.SubFloatToString(mapExpiredContractMoney[keyMapTtoal], 2), " / ", mapExpiredContractNum[keyMapTtoal]) //"到期合同(金额/数量)-(数据)"
  501. sellerItem.RenewedContractData = fmt.Sprint(utils.SubFloatToString(mapRenewedContractMoney[keyMapTtoal], 2), " / ", mapRenewedContractNum[keyMapTtoal]) // "续约合同(金额/数量)-(数据)"
  502. var renewalRateMoey string
  503. var renewalRateNum string
  504. if mapRenewedContractMoney[keyMapTtoal] == 0 || mapExpiredContractMoney[keyMapTtoal] == 0 {
  505. renewalRateMoey = "0%"
  506. } else {
  507. renewalRateMoey = utils.SubFloatToString(mapRenewedContractMoney[keyMapTtoal]/mapExpiredContractMoney[keyMapTtoal]*100, 2) + "%"
  508. }
  509. if mapRenewedContractCompanyNum[keyMapTtoal] == 0 || mapExpiredContractCompanyNum[keyMapTtoal] == 0 {
  510. renewalRateNum = "0%"
  511. } else {
  512. renewalRateNum = utils.SubFloatToString(float64(mapRenewedContractCompanyNum[keyMapTtoal])/float64(mapExpiredContractCompanyNum[keyMapTtoal])*100, 2) + "%"
  513. }
  514. sellerItem.RenewalRateData = fmt.Sprint(renewalRateMoey, " / ", renewalRateNum) //"续约率(金额/数量)-(数据)"
  515. sellerItem.ConfirmedNoRenewalContractData = fmt.Sprint(utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMapTtoal], 2), " / ", confirmedNoRenewalContractNum[keyMapTtoal]) //"确认不续约合同(金额/数量)-(数据)"
  516. var confirmNonRenewalRateMoey string
  517. var confirmNonRenewalRateNum string
  518. if confirmedNoRenewalContractMoney[keyMapTtoal] == 0 || mapExpiredContractMoney[keyMapTtoal] == 0 {
  519. confirmNonRenewalRateMoey = "0%"
  520. } else {
  521. confirmNonRenewalRateMoey = utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMapTtoal]/mapExpiredContractMoney[keyMapTtoal]*100, 2) + "%"
  522. }
  523. if confirmedNoRenewalContractNum[keyMapTtoal] == 0 || mapExpiredContractCompanyNum[keyMapTtoal] == 0 {
  524. confirmNonRenewalRateNum = "0%"
  525. } else {
  526. confirmNonRenewalRateNum = utils.SubFloatToString(float64(confirmedNoRenewalContractNum[keyMapTtoal])/float64(mapExpiredContractCompanyNum[keyMapTtoal])*100, 2) + "%"
  527. }
  528. sellerItem.ConfirmNonRenewalRateData = fmt.Sprint(confirmNonRenewalRateMoey, " / ", confirmNonRenewalRateNum) //确认不续约率(金额/数量)-(数据)"
  529. sellerItem.SignedClientCount = fmt.Sprint(mapSignedClientNum[keyMapTtoal]) // 签约客户数量
  530. if mapSignedClientNum[keyMapTtoal] == 0 || mapSignedClientMoney[keyMapTtoal] == 0 {
  531. sellerItem.AverageRevenueCount = "0"
  532. } else {
  533. sellerItem.AverageRevenueCount = utils.SubFloatToString(mapSignedClientMoney[keyMapTtoal]/float64(mapSignedClientNum[keyMapTtoal]), 2) //客单价
  534. }
  535. sellerItem.InvoiceAmountCount = utils.SubFloatToString(mapInvoiceAmountMoney[keyMapTtoal], 2) //"开票金额-(数据)"
  536. sellerItem.PaymentReceivedCount = utils.SubFloatToString(mapPaymentAmountMoney[keyMapTtoal], 2) //"开票金额-(数据)"
  537. if mapInvoiceAmountMoney[keyMapTtoal] == 0 || mapPaymentAmountMoney[keyMapTtoal] == 0 {
  538. sellerItem.UnpaidRatioCount = "0%"
  539. } else {
  540. sellerItem.UnpaidRatioCount = utils.SubFloatToString((mapInvoiceAmountMoney[keyMapTtoal]-mapPaymentAmountMoney[keyMapTtoal])/mapInvoiceAmountMoney[keyMapTtoal]*100, 2) + "%" //"未到款比例-(数据)"
  541. }
  542. sellerItem.NewCustomerInvoicingCount = utils.SubFloatToString(mapNewCustomerInvoicingMoney[keyMapTtoal], 2) // "新客开票-(数据)"
  543. sellerItem.NewCustomerPaymentsReceivedCount = utils.SubFloatToString(mapNewCustomerPaymentsReceivedMoney[keyMapTtoal], 2) // "新客到款-(数据)"
  544. item.DataList = append(item.DataList, sellerItem)
  545. }
  546. for _, vS := range sellerService {
  547. sellerItem := new(statistic_report.RaiDataSummaryDetail)
  548. sellerItem.SellerId = strconv.Itoa(vS.AdminId)
  549. sellerServiceIds = append(sellerServiceIds, sellerItem.SellerId)
  550. sellerItem.SellerName = vS.RealName
  551. sellerItem.AddTrialCount = fmt.Sprint(mapAddTrialNum[keyMap])
  552. sellerItem.NewContractData = fmt.Sprint(utils.SubFloatToString(mapNewContractMoney[keyMap], 2), " / ", mapNewContractNum[keyMap]) // 新签合同(金额/数量)-(数据)
  553. sellerItem.ExpiredContractData = fmt.Sprint(utils.SubFloatToString(mapExpiredContractMoney[keyMap], 2), " / ", mapExpiredContractNum[keyMap]) //"到期合同(金额/数量)-(数据)"
  554. sellerItem.RenewedContractData = fmt.Sprint(utils.SubFloatToString(mapRenewedContractMoney[keyMap], 2), " / ", mapRenewedContractNum[keyMap]) // "续约合同(金额/数量)-(数据)"
  555. var renewalRateMoey string
  556. var renewalRateNum string
  557. if mapRenewedContractMoney[keyMap] == 0 || mapExpiredContractMoney[keyMap] == 0 {
  558. renewalRateMoey = "0%"
  559. } else {
  560. renewalRateMoey = utils.SubFloatToString(mapRenewedContractMoney[keyMap]/mapExpiredContractMoney[keyMap]*100, 2) + "%"
  561. }
  562. if mapRenewedContractCompanyNum[keyMap] == 0 || mapExpiredContractCompanyNum[keyMap] == 0 {
  563. renewalRateNum = "0%"
  564. } else {
  565. renewalRateNum = utils.SubFloatToString(float64(mapRenewedContractCompanyNum[keyMap])/float64(mapExpiredContractCompanyNum[keyMap])*100, 2) + "%"
  566. }
  567. sellerItem.RenewalRateData = fmt.Sprint(renewalRateMoey, " / ", renewalRateNum) //"续约率(金额/数量)-(数据)"
  568. sellerItem.ConfirmedNoRenewalContractData = fmt.Sprint(utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMap], 2), " / ", confirmedNoRenewalContractNum[keyMap]) //"确认不续约合同(金额/数量)-(数据)"
  569. var confirmNonRenewalRateMoey string
  570. var confirmNonRenewalRateNum string
  571. if confirmedNoRenewalContractMoney[keyMap] == 0 || mapExpiredContractMoney[keyMap] == 0 {
  572. confirmNonRenewalRateMoey = "0%"
  573. } else {
  574. confirmNonRenewalRateMoey = utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMap]/mapExpiredContractMoney[keyMap]*100, 2) + "%"
  575. }
  576. if confirmedNoRenewalContractNum[keyMap] == 0 || mapExpiredContractCompanyNum[keyMap] == 0 {
  577. confirmNonRenewalRateNum = "0%"
  578. } else {
  579. confirmNonRenewalRateNum = utils.SubFloatToString(float64(confirmedNoRenewalContractNum[keyMap])/float64(mapExpiredContractCompanyNum[keyMap])*100, 2) + "%"
  580. }
  581. sellerItem.ConfirmNonRenewalRateData = fmt.Sprint(confirmNonRenewalRateMoey, " / ", confirmNonRenewalRateNum) //确认不续约率(金额/数量)-(数据)"
  582. sellerItem.SignedClientCount = fmt.Sprint(mapSignedClientNum[keyMap]) // 签约客户数量
  583. if mapSignedClientNum[keyMap] == 0 || mapSignedClientMoney[keyMap] == 0 {
  584. sellerItem.AverageRevenueCount = "0"
  585. } else {
  586. sellerItem.AverageRevenueCount = utils.SubFloatToString(mapSignedClientMoney[keyMap]/float64(mapSignedClientNum[keyMap]), 2) //客单价
  587. }
  588. sellerItem.InvoiceAmountCount = utils.SubFloatToString(mapInvoiceAmountMoney[keyMap], 2) //"开票金额-(数据)"
  589. sellerItem.PaymentReceivedCount = utils.SubFloatToString(mapPaymentAmountMoney[keyMap], 2) //"开票金额-(数据)"
  590. if mapInvoiceAmountMoney[keyMap] == 0 || mapPaymentAmountMoney[keyMap] == 0 {
  591. sellerItem.UnpaidRatioCount = "0%"
  592. } else {
  593. sellerItem.UnpaidRatioCount = utils.SubFloatToString((mapInvoiceAmountMoney[keyMap]-mapPaymentAmountMoney[keyMap])/mapInvoiceAmountMoney[keyMap]*100, 2) + "%" //"未到款比例-(数据)"
  594. }
  595. sellerItem.NewCustomerInvoicingCount = utils.SubFloatToString(mapNewCustomerInvoicingMoney[keyMap], 2) // "新客开票-(数据)"
  596. sellerItem.NewCustomerPaymentsReceivedCount = utils.SubFloatToString(mapNewCustomerPaymentsReceivedMoney[keyMap], 2) // "新客到款-(数据)"
  597. if developButton || serverButton || serviceAdminId != "" {
  598. item.DataList = append(item.DataList, sellerItem)
  599. }
  600. }
  601. if len(sellerService) > 0 {
  602. sellerItem := new(statistic_report.RaiDataSummaryDetail)
  603. sellerItem.SellerId = strings.Join(sellerServiceIds, ",")
  604. sellerItem.SellerName = "服务组合计"
  605. sellerItem.AddTrialCount = fmt.Sprint(mapAddTrialNum[keyMapTtoalServer])
  606. sellerItem.NewContractData = fmt.Sprint(utils.SubFloatToString(mapNewContractMoney[keyMapTtoalServer], 2), " / ", mapNewContractNum[keyMapTtoalServer]) // 新签合同(金额/数量)-(数据)
  607. sellerItem.ExpiredContractData = fmt.Sprint(utils.SubFloatToString(mapExpiredContractMoney[keyMapTtoalServer], 2), " / ", mapExpiredContractNum[keyMapTtoalServer]) //"到期合同(金额/数量)-(数据)"
  608. sellerItem.RenewedContractData = fmt.Sprint(utils.SubFloatToString(mapRenewedContractMoney[keyMapTtoalServer], 2), " / ", mapRenewedContractNum[keyMapTtoalServer]) // "续约合同(金额/数量)-(数据)"
  609. var renewalRateMoey string
  610. var renewalRateNum string
  611. if mapRenewedContractMoney[keyMapTtoalServer] == 0 || mapExpiredContractMoney[keyMapTtoalServer] == 0 {
  612. renewalRateMoey = "0%"
  613. } else {
  614. renewalRateMoey = utils.SubFloatToString(mapRenewedContractMoney[keyMapTtoalServer]/mapExpiredContractMoney[keyMapTtoalServer]*100, 2) + "%"
  615. }
  616. if mapRenewedContractCompanyNum[keyMapTtoalServer] == 0 || mapExpiredContractCompanyNum[keyMapTtoalServer] == 0 {
  617. renewalRateNum = "0%"
  618. } else {
  619. renewalRateNum = utils.SubFloatToString(float64(mapRenewedContractCompanyNum[keyMapTtoalServer])/float64(mapExpiredContractCompanyNum[keyMapTtoalServer])*100, 2) + "%"
  620. }
  621. sellerItem.RenewalRateData = fmt.Sprint(renewalRateMoey, " / ", renewalRateNum) //"续约率(金额/数量)-(数据)"
  622. sellerItem.ConfirmedNoRenewalContractData = fmt.Sprint(utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMapTtoalServer], 2), " / ", confirmedNoRenewalContractNum[keyMapTtoalServer]) //"确认不续约合同(金额/数量)-(数据)"
  623. var confirmNonRenewalRateMoey string
  624. var confirmNonRenewalRateNum string
  625. if confirmedNoRenewalContractMoney[keyMapTtoalServer] == 0 || mapExpiredContractMoney[keyMapTtoalServer] == 0 {
  626. confirmNonRenewalRateMoey = "0%"
  627. } else {
  628. confirmNonRenewalRateMoey = utils.SubFloatToString(confirmedNoRenewalContractMoney[keyMapTtoalServer]/mapExpiredContractMoney[keyMapTtoalServer]*100, 2) + "%"
  629. }
  630. if confirmedNoRenewalContractNum[keyMapTtoalServer] == 0 || mapExpiredContractCompanyNum[keyMapTtoalServer] == 0 {
  631. confirmNonRenewalRateNum = "0%"
  632. } else {
  633. confirmNonRenewalRateNum = utils.SubFloatToString(float64(confirmedNoRenewalContractNum[keyMapTtoalServer])/float64(mapExpiredContractCompanyNum[keyMapTtoalServer])*100, 2) + "%"
  634. }
  635. sellerItem.ConfirmNonRenewalRateData = fmt.Sprint(confirmNonRenewalRateMoey, " / ", confirmNonRenewalRateNum) //确认不续约率(金额/数量)-(数据)"
  636. sellerItem.SignedClientCount = fmt.Sprint(mapSignedClientNum[keyMapTtoalServer]) // 签约客户数量
  637. if mapSignedClientNum[keyMapTtoalServer] == 0 || mapSignedClientMoney[keyMapTtoalServer] == 0 {
  638. sellerItem.AverageRevenueCount = "0"
  639. } else {
  640. sellerItem.AverageRevenueCount = utils.SubFloatToString(mapSignedClientMoney[keyMapTtoalServer]/float64(mapSignedClientNum[keyMapTtoalServer]), 2) //客单价
  641. }
  642. sellerItem.InvoiceAmountCount = utils.SubFloatToString(mapInvoiceAmountMoney[keyMapTtoalServer], 2) //"开票金额-(数据)"
  643. sellerItem.PaymentReceivedCount = utils.SubFloatToString(mapPaymentAmountMoney[keyMapTtoalServer], 2) //"开票金额-(数据)"
  644. if mapInvoiceAmountMoney[keyMapTtoalServer] == 0 || mapPaymentAmountMoney[keyMapTtoalServer] == 0 {
  645. sellerItem.UnpaidRatioCount = "0%"
  646. } else {
  647. sellerItem.UnpaidRatioCount = utils.SubFloatToString((mapInvoiceAmountMoney[keyMapTtoalServer]-mapPaymentAmountMoney[keyMapTtoalServer])/mapInvoiceAmountMoney[keyMapTtoalServer]*100, 2) + "%" //"未到款比例-(数据)"
  648. }
  649. sellerItem.NewCustomerInvoicingCount = utils.SubFloatToString(mapNewCustomerInvoicingMoney[keyMapTtoalServer], 2) // "新客开票-(数据)"
  650. sellerItem.NewCustomerPaymentsReceivedCount = utils.SubFloatToString(mapNewCustomerPaymentsReceivedMoney[keyMapTtoalServer], 2) // "新客到款-(数据)"
  651. item.DataList = append(item.DataList, sellerItem)
  652. }
  653. items = append(items, item)
  654. }
  655. }
  656. resp.List = items
  657. br.Ret = 200
  658. br.Success = true
  659. br.Msg = "获取成功"
  660. br.Data = resp
  661. }
  662. // MergeCompanyList
  663. // @Title 权益数据汇总弹窗详情
  664. // @Description 权益数据汇总弹窗详情接口
  665. // @Param SellerId query int true "销售ID"
  666. // @Param DataType query string false "报表类型,枚举值:`季度`,`年度`,`半年度`"
  667. // @Param PopupType query string false "弹窗数据类型,枚举值:"
  668. // @Param PageSize query int true "每页数据条数"
  669. // @Param CurrentIndex query int true "当前页页码,从1开始"
  670. // @Success 200 {object} statistic_report.RaiDataSummaryPopupTypeResp
  671. // @router /rai_data_summary/detail [get]
  672. func (this *StatisticRaiDataSummaryController) RaiDataSummaryDetail() {
  673. br := new(models.BaseResponse).Init()
  674. defer func() {
  675. this.Data["json"] = br
  676. this.ServeJSON()
  677. }()
  678. sysUser := this.SysUser
  679. if sysUser == nil {
  680. br.Msg = "请登录"
  681. br.ErrMsg = "请登录,SysUser Is Empty"
  682. br.Ret = 408
  683. return
  684. }
  685. pageSize, _ := this.GetInt("PageSize")
  686. currentIndex, _ := this.GetInt("CurrentIndex")
  687. dataType := this.GetString("DataType")
  688. sellerId := this.GetString("SellerId")
  689. popupType := this.GetString("PopupType")
  690. var startSize int
  691. if pageSize <= 0 {
  692. pageSize = utils.PageSize20
  693. }
  694. if currentIndex <= 0 {
  695. currentIndex = 1
  696. }
  697. startSize = utils.StartIndex(currentIndex, pageSize)
  698. var startDate string
  699. var endDate string
  700. year := (dataType[:4])
  701. if strings.Contains(dataType, "Q1") {
  702. startDate = year + "-01-01"
  703. endDate = year + "-03-31"
  704. } else if strings.Contains(dataType, "Q2") {
  705. startDate = year + "-04-01"
  706. endDate = year + "-06-30"
  707. } else if strings.Contains(dataType, "Q3") {
  708. startDate = year + "-07-01"
  709. endDate = year + "-09-30"
  710. } else if strings.Contains(dataType, "Q4") {
  711. startDate = year + "-10-01"
  712. endDate = year + "-12-31"
  713. } else if strings.Contains(dataType, "H1") {
  714. startDate = year + "-01-01"
  715. endDate = year + "-06-31"
  716. } else if strings.Contains(dataType, "H2") {
  717. startDate = year + "-07-01"
  718. endDate = year + "-12-31"
  719. } else {
  720. startDate = year + "-01-01"
  721. endDate = year + "-12-31"
  722. }
  723. resp := new(statistic_report.RaiDataSummaryPopupTypeResp)
  724. var listResp []*statistic_report.RaiDataSummaryDetailResp
  725. var listGroup []*models.CompanyContractGroupList
  726. var trialTotal int
  727. var condition string
  728. var pars []interface{}
  729. //新签合同的客户ID
  730. var companyIdsNew []int
  731. {
  732. var conditionNew string
  733. var parsNew []interface{}
  734. if sellerId != "" {
  735. conditionNew += ` AND a.sys_user_id IN (` + sellerId + `) `
  736. }
  737. conditionNew = " AND a.product_id = 2 AND a.status = 1 AND a.start_date >= ? AND a.start_date <= ? AND a.rai_contract_type = '新签合同' "
  738. parsNew = append(parsNew, startDate, endDate)
  739. listNewData, err := statistic_report.GetRaiDataSummaryList(conditionNew, parsNew)
  740. if err != nil {
  741. br.Msg = "获取数据信息失败"
  742. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  743. return
  744. }
  745. for _, v := range listNewData {
  746. companyIdsNew = append(companyIdsNew, v.CompanyId)
  747. }
  748. }
  749. companyIdsNew = append(companyIdsNew, 0)
  750. lenArrCompany := len(companyIdsNew)
  751. switch popupType {
  752. case "新增试用":
  753. var parsTry []interface{}
  754. var conditionTry string
  755. if sellerId != "" {
  756. conditionTry += ` AND a.sys_user_id IN (` + sellerId + `) `
  757. }
  758. 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') ) `
  759. parsTry = append(parsTry, startDate, endDate)
  760. total, err := models.GetIncrementalCompanyCountByOperationRecordRai(conditionTry, parsTry)
  761. if err != nil {
  762. br.Msg = "获取失败"
  763. br.ErrMsg = "获取新增试用客户数量失败,Err:" + err.Error()
  764. return
  765. }
  766. trialTotal = total
  767. //列表页数据
  768. tryList, err := models.GetIncrementalCompanyListByOperationRecordRai(conditionTry, parsTry, startSize, pageSize)
  769. if err != nil {
  770. br.Msg = "获取失败"
  771. br.ErrMsg = "获取失败,Err:" + err.Error()
  772. return
  773. }
  774. if len(tryList) > 0 {
  775. for _, v := range tryList {
  776. item := new(statistic_report.RaiDataSummaryDetailResp)
  777. v.SellerName = v.SysRealName
  778. if v.Operation == "add" {
  779. item.AddType = "新建"
  780. } else if v.Operation == "receive" || v.Operation == "apply_receive" {
  781. item.AddType = "领取"
  782. }
  783. item.CompanyName = v.CompanyName
  784. item.SellerName = v.SellerName
  785. item.CreateTime = v.CreateTime
  786. listResp = append(listResp, item)
  787. }
  788. }
  789. case "新签合同":
  790. if sellerId != "" {
  791. condition += ` AND a.seller_id_init IN (` + sellerId + `) `
  792. }
  793. condition += ` AND a.status = 1 AND a.start_date >= ? AND a.start_date <= ? `
  794. pars = append(pars, startDate, endDate)
  795. condition += ` AND a.rai_contract_type = ? `
  796. pars = append(pars, "新签合同")
  797. total, err := company.GetIncrementalNewCompanyProductMergeCount(condition, pars)
  798. if err != nil && err.Error() != utils.ErrNoRow() {
  799. br.Msg = "获取失败"
  800. br.ErrMsg = "获取失败,Err:" + err.Error()
  801. return
  802. }
  803. trialTotal = total
  804. //列表页数据
  805. tmpList, err := models.GetIncrementalCompanyMergeList(condition, pars, startSize, pageSize)
  806. if err != nil {
  807. br.Msg = "获取失败"
  808. br.ErrMsg = "获取失败,Err:" + err.Error()
  809. return
  810. }
  811. if len(tmpList) > 0 {
  812. var companyContractIds []int
  813. for _, v := range tmpList {
  814. companyContractIds = append(companyContractIds, v.CompanyContractId)
  815. }
  816. //合并合同所对应的权限
  817. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  818. if err != nil {
  819. br.Msg = "获取失败"
  820. br.ErrMsg = "获取失败,Err:" + err.Error()
  821. return
  822. }
  823. for _, v := range tmpList {
  824. item := new(statistic_report.RaiDataSummaryDetailResp)
  825. item.CompanyName = v.CompanyName
  826. item.SellerName = v.SellerNameInit
  827. item.StartDate = v.StartDate
  828. item.EndDate = v.EndDate
  829. item.Money = v.Money
  830. item.PermissionName = mappermissionName[v.CompanyContractId]
  831. listResp = append(listResp, item)
  832. }
  833. listGroup, err = models.GetCompanyContractGroupList(condition, pars)
  834. if err != nil {
  835. br.Msg = "获取失败"
  836. br.ErrMsg = "获取失败,Err:" + err.Error()
  837. return
  838. }
  839. }
  840. case "到期合同":
  841. if sellerId != "" {
  842. condition += ` AND a.seller_id_last IN (` + sellerId + `) `
  843. }
  844. condition += ` AND a.status = 1 AND a.end_date >= ? AND a.end_date <= ? AND a.end_date < ? `
  845. pars = append(pars, startDate, endDate, time.Now().Format(utils.FormatDate))
  846. total, err := company.GetIncrementalNewCompanyProductMergeCount(condition, pars)
  847. if err != nil && err.Error() != utils.ErrNoRow() {
  848. br.Msg = "获取失败"
  849. br.ErrMsg = "获取失败,Err:" + err.Error()
  850. return
  851. }
  852. trialTotal = total
  853. //列表页数据
  854. tmpList, err := models.GetIncrementalCompanyMergeList(condition, pars, startSize, pageSize)
  855. if err != nil {
  856. br.Msg = "获取失败"
  857. br.ErrMsg = "获取失败,Err:" + err.Error()
  858. return
  859. }
  860. if len(tmpList) > 0 {
  861. var companyContractIds []int
  862. for _, v := range tmpList {
  863. companyContractIds = append(companyContractIds, v.CompanyContractId)
  864. }
  865. //合并合同所对应的权限
  866. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  867. if err != nil {
  868. br.Msg = "获取失败"
  869. br.ErrMsg = "获取失败,Err:" + err.Error()
  870. return
  871. }
  872. for _, v := range tmpList {
  873. item := new(statistic_report.RaiDataSummaryDetailResp)
  874. item.CompanyName = v.CompanyName
  875. item.SellerName = v.SellerNameLast
  876. item.StartDate = v.StartDate
  877. item.EndDate = v.EndDate
  878. item.Money = v.Money
  879. item.PermissionName = mappermissionName[v.CompanyContractId]
  880. listResp = append(listResp, item)
  881. }
  882. listGroup, err = models.GetCompanyContractGroupList(condition, pars)
  883. if err != nil {
  884. br.Msg = "获取失败"
  885. br.ErrMsg = "获取失败,Err:" + err.Error()
  886. return
  887. }
  888. }
  889. case "续约合同":
  890. if sellerId != "" {
  891. condition += ` AND a.seller_id_last IN (` + sellerId + `) `
  892. }
  893. condition += ` AND a.status = 1 AND a.inherit_end_date >= ? AND a.inherit_end_date <= ? AND a.inherit_company_contract_id > 0 AND a.rai_contract_type = '续约合同' AND a.company_id NOT IN (` + utils.GetOrmInReplace(lenArrCompany) + `) `
  894. pars = append(pars, startDate, endDate, companyIdsNew)
  895. total, err := company.GetIncrementalNewCompanyProductMergeCount(condition, pars)
  896. if err != nil && err.Error() != utils.ErrNoRow() {
  897. br.Msg = "获取失败"
  898. br.ErrMsg = "获取失败,Err:" + err.Error()
  899. return
  900. }
  901. trialTotal = total
  902. //列表页数据
  903. tmpList, err := models.GetIncrementalCompanyMergeList(condition, pars, startSize, pageSize)
  904. if err != nil {
  905. br.Msg = "获取失败"
  906. br.ErrMsg = "获取失败,Err:" + err.Error()
  907. return
  908. }
  909. if len(tmpList) > 0 {
  910. var companyContractIds []int
  911. for _, v := range tmpList {
  912. companyContractIds = append(companyContractIds, v.CompanyContractId)
  913. }
  914. //合并合同所对应的权限
  915. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  916. if err != nil {
  917. br.Msg = "获取失败"
  918. br.ErrMsg = "获取失败,Err:" + err.Error()
  919. return
  920. }
  921. for _, v := range tmpList {
  922. item := new(statistic_report.RaiDataSummaryDetailResp)
  923. item.CompanyName = v.CompanyName
  924. item.SellerName = v.SellerNameLast
  925. item.StartDate = v.StartDate
  926. item.EndDate = v.EndDate
  927. item.Money = v.Money
  928. item.PermissionName = mappermissionName[v.CompanyContractId]
  929. listResp = append(listResp, item)
  930. }
  931. listGroup, err = models.GetCompanyContractGroupList(condition, pars)
  932. if err != nil {
  933. br.Msg = "获取失败"
  934. br.ErrMsg = "获取失败,Err:" + err.Error()
  935. return
  936. }
  937. }
  938. case "续约率":
  939. var conditionEnd string
  940. var parsEnd []interface{}
  941. if sellerId != "" {
  942. condition += ` AND a.seller_id_last IN (` + sellerId + `) `
  943. conditionEnd += ` AND a.seller_id_last IN (` + sellerId + `) `
  944. }
  945. condition += ` AND a.status = 1 AND a.inherit_end_date >= ? AND a.inherit_end_date <= ? AND inherit_company_contract_id > 0 AND a.rai_contract_type = '续约合同' AND a.company_id NOT IN (` + utils.GetOrmInReplace(lenArrCompany) + `) `
  946. pars = append(pars, startDate, endDate, companyIdsNew)
  947. listRaiData, err := statistic_report.GetRaiDataSummaryList(condition, pars)
  948. if err != nil {
  949. br.Msg = "获取数据信息失败"
  950. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  951. return
  952. }
  953. //fmt.Println("listRaiData", len(listRaiData))
  954. //到期合同数据
  955. conditionEnd += ` AND a.status = 1 AND a.end_date >= ? AND a.end_date <= ? AND a.end_date < ? `
  956. parsEnd = append(parsEnd, startDate, endDate, time.Now().Format(utils.FormatDate))
  957. listEndData, err := statistic_report.GetRaiDataSummaryList(conditionEnd, parsEnd)
  958. if err != nil {
  959. br.Msg = "获取数据信息失败"
  960. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  961. return
  962. }
  963. var renewedContractMoney float64 // 续约金额
  964. var renewedContractCompany float64 // 续约客户数
  965. maprenewedContractCompany := make(map[int]bool)
  966. var expiredContractMoney float64 //到期金额
  967. var expiredContractCompany float64 // 到期客户数
  968. mapexpiredContractCompany := make(map[int]bool)
  969. for _, v := range listRaiData {
  970. renewedContractMoney += v.Money
  971. if !maprenewedContractCompany[v.CompanyId] {
  972. renewedContractCompany++
  973. maprenewedContractCompany[v.CompanyId] = true
  974. }
  975. //startDateTime := utils.StrDateToDate(v.StartDate)
  976. //if startDateTime.Before(time.Now().AddDate(0, 0, -1)) { //到期合同数据
  977. // expiredContractMoney += v.Money
  978. // if !mapexpiredContractCompany[v.CompanyId] {
  979. // expiredContractCompany++
  980. // mapexpiredContractCompany[v.CompanyId] = true
  981. // }
  982. //}
  983. }
  984. for _, v := range listEndData {
  985. expiredContractMoney += v.Money
  986. if !mapexpiredContractCompany[v.CompanyId] {
  987. expiredContractCompany++
  988. mapexpiredContractCompany[v.CompanyId] = true
  989. }
  990. }
  991. item := new(statistic_report.RaiDataSummaryDetailResp)
  992. item.TbaleNameAText = "金额续约率"
  993. item.RenewedContractMoney = fmt.Sprint(utils.SubFloatToString(renewedContractMoney, 2))
  994. item.ExpiredContractMoney = fmt.Sprint(utils.SubFloatToString(expiredContractMoney, 2))
  995. if renewedContractMoney == 0 || expiredContractMoney == 0 {
  996. item.RenewalRate = "0%"
  997. } else {
  998. item.RenewalRate = fmt.Sprint(utils.SubFloatToString(renewedContractMoney/expiredContractMoney*100, 2), "%")
  999. }
  1000. listResp = append(listResp, item)
  1001. item2 := new(statistic_report.RaiDataSummaryDetailResp)
  1002. item2.TbaleNameAText = "客户续约率"
  1003. item2.RenewedContractMoney = fmt.Sprint(renewedContractCompany)
  1004. item2.ExpiredContractMoney = fmt.Sprint(expiredContractCompany)
  1005. if renewedContractCompany == 0 || expiredContractCompany == 0 {
  1006. item2.RenewalRate = "0%"
  1007. } else {
  1008. item2.RenewalRate = fmt.Sprint(utils.SubFloatToString(renewedContractCompany/expiredContractCompany*100, 2), "%")
  1009. }
  1010. listResp = append(listResp, item2)
  1011. case "确认不续约合同":
  1012. noRenewedcompanyContractIds := services.GetCompanyContractNoRenewedAscribeListArr()
  1013. condition += ` AND a.company_contract_id IN (` + utils.GetOrmInReplace(len(noRenewedcompanyContractIds)) + `) ` // 已确认
  1014. pars = append(pars, noRenewedcompanyContractIds)
  1015. if sellerId != "" {
  1016. condition += ` AND a.seller_id_last IN (` + sellerId + `) `
  1017. }
  1018. condition += ` AND a.product_id = 2 AND a.status = 1 AND a.end_date >= ? AND a.end_date <= ? AND a.end_date <= ? `
  1019. pars = append(pars, startDate, endDate, time.Now().Format(utils.FormatDate))
  1020. total, err := company.GetIncrementalNewCompanyProductMergeCount(condition, pars)
  1021. if err != nil && err.Error() != utils.ErrNoRow() {
  1022. br.Msg = "获取失败"
  1023. br.ErrMsg = "获取失败,Err:" + err.Error()
  1024. return
  1025. }
  1026. trialTotal = total
  1027. //列表页数据
  1028. tmpList, err := models.GetIncrementalCompanyMergeList(condition, pars, startSize, pageSize)
  1029. if err != nil {
  1030. br.Msg = "获取失败"
  1031. br.ErrMsg = "获取失败,Err:" + err.Error()
  1032. return
  1033. }
  1034. if len(tmpList) > 0 {
  1035. var companyContractIds []int
  1036. for _, v := range tmpList {
  1037. companyContractIds = append(companyContractIds, v.CompanyContractId)
  1038. }
  1039. //合并合同所对应的权限
  1040. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  1041. if err != nil {
  1042. br.Msg = "获取失败"
  1043. br.ErrMsg = "获取失败,Err:" + err.Error()
  1044. return
  1045. }
  1046. for _, v := range tmpList {
  1047. item := new(statistic_report.RaiDataSummaryDetailResp)
  1048. item.CompanyName = v.CompanyName
  1049. item.SellerName = v.SellerNameInit
  1050. item.StartDate = v.StartDate
  1051. item.EndDate = v.EndDate
  1052. item.Money = v.Money
  1053. item.PermissionName = mappermissionName[v.CompanyContractId]
  1054. listResp = append(listResp, item)
  1055. }
  1056. listGroup, err = models.GetCompanyContractGroupList(condition, pars)
  1057. if err != nil {
  1058. br.Msg = "获取失败"
  1059. br.ErrMsg = "获取失败,Err:" + err.Error()
  1060. return
  1061. }
  1062. }
  1063. case "确认不续约率":
  1064. noRenewedcompanyContractIdsMap := services.GetCompanyContractNoRenewedAscribeListMap()
  1065. var conditionEnd string
  1066. var parsEnd []interface{}
  1067. if sellerId != "" {
  1068. condition += ` AND a.seller_id_last IN (` + sellerId + `) `
  1069. conditionEnd += ` AND a.seller_id_last IN (` + sellerId + `) `
  1070. }
  1071. condition += ` AND a.product_id = 2 AND a.status = 1 AND a.end_date >= ? AND a.end_date <= ? AND a.end_date <= ? `
  1072. pars = append(pars, startDate, endDate, time.Now().Format(utils.FormatDate))
  1073. listRaiData, err := statistic_report.GetRaiDataSummaryList(condition, pars)
  1074. if err != nil {
  1075. br.Msg = "获取数据信息失败"
  1076. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1077. return
  1078. }
  1079. conditionEnd += ` AND a.product_id = 2 AND a.status = 1 AND a.end_date >= ? AND a.end_date <= ? AND a.end_date < ? `
  1080. parsEnd = append(parsEnd, startDate, endDate, time.Now().Format(utils.FormatDate))
  1081. listEndData, err := statistic_report.GetRaiDataSummaryList(conditionEnd, parsEnd)
  1082. if err != nil {
  1083. br.Msg = "获取数据信息失败"
  1084. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1085. return
  1086. }
  1087. var norenewedContractMoney float64 // 不续约金额
  1088. var norenewedContractCompany float64 // 不续约客户数
  1089. mapnorenewedContractCompany := make(map[int]bool)
  1090. var expiredContractMoney float64 //到期金额
  1091. var expiredContractCompany float64 // 到期客户数
  1092. mapexpiredContractCompany := make(map[int]bool)
  1093. for _, v := range listRaiData {
  1094. if noRenewedcompanyContractIdsMap[v.CompanyContractId] {
  1095. norenewedContractMoney += v.Money
  1096. if !mapnorenewedContractCompany[v.CompanyId] {
  1097. norenewedContractCompany++
  1098. mapnorenewedContractCompany[v.CompanyId] = true
  1099. }
  1100. }
  1101. }
  1102. for _, v := range listEndData {
  1103. expiredContractMoney += v.Money
  1104. if !mapexpiredContractCompany[v.CompanyId] {
  1105. expiredContractCompany++
  1106. mapexpiredContractCompany[v.CompanyId] = true
  1107. }
  1108. }
  1109. item := new(statistic_report.RaiDataSummaryDetailResp)
  1110. item.TbaleNameAText = "金额不续约率"
  1111. item.RenewedContractMoney = fmt.Sprint(utils.SubFloatToString(norenewedContractMoney, 2))
  1112. item.ExpiredContractMoney = fmt.Sprint(utils.SubFloatToString(expiredContractMoney, 2))
  1113. if norenewedContractMoney == 0 || expiredContractMoney == 0 {
  1114. item.RenewalRate = "0%"
  1115. } else {
  1116. item.RenewalRate = fmt.Sprint(utils.SubFloatToString(norenewedContractMoney/expiredContractMoney*100, 2), "%")
  1117. }
  1118. listResp = append(listResp, item)
  1119. item2 := new(statistic_report.RaiDataSummaryDetailResp)
  1120. item2.TbaleNameAText = "客户不续约率"
  1121. item2.RenewedContractMoney = fmt.Sprint(norenewedContractCompany)
  1122. item2.ExpiredContractMoney = fmt.Sprint(expiredContractCompany)
  1123. if norenewedContractCompany == 0 || expiredContractCompany == 0 {
  1124. item2.RenewalRate = "0%"
  1125. } else {
  1126. item2.RenewalRate = fmt.Sprint(utils.SubFloatToString(norenewedContractCompany/expiredContractCompany*100, 2), "%")
  1127. }
  1128. listResp = append(listResp, item2)
  1129. case "签约客户数量":
  1130. var conditionInherit string
  1131. var parsInherit []interface{}
  1132. if sellerId != "" {
  1133. condition += ` AND a.seller_id_last IN (` + sellerId + `) `
  1134. conditionInherit += ` AND a.seller_id_last IN (` + sellerId + `) `
  1135. }
  1136. condition += " AND a.product_id = 2 AND a.status = 1 AND a.start_date >= ? AND a.start_date <= ? AND a.rai_contract_type = '新签合同' "
  1137. pars = append(pars, startDate, endDate)
  1138. listRaiData, err := statistic_report.GetRaiDataSummaryList(condition, pars)
  1139. if err != nil {
  1140. br.Msg = "获取数据信息失败"
  1141. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1142. return
  1143. }
  1144. //续约部分的数据
  1145. conditionInherit += ` AND a.product_id = 2 AND a.status = 1 AND a.inherit_end_date >= ? AND a.inherit_end_date <= ? AND a.inherit_company_contract_id > 0 AND a.rai_contract_type = '续约合同' AND a.company_id NOT IN (` + utils.GetOrmInReplace(lenArrCompany) + `) `
  1146. parsInherit = append(parsInherit, startDate, endDate, companyIdsNew)
  1147. listInheritData, err := statistic_report.GetRaiDataSummaryInheritList(conditionInherit, parsInherit)
  1148. if err != nil {
  1149. br.Msg = "获取数据信息失败"
  1150. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1151. return
  1152. }
  1153. fmt.Println("listInheritData", len(listInheritData))
  1154. var renewedContractCompany int // 续约客户数
  1155. var renewedContract int // 续约合同数
  1156. maprenewedContractCompany := make(map[int]bool)
  1157. var newContractCompany int // 新签客户数
  1158. var newContract int // 新签合同数
  1159. mapenewContractCompany := make(map[int]bool)
  1160. for _, v := range listRaiData {
  1161. if v.RaiContractType == "新签合同" {
  1162. newContract++
  1163. if !mapenewContractCompany[v.CompanyId] {
  1164. newContractCompany++
  1165. mapenewContractCompany[v.CompanyId] = true
  1166. }
  1167. }
  1168. //else if v.RaiContractType == "续约合同" {
  1169. // renewedContract++
  1170. // if !maprenewedContractCompany[v.CompanyId] {
  1171. // renewedContractCompany++
  1172. // maprenewedContractCompany[v.CompanyId] = true
  1173. // }
  1174. //}
  1175. }
  1176. for _, v := range listInheritData {
  1177. renewedContract++
  1178. if !maprenewedContractCompany[v.CompanyId] {
  1179. renewedContractCompany++
  1180. maprenewedContractCompany[v.CompanyId] = true
  1181. }
  1182. }
  1183. item := new(statistic_report.RaiDataSummaryDetailResp)
  1184. item.RenewedContractCompany = renewedContractCompany
  1185. item.RenewedContract = renewedContract
  1186. item.NewContractCompany = newContractCompany
  1187. item.NewContract = newContract
  1188. listResp = append(listResp, item)
  1189. case "客单价":
  1190. //续约部分的数据
  1191. var conditionInherit string
  1192. var parsInherit []interface{}
  1193. if sellerId != "" {
  1194. condition += ` AND a.seller_id_last IN (` + sellerId + `) `
  1195. conditionInherit += ` AND a.seller_id_last IN (` + sellerId + `) `
  1196. }
  1197. condition += " AND a.product_id = 2 AND a.status = 1 AND a.start_date >= ? AND a.start_date <= ? AND a.rai_contract_type = '新签合同' "
  1198. pars = append(pars, startDate, endDate)
  1199. listRaiData, err := statistic_report.GetRaiDataSummaryList(condition, pars)
  1200. if err != nil {
  1201. br.Msg = "获取数据信息失败"
  1202. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1203. return
  1204. }
  1205. var ontractMoney float64 // 签约总金额
  1206. var contractNum int // 签约客户数
  1207. mapCompany := make(map[int]bool)
  1208. for _, v := range listRaiData {
  1209. if v.RaiContractType != "新签合同" {
  1210. continue
  1211. }
  1212. ontractMoney += v.Money
  1213. if !mapCompany[v.CompanyId] {
  1214. contractNum++
  1215. mapCompany[v.CompanyId] = true
  1216. }
  1217. }
  1218. conditionInherit += ` AND a.product_id = 2 AND a.status = 1 AND a.inherit_end_date >= ? AND a.inherit_end_date <= ? AND a.rai_contract_type = '续约合同' AND a.company_id NOT IN (` + utils.GetOrmInReplace(lenArrCompany) + `) `
  1219. parsInherit = append(parsInherit, startDate, endDate, companyIdsNew)
  1220. listInheritData, err := statistic_report.GetRaiDataSummaryInheritList(conditionInherit, parsInherit)
  1221. if err != nil {
  1222. br.Msg = "获取数据信息失败"
  1223. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1224. return
  1225. }
  1226. for _, v := range listInheritData {
  1227. ontractMoney += v.Money
  1228. if !mapCompany[v.CompanyId] && v.RaiContractType == "续约合同" {
  1229. contractNum++
  1230. mapCompany[v.CompanyId] = true
  1231. }
  1232. }
  1233. item := new(statistic_report.RaiDataSummaryDetailResp)
  1234. item.ContractMoney = fmt.Sprint(utils.SubFloatToString(ontractMoney, 2))
  1235. item.ContractNum = contractNum
  1236. listResp = append(listResp, item)
  1237. case "开票金额":
  1238. if sellerId != "" {
  1239. condition += ` AND a.rai_seller_id IN (` + sellerId + `) `
  1240. }
  1241. condition += " AND a.is_deleted = 0 AND a.start_date >= ? AND a.start_date <= ? AND invoiced_amount > 0 AND product_ids LIKE '%2%' "
  1242. pars = append(pars, startDate, endDate)
  1243. listFmsData, err := fms.GetContractRegisterList(condition, pars, startSize, pageSize)
  1244. if err != nil {
  1245. br.Msg = "获取数据信息失败"
  1246. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1247. return
  1248. }
  1249. if len(listFmsData) > 0 {
  1250. var contractRegisterId []int
  1251. for _, v := range listFmsData {
  1252. contractRegisterId = append(contractRegisterId, v.ContractRegisterId)
  1253. }
  1254. lenArr := len(contractRegisterId)
  1255. var conditionFms string
  1256. var parsFms []interface{}
  1257. conditionFms += ` AND contract_register_id IN (` + utils.GetOrmInReplace(lenArr) + `) GROUP BY contract_register_id `
  1258. parsFms = append(parsFms, contractRegisterId)
  1259. listInvoiceData, err := fms.GetContractInvoiceList(conditionFms, parsFms, 0, lenArr)
  1260. if err != nil {
  1261. br.Msg = "获取数据信息失败"
  1262. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1263. return
  1264. }
  1265. mapInvoiceTime := make(map[int]string)
  1266. for _, v := range listInvoiceData {
  1267. mapInvoiceTime[v.ContractRegisterId] = v.InvoiceTime.Format(utils.FormatDate)
  1268. }
  1269. for _, v := range listFmsData {
  1270. item := new(statistic_report.RaiDataSummaryDetailResp)
  1271. item.CompanyName = v.CompanyName
  1272. item.SellerName = v.RaiSellerName
  1273. item.ContractCode = v.ContractCode
  1274. item.InvoicedAmount = v.InvoicedAmount
  1275. item.CreateTime = mapInvoiceTime[v.ContractRegisterId]
  1276. listResp = append(listResp, item)
  1277. }
  1278. }
  1279. case "到款金额":
  1280. if sellerId != "" {
  1281. condition += ` AND a.rai_seller_id IN (` + sellerId + `) `
  1282. }
  1283. condition += " AND a.is_deleted = 0 AND a.start_date >= ? AND a.start_date <= ? AND payment_amount > 0 AND product_ids LIKE '%2%' "
  1284. pars = append(pars, startDate, endDate)
  1285. listFmsData, err := fms.GetContractRegisterList(condition, pars, startSize, pageSize)
  1286. if err != nil {
  1287. br.Msg = "获取数据信息失败"
  1288. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1289. return
  1290. }
  1291. if len(listFmsData) > 0 {
  1292. var contractRegisterId []int
  1293. for _, v := range listFmsData {
  1294. contractRegisterId = append(contractRegisterId, v.ContractRegisterId)
  1295. }
  1296. lenArr := len(contractRegisterId)
  1297. var conditionFms string
  1298. var parsFms []interface{}
  1299. conditionFms += ` AND contract_register_id IN (` + utils.GetOrmInReplace(lenArr) + `) GROUP BY contract_register_id `
  1300. parsFms = append(parsFms, contractRegisterId)
  1301. listInvoiceData, err := fms.GetContractInvoiceList(conditionFms, parsFms, 0, lenArr)
  1302. if err != nil {
  1303. br.Msg = "获取数据信息失败"
  1304. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1305. return
  1306. }
  1307. mapInvoiceTime := make(map[int]string)
  1308. for _, v := range listInvoiceData {
  1309. mapInvoiceTime[v.ContractRegisterId] = v.InvoiceTime.Format(utils.FormatDate)
  1310. }
  1311. for _, v := range listFmsData {
  1312. item := new(statistic_report.RaiDataSummaryDetailResp)
  1313. item.CompanyName = v.CompanyName
  1314. item.SellerName = v.RaiSellerName
  1315. item.ContractCode = v.ContractCode
  1316. item.PaymentAmount = v.PaymentAmount
  1317. item.CreateTime = v.CreateTime.Format(utils.FormatDate)
  1318. listResp = append(listResp, item)
  1319. }
  1320. }
  1321. case "未到款比例":
  1322. if sellerId != "" {
  1323. condition += ` AND a.rai_seller_id IN (` + sellerId + `) `
  1324. }
  1325. condition += " AND a.is_deleted = 0 AND a.start_date >= ? AND a.start_date <= ? AND product_ids LIKE '%2%' "
  1326. pars = append(pars, startDate, endDate)
  1327. listFmsData, err := fms.GetContractRegisterAmountList(condition, pars)
  1328. if err != nil {
  1329. br.Msg = "获取数据信息失败"
  1330. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1331. return
  1332. }
  1333. for _, v := range listFmsData {
  1334. item := new(statistic_report.RaiDataSummaryDetailResp)
  1335. item.InvoicedAmount = v.InvoicedAmount
  1336. item.PaymentAmount = v.PaymentAmount
  1337. item.NotReceivedtAmount = v.InvoicedAmount - v.PaymentAmount
  1338. item.CreateTime = v.CreateTime.Format(utils.FormatDate)
  1339. listResp = append(listResp, item)
  1340. }
  1341. case "新客开票":
  1342. if sellerId != "" {
  1343. condition += ` AND a.rai_seller_id IN (` + sellerId + `) `
  1344. }
  1345. condition += " AND a.is_deleted = 0 AND contract_type = 1 AND a.start_date >= ? AND a.start_date <= ? AND product_ids LIKE '%2%' "
  1346. pars = append(pars, startDate, endDate)
  1347. listFmsData, err := fms.GetContractRegisterList(condition, pars, startSize, pageSize)
  1348. if err != nil {
  1349. br.Msg = "获取数据信息失败"
  1350. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1351. return
  1352. }
  1353. if len(listFmsData) > 0 {
  1354. var contractRegisterId []int
  1355. for _, v := range listFmsData {
  1356. contractRegisterId = append(contractRegisterId, v.ContractRegisterId)
  1357. }
  1358. lenArr := len(contractRegisterId)
  1359. var conditionFms string
  1360. var parsFms []interface{}
  1361. conditionFms += ` AND contract_register_id IN (` + utils.GetOrmInReplace(lenArr) + `) GROUP BY contract_register_id `
  1362. parsFms = append(parsFms, contractRegisterId)
  1363. listInvoiceData, err := fms.GetContractInvoiceList(conditionFms, parsFms, 0, lenArr)
  1364. if err != nil {
  1365. br.Msg = "获取数据信息失败"
  1366. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1367. return
  1368. }
  1369. mapInvoiceTime := make(map[int]string)
  1370. for _, v := range listInvoiceData {
  1371. mapInvoiceTime[v.ContractRegisterId] = v.InvoiceTime.Format(utils.FormatDate)
  1372. }
  1373. for _, v := range listFmsData {
  1374. item := new(statistic_report.RaiDataSummaryDetailResp)
  1375. item.CompanyName = v.CompanyName
  1376. item.SellerName = v.RaiSellerName
  1377. item.ContractCode = v.ContractCode
  1378. item.InvoicedAmount = v.InvoicedAmount
  1379. item.CreateTime = mapInvoiceTime[v.ContractRegisterId]
  1380. listResp = append(listResp, item)
  1381. }
  1382. }
  1383. case "新客到款":
  1384. if sellerId != "" {
  1385. condition += ` AND a.rai_seller_id IN (` + sellerId + `) `
  1386. }
  1387. condition += " AND a.is_deleted = 0 AND contract_type = 1 AND a.start_date >= ? AND a.start_date <= ? AND payment_amount > 0 AND product_ids LIKE '%2%' "
  1388. pars = append(pars, startDate, endDate)
  1389. listFmsData, err := fms.GetContractRegisterList(condition, pars, startSize, pageSize)
  1390. if err != nil {
  1391. br.Msg = "获取数据信息失败"
  1392. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1393. return
  1394. }
  1395. if len(listFmsData) > 0 {
  1396. var contractRegisterId []int
  1397. for _, v := range listFmsData {
  1398. contractRegisterId = append(contractRegisterId, v.ContractRegisterId)
  1399. }
  1400. lenArr := len(contractRegisterId)
  1401. var conditionFms string
  1402. var parsFms []interface{}
  1403. conditionFms += ` AND contract_register_id IN (` + utils.GetOrmInReplace(lenArr) + `) GROUP BY contract_register_id `
  1404. parsFms = append(parsFms, contractRegisterId)
  1405. listInvoiceData, err := fms.GetContractInvoiceList(conditionFms, parsFms, 0, lenArr)
  1406. if err != nil {
  1407. br.Msg = "获取数据信息失败"
  1408. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1409. return
  1410. }
  1411. mapInvoiceTime := make(map[int]string)
  1412. for _, v := range listInvoiceData {
  1413. mapInvoiceTime[v.ContractRegisterId] = v.InvoiceTime.Format(utils.FormatDate)
  1414. }
  1415. for _, v := range listFmsData {
  1416. item := new(statistic_report.RaiDataSummaryDetailResp)
  1417. item.CompanyName = v.CompanyName
  1418. item.SellerName = v.RaiSellerName
  1419. item.ContractCode = v.ContractCode
  1420. item.PaymentAmount = v.PaymentAmount
  1421. item.CreateTime = mapInvoiceTime[v.ContractRegisterId]
  1422. listResp = append(listResp, item)
  1423. }
  1424. }
  1425. }
  1426. if len(listResp) == 0 {
  1427. listResp = make([]*statistic_report.RaiDataSummaryDetailResp, 0)
  1428. }
  1429. if len(listGroup) > 0 {
  1430. for _, v := range listGroup {
  1431. if v.CompanyCount > 1 {
  1432. resp.CompanyMultiple++
  1433. }
  1434. }
  1435. resp.CompanyNum = len(listGroup)
  1436. }
  1437. resp.List = listResp
  1438. page := paging.GetPaging(currentIndex, pageSize, trialTotal)
  1439. resp.Paging = page
  1440. br.Ret = 200
  1441. br.Success = true
  1442. br.Msg = "获取成功"
  1443. br.Data = resp
  1444. }