rai_data_summary.go 71 KB

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