rai_data_summary.go 71 KB

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