rai_data_summary.go 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  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.CompanyId = v.CompanyId
  792. item.CompanyName = v.CompanyName
  793. item.SellerName = v.SellerName
  794. item.CreateTime = v.CreateTime
  795. listResp = append(listResp, item)
  796. }
  797. }
  798. case "新签合同":
  799. if sellerId != "" {
  800. condition += ` AND a.seller_id_init IN (` + sellerId + `) `
  801. }
  802. condition += ` AND a.status = 1 AND a.start_date >= ? AND a.start_date <= ? `
  803. pars = append(pars, startDate, endDate)
  804. condition += ` AND a.rai_contract_type = ? `
  805. pars = append(pars, "新签合同")
  806. total, err := company.GetIncrementalNewCompanyProductMergeCount(condition, pars)
  807. if err != nil && err.Error() != utils.ErrNoRow() {
  808. br.Msg = "获取失败"
  809. br.ErrMsg = "获取失败,Err:" + err.Error()
  810. return
  811. }
  812. trialTotal = total
  813. //列表页数据
  814. tmpList, err := models.GetIncrementalCompanyMergeList(condition, pars, startSize, pageSize)
  815. if err != nil {
  816. br.Msg = "获取失败"
  817. br.ErrMsg = "获取失败,Err:" + err.Error()
  818. return
  819. }
  820. if len(tmpList) > 0 {
  821. var companyContractIds []int
  822. for _, v := range tmpList {
  823. companyContractIds = append(companyContractIds, v.CompanyContractId)
  824. }
  825. //合并合同所对应的权限
  826. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  827. if err != nil {
  828. br.Msg = "获取失败"
  829. br.ErrMsg = "获取失败,Err:" + err.Error()
  830. return
  831. }
  832. for _, v := range tmpList {
  833. item := new(statistic_report.RaiDataSummaryDetailResp)
  834. item.CompanyId = v.CompanyId
  835. item.CompanyName = v.CompanyName
  836. item.SellerName = v.SellerNameInit
  837. item.StartDate = v.StartDate
  838. item.EndDate = v.EndDate
  839. item.Money = v.Money
  840. item.PermissionName = mappermissionName[v.CompanyContractId]
  841. listResp = append(listResp, item)
  842. }
  843. listGroup, err = models.GetCompanyContractGroupList(condition, pars)
  844. if err != nil {
  845. br.Msg = "获取失败"
  846. br.ErrMsg = "获取失败,Err:" + err.Error()
  847. return
  848. }
  849. }
  850. case "到期合同":
  851. if sellerId != "" {
  852. condition += ` AND a.seller_id_last IN (` + sellerId + `) `
  853. }
  854. condition += ` AND a.status = 1 AND a.end_date >= ? AND a.end_date <= ? AND a.end_date < ? `
  855. pars = append(pars, startDate, endDate, time.Now().Format(utils.FormatDate))
  856. total, err := company.GetIncrementalNewCompanyProductMergeCount(condition, pars)
  857. if err != nil && err.Error() != utils.ErrNoRow() {
  858. br.Msg = "获取失败"
  859. br.ErrMsg = "获取失败,Err:" + err.Error()
  860. return
  861. }
  862. trialTotal = total
  863. //列表页数据
  864. tmpList, err := models.GetIncrementalCompanyMergeListEnd(condition, pars, startSize, pageSize)
  865. if err != nil {
  866. br.Msg = "获取失败"
  867. br.ErrMsg = "获取失败,Err:" + err.Error()
  868. return
  869. }
  870. if len(tmpList) > 0 {
  871. var companyContractIds []int
  872. for _, v := range tmpList {
  873. companyContractIds = append(companyContractIds, v.CompanyContractId)
  874. }
  875. //合并合同所对应的权限
  876. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  877. if err != nil {
  878. br.Msg = "获取失败"
  879. br.ErrMsg = "获取失败,Err:" + err.Error()
  880. return
  881. }
  882. for _, v := range tmpList {
  883. item := new(statistic_report.RaiDataSummaryDetailResp)
  884. item.CompanyId = v.CompanyId
  885. item.CompanyName = v.CompanyName
  886. item.SellerName = v.SellerNameLast
  887. item.StartDate = v.StartDate
  888. item.EndDate = v.EndDate
  889. item.Money = v.Money
  890. item.PermissionName = mappermissionName[v.CompanyContractId]
  891. listResp = append(listResp, item)
  892. }
  893. listGroup, err = models.GetCompanyContractGroupList(condition, pars)
  894. if err != nil {
  895. br.Msg = "获取失败"
  896. br.ErrMsg = "获取失败,Err:" + err.Error()
  897. return
  898. }
  899. }
  900. case "续约合同":
  901. if sellerId != "" {
  902. condition += ` AND a.seller_id_last IN (` + sellerId + `) `
  903. }
  904. 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) + `) `
  905. pars = append(pars, startDate, endDate, companyIdsNew)
  906. total, err := company.GetIncrementalNewCompanyProductMergeCount(condition, pars)
  907. if err != nil && err.Error() != utils.ErrNoRow() {
  908. br.Msg = "获取失败"
  909. br.ErrMsg = "获取失败,Err:" + err.Error()
  910. return
  911. }
  912. trialTotal = total
  913. //列表页数据
  914. tmpList, err := models.GetIncrementalCompanyMergeList(condition, pars, startSize, pageSize)
  915. if err != nil {
  916. br.Msg = "获取失败"
  917. br.ErrMsg = "获取失败,Err:" + err.Error()
  918. return
  919. }
  920. if len(tmpList) > 0 {
  921. var companyContractIds []int
  922. for _, v := range tmpList {
  923. companyContractIds = append(companyContractIds, v.CompanyContractId)
  924. }
  925. //合并合同所对应的权限
  926. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  927. if err != nil {
  928. br.Msg = "获取失败"
  929. br.ErrMsg = "获取失败,Err:" + err.Error()
  930. return
  931. }
  932. for _, v := range tmpList {
  933. item := new(statistic_report.RaiDataSummaryDetailResp)
  934. item.CompanyId = v.CompanyId
  935. item.CompanyName = v.CompanyName
  936. item.SellerName = v.SellerNameLast
  937. item.StartDate = v.StartDate
  938. item.EndDate = v.EndDate
  939. item.Money = v.Money
  940. item.PermissionName = mappermissionName[v.CompanyContractId]
  941. listResp = append(listResp, item)
  942. }
  943. listGroup, err = models.GetCompanyContractGroupList(condition, pars)
  944. if err != nil {
  945. br.Msg = "获取失败"
  946. br.ErrMsg = "获取失败,Err:" + err.Error()
  947. return
  948. }
  949. }
  950. case "续约率":
  951. var conditionEnd string
  952. var parsEnd []interface{}
  953. if sellerId != "" {
  954. condition += ` AND a.seller_id_last IN (` + sellerId + `) `
  955. conditionEnd += ` AND a.seller_id_last IN (` + sellerId + `) `
  956. }
  957. 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) + `) `
  958. pars = append(pars, startDate, endDate, companyIdsNew)
  959. listRaiData, err := statistic_report.GetRaiDataSummaryList(condition, pars)
  960. if err != nil {
  961. br.Msg = "获取数据信息失败"
  962. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  963. return
  964. }
  965. //到期合同数据
  966. conditionEnd += ` AND a.status = 1 AND a.end_date >= ? AND a.end_date <= ? AND a.end_date < ? `
  967. parsEnd = append(parsEnd, startDate, endDate, time.Now().Format(utils.FormatDate))
  968. listEndData, err := statistic_report.GetRaiDataSummaryList(conditionEnd, parsEnd)
  969. if err != nil {
  970. br.Msg = "获取数据信息失败"
  971. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  972. return
  973. }
  974. var renewedContractMoney float64 // 续约金额
  975. var renewedContractCompany float64 // 续约客户数
  976. maprenewedContractCompany := make(map[int]bool)
  977. var expiredContractMoney float64 //到期金额
  978. var expiredContractCompany float64 // 到期客户数
  979. mapexpiredContractCompany := make(map[int]bool)
  980. for _, v := range listRaiData {
  981. renewedContractMoney += v.Money
  982. if !maprenewedContractCompany[v.CompanyId] {
  983. renewedContractCompany++
  984. maprenewedContractCompany[v.CompanyId] = true
  985. }
  986. //startDateTime := utils.StrDateToDate(v.StartDate)
  987. //if startDateTime.Before(time.Now().AddDate(0, 0, -1)) { //到期合同数据
  988. // expiredContractMoney += v.Money
  989. // if !mapexpiredContractCompany[v.CompanyId] {
  990. // expiredContractCompany++
  991. // mapexpiredContractCompany[v.CompanyId] = true
  992. // }
  993. //}
  994. }
  995. for _, v := range listEndData {
  996. expiredContractMoney += v.Money
  997. if !mapexpiredContractCompany[v.CompanyId] {
  998. expiredContractCompany++
  999. mapexpiredContractCompany[v.CompanyId] = true
  1000. }
  1001. }
  1002. item := new(statistic_report.RaiDataSummaryDetailResp)
  1003. item.TbaleNameAText = "金额续约率"
  1004. item.RenewedContractMoney = fmt.Sprint(utils.SubFloatToString(renewedContractMoney, 2))
  1005. item.ExpiredContractMoney = fmt.Sprint(utils.SubFloatToString(expiredContractMoney, 2))
  1006. if renewedContractMoney == 0 || expiredContractMoney == 0 {
  1007. item.RenewalRate = "0%"
  1008. } else {
  1009. item.RenewalRate = fmt.Sprint(utils.SubFloatToString(renewedContractMoney/expiredContractMoney*100, 2), "%")
  1010. }
  1011. listResp = append(listResp, item)
  1012. item2 := new(statistic_report.RaiDataSummaryDetailResp)
  1013. item2.TbaleNameAText = "客户续约率"
  1014. item2.RenewedContractMoney = fmt.Sprint(renewedContractCompany)
  1015. item2.ExpiredContractMoney = fmt.Sprint(expiredContractCompany)
  1016. if renewedContractCompany == 0 || expiredContractCompany == 0 {
  1017. item2.RenewalRate = "0%"
  1018. } else {
  1019. item2.RenewalRate = fmt.Sprint(utils.SubFloatToString(renewedContractCompany/expiredContractCompany*100, 2), "%")
  1020. }
  1021. listResp = append(listResp, item2)
  1022. case "确认不续约合同":
  1023. noRenewedcompanyContractIds := services.GetCompanyContractNoRenewedAscribeListArr()
  1024. condition += ` AND a.company_contract_id IN (` + utils.GetOrmInReplace(len(noRenewedcompanyContractIds)) + `) ` // 已确认
  1025. pars = append(pars, noRenewedcompanyContractIds)
  1026. if sellerId != "" {
  1027. condition += ` AND a.seller_id_last IN (` + sellerId + `) `
  1028. }
  1029. condition += ` AND a.product_id = 2 AND a.status = 1 AND a.end_date >= ? AND a.end_date <= ? AND a.end_date <= ? `
  1030. pars = append(pars, startDate, endDate, time.Now().Format(utils.FormatDate))
  1031. total, err := company.GetIncrementalNewCompanyProductMergeCount(condition, pars)
  1032. if err != nil && err.Error() != utils.ErrNoRow() {
  1033. br.Msg = "获取失败"
  1034. br.ErrMsg = "获取失败,Err:" + err.Error()
  1035. return
  1036. }
  1037. trialTotal = total
  1038. //列表页数据
  1039. tmpList, err := models.GetIncrementalCompanyMergeList(condition, pars, startSize, pageSize)
  1040. if err != nil {
  1041. br.Msg = "获取失败"
  1042. br.ErrMsg = "获取失败,Err:" + err.Error()
  1043. return
  1044. }
  1045. if len(tmpList) > 0 {
  1046. var companyContractIds []int
  1047. for _, v := range tmpList {
  1048. companyContractIds = append(companyContractIds, v.CompanyContractId)
  1049. }
  1050. //合并合同所对应的权限
  1051. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  1052. if err != nil {
  1053. br.Msg = "获取失败"
  1054. br.ErrMsg = "获取失败,Err:" + err.Error()
  1055. return
  1056. }
  1057. for _, v := range tmpList {
  1058. item := new(statistic_report.RaiDataSummaryDetailResp)
  1059. item.CompanyId = v.CompanyId
  1060. item.CompanyName = v.CompanyName
  1061. item.SellerName = v.SellerNameInit
  1062. item.StartDate = v.StartDate
  1063. item.EndDate = v.EndDate
  1064. item.Money = v.Money
  1065. item.PermissionName = mappermissionName[v.CompanyContractId]
  1066. listResp = append(listResp, item)
  1067. }
  1068. listGroup, err = models.GetCompanyContractGroupList(condition, pars)
  1069. if err != nil {
  1070. br.Msg = "获取失败"
  1071. br.ErrMsg = "获取失败,Err:" + err.Error()
  1072. return
  1073. }
  1074. }
  1075. case "确认不续约率":
  1076. noRenewedcompanyContractIdsMap := services.GetCompanyContractNoRenewedAscribeListMap()
  1077. var conditionEnd string
  1078. var parsEnd []interface{}
  1079. if sellerId != "" {
  1080. condition += ` AND a.seller_id_last IN (` + sellerId + `) `
  1081. conditionEnd += ` AND a.seller_id_last IN (` + sellerId + `) `
  1082. }
  1083. condition += ` AND a.product_id = 2 AND a.status = 1 AND a.end_date >= ? AND a.end_date <= ? AND a.end_date <= ? `
  1084. pars = append(pars, startDate, endDate, time.Now().Format(utils.FormatDate))
  1085. listRaiData, err := statistic_report.GetRaiDataSummaryList(condition, pars)
  1086. if err != nil {
  1087. br.Msg = "获取数据信息失败"
  1088. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1089. return
  1090. }
  1091. conditionEnd += ` AND a.product_id = 2 AND a.status = 1 AND a.end_date >= ? AND a.end_date <= ? AND a.end_date < ? `
  1092. parsEnd = append(parsEnd, startDate, endDate, time.Now().Format(utils.FormatDate))
  1093. listEndData, err := statistic_report.GetRaiDataSummaryList(conditionEnd, parsEnd)
  1094. if err != nil {
  1095. br.Msg = "获取数据信息失败"
  1096. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1097. return
  1098. }
  1099. var norenewedContractMoney float64 // 不续约金额
  1100. var norenewedContractCompany float64 // 不续约客户数
  1101. mapnorenewedContractCompany := make(map[int]bool)
  1102. var expiredContractMoney float64 //到期金额
  1103. var expiredContractCompany float64 // 到期客户数
  1104. mapexpiredContractCompany := make(map[int]bool)
  1105. for _, v := range listRaiData {
  1106. if noRenewedcompanyContractIdsMap[v.CompanyContractId] {
  1107. norenewedContractMoney += v.Money
  1108. if !mapnorenewedContractCompany[v.CompanyId] {
  1109. norenewedContractCompany++
  1110. mapnorenewedContractCompany[v.CompanyId] = true
  1111. }
  1112. }
  1113. }
  1114. for _, v := range listEndData {
  1115. expiredContractMoney += v.Money
  1116. if !mapexpiredContractCompany[v.CompanyId] {
  1117. expiredContractCompany++
  1118. mapexpiredContractCompany[v.CompanyId] = true
  1119. }
  1120. }
  1121. item := new(statistic_report.RaiDataSummaryDetailResp)
  1122. item.TbaleNameAText = "金额不续约率"
  1123. item.RenewedContractMoney = fmt.Sprint(utils.SubFloatToString(norenewedContractMoney, 2))
  1124. item.ExpiredContractMoney = fmt.Sprint(utils.SubFloatToString(expiredContractMoney, 2))
  1125. if norenewedContractMoney == 0 || expiredContractMoney == 0 {
  1126. item.RenewalRate = "0%"
  1127. } else {
  1128. item.RenewalRate = fmt.Sprint(utils.SubFloatToString(norenewedContractMoney/expiredContractMoney*100, 2), "%")
  1129. }
  1130. listResp = append(listResp, item)
  1131. item2 := new(statistic_report.RaiDataSummaryDetailResp)
  1132. item2.TbaleNameAText = "客户不续约率"
  1133. item2.RenewedContractMoney = fmt.Sprint(norenewedContractCompany)
  1134. item2.ExpiredContractMoney = fmt.Sprint(expiredContractCompany)
  1135. if norenewedContractCompany == 0 || expiredContractCompany == 0 {
  1136. item2.RenewalRate = "0%"
  1137. } else {
  1138. item2.RenewalRate = fmt.Sprint(utils.SubFloatToString(norenewedContractCompany/expiredContractCompany*100, 2), "%")
  1139. }
  1140. listResp = append(listResp, item2)
  1141. case "签约客户数量":
  1142. var conditionInherit string
  1143. var parsInherit []interface{}
  1144. if sellerId != "" {
  1145. condition += ` AND a.seller_id_last IN (` + sellerId + `) `
  1146. conditionInherit += ` AND a.seller_id_last IN (` + sellerId + `) `
  1147. }
  1148. condition += " AND a.product_id = 2 AND a.status = 1 AND a.start_date >= ? AND a.start_date <= ? AND a.rai_contract_type = '新签合同' "
  1149. pars = append(pars, startDate, endDate)
  1150. listRaiData, err := statistic_report.GetRaiDataSummaryList(condition, pars)
  1151. if err != nil {
  1152. br.Msg = "获取数据信息失败"
  1153. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1154. return
  1155. }
  1156. //续约部分的数据
  1157. 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) + `) `
  1158. parsInherit = append(parsInherit, startDate, endDate, companyIdsNew)
  1159. listInheritData, err := statistic_report.GetRaiDataSummaryInheritList(conditionInherit, parsInherit)
  1160. if err != nil {
  1161. br.Msg = "获取数据信息失败"
  1162. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1163. return
  1164. }
  1165. var renewedContractCompany int // 续约客户数
  1166. var renewedContract int // 续约合同数
  1167. maprenewedContractCompany := make(map[int]bool)
  1168. var newContractCompany int // 新签客户数
  1169. var newContract int // 新签合同数
  1170. mapenewContractCompany := make(map[int]bool)
  1171. for _, v := range listRaiData {
  1172. if v.RaiContractType == "新签合同" {
  1173. newContract++
  1174. if !mapenewContractCompany[v.CompanyId] {
  1175. newContractCompany++
  1176. mapenewContractCompany[v.CompanyId] = true
  1177. }
  1178. }
  1179. //else if v.RaiContractType == "续约合同" {
  1180. // renewedContract++
  1181. // if !maprenewedContractCompany[v.CompanyId] {
  1182. // renewedContractCompany++
  1183. // maprenewedContractCompany[v.CompanyId] = true
  1184. // }
  1185. //}
  1186. }
  1187. for _, v := range listInheritData {
  1188. renewedContract++
  1189. if !maprenewedContractCompany[v.CompanyId] {
  1190. renewedContractCompany++
  1191. maprenewedContractCompany[v.CompanyId] = true
  1192. }
  1193. }
  1194. item := new(statistic_report.RaiDataSummaryDetailResp)
  1195. item.RenewedContractCompany = renewedContractCompany
  1196. item.RenewedContract = renewedContract
  1197. item.NewContractCompany = newContractCompany
  1198. item.NewContract = newContract
  1199. listResp = append(listResp, item)
  1200. case "客单价":
  1201. //续约部分的数据
  1202. var conditionInherit string
  1203. var parsInherit []interface{}
  1204. if sellerId != "" {
  1205. condition += ` AND a.seller_id_last IN (` + sellerId + `) `
  1206. conditionInherit += ` AND a.seller_id_last IN (` + sellerId + `) `
  1207. }
  1208. condition += " AND a.product_id = 2 AND a.status = 1 AND a.start_date >= ? AND a.start_date <= ? AND a.rai_contract_type = '新签合同' "
  1209. pars = append(pars, startDate, endDate)
  1210. listRaiData, err := statistic_report.GetRaiDataSummaryList(condition, pars)
  1211. if err != nil {
  1212. br.Msg = "获取数据信息失败"
  1213. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1214. return
  1215. }
  1216. var ontractMoney float64 // 签约总金额
  1217. var contractNum int // 签约客户数
  1218. mapCompany := make(map[int]bool)
  1219. for _, v := range listRaiData {
  1220. if v.RaiContractType != "新签合同" {
  1221. continue
  1222. }
  1223. ontractMoney += v.Money
  1224. if !mapCompany[v.CompanyId] {
  1225. contractNum++
  1226. mapCompany[v.CompanyId] = true
  1227. }
  1228. }
  1229. 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) + `) `
  1230. parsInherit = append(parsInherit, startDate, endDate, companyIdsNew)
  1231. listInheritData, err := statistic_report.GetRaiDataSummaryInheritList(conditionInherit, parsInherit)
  1232. if err != nil {
  1233. br.Msg = "获取数据信息失败"
  1234. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1235. return
  1236. }
  1237. for _, v := range listInheritData {
  1238. ontractMoney += v.Money
  1239. if !mapCompany[v.CompanyId] && v.RaiContractType == "续约合同" {
  1240. contractNum++
  1241. mapCompany[v.CompanyId] = true
  1242. }
  1243. }
  1244. item := new(statistic_report.RaiDataSummaryDetailResp)
  1245. item.ContractMoney = fmt.Sprint(utils.SubFloatToString(ontractMoney, 2))
  1246. item.ContractNum = contractNum
  1247. listResp = append(listResp, item)
  1248. case "开票金额":
  1249. if sellerId != "" {
  1250. condition += ` AND a.rai_seller_id IN (` + sellerId + `) `
  1251. }
  1252. condition += " AND a.is_deleted = 0 AND a.start_date >= ? AND a.start_date <= ? AND invoiced_amount > 0 AND product_ids LIKE '%2%' "
  1253. pars = append(pars, startDate, endDate)
  1254. listFmsData, err := fms.GetContractRegisterList(condition, pars, startSize, pageSize)
  1255. if err != nil {
  1256. br.Msg = "获取数据信息失败"
  1257. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1258. return
  1259. }
  1260. if len(listFmsData) > 0 {
  1261. var contractRegisterId []int
  1262. for _, v := range listFmsData {
  1263. contractRegisterId = append(contractRegisterId, v.ContractRegisterId)
  1264. }
  1265. lenArr := len(contractRegisterId)
  1266. var conditionFms string
  1267. var parsFms []interface{}
  1268. conditionFms += ` AND contract_register_id IN (` + utils.GetOrmInReplace(lenArr) + `) GROUP BY contract_register_id `
  1269. parsFms = append(parsFms, contractRegisterId)
  1270. listInvoiceData, err := fms.GetContractInvoiceList(conditionFms, parsFms, 0, lenArr)
  1271. if err != nil {
  1272. br.Msg = "获取数据信息失败"
  1273. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1274. return
  1275. }
  1276. mapInvoiceTime := make(map[int]string)
  1277. for _, v := range listInvoiceData {
  1278. mapInvoiceTime[v.ContractRegisterId] = v.InvoiceTime.Format(utils.FormatDate)
  1279. }
  1280. for _, v := range listFmsData {
  1281. item := new(statistic_report.RaiDataSummaryDetailResp)
  1282. item.CompanyName = v.CompanyName
  1283. item.SellerName = v.RaiSellerName
  1284. item.ContractCode = v.ContractCode
  1285. item.InvoicedAmount = v.InvoicedAmount
  1286. item.CreateTime = mapInvoiceTime[v.ContractRegisterId]
  1287. listResp = append(listResp, item)
  1288. }
  1289. }
  1290. case "到款金额":
  1291. if sellerId != "" {
  1292. condition += ` AND a.rai_seller_id IN (` + sellerId + `) `
  1293. }
  1294. condition += " AND a.is_deleted = 0 AND a.start_date >= ? AND a.start_date <= ? AND payment_amount > 0 AND product_ids LIKE '%2%' "
  1295. pars = append(pars, startDate, endDate)
  1296. listFmsData, err := fms.GetContractRegisterList(condition, pars, startSize, pageSize)
  1297. if err != nil {
  1298. br.Msg = "获取数据信息失败"
  1299. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1300. return
  1301. }
  1302. if len(listFmsData) > 0 {
  1303. var contractRegisterId []int
  1304. for _, v := range listFmsData {
  1305. contractRegisterId = append(contractRegisterId, v.ContractRegisterId)
  1306. }
  1307. lenArr := len(contractRegisterId)
  1308. var conditionFms string
  1309. var parsFms []interface{}
  1310. conditionFms += ` AND contract_register_id IN (` + utils.GetOrmInReplace(lenArr) + `) GROUP BY contract_register_id `
  1311. parsFms = append(parsFms, contractRegisterId)
  1312. listInvoiceData, err := fms.GetContractInvoiceList(conditionFms, parsFms, 0, lenArr)
  1313. if err != nil {
  1314. br.Msg = "获取数据信息失败"
  1315. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1316. return
  1317. }
  1318. mapInvoiceTime := make(map[int]string)
  1319. for _, v := range listInvoiceData {
  1320. mapInvoiceTime[v.ContractRegisterId] = v.InvoiceTime.Format(utils.FormatDate)
  1321. }
  1322. for _, v := range listFmsData {
  1323. item := new(statistic_report.RaiDataSummaryDetailResp)
  1324. item.CompanyName = v.CompanyName
  1325. item.SellerName = v.RaiSellerName
  1326. item.ContractCode = v.ContractCode
  1327. item.PaymentAmount = v.PaymentAmount
  1328. item.CreateTime = v.CreateTime.Format(utils.FormatDate)
  1329. listResp = append(listResp, item)
  1330. }
  1331. }
  1332. case "未到款比例":
  1333. if sellerId != "" {
  1334. condition += ` AND a.rai_seller_id IN (` + sellerId + `) `
  1335. }
  1336. condition += " AND a.is_deleted = 0 AND a.start_date >= ? AND a.start_date <= ? AND product_ids LIKE '%2%' "
  1337. pars = append(pars, startDate, endDate)
  1338. listFmsData, err := fms.GetContractRegisterAmountList(condition, pars)
  1339. if err != nil {
  1340. br.Msg = "获取数据信息失败"
  1341. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1342. return
  1343. }
  1344. for _, v := range listFmsData {
  1345. item := new(statistic_report.RaiDataSummaryDetailResp)
  1346. item.InvoicedAmount = v.InvoicedAmount
  1347. item.PaymentAmount = v.PaymentAmount
  1348. item.NotReceivedtAmount = v.InvoicedAmount - v.PaymentAmount
  1349. item.CreateTime = v.CreateTime.Format(utils.FormatDate)
  1350. listResp = append(listResp, item)
  1351. }
  1352. case "新客开票":
  1353. if sellerId != "" {
  1354. condition += ` AND a.rai_seller_id IN (` + sellerId + `) `
  1355. }
  1356. condition += " AND a.is_deleted = 0 AND contract_type = 1 AND a.start_date >= ? AND a.start_date <= ? AND product_ids LIKE '%2%' "
  1357. pars = append(pars, startDate, endDate)
  1358. listFmsData, err := fms.GetContractRegisterList(condition, pars, startSize, pageSize)
  1359. if err != nil {
  1360. br.Msg = "获取数据信息失败"
  1361. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1362. return
  1363. }
  1364. if len(listFmsData) > 0 {
  1365. var contractRegisterId []int
  1366. for _, v := range listFmsData {
  1367. contractRegisterId = append(contractRegisterId, v.ContractRegisterId)
  1368. }
  1369. lenArr := len(contractRegisterId)
  1370. var conditionFms string
  1371. var parsFms []interface{}
  1372. conditionFms += ` AND contract_register_id IN (` + utils.GetOrmInReplace(lenArr) + `) GROUP BY contract_register_id `
  1373. parsFms = append(parsFms, contractRegisterId)
  1374. listInvoiceData, err := fms.GetContractInvoiceList(conditionFms, parsFms, 0, lenArr)
  1375. if err != nil {
  1376. br.Msg = "获取数据信息失败"
  1377. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1378. return
  1379. }
  1380. mapInvoiceTime := make(map[int]string)
  1381. for _, v := range listInvoiceData {
  1382. mapInvoiceTime[v.ContractRegisterId] = v.InvoiceTime.Format(utils.FormatDate)
  1383. }
  1384. for _, v := range listFmsData {
  1385. item := new(statistic_report.RaiDataSummaryDetailResp)
  1386. item.CompanyName = v.CompanyName
  1387. item.SellerName = v.RaiSellerName
  1388. item.ContractCode = v.ContractCode
  1389. item.InvoicedAmount = v.InvoicedAmount
  1390. item.CreateTime = mapInvoiceTime[v.ContractRegisterId]
  1391. listResp = append(listResp, item)
  1392. }
  1393. }
  1394. case "新客到款":
  1395. if sellerId != "" {
  1396. condition += ` AND a.rai_seller_id IN (` + sellerId + `) `
  1397. }
  1398. 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%' "
  1399. pars = append(pars, startDate, endDate)
  1400. listFmsData, err := fms.GetContractRegisterList(condition, pars, startSize, pageSize)
  1401. if err != nil {
  1402. br.Msg = "获取数据信息失败"
  1403. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1404. return
  1405. }
  1406. if len(listFmsData) > 0 {
  1407. var contractRegisterId []int
  1408. for _, v := range listFmsData {
  1409. contractRegisterId = append(contractRegisterId, v.ContractRegisterId)
  1410. }
  1411. lenArr := len(contractRegisterId)
  1412. var conditionFms string
  1413. var parsFms []interface{}
  1414. conditionFms += ` AND contract_register_id IN (` + utils.GetOrmInReplace(lenArr) + `) GROUP BY contract_register_id `
  1415. parsFms = append(parsFms, contractRegisterId)
  1416. listInvoiceData, err := fms.GetContractInvoiceList(conditionFms, parsFms, 0, lenArr)
  1417. if err != nil {
  1418. br.Msg = "获取数据信息失败"
  1419. br.ErrMsg = "获取数据信息失败,Err:" + err.Error()
  1420. return
  1421. }
  1422. mapInvoiceTime := make(map[int]string)
  1423. for _, v := range listInvoiceData {
  1424. mapInvoiceTime[v.ContractRegisterId] = v.InvoiceTime.Format(utils.FormatDate)
  1425. }
  1426. for _, v := range listFmsData {
  1427. item := new(statistic_report.RaiDataSummaryDetailResp)
  1428. item.CompanyName = v.CompanyName
  1429. item.SellerName = v.RaiSellerName
  1430. item.ContractCode = v.ContractCode
  1431. item.PaymentAmount = v.PaymentAmount
  1432. item.CreateTime = mapInvoiceTime[v.ContractRegisterId]
  1433. listResp = append(listResp, item)
  1434. }
  1435. }
  1436. }
  1437. if len(listResp) == 0 {
  1438. listResp = make([]*statistic_report.RaiDataSummaryDetailResp, 0)
  1439. }
  1440. if len(listGroup) > 0 {
  1441. for _, v := range listGroup {
  1442. if v.CompanyCount > 1 {
  1443. resp.CompanyMultiple++
  1444. }
  1445. }
  1446. resp.CompanyNum = len(listGroup)
  1447. }
  1448. resp.List = listResp
  1449. page := paging.GetPaging(currentIndex, pageSize, trialTotal)
  1450. resp.Paging = page
  1451. br.Ret = 200
  1452. br.Success = true
  1453. br.Msg = "获取成功"
  1454. br.Data = resp
  1455. }