rai_data_summary.go 64 KB

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