rai_data_summary.go 62 KB

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