rai_data_summary.go 71 KB

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