rai_data_summary.go 62 KB

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