rai_data_summary.go 71 KB

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