statistic_company_merge.go 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510
  1. package controllers
  2. import (
  3. "fmt"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "github.com/tealeg/xlsx"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/models/company"
  8. "hongze/hz_crm_api/models/response"
  9. "hongze/hz_crm_api/models/system"
  10. "hongze/hz_crm_api/services"
  11. cygxService "hongze/hz_crm_api/services/cygx"
  12. "os"
  13. "path/filepath"
  14. "strconv"
  15. "strings"
  16. //"hongze/hz_crm_api/services"
  17. "hongze/hz_crm_api/utils"
  18. //"strings"
  19. "time"
  20. )
  21. // 统计报表模块下权益合同合并
  22. // StatisticReportController 统计报告基类
  23. type StatisticCompanyMergerController struct {
  24. BaseAuthController
  25. }
  26. // MergeCompanyList
  27. // @Title 权益客户统计列表(合同进行合并之后的)
  28. // @Description 权益客户统计列表接口
  29. // @Param PageSize query int true "每页数据条数"
  30. // @Param CurrentIndex query int true "当前页页码,从1开始"
  31. // @Param Keyword query string true "客户名称"
  32. // @Param CompanyType query string true "客户类型:传空字符串或者不传为全部,'ficc','权益'"
  33. // @Param AdminId query string true "销售id,多个用英文逗号隔开,空字符串为全部"
  34. // @Param RegionType query string false "所属区域:传空字符串或者不传为全部,'国内','海外'"
  35. // @Param StartDate query string false "开始日期"
  36. // @Param EndDate query string false "结束日期"
  37. // @Param DataType query string false "报表类型,枚举值:`新增试用`,`新签客户`,`续约客户`,`未续约客户`"
  38. // @Param TryOutType query string false " '试用', '非试用' 非试用即为冻结/流失"
  39. // @Param IsExport query bool false "是否导出excel,默认是false"
  40. // @Param IsConfirm query int false "是否确认续约: -1-默认全部; 0-待确认; 1-已确认 ;2-到期合同"
  41. // @Param CompanyAscribeId query int false "归因ID"
  42. // @Param PackageDifference query string false "和上一份合同的区别,枚举值:`增加套餐`,`减少套餐`,`维持套餐`"
  43. // @Success 200 {object} response.IncrementalCompanyListResp
  44. // @router /merge_company_list [get]
  45. func (this *StatisticCompanyMergerController) MergeCompanyList() {
  46. br := new(models.BaseResponse).Init()
  47. defer func() {
  48. this.Data["json"] = br
  49. this.ServeJSON()
  50. }()
  51. sysUser := this.SysUser
  52. if sysUser == nil {
  53. br.Msg = "请登录"
  54. br.ErrMsg = "请登录,SysUser Is Empty"
  55. br.Ret = 408
  56. return
  57. }
  58. pageSize, _ := this.GetInt("PageSize")
  59. currentIndex, _ := this.GetInt("CurrentIndex")
  60. adminId := this.GetString("AdminId")
  61. regionType := this.GetString("RegionType")
  62. //companyType := this.GetString("CompanyType")
  63. dataType := this.GetString("DataType")
  64. tryOutType := this.GetString("TryOutType")
  65. keyword := this.GetString("Keyword")
  66. packageDifference := this.GetString("PackageDifference")
  67. startDate := this.GetString("StartDate")
  68. endDate := this.GetString("EndDate")
  69. isConfirm, _ := this.GetInt("IsConfirm", -1)
  70. companyAscribeId, _ := this.GetInt("CompanyAscribeId", -1)
  71. if dataType != "续约客户" {
  72. packageDifference = "" // 只有续约客户才会有值,过滤前端传过来的脏数据
  73. }
  74. if startDate == "" {
  75. startDate = "2015-01-01"
  76. }
  77. if endDate == "" {
  78. endDate = time.Now().Format(utils.FormatDate)
  79. }
  80. var startSize int
  81. if pageSize <= 0 {
  82. pageSize = utils.PageSize20
  83. }
  84. if currentIndex <= 0 {
  85. currentIndex = 1
  86. }
  87. startSize = utils.StartIndex(currentIndex, pageSize)
  88. //是否导出报表
  89. isExport, _ := this.GetBool("IsExport")
  90. if isExport {
  91. pageSize = 10000
  92. currentIndex = 1
  93. }
  94. var condition string
  95. var pars []interface{}
  96. //条件
  97. //if adminId != "" {
  98. // //condition += ` AND a.seller_id_init in (` + adminId + `) `
  99. // //condition += ` AND c.seller_id in (` + adminId + `) `
  100. // //pars = append(pars, adminId)
  101. //} else {
  102. //
  103. // //if dataType == "未续约客户" {
  104. // // //根据当前角色来获取查询条件
  105. // // condition, pars = getQueryParams(condition, pars, sysUser, "c.")
  106. // //} else {
  107. // // //根据当前角色来获取查询条件
  108. // // condition, pars = getQueryParamsInit(condition, pars, sysUser, "c.")
  109. // //}
  110. //
  111. //}
  112. if regionType != "" {
  113. condition += ` AND b.region_type = ? `
  114. pars = append(pars, regionType)
  115. }
  116. //关键字搜索
  117. if keyword != "" {
  118. condition += ` and b.company_name like "%` + keyword + `%" `
  119. }
  120. var conditionAscribRai string // 处理权益未续约客户检索列表SQL查询条件
  121. var conditionAscribRaiTotal string // 处理权益未续约客户总量查询条件
  122. var conditionAscribRaiToBeTotal string // 处理权益未续约客户待确认总量查询条件
  123. var parsAscribeRai []interface{}
  124. var parsAscribeRaiTotal []interface{} // 处理权益未续约客户总量查询条件
  125. var parsAscribeRaiTobeTotal []interface{} // 处理权益未续约客户待确认总量查询条件
  126. var conditionConfirm string
  127. var parsConfirm []interface{}
  128. companyConfirmList, err := company.GetCompanyContractNoRenewedAscribeList(conditionConfirm, parsConfirm, 0, 0)
  129. if err != nil && err.Error() != utils.ErrNoRow() {
  130. br.Msg = "获取失败"
  131. br.ErrMsg = "获取失败,GetCompanyNoRenewedAscribeList Err:" + err.Error()
  132. return
  133. }
  134. var noRenewedcompanyContractIds []int //已经确定未续约的公司ID
  135. if len(companyConfirmList) == 0 {
  136. noRenewedcompanyContractIds = append(noRenewedcompanyContractIds, 0) // 给一个不存在的ID
  137. } else {
  138. for _, v := range companyConfirmList {
  139. noRenewedcompanyContractIds = append(noRenewedcompanyContractIds, v.CompanyContractId)
  140. }
  141. }
  142. //是否确认续约 CRM 13.9
  143. conditionAscribRaiTotal += ` AND a.company_contract_id IN (` + utils.GetOrmInReplace(len(noRenewedcompanyContractIds)) + `) ` // 已确认
  144. parsAscribeRaiTotal = append(parsAscribeRaiTotal, noRenewedcompanyContractIds)
  145. conditionAscribRaiToBeTotal += ` AND a.company_contract_id NOT IN (` + utils.GetOrmInReplace(len(noRenewedcompanyContractIds)) + `) ` // 待确认
  146. parsAscribeRaiTobeTotal = append(parsAscribeRaiTobeTotal, noRenewedcompanyContractIds)
  147. if isConfirm != -1 {
  148. if isConfirm == 0 {
  149. conditionAscribRai += ` AND a.company_contract_id NOT IN (` + utils.GetOrmInReplace(len(noRenewedcompanyContractIds)) + `) ` // 待确认
  150. parsAscribeRai = append(parsAscribeRai, noRenewedcompanyContractIds)
  151. } else if isConfirm == 1 {
  152. conditionAscribRai += ` AND a.company_contract_id IN (` + utils.GetOrmInReplace(len(noRenewedcompanyContractIds)) + `) ` // 已确认
  153. parsAscribeRai = append(parsAscribeRai, noRenewedcompanyContractIds)
  154. } else if isConfirm == 2 {
  155. conditionAscribRai += ` AND a.start_date < ? ` // 已确认
  156. parsAscribeRai = append(parsAscribeRai, time.Now().Format(utils.FormatDate))
  157. }
  158. }
  159. //归因ID CRM 13.9
  160. if companyAscribeId > 0 {
  161. var conditionAscribe string
  162. var parsAscribe []interface{}
  163. conditionAscribe = " AND company_ascribe_id = ? "
  164. parsAscribe = append(parsAscribe, companyAscribeId)
  165. companyNoRenewedAscribeList, err := company.GetCompanyContractNoRenewedAscribeList(conditionAscribe, parsAscribe, 0, 0)
  166. if err != nil && err.Error() != utils.ErrNoRow() {
  167. br.Msg = "获取失败"
  168. br.ErrMsg = "获取失败,GetCompanyNoRenewedAscribeList Err:" + err.Error()
  169. return
  170. }
  171. var noRenewedcompanyContractIds []int
  172. if len(companyNoRenewedAscribeList) == 0 {
  173. noRenewedcompanyContractIds = append(noRenewedcompanyContractIds, 0) // 给一个不存在的ID
  174. } else {
  175. for _, v := range companyNoRenewedAscribeList {
  176. noRenewedcompanyContractIds = append(noRenewedcompanyContractIds, v.CompanyContractId)
  177. }
  178. }
  179. conditionAscribRai += ` AND a.company_contract_id IN (` + utils.GetOrmInReplace(len(noRenewedcompanyContractIds)) + `)`
  180. parsAscribeRai = append(parsAscribeRai, noRenewedcompanyContractIds)
  181. }
  182. condition += ` AND c.product_id = ? `
  183. pars = append(pars, 2)
  184. var list []*models.IncrementalList
  185. //moreListMap := make(map[int][]*models.IncrementalList)
  186. var newCompanyTotal int //新签合同数量
  187. var notRenewalCompanyTotal int //未续约客户数已确认
  188. var notRenewalCompanyToBeConfirmTotal int //未续约客户数待确认
  189. var dataTotal, trialTotal, renewalCompanyTotal int
  190. var notRenewalTryOut, notRenewalNotTryOut int
  191. //试用客户数
  192. {
  193. condition1 := condition
  194. pars1 := pars
  195. //销售筛选条件
  196. if adminId != "" {
  197. condition1 += ` AND c.seller_id in (` + adminId + `) `
  198. } else {
  199. condition1, pars1 = getQueryParams(condition1, pars1, sysUser, "c.")
  200. }
  201. condition1 += ` AND a.create_time >= ? AND a.create_time <= ? AND a.operation in ("add","receive") AND b.company_id NOT IN ( SELECT company_id FROM company_operation_record WHERE product_id = 2 AND operation ="loss" GROUP BY company_id ) AND c.status = '试用' `
  202. pars1 = append(pars1, startDate, endDate)
  203. total, err := models.GetIncrementalCompanyCountByOperationRecord(condition1, pars1)
  204. if err != nil && err.Error() != utils.ErrNoRow() {
  205. br.Msg = "获取失败"
  206. br.ErrMsg = "获取失败,Err:" + err.Error()
  207. return
  208. }
  209. trialTotal = total
  210. if dataType == "新增试用" {
  211. //列表数据数量
  212. total, err := models.GetIncrementalCompanyProductCountByOperationRecord(condition1, pars1)
  213. if err != nil && err.Error() != utils.ErrNoRow() {
  214. br.Msg = "获取失败"
  215. br.ErrMsg = "获取失败,Err:" + err.Error()
  216. return
  217. }
  218. //列表页数据
  219. tmpList, err := models.GetIncrementalCompanyListByOperationRecord(condition1, pars1, startSize, pageSize)
  220. if err != nil {
  221. br.Msg = "获取失败"
  222. br.ErrMsg = "获取失败,Err:" + err.Error()
  223. return
  224. }
  225. for _, v := range tmpList {
  226. v.SellerName = v.SellerNameInit
  227. }
  228. list = tmpList
  229. dataTotal = total
  230. }
  231. }
  232. //新签客户数
  233. {
  234. condition1 := condition
  235. pars1 := pars
  236. //销售筛选条件
  237. if adminId != "" {
  238. condition1 += ` AND a.seller_id_init in (` + adminId + `) `
  239. } else {
  240. condition1, pars1 = getQueryParamsInit(condition1, pars1, sysUser, "c.")
  241. }
  242. condition1 += ` AND a.status = 1 AND a.start_date >= ? AND a.start_date <= ? `
  243. pars1 = append(pars1, startDate, endDate)
  244. condition1 += ` AND a.rai_contract_type = ? `
  245. pars1 = append(pars1, "新签合同")
  246. newCompanyTotal, err = company.GetIncrementalNewCompanyProductMergeCount(condition1, pars1)
  247. if err != nil && err.Error() != utils.ErrNoRow() {
  248. br.Msg = "获取失败"
  249. br.ErrMsg = "获取失败,Err:" + err.Error()
  250. return
  251. }
  252. if dataType == "新签客户" {
  253. //列表数据数量
  254. total, err := company.GetIncrementalNewCompanyProductMergeCount(condition1, pars1)
  255. if err != nil && err.Error() != utils.ErrNoRow() {
  256. br.Msg = "获取失败"
  257. br.ErrMsg = "获取失败,Err:" + err.Error()
  258. return
  259. }
  260. //列表页数据
  261. tmpList, err := models.GetIncrementalCompanyMergeList(condition1, pars1, startSize, pageSize)
  262. if err != nil {
  263. br.Msg = "获取失败"
  264. br.ErrMsg = "获取失败,Err:" + err.Error()
  265. return
  266. }
  267. list = tmpList
  268. dataTotal = total
  269. }
  270. }
  271. //续约客户数
  272. {
  273. condition1 := condition
  274. pars1 := pars
  275. condition1 += ` AND a.status = 1 AND a.start_date >= ? AND a.start_date <= ? `
  276. pars1 = append(pars1, startDate, endDate)
  277. condition1 += ` AND a.rai_contract_type = ? `
  278. pars1 = append(pars1, "续约合同")
  279. //销售筛选条件
  280. if adminId != "" {
  281. condition1 += ` AND a.seller_id_init in (` + adminId + `) `
  282. } else {
  283. condition1, pars1 = getQueryParamsInit(condition1, pars1, sysUser, "c.")
  284. }
  285. ////额外条件(续约合同的起始日期包含在所选时间段内且不包含在新签合同存续期内的客户)
  286. //pars1 = append(pars1, endDate)
  287. total, err := company.GetIncrementalNewCompanyProductMergeCount(condition1, pars1)
  288. if err != nil && err.Error() != utils.ErrNoRow() {
  289. br.Msg = "获取失败"
  290. br.ErrMsg = "获取失败,Err:" + err.Error()
  291. return
  292. }
  293. renewalCompanyTotal = total
  294. if dataType == "续约客户" {
  295. //续约的客户才会查询
  296. if packageDifference != "" {
  297. condition1 += ` AND a.package_difference = ? `
  298. pars1 = append(pars1, packageDifference)
  299. }
  300. //列表数据数量
  301. total, err := company.GetIncrementalRenewalCompanyProductMergeCount(condition1, pars1)
  302. if err != nil && err.Error() != utils.ErrNoRow() {
  303. br.Msg = "获取失败"
  304. br.ErrMsg = "获取失败,Err:" + err.Error()
  305. return
  306. }
  307. //列表页数据
  308. tmpList, err := models.GetIncrementalCompanyMergeList(condition1, pars1, startSize, pageSize)
  309. if err != nil {
  310. br.Msg = "获取失败"
  311. br.ErrMsg = "获取失败,Err:" + err.Error()
  312. return
  313. }
  314. list = tmpList
  315. dataTotal = total
  316. }
  317. }
  318. //未续约客户数
  319. {
  320. condition1 := condition
  321. pars1 := pars
  322. condition1 += condition
  323. pars1 = append(pars1, pars)
  324. condition1 += ` AND a.not_renewal_hide = 0 AND a.status = 1 AND a.start_date >= '2020-01-01' AND a.end_date >= ? AND a.end_date <= ? `
  325. pars1 = append(pars1, startDate, endDate)
  326. //condition1 += ` AND a.operation = ? `
  327. //pars1 = append(pars1, "try_out")
  328. condition1 += ` AND c.status not in ("永续","正式","关闭") `
  329. //销售筛选条件
  330. if adminId != "" {
  331. condition1 += ` AND c.seller_id_last in (` + adminId + `) `
  332. } else {
  333. condition1, pars1 = getQueryParamsLast(condition1, pars1, sysUser, "c.")
  334. }
  335. //未续约已确认数量
  336. notRenewalCondition := condition1
  337. notRenewalPars := pars1
  338. notRenewalCondition += conditionAscribRaiTotal
  339. notRenewalPars = append(notRenewalPars, parsAscribeRaiTotal)
  340. total, err := company.GetIncrementalRenewalCompanyProductMergeCount(notRenewalCondition, notRenewalPars)
  341. if err != nil && err.Error() != utils.ErrNoRow() {
  342. br.Msg = "获取失败"
  343. br.ErrMsg = "获取失败,Err:" + err.Error()
  344. return
  345. }
  346. notRenewalCompanyTotal = total
  347. //未续约待确认数量
  348. notRenewalToBeCondition := condition1
  349. notRenewalToBePars := pars1
  350. notRenewalToBeCondition += conditionAscribRaiToBeTotal
  351. notRenewalToBePars = append(notRenewalToBePars, parsAscribeRaiTobeTotal)
  352. notRenewalCompanyToBeConfirmTotal, err = company.GetIncrementalRenewalCompanyProductMergeCount(notRenewalToBeCondition, notRenewalToBePars)
  353. if err != nil && err.Error() != utils.ErrNoRow() {
  354. br.Msg = "获取失败"
  355. br.ErrMsg = "获取失败,Err:" + err.Error()
  356. return
  357. }
  358. condition1 += conditionAscribRai
  359. pars1 = append(pars1, parsAscribeRai)
  360. if dataType == "未续约客户" {
  361. //统计数据
  362. for _, v := range []string{"试用", "非试用"} {
  363. totalCondition1 := condition1
  364. totalPars1 := pars1
  365. var tmpTotal int
  366. if v == "试用" {
  367. totalCondition1 += ` AND c.status = "试用" `
  368. tmpTotal, err = company.GetIncrementalRenewalCompanyProductMergeCount(totalCondition1, totalPars1)
  369. if err != nil && err.Error() != utils.ErrNoRow() {
  370. br.Msg = "获取失败"
  371. br.ErrMsg = "获取失败,Err:" + err.Error()
  372. return
  373. }
  374. notRenewalTryOut = tmpTotal
  375. } else if v == "非试用" {
  376. totalCondition1 += ` AND c.status IN ("冻结","流失") `
  377. tmpTotal, err = company.GetIncrementalRenewalCompanyProductMergeCount(totalCondition1, totalPars1)
  378. if err != nil && err.Error() != utils.ErrNoRow() {
  379. br.Msg = "获取失败"
  380. br.ErrMsg = "获取失败,Err:" + err.Error()
  381. return
  382. }
  383. notRenewalNotTryOut = tmpTotal
  384. }
  385. }
  386. //列表数据数量
  387. if tryOutType == "试用" {
  388. condition1 += ` AND c.status = "试用" `
  389. total = notRenewalTryOut
  390. } else if tryOutType == "非试用" {
  391. condition1 += ` AND c.status IN ("冻结","流失") `
  392. total = notRenewalNotTryOut
  393. }
  394. //分页total单独计算
  395. total, err = company.GetIncrementalRenewalCompanyProductMergeCount(condition1, pars1)
  396. if err != nil && err.Error() != utils.ErrNoRow() {
  397. br.Msg = "获取失败"
  398. br.ErrMsg = "获取失败,Err:" + err.Error()
  399. return
  400. }
  401. //return
  402. //列表页数据
  403. tmpList, err := models.GetIncrementalCompanyListByOperationRecordMerge(condition1, pars1, startSize, pageSize)
  404. if err != nil {
  405. br.Msg = "获取失败"
  406. br.ErrMsg = "获取失败,Err:" + err.Error()
  407. return
  408. }
  409. var ascribecompanyIds []int
  410. var companyContractIds []int
  411. for _, item := range tmpList {
  412. //endDateTime, _ := time.Parse(utils.FormatDateTime, item.CreateTime)
  413. //item.EndDate = endDateTime.Format(utils.FormatDate)
  414. ascribecompanyIds = append(ascribecompanyIds, item.CompanyId)
  415. companyContractIds = append(companyContractIds, item.CompanyContractId)
  416. }
  417. //合同归因标签
  418. mapGetCompanyAscribeContent, mapContent := services.GetCompanyContractAscribeContentMap(companyContractIds)
  419. mapNoRenewedNote := services.GetCompanyNoRenewedNoteMap(ascribecompanyIds)
  420. for _, item := range tmpList {
  421. item.AscribeContent = mapGetCompanyAscribeContent[item.CompanyContractId]
  422. item.Content = mapContent[item.CompanyContractId]
  423. item.IsShowNoRenewedNote = mapNoRenewedNote[fmt.Sprint("CID_", item.CompanyId, "PID_", item.ProductId)]
  424. }
  425. list = tmpList
  426. dataTotal = total
  427. }
  428. }
  429. listLen := len(list)
  430. if listLen == 0 {
  431. list = make([]*models.IncrementalList, 0)
  432. }
  433. var companyContractIds []int
  434. var companyIds []int
  435. for i := 0; i < listLen; i++ {
  436. item := list[i]
  437. companyContractIds = append(companyContractIds, item.CompanyContractId)
  438. //新增试用不需要计算剩余日期
  439. if dataType != "新增试用" {
  440. //剩余可用天数
  441. endDateTime, _ := time.Parse(utils.FormatDate, item.EndDate)
  442. endDateTime = endDateTime.AddDate(0, 0, 1)
  443. sub := endDateTime.Sub(time.Now())
  444. expireDay := fmt.Sprintf("%v", int(sub.Hours()/24))
  445. list[i].ExpireDay = expireDay
  446. }
  447. companyIds = append(companyIds, item.CompanyId)
  448. }
  449. //合并合同所对应的权限
  450. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  451. if err != nil {
  452. br.Msg = "获取失败"
  453. br.ErrMsg = "获取失败,Err:" + err.Error()
  454. return
  455. }
  456. mapIsUserMaker := cygxService.GetCompanyProductIsUserMakerByCompanyIds(companyIds) //根据公司ID获取近四周之内有决策人互动的客户
  457. for _, v := range list {
  458. v.PermissionName = mappermissionName[v.CompanyContractId]
  459. v.IsUserMaker = mapIsUserMaker[v.CompanyId]
  460. }
  461. page := paging.GetPaging(currentIndex, pageSize, dataTotal)
  462. resp := response.IncrementalCompanyListResp{
  463. Paging: page,
  464. List: list,
  465. TrialTotal: trialTotal,
  466. NewCompanyTotal: newCompanyTotal,
  467. RenewalCompanyTotal: renewalCompanyTotal,
  468. NotRenewalCompanyTotal: notRenewalCompanyTotal,
  469. NotRenewalCompanyToBeConfirmTotal: notRenewalCompanyToBeConfirmTotal,
  470. NotRenewalTryOut: notRenewalTryOut,
  471. NotRenewalNotTryOut: notRenewalNotTryOut,
  472. }
  473. //fmt.Println()
  474. //导出excel
  475. if isExport {
  476. MergeCompanyListListExport(this, dataType, resp, br)
  477. return
  478. }
  479. br.Ret = 200
  480. br.Success = true
  481. br.Msg = "获取成功"
  482. br.Data = resp
  483. }
  484. // IncrementalCompanyListExport 导出增量客户数据报表excel
  485. func MergeCompanyListListExport(this *StatisticCompanyMergerController, dataType string, resp response.IncrementalCompanyListResp, br *models.BaseResponse) {
  486. dir, err := os.Executable()
  487. exPath := filepath.Dir(dir)
  488. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  489. xlsxFile := xlsx.NewFile()
  490. if err != nil {
  491. br.Msg = "生成文件失败"
  492. br.ErrMsg = "生成文件失败"
  493. return
  494. }
  495. style := xlsx.NewStyle()
  496. alignment := xlsx.Alignment{
  497. Horizontal: "center",
  498. Vertical: "center",
  499. WrapText: true,
  500. }
  501. style.Alignment = alignment
  502. style.ApplyAlignment = true
  503. sheel, err := xlsxFile.AddSheet("增量客户数据")
  504. if err != nil {
  505. br.Msg = "新增Sheet失败"
  506. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  507. return
  508. }
  509. sheel.SetColWidth(0, 0, 30)
  510. sheel.SetColWidth(1, 1, 15)
  511. sheel.SetColWidth(2, 2, 15)
  512. sheel.SetColWidth(3, 3, 18)
  513. sheel.SetColWidth(4, 4, 40)
  514. sheel.SetColWidth(5, 5, 18)
  515. //统计数据
  516. statisticRow := sheel.AddRow()
  517. cell1 := statisticRow.AddCell()
  518. cell1.SetStyle(style)
  519. cell1.SetValue(fmt.Sprint("新增试用客户数:", resp.TrialTotal))
  520. cell2 := statisticRow.AddCell()
  521. cell2.SetStyle(style)
  522. cell2.SetValue(fmt.Sprint("新签客户数:", resp.NewCompanyTotal))
  523. cell3 := statisticRow.AddCell()
  524. cell3.SetStyle(style)
  525. cell3.SetValue(fmt.Sprint("续约客户数:", resp.RenewalCompanyTotal))
  526. cell4 := statisticRow.AddCell()
  527. cell4.SetStyle(style)
  528. cell4.SetValue(fmt.Sprint("未续约客户数:", resp.NotRenewalCompanyTotal))
  529. //表头
  530. titleRow := sheel.AddRow()
  531. cellName := titleRow.AddCell()
  532. cellName.SetStyle(style)
  533. cellName.SetValue("客户名称")
  534. //cellProName := titleRow.AddCell()
  535. //cellProName.SetStyle(style)
  536. //cellProName.SetValue("客户类型")
  537. cellSellerName := titleRow.AddCell()
  538. cellSellerName.SetStyle(style)
  539. cellSellerName.SetValue("所属销售")
  540. if dataType != "新增试用" {
  541. cellShareSeller := titleRow.AddCell()
  542. cellShareSeller.SetStyle(style)
  543. cellShareSeller.SetValue("服务销售")
  544. cellMoney := titleRow.AddCell()
  545. cellMoney.SetStyle(style)
  546. cellMoney.SetValue("合同金额")
  547. }
  548. cellTime := titleRow.AddCell()
  549. cellTime.SetStyle(style)
  550. switch dataType {
  551. case "新增试用":
  552. cellTime.SetValue("新增时间")
  553. case "新签客户":
  554. cellTime.SetValue("合同期限")
  555. case "续约客户":
  556. cellTime.SetValue("合同期限")
  557. case "未续约客户":
  558. cellTime.SetValue("合同期限")
  559. cellAscribeContent := titleRow.AddCell()
  560. cellAscribeContent.SetStyle(style)
  561. cellAscribeContent.SetValue("不续约归因")
  562. cellContent := titleRow.AddCell()
  563. cellContent.SetStyle(style)
  564. cellContent.SetValue("详细原因")
  565. }
  566. if dataType != "新增试用" {
  567. cellPermissionName := titleRow.AddCell()
  568. cellPermissionName.SetStyle(style)
  569. cellPermissionName.SetValue("签约套餐")
  570. }
  571. for _, v := range resp.List {
  572. dataRow := sheel.AddRow()
  573. dataRow.SetHeight(20)
  574. cellDataName := dataRow.AddCell()
  575. cellDataName.SetStyle(style)
  576. cellDataName.SetValue(v.CompanyName)
  577. //cellDataProName := dataRow.AddCell()
  578. //cellDataProName.SetStyle(style)
  579. //cellDataProName.SetValue(v.ProductName)
  580. cellDataSellerName := dataRow.AddCell()
  581. cellDataSellerName.SetStyle(style)
  582. cellDataSellerName.SetValue(v.SellerName)
  583. if dataType != "新增试用" {
  584. cellDataShareSeller := dataRow.AddCell()
  585. cellDataShareSeller.SetStyle(style)
  586. cellDataShareSeller.SetValue(v.ShareSeller)
  587. cellDataMoney := dataRow.AddCell()
  588. cellDataMoney.SetStyle(style)
  589. cellDataMoney.SetValue(v.Money)
  590. }
  591. //if dataType == "新签客户" || dataType == "续约客户" {
  592. //}
  593. cellDataTime := dataRow.AddCell()
  594. cellDataTime.SetStyle(style)
  595. switch dataType {
  596. case "新增试用":
  597. cellDataTime.SetValue(v.CreateTime)
  598. case "新签客户":
  599. cellDataTime.SetValue(fmt.Sprint(v.StartDate, " ~ ", v.EndDate))
  600. case "续约客户":
  601. cellDataTime.SetValue(fmt.Sprint(v.StartDate, " ~ ", v.EndDate))
  602. case "未续约客户":
  603. cellDataTime.SetValue(fmt.Sprint(v.StartDate, " ~ ", v.EndDate))
  604. cellAscribeContent := dataRow.AddCell()
  605. cellAscribeContent.SetStyle(style)
  606. cellAscribeContent.SetValue(v.AscribeContent)
  607. cellContent := dataRow.AddCell()
  608. cellContent.SetStyle(style)
  609. cellContent.SetValue(v.Content)
  610. }
  611. cellDataPermissionName := dataRow.AddCell()
  612. cellDataPermissionName.SetStyle(style)
  613. cellDataPermissionName.SetValue(v.PermissionName)
  614. }
  615. err = xlsxFile.Save(downLoadnFilePath)
  616. if err != nil {
  617. br.Msg = "保存文件失败"
  618. br.ErrMsg = "保存文件失败"
  619. return
  620. }
  621. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  622. downloadFileName := dataType + "数据_" + randStr + ".xlsx"
  623. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  624. defer func() {
  625. os.Remove(downLoadnFilePath)
  626. }()
  627. br.Ret = 200
  628. br.Success = true
  629. br.Msg = "导出成功"
  630. }
  631. // @Title 上一份合同详情
  632. // @Description 上一份合同详情接口
  633. // @Param CompanyContractId query int true "合同ID"
  634. // @Success Ret=200 {object} company.GetCompanyContractMergeDetailResp
  635. // @router /merge_company_previous/detail [get]
  636. func (this *StatisticCompanyMergerController) CompanyPreviousDetail() {
  637. br := new(models.BaseResponse).Init()
  638. defer func() {
  639. this.Data["json"] = br
  640. this.ServeJSON()
  641. }()
  642. AdminUser := this.SysUser
  643. if AdminUser == nil {
  644. br.Msg = "请登录"
  645. br.ErrMsg = "请登录,用户信息为空"
  646. br.Ret = 408
  647. return
  648. }
  649. resp := new(company.GetCompanyContractDetailResp)
  650. companyContractId, _ := this.GetInt("CompanyContractId")
  651. if companyContractId < 1 {
  652. br.Msg = "请输入合同ID"
  653. return
  654. }
  655. var condition string
  656. var pars []interface{}
  657. //获取最新的一条到期的合同信息
  658. condition = " AND company_contract_id = ? "
  659. pars = append(pars, companyContractId)
  660. detail, err := company.GetCompanyContracDetail(condition, pars)
  661. if err != nil {
  662. br.Msg = "详情不存在"
  663. br.ErrMsg = "获取失败,Err:" + err.Error()
  664. return
  665. }
  666. //获取前一份合同的信息
  667. pars = make([]interface{}, 0)
  668. condition = " AND company_id = ? AND company_contract_id < ? AND status = 1 AND product_id = 2 ORDER BY company_contract_id DESC LIMIT 1 "
  669. pars = append(pars, detail.CompanyId, companyContractId)
  670. detailPrevious, err := company.GetCompanyContracDetail(condition, pars)
  671. if err != nil {
  672. br.Msg = "详情不存在"
  673. br.ErrMsg = "获取失败,Err:" + err.Error()
  674. return
  675. }
  676. //合并合同所对应的权限
  677. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById([]int{detailPrevious.CompanyContractId})
  678. if err != nil {
  679. br.Msg = "获取失败"
  680. br.ErrMsg = "获取失败,Err:" + err.Error()
  681. return
  682. }
  683. detailPrevious.PermissionName = mappermissionName[detailPrevious.CompanyContractId]
  684. resp.Detail = detailPrevious
  685. br.Ret = 200
  686. br.Success = true
  687. br.Msg = "获取成功"
  688. br.Data = resp
  689. }
  690. // @Title 权益行业套餐统计
  691. // @Description 权益客户统计列表接口
  692. // @Param PageSize query int true "每页数据条数"
  693. // @Param CurrentIndex query int true "当前页页码,从1开始"
  694. // @Param Keyword query string true "客户名称"
  695. // @Param AdminId query string true "销售id,多个用英文逗号隔开,空字符串为全部"
  696. // @Param StartDate query string false "开始日期"
  697. // @Param EndDate query string false "结束日期"
  698. // @Param DataType query string false "报表类型,枚举值:`行业新签`,`行业续约`,`行业未续约`"
  699. // @Param PermissionName query string false "行业权限名称"
  700. // @Param IsExport query bool false "是否导出excel,默认是false"
  701. // @Success 200 {object} response.IncrementalCompanyListResp
  702. // @router /merge_company/company_contract_permission/list [get]
  703. func (this *StatisticCompanyMergerController) CompanyContractPermissionList() {
  704. br := new(models.BaseResponse).Init()
  705. defer func() {
  706. this.Data["json"] = br
  707. this.ServeJSON()
  708. }()
  709. sysUser := this.SysUser
  710. if sysUser == nil {
  711. br.Msg = "请登录"
  712. br.ErrMsg = "请登录,SysUser Is Empty"
  713. br.Ret = 408
  714. return
  715. }
  716. pageSize, _ := this.GetInt("PageSize")
  717. currentIndex, _ := this.GetInt("CurrentIndex")
  718. adminId := this.GetString("AdminId")
  719. regionType := this.GetString("RegionType")
  720. //companyType := this.GetString("CompanyType")
  721. dataType := this.GetString("DataType")
  722. keyword := this.GetString("Keyword")
  723. startDate := this.GetString("StartDate")
  724. endDate := this.GetString("EndDate")
  725. permissionName := this.GetString("PermissionName")
  726. if permissionName == "" { //默认所筛选行业为医药
  727. permissionName = "医药"
  728. }
  729. //if startDate == "" || endDate == "" {
  730. // br.Msg = "获取失败,开始日期或结束日期未传"
  731. // br.ErrMsg = "获取失败,开始日期或结束日期未传"
  732. // return
  733. //}
  734. if startDate == "" {
  735. startDate = "2015-01-01"
  736. }
  737. if endDate == "" {
  738. endDate = time.Now().Format(utils.FormatDate)
  739. }
  740. var startSize int
  741. if pageSize <= 0 {
  742. pageSize = utils.PageSize20
  743. }
  744. if currentIndex <= 0 {
  745. currentIndex = 1
  746. }
  747. startSize = utils.StartIndex(currentIndex, pageSize)
  748. //是否导出报表
  749. isExport, _ := this.GetBool("IsExport")
  750. if isExport {
  751. pageSize = 10000
  752. currentIndex = 1
  753. permissionName = "" //点击下载的时候不做行业筛选
  754. }
  755. var condition string
  756. var pars []interface{}
  757. //条件
  758. if adminId != "" {
  759. condition += ` AND c.seller_id in (` + adminId + `) `
  760. //pars = append(pars, adminId)
  761. } else {
  762. //根据当前角色来获取查询条件
  763. condition, pars = getQueryParams(condition, pars, sysUser, "c.")
  764. }
  765. if regionType != "" {
  766. condition += ` AND b.region_type = ? `
  767. pars = append(pars, regionType)
  768. }
  769. //关键字搜索
  770. if keyword != "" {
  771. condition += ` and b.company_name like "%` + keyword + `%" `
  772. }
  773. var conditionAscribRai string // 处理权益未续约客户检索列表SQL查询条件
  774. var conditionAscribRaiTotal string // 处理权益未续约客户总量查询条件
  775. var parsAscribeRai []interface{}
  776. var parsAscribeRaiTotal []interface{} // 处理权益未续约客户总量查询条件
  777. condition += ` AND c.product_id = ? AND a.status = 1 `
  778. pars = append(pars, 2)
  779. var list []*models.IncrementalList
  780. var listPessionGroup []*models.CompanyContractPermissionNameGroupCountResp
  781. var newCompanyTotal int //新签合同数量
  782. var notRenewalCompanyTotal int //未续约客户数已确认
  783. var notRenewalCompanyToBeConfirmTotal int //未续约客户数待确认
  784. var dataTotal, trialTotal, renewalCompanyTotal int
  785. var notRenewalTryOut, notRenewalNotTryOut int
  786. //if dataType == "行业未续约" {
  787. // condition += ` AND a.end_date >= ? AND a.end_date <= ? `
  788. // pars = append(pars, startDate, endDate)
  789. //} else {
  790. // condition += ` AND a.start_date >= ? AND a.start_date <= ? `
  791. // pars = append(pars, startDate, endDate)
  792. //}
  793. //
  794. //conditionPerssion := condition
  795. //parsPerssion := pars
  796. //conditionPerssion += ` AND d.permission_name IN ('医药','消费','科技','智造','策略','买方研选')`
  797. //
  798. //listPessionGroup, err := models.GetCompanyContractPermissionNameGroupCount(conditionPerssion, parsPerssion)
  799. //if err != nil && err.Error() != utils.ErrNoRow() {
  800. // br.Msg = "获取失败"
  801. // br.ErrMsg = "获取失败,Err:" + err.Error()
  802. // return
  803. //}
  804. //if permissionName == "" {
  805. condition += ` AND d.permission_name IN ('医药','消费','科技','智造','策略','研选订阅','研选扣点包')`
  806. //}
  807. var err error
  808. //新签客户数
  809. {
  810. condition1 := condition
  811. pars1 := pars
  812. condition1 += ` AND a.start_date >= ? AND a.start_date <= ? `
  813. pars1 = append(pars1, startDate, endDate)
  814. condition1 += ` AND d.contract_type = ? `
  815. pars1 = append(pars1, "行业新签")
  816. newCompanyTotal, err = company.GetIncrementalNewCompanyProductPermissionCount(condition1, pars1)
  817. if err != nil && err.Error() != utils.ErrNoRow() {
  818. br.Msg = "获取失败"
  819. br.ErrMsg = "获取失败,Err:" + err.Error()
  820. return
  821. }
  822. if dataType == "行业新签" {
  823. conditionPerssion := condition1
  824. parsPerssion := pars1
  825. conditionPerssion += ` AND d.permission_name IN ('医药','消费','科技','智造','策略','研选订阅','研选扣点包')`
  826. listPessionGroup, err = models.GetCompanyContractPermissionNameGroupCount(conditionPerssion, parsPerssion)
  827. if err != nil && err.Error() != utils.ErrNoRow() {
  828. br.Msg = "获取失败"
  829. br.ErrMsg = "获取失败,Err:" + err.Error()
  830. return
  831. }
  832. //行业筛选查询
  833. if permissionName != "" {
  834. condition1 += ` AND d.permission_name = ? `
  835. pars1 = append(pars1, permissionName)
  836. }
  837. total, err := company.GetIncrementalNewCompanyProductPermissionCount(condition1, pars1)
  838. if err != nil && err.Error() != utils.ErrNoRow() {
  839. br.Msg = "获取失败"
  840. br.ErrMsg = "获取失败,Err:" + err.Error()
  841. return
  842. }
  843. //列表页数据
  844. tmpList, err := models.GetIncrementalCompanyPermissionList(condition1, pars1, startSize, pageSize)
  845. if err != nil {
  846. br.Msg = "获取失败"
  847. br.ErrMsg = "获取失败,Err:" + err.Error()
  848. return
  849. }
  850. list = tmpList
  851. dataTotal = total
  852. }
  853. }
  854. //续约客户数
  855. {
  856. condition1 := condition
  857. pars1 := pars
  858. condition1 += ` AND a.start_date >= ? AND a.start_date <= ? `
  859. pars1 = append(pars1, startDate, endDate)
  860. condition1 += ` AND d.contract_type = ? `
  861. pars1 = append(pars1, "行业续约")
  862. ////额外条件(续约合同的起始日期包含在所选时间段内且不包含在新签合同存续期内的客户)
  863. //pars1 = append(pars1, endDate)
  864. total, err := company.GetIncrementalNewCompanyProductPermissionCount(condition1, pars1)
  865. if err != nil && err.Error() != utils.ErrNoRow() {
  866. br.Msg = "获取失败"
  867. br.ErrMsg = "获取失败,Err:" + err.Error()
  868. return
  869. }
  870. renewalCompanyTotal = total
  871. if dataType == "行业续约" {
  872. conditionPerssion := condition1
  873. parsPerssion := pars1
  874. conditionPerssion += ` AND d.permission_name IN ('医药','消费','科技','智造','策略','研选订阅','研选扣点包')`
  875. listPessionGroup, err = models.GetCompanyContractPermissionNameGroupCount(conditionPerssion, parsPerssion)
  876. if err != nil && err.Error() != utils.ErrNoRow() {
  877. br.Msg = "获取失败"
  878. br.ErrMsg = "获取失败,Err:" + err.Error()
  879. return
  880. }
  881. //行业筛选查询
  882. if permissionName != "" {
  883. condition1 += ` AND d.permission_name = ? `
  884. pars1 = append(pars1, permissionName)
  885. }
  886. //列表数据数量
  887. total, err := company.GetIncrementalNewCompanyProductPermissionCount(condition1, pars1)
  888. if err != nil && err.Error() != utils.ErrNoRow() {
  889. br.Msg = "获取失败"
  890. br.ErrMsg = "获取失败,Err:" + err.Error()
  891. return
  892. }
  893. //列表页数据
  894. tmpList, err := models.GetIncrementalCompanyPermissionList(condition1, pars1, startSize, pageSize)
  895. if err != nil {
  896. br.Msg = "获取失败"
  897. br.ErrMsg = "获取失败,Err:" + err.Error()
  898. return
  899. }
  900. list = tmpList
  901. dataTotal = total
  902. }
  903. }
  904. //return
  905. //行业未续约
  906. {
  907. condition1 := condition
  908. pars1 := pars
  909. condition1 += ` AND a.end_date >= ? AND a.end_date <= ? `
  910. pars1 = append(pars1, startDate, endDate)
  911. condition1 += ` AND c.status not in ("永续","正式","关闭") `
  912. //未续约数量
  913. notRenewalCondition := condition1
  914. notRenewalPars := pars1
  915. notRenewalCondition += conditionAscribRaiTotal
  916. notRenewalPars = append(notRenewalPars, parsAscribeRaiTotal)
  917. total, err := company.GetIncrementalNewCompanyProductPermissionCount(notRenewalCondition, notRenewalPars)
  918. if err != nil && err.Error() != utils.ErrNoRow() {
  919. br.Msg = "获取失败"
  920. br.ErrMsg = "获取失败,Err:" + err.Error()
  921. return
  922. }
  923. notRenewalCompanyTotal = total
  924. condition1 += conditionAscribRai
  925. pars1 = append(pars1, parsAscribeRai)
  926. if dataType == "行业未续约" {
  927. conditionPerssion := condition1
  928. parsPerssion := pars1
  929. conditionPerssion += ` AND d.permission_name IN ('医药','消费','科技','智造','策略','研选订阅','研选扣点包')`
  930. listPessionGroup, err = models.GetCompanyContractPermissionNameGroupCount(conditionPerssion, parsPerssion)
  931. if err != nil && err.Error() != utils.ErrNoRow() {
  932. br.Msg = "获取失败"
  933. br.ErrMsg = "获取失败,Err:" + err.Error()
  934. return
  935. }
  936. //行业筛选查询
  937. if permissionName != "" {
  938. condition1 += ` AND d.permission_name = ? `
  939. pars1 = append(pars1, permissionName)
  940. }
  941. //分页total单独计算
  942. total, err = company.GetIncrementalNewCompanyProductPermissionCount(condition1, pars1)
  943. if err != nil && err.Error() != utils.ErrNoRow() {
  944. br.Msg = "获取失败"
  945. br.ErrMsg = "获取失败,Err:" + err.Error()
  946. return
  947. }
  948. //return
  949. //列表页数据
  950. tmpList, err := models.GetIncrementalCompanyPermissionList(condition1, pars1, startSize, pageSize)
  951. if err != nil {
  952. br.Msg = "获取失败"
  953. br.ErrMsg = "获取失败,Err:" + err.Error()
  954. return
  955. }
  956. list = tmpList
  957. dataTotal = total
  958. }
  959. }
  960. listLen := len(list)
  961. if listLen == 0 {
  962. list = make([]*models.IncrementalList, 0)
  963. }
  964. var companyContractIds []int
  965. var companyIds []int
  966. for i := 0; i < listLen; i++ {
  967. item := list[i]
  968. companyContractIds = append(companyContractIds, item.CompanyContractId)
  969. companyIds = append(companyIds, item.CompanyId)
  970. //新增试用不需要计算剩余日期
  971. if dataType != "新增试用" {
  972. //剩余可用天数
  973. endDateTime, _ := time.Parse(utils.FormatDate, item.EndDate)
  974. endDateTime = endDateTime.AddDate(0, 0, 1)
  975. sub := endDateTime.Sub(time.Now())
  976. expireDay := fmt.Sprintf("%v", int(sub.Hours()/24))
  977. list[i].ExpireDay = expireDay
  978. }
  979. }
  980. //行业未续约 需要处理公司对应行业权限下的权限状态
  981. if dataType == "行业未续约" {
  982. mappermissionNameStatus, err := cygxService.GetCompanyPermissionNameStatusMapByCompanyId(companyIds)
  983. if err != nil {
  984. br.Msg = "获取失败"
  985. br.ErrMsg = "获取失败,Err:" + err.Error()
  986. return
  987. }
  988. for _, v := range list {
  989. v.PermissionNameStatus = mappermissionNameStatus[fmt.Sprint(v.CompanyId, v.PermissionNameExport)]
  990. }
  991. }
  992. //合并合同所对应的权限
  993. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  994. if err != nil {
  995. br.Msg = "获取失败"
  996. br.ErrMsg = "获取失败,Err:" + err.Error()
  997. return
  998. }
  999. for _, v := range list {
  1000. v.PermissionName = mappermissionName[v.CompanyContractId]
  1001. }
  1002. //处理展示顺序
  1003. permissionNameSlice := []string{"医药", "消费", "科技", "智造", "策略", utils.CHART_PERMISSION_NAME_MF_YANXUAN, utils.YAN_XUAN_KOU_DIAN_BAO_NAME}
  1004. var itemspermissionNameSlice []*models.CompanyContractPermissionNameGroupCountResp
  1005. for _, vName := range permissionNameSlice {
  1006. item := new(models.CompanyContractPermissionNameGroupCountResp)
  1007. for _, v := range listPessionGroup {
  1008. if v.PermissionName != vName {
  1009. continue
  1010. }
  1011. item.Total = v.Total
  1012. }
  1013. item.PermissionName = vName
  1014. itemspermissionNameSlice = append(itemspermissionNameSlice, item)
  1015. }
  1016. page := paging.GetPaging(currentIndex, pageSize, dataTotal)
  1017. resp := response.IncrementalCompanyListResp{
  1018. Paging: page,
  1019. ListPermissionName: itemspermissionNameSlice,
  1020. List: list,
  1021. TrialTotal: trialTotal,
  1022. NewCompanyTotal: newCompanyTotal,
  1023. RenewalCompanyTotal: renewalCompanyTotal,
  1024. NotRenewalCompanyTotal: notRenewalCompanyTotal,
  1025. NotRenewalCompanyToBeConfirmTotal: notRenewalCompanyToBeConfirmTotal,
  1026. NotRenewalTryOut: notRenewalTryOut,
  1027. NotRenewalNotTryOut: notRenewalNotTryOut,
  1028. }
  1029. //fmt.Println()
  1030. //导出excel
  1031. if isExport {
  1032. PermissionCompanyListListExport(this, dataType, resp, br)
  1033. return
  1034. }
  1035. br.Ret = 200
  1036. br.Success = true
  1037. br.Msg = "获取成功"
  1038. br.Data = resp
  1039. }
  1040. // PermissionCompanyListListExport 导出增量客户数据报表excel
  1041. func PermissionCompanyListListExport(this *StatisticCompanyMergerController, dataType string, resp response.IncrementalCompanyListResp, br *models.BaseResponse) {
  1042. dir, err := os.Executable()
  1043. exPath := filepath.Dir(dir)
  1044. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1045. xlsxFile := xlsx.NewFile()
  1046. if err != nil {
  1047. br.Msg = "生成文件失败"
  1048. br.ErrMsg = "生成文件失败"
  1049. return
  1050. }
  1051. style := xlsx.NewStyle()
  1052. alignment := xlsx.Alignment{
  1053. Horizontal: "center",
  1054. Vertical: "center",
  1055. WrapText: true,
  1056. }
  1057. permissionNameSlice := []string{"医药", "消费", "科技", "智造", "策略", "买方研选"}
  1058. mapExport := make(map[string][]*models.IncrementalList)
  1059. type IncrementalCompanyListExport struct {
  1060. PermissionName string `description:"权限名"`
  1061. List []*models.IncrementalList
  1062. }
  1063. for _, v := range resp.List {
  1064. if v.PermissionNameExport == "" {
  1065. continue
  1066. }
  1067. mapExport[v.PermissionNameExport] = append(mapExport[v.PermissionNameExport], v)
  1068. }
  1069. style.Alignment = alignment
  1070. style.ApplyAlignment = true
  1071. for _, vName := range permissionNameSlice {
  1072. sheel, err := xlsxFile.AddSheet(vName)
  1073. if err != nil {
  1074. br.Msg = "新增Sheet失败"
  1075. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1076. return
  1077. }
  1078. sheel.SetColWidth(0, 0, 30)
  1079. sheel.SetColWidth(1, 1, 15)
  1080. sheel.SetColWidth(2, 2, 15)
  1081. sheel.SetColWidth(3, 3, 18)
  1082. sheel.SetColWidth(4, 4, 40)
  1083. sheel.SetColWidth(5, 5, 18)
  1084. //表头
  1085. titleRow := sheel.AddRow()
  1086. cellName := titleRow.AddCell()
  1087. cellName.SetStyle(style)
  1088. cellName.SetValue("客户名称")
  1089. cellSellerName := titleRow.AddCell()
  1090. cellSellerName.SetStyle(style)
  1091. cellSellerName.SetValue("所属销售")
  1092. cellMoney := titleRow.AddCell()
  1093. cellMoney.SetStyle(style)
  1094. cellMoney.SetValue("合同金额")
  1095. cellTime := titleRow.AddCell()
  1096. cellTime.SetStyle(style)
  1097. cellTime.SetValue("合同期限")
  1098. cellPermissionName := titleRow.AddCell()
  1099. cellPermissionName.SetStyle(style)
  1100. cellPermissionName.SetValue("签约套餐")
  1101. if dataType == "行业未续约" {
  1102. cellPermissionNameStatus := titleRow.AddCell()
  1103. cellPermissionNameStatus.SetStyle(style)
  1104. cellPermissionNameStatus.SetValue("当前行业状态")
  1105. cellCompanyProductStatus := titleRow.AddCell()
  1106. cellCompanyProductStatus.SetStyle(style)
  1107. cellCompanyProductStatus.SetValue("当前客户状态")
  1108. }
  1109. for _, v := range mapExport[vName] {
  1110. dataRow := sheel.AddRow()
  1111. dataRow.SetHeight(20)
  1112. cellDataName := dataRow.AddCell()
  1113. cellDataName.SetStyle(style)
  1114. cellDataName.SetValue(v.CompanyName)
  1115. cellDataSellerName := dataRow.AddCell()
  1116. cellDataSellerName.SetStyle(style)
  1117. cellDataSellerName.SetValue(v.SellerName)
  1118. cellDataMoney := dataRow.AddCell()
  1119. cellDataMoney.SetStyle(style)
  1120. cellDataMoney.SetValue(v.Money)
  1121. cellDataTime := dataRow.AddCell()
  1122. cellDataTime.SetStyle(style)
  1123. cellDataTime.SetValue(v.StartDate + "~" + v.EndDate)
  1124. cellDataPermissionName := dataRow.AddCell()
  1125. cellDataPermissionName.SetStyle(style)
  1126. cellDataPermissionName.SetValue(v.PermissionName)
  1127. if dataType == "行业未续约" {
  1128. cellDataPermissionNameStatus := dataRow.AddCell()
  1129. cellDataPermissionNameStatus.SetStyle(style)
  1130. cellDataPermissionNameStatus.SetValue(v.PermissionNameStatus)
  1131. cellDataCompanyProductStatus := dataRow.AddCell()
  1132. cellDataCompanyProductStatus.SetStyle(style)
  1133. cellDataCompanyProductStatus.SetValue(v.CompanyProductStatus)
  1134. }
  1135. }
  1136. }
  1137. err = xlsxFile.Save(downLoadnFilePath)
  1138. if err != nil {
  1139. br.Msg = "保存文件失败"
  1140. br.ErrMsg = "保存文件失败"
  1141. return
  1142. }
  1143. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1144. downloadFileName := dataType + "数据_" + randStr + ".xlsx"
  1145. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  1146. defer func() {
  1147. os.Remove(downLoadnFilePath)
  1148. }()
  1149. br.Ret = 200
  1150. br.Success = true
  1151. br.Msg = "导出成功"
  1152. }
  1153. // @Title 权益客户续约率统计
  1154. // @Description 权益客户续约率统计接口
  1155. // @Param PageSize query int true "每页数据条数"
  1156. // @Param CurrentIndex query int true "当前页页码,从1开始"
  1157. // @Param AdminId query string true "销售id,多个用英文逗号隔开,空字符串为全部"
  1158. // @Param StartDate query string false "开始日期"
  1159. // @Param EndDate query string false "结束日期"
  1160. // @Param ContractDataType query string false "合同类型,枚举值:`续约合同`,`到期合同`"
  1161. // @Success 200 {object} response.IncrementalCompanyListResp
  1162. // @router /merge_company/company_contract_percentage/list [get]
  1163. func (this *StatisticCompanyMergerController) CompanyContractPercentageList() {
  1164. br := new(models.BaseResponse).Init()
  1165. defer func() {
  1166. this.Data["json"] = br
  1167. this.ServeJSON()
  1168. }()
  1169. sysUser := this.SysUser
  1170. if sysUser == nil {
  1171. br.Msg = "请登录"
  1172. br.ErrMsg = "请登录,SysUser Is Empty"
  1173. br.Ret = 408
  1174. return
  1175. }
  1176. pageSize, _ := this.GetInt("PageSize")
  1177. currentIndex, _ := this.GetInt("CurrentIndex")
  1178. adminId := this.GetString("AdminId")
  1179. contractDataType := this.GetString("ContractDataType")
  1180. startDate := this.GetString("StartDate")
  1181. endDate := this.GetString("EndDate")
  1182. initendDate := this.GetString("EndDate")
  1183. if startDate == "" || endDate == "" {
  1184. br.Msg = "开始时间或结束时间不能为空"
  1185. return
  1186. }
  1187. //如果传的查询方式是今年,那么就以当前时间作为截止时间
  1188. //if endDate == strconv.Itoa(time.Now().Year())+"-12-31" {
  1189. // endDate = time.Now().Format(utils.FormatDate)
  1190. //}
  1191. //判断结束时间是否晚于当前时间,如果晚于当前时间,那么就把当前时间作为截止时间。
  1192. endDateTime, _ := time.Parse(utils.FormatDate, endDate)
  1193. if endDateTime.After(time.Now()) {
  1194. endDate = time.Now().Format(utils.FormatDate)
  1195. }
  1196. //fmt.Println("endDate", endDate)
  1197. if contractDataType == "" {
  1198. contractDataType = "续约合同"
  1199. }
  1200. var startSize int
  1201. if pageSize <= 0 {
  1202. pageSize = utils.PageSize20
  1203. }
  1204. if currentIndex <= 0 {
  1205. currentIndex = 1
  1206. }
  1207. startSize = utils.StartIndex(currentIndex, pageSize)
  1208. var condition string
  1209. var pars []interface{}
  1210. //条件
  1211. if adminId != "" {
  1212. condition += ` AND c.seller_id in (` + adminId + `) `
  1213. } else {
  1214. //根据当前角色来获取查询条件
  1215. condition, pars = getQueryParams(condition, pars, sysUser, "c.")
  1216. }
  1217. //权益有效合同
  1218. condition += ` AND c.product_id = 2 AND a.status = 1 `
  1219. var list []*models.IncrementalList
  1220. var renewalRateMoney string //合同金额续约率
  1221. var renewalRateMoneyContent string //合同金额续约率详情
  1222. var renewalRateTotal string //合同数量续约率
  1223. var renewalRateTotalContent string //合同数量续约率详情
  1224. var renewalContractTotal int //续约合同数量
  1225. var renewalContractMoney int //续约合同数量
  1226. var expireRenewalContractTotal int //到期合同数量
  1227. var expireRenewalContractMoney int //到期合同数量
  1228. var dataTotal int //分页数据
  1229. conditionRenewal := condition // 续约合同查询条件查询条件
  1230. parsRenewal := pars
  1231. conditionRenewal += ` AND a.start_date >= ? AND a.start_date <= ? `
  1232. parsRenewal = append(parsRenewal, startDate, endDate)
  1233. conditionRenewal += ` AND a.rai_contract_type = ? `
  1234. parsRenewal = append(parsRenewal, "续约合同")
  1235. totalRenewal, err := company.GetIncrementalRenewalCompanyProductMergeCount(conditionRenewal, parsRenewal)
  1236. if err != nil {
  1237. br.Msg = "获取失败"
  1238. br.ErrMsg = "获取失败,Err:" + err.Error()
  1239. return
  1240. }
  1241. totalMoneyRenewal, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(conditionRenewal, parsRenewal)
  1242. if err != nil {
  1243. br.Msg = "获取失败"
  1244. br.ErrMsg = "获取失败,Err:" + err.Error()
  1245. return
  1246. }
  1247. //续约合同查询条件查询条件 end
  1248. conditionRenewalEnd := condition // 今年结束的续约合同查询条件
  1249. parsRenewalEnd := pars
  1250. conditionRenewalEnd += ` AND a.end_date >= ? AND a.end_date <= ? `
  1251. parsRenewalEnd = append(parsRenewalEnd, startDate, endDate)
  1252. conditionRenewalEnd += ` AND a.rai_contract_type = ? `
  1253. parsRenewalEnd = append(parsRenewalEnd, "续约合同")
  1254. totalRenewalEnd, err := company.GetIncrementalRenewalCompanyProductMergeCount(conditionRenewalEnd, parsRenewalEnd)
  1255. if err != nil && err.Error() != utils.ErrNoRow() {
  1256. br.Msg = "获取失败"
  1257. br.ErrMsg = "获取失败,Err:" + err.Error()
  1258. return
  1259. }
  1260. totalMoneyRenewalEnd, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(conditionRenewalEnd, parsRenewalEnd)
  1261. if err != nil {
  1262. br.Msg = "获取失败"
  1263. br.ErrMsg = "获取失败,Err:" + err.Error()
  1264. return
  1265. } // 今年结束的续约合同查询条件end
  1266. conditionNew := condition // 新签合同查询条件
  1267. parsNew := pars
  1268. conditionNew += ` AND a.end_date >= ? AND a.end_date <= ? `
  1269. parsNew = append(parsNew, startDate, endDate)
  1270. conditionNew += ` AND a.rai_contract_type = ? `
  1271. parsNew = append(parsNew, "新签合同")
  1272. totalNew, err := company.GetIncrementalRenewalCompanyProductMergeCount(conditionNew, parsNew)
  1273. if err != nil && err.Error() != utils.ErrNoRow() {
  1274. br.Msg = "获取失败"
  1275. br.ErrMsg = "获取失败,Err:" + err.Error()
  1276. return
  1277. }
  1278. totalMoneyNew, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(conditionNew, parsNew)
  1279. if err != nil {
  1280. br.Msg = "获取失败"
  1281. br.ErrMsg = "获取失败,Err:" + err.Error()
  1282. return
  1283. } // 新签合同查询条件 end
  1284. //分子或者分母为零的时候,不做计算
  1285. if totalRenewal == 0 || totalNew+totalRenewalEnd == 0 {
  1286. renewalRateMoney = "0%"
  1287. renewalRateTotal = "0%"
  1288. } else {
  1289. renewalRateMoney = fmt.Sprint(utils.SubFloatToString(float64(totalMoneyRenewal)/float64(totalMoneyNew+totalMoneyRenewalEnd)*100, 2), "%")
  1290. renewalRateTotal = fmt.Sprint(utils.SubFloatToString(float64(totalRenewal)/float64(totalNew+totalRenewalEnd)*100, 2), "%")
  1291. }
  1292. renewalRateTotalContent = fmt.Sprint(int(totalMoneyRenewal), "/", int(totalMoneyNew+totalMoneyRenewalEnd))
  1293. renewalRateMoneyContent = fmt.Sprint(totalRenewal, "份/", totalNew+totalRenewalEnd, "份")
  1294. if contractDataType == "续约合同" {
  1295. condition1 := condition
  1296. pars1 := pars
  1297. condition1 += ` AND a.start_date >= ? AND a.start_date <= ? `
  1298. pars1 = append(pars1, startDate, endDate)
  1299. condition1 += ` AND a.rai_contract_type = ? `
  1300. pars1 = append(pars1, "续约合同")
  1301. total, err := company.GetIncrementalRenewalCompanyProductMergeCount(condition1, pars1)
  1302. if err != nil && err.Error() != utils.ErrNoRow() {
  1303. br.Msg = "获取失败"
  1304. br.ErrMsg = "获取失败,Err:" + err.Error()
  1305. return
  1306. }
  1307. totalMoney, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(condition1, pars1)
  1308. if err != nil && err.Error() != utils.ErrNoRow() {
  1309. br.Msg = "获取失败"
  1310. br.ErrMsg = "获取失败,Err:" + err.Error()
  1311. return
  1312. }
  1313. renewalContractTotal = total
  1314. renewalContractMoney = int(totalMoney)
  1315. //列表页数据
  1316. tmpList, err := models.GetIncrementalCompanyMergeList(condition1, pars1, startSize, pageSize)
  1317. if err != nil {
  1318. br.Msg = "获取失败"
  1319. br.ErrMsg = "获取失败,Err:" + err.Error()
  1320. return
  1321. }
  1322. list = tmpList
  1323. dataTotal = total
  1324. }
  1325. if contractDataType == "到期合同" {
  1326. condition1 := condition
  1327. pars1 := pars
  1328. //判断结束时间是否晚于当前时间,如果晚于当前时间,那么就把当前时间作为截止时间。并且当前这天的到期的合同,不查询
  1329. endDateTime, _ := time.Parse(utils.FormatDate, initendDate)
  1330. if endDateTime.After(time.Now()) {
  1331. endDate = time.Now().Format(utils.FormatDate)
  1332. condition1 += ` AND a.end_date >= ? AND a.end_date < ? `
  1333. } else {
  1334. condition1 += ` AND a.end_date >= ? AND a.end_date <= ? `
  1335. }
  1336. //condition1 += ` AND a.end_date >= ? AND a.end_date <= ? `
  1337. pars1 = append(pars1, startDate, endDate)
  1338. total, err := company.GetIncrementalRenewalCompanyProductMergeCount(condition1, pars1)
  1339. if err != nil && err.Error() != utils.ErrNoRow() {
  1340. br.Msg = "获取失败"
  1341. br.ErrMsg = "获取失败,Err:" + err.Error()
  1342. return
  1343. }
  1344. totalMoney, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(condition1, pars1)
  1345. if err != nil && err.Error() != utils.ErrNoRow() {
  1346. br.Msg = "获取失败"
  1347. br.ErrMsg = "获取失败,Err:" + err.Error()
  1348. return
  1349. }
  1350. expireRenewalContractTotal = total
  1351. expireRenewalContractMoney = int(totalMoney)
  1352. //列表页数据
  1353. tmpList, err := models.GetIncrementalCompanyMergeListEnd(condition1, pars1, startSize, pageSize)
  1354. if err != nil {
  1355. br.Msg = "获取失败"
  1356. br.ErrMsg = "获取失败,Err:" + err.Error()
  1357. return
  1358. }
  1359. list = tmpList
  1360. dataTotal = total
  1361. }
  1362. listLen := len(list)
  1363. if listLen == 0 {
  1364. list = make([]*models.IncrementalList, 0)
  1365. }
  1366. var companyContractIds []int
  1367. for i := 0; i < listLen; i++ {
  1368. item := list[i]
  1369. companyContractIds = append(companyContractIds, item.CompanyContractId)
  1370. }
  1371. //合并合同所对应的权限
  1372. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  1373. if err != nil {
  1374. br.Msg = "获取失败"
  1375. br.ErrMsg = "获取失败,Err:" + err.Error()
  1376. return
  1377. }
  1378. for _, v := range list {
  1379. v.PermissionName = mappermissionName[v.CompanyContractId]
  1380. }
  1381. page := paging.GetPaging(currentIndex, pageSize, dataTotal)
  1382. resp := response.IncrementalCompanyPercentageListResp{
  1383. Paging: page,
  1384. RenewalContractTotal: renewalContractTotal,
  1385. RenewalContractMoney: renewalContractMoney,
  1386. ExpireRenewalContractTotal: expireRenewalContractTotal,
  1387. ExpireRenewalContractMoney: expireRenewalContractMoney,
  1388. RenewalRateMoney: renewalRateMoney,
  1389. RenewalRateMoneyContent: renewalRateMoneyContent,
  1390. RenewalRateTotal: renewalRateTotal,
  1391. RenewalRateTotalContent: renewalRateTotalContent,
  1392. List: list,
  1393. }
  1394. br.Ret = 200
  1395. br.Success = true
  1396. br.Msg = "获取成功"
  1397. br.Data = resp
  1398. }
  1399. // @Title 权益客户续约率统计所能查询的年份
  1400. // @Description权益客户续约率统计所能查询的年份接口
  1401. // @Success 200 {object} response.IncrementalCompanyListResp
  1402. // @router /merge_company/get_year_list [get]
  1403. func (this *StatisticCompanyMergerController) GetYearList() {
  1404. br := new(models.BaseResponse).Init()
  1405. defer func() {
  1406. this.Data["json"] = br
  1407. this.ServeJSON()
  1408. }()
  1409. sysUser := this.SysUser
  1410. if sysUser == nil {
  1411. br.Msg = "请登录"
  1412. br.ErrMsg = "请登录,SysUser Is Empty"
  1413. br.Ret = 408
  1414. return
  1415. }
  1416. thisYear := time.Now().Year()
  1417. startYear := 2022 //初始查询年份
  1418. resp := new(company.SearchYearListResp)
  1419. var yearList []int
  1420. for i := startYear; i <= thisYear; i++ {
  1421. yearList = append(yearList, i)
  1422. }
  1423. resp.List = yearList
  1424. br.Ret = 200
  1425. br.Success = true
  1426. br.Msg = "获取成功"
  1427. br.Data = resp
  1428. }
  1429. // @Title 权益客户续约率统计
  1430. // @Description 权益客户续约率统计接口
  1431. // @Param PageSize query int true "每页数据条数"
  1432. // @Param CurrentIndex query int true "当前页页码,从1开始"
  1433. // @Param AdminId query string true "销售id,多个用英文逗号隔开,空字符串为全部"
  1434. // @Param StartDate query string false "开始日期"
  1435. // @Param EndDate query string false "结束日期"
  1436. // @Param ContractDataType query string false "合同类型,枚举值:`续约合同`,`到期合同`"
  1437. // @Success 200 {object} response.IncrementalCompanyListResp
  1438. // @router /merge_company/company_contract_percentage/listV2 [get]
  1439. func (this *StatisticCompanyMergerController) CompanyContractPercentageListV2() {
  1440. br := new(models.BaseResponse).Init()
  1441. defer func() {
  1442. this.Data["json"] = br
  1443. this.ServeJSON()
  1444. }()
  1445. sysUser := this.SysUser
  1446. if sysUser == nil {
  1447. br.Msg = "请登录"
  1448. br.ErrMsg = "请登录,SysUser Is Empty"
  1449. br.Ret = 408
  1450. return
  1451. }
  1452. pageSize, _ := this.GetInt("PageSize")
  1453. currentIndex, _ := this.GetInt("CurrentIndex")
  1454. adminId := this.GetString("AdminId")
  1455. contractDataType := this.GetString("ContractDataType")
  1456. startDate := this.GetString("StartDate")
  1457. endDate := this.GetString("EndDate")
  1458. //initendDate := this.GetString("EndDate")
  1459. if startDate == "" || endDate == "" {
  1460. br.Msg = "开始时间或结束时间不能为空"
  1461. return
  1462. }
  1463. //如果传的查询方式是今年,那么就以当前时间作为截止时间
  1464. //if endDate == strconv.Itoa(time.Now().Year())+"-12-31" {
  1465. // endDate = time.Now().Format(utils.FormatDate)
  1466. //}
  1467. //判断结束时间是否晚于当前时间,如果晚于当前时间,那么就把当前时间作为截止时间。
  1468. endDateTime, _ := time.Parse(utils.FormatDate, endDate)
  1469. if endDateTime.After(time.Now()) {
  1470. endDate = time.Now().Format(utils.FormatDate)
  1471. }
  1472. //fmt.Println("endDate", endDate)
  1473. if contractDataType == "" {
  1474. contractDataType = "续约合同"
  1475. }
  1476. var startSize int
  1477. if pageSize <= 0 {
  1478. pageSize = utils.PageSize20
  1479. }
  1480. if currentIndex <= 0 {
  1481. currentIndex = 1
  1482. }
  1483. startSize = utils.StartIndex(currentIndex, pageSize)
  1484. var condition string
  1485. var pars []interface{}
  1486. //条件
  1487. if adminId != "" {
  1488. condition += ` AND c.seller_id in (` + adminId + `) `
  1489. } else {
  1490. //根据当前角色来获取查询条件
  1491. condition, pars = getQueryParams(condition, pars, sysUser, "c.")
  1492. }
  1493. //权益有效合同
  1494. condition += ` AND c.product_id = 2 AND a.status = 1 `
  1495. var list []*models.IncrementalList
  1496. var renewalRateMoney string //合同金额续约率
  1497. var renewalRateMoneyContent string //合同金额续约率详情
  1498. var renewalRateTotal string //合同数量续约率
  1499. var renewalRateTotalContent string //合同数量续约率详情
  1500. var renewalContractTotal int //续约合同数量
  1501. var renewalContractMoney int //续约合同数量
  1502. var expireRenewalContractTotal int //到期合同数量
  1503. var expireRenewalContractMoney int //到期合同数量
  1504. var dataTotal int //分页数据
  1505. //conditionRenewal := condition // 续约合同查询条件查询条件
  1506. //parsRenewal := pars
  1507. //conditionRenewal += ` AND a.start_date >= ? AND a.start_date <= ? `
  1508. //conditionRenewal += ` AND a.end_date >= ? AND a.end_date <= ? `
  1509. //parsRenewal = append(parsRenewal, startDate, endDate)
  1510. ////conditionRenewal += ` AND a.rai_contract_type = ? `
  1511. ////parsRenewal = append(parsRenewal, "续约合同")
  1512. //
  1513. //conditionRenewal += ` AND a.company_ascribe_id > 0 AND a.company_ascribe_id !=9 ` // 已确认未续约
  1514. //totalRenewal, err := company.GetIncrementalRenewalCompanyProductMergeCount(conditionRenewal, parsRenewal)
  1515. //if err != nil {
  1516. // br.Msg = "获取失败"
  1517. // br.ErrMsg = "获取失败,Err:" + err.Error()
  1518. // return
  1519. //}
  1520. //
  1521. //totalMoneyRenewal, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(conditionRenewal, parsRenewal)
  1522. //if err != nil {
  1523. // br.Msg = "获取失败"
  1524. // br.ErrMsg = "获取失败,Err:" + err.Error()
  1525. // return
  1526. //}
  1527. //续约合同查询条件查询条件 end
  1528. //conditionRenewalEnd := condition // 今年结束的续约合同查询条件
  1529. //parsRenewalEnd := pars
  1530. //
  1531. ////startDateTime, _ := time.Parse(utils.FormatDate, endDate)
  1532. ////startDateTime = startDateTime.AddDate(-1, 0, 2) //合同开始时间,与查询的结束时间,之间相差的天数大于 365天。
  1533. ////fmt.Println("startDateTime", startDateTime)
  1534. //conditionRenewalEnd += ` AND a.end_date >= ? AND a.end_date <= ? `
  1535. //parsRenewalEnd = append(parsRenewalEnd, startDate, endDate)
  1536. //conditionRenewalEnd += ` AND a.rai_contract_type = ? `
  1537. //parsRenewalEnd = append(parsRenewalEnd, "续约合同")
  1538. //
  1539. //totalRenewalEnd, err := company.GetIncrementalRenewalCompanyProductMergeCount(conditionRenewalEnd, parsRenewalEnd)
  1540. //if err != nil && err.Error() != utils.ErrNoRow() {
  1541. // br.Msg = "获取失败"
  1542. // br.ErrMsg = "获取失败,Err:" + err.Error()
  1543. // return
  1544. //}
  1545. ////return
  1546. //totalMoneyRenewalEnd, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(conditionRenewalEnd, parsRenewalEnd)
  1547. //if err != nil {
  1548. // br.Msg = "获取失败"
  1549. // br.ErrMsg = "获取失败,Err:" + err.Error()
  1550. // return
  1551. //} // 今年结束的续约合同查询条件end
  1552. //
  1553. //conditionNew := condition // 新签合同查询条件
  1554. //parsNew := pars
  1555. //conditionNew += ` AND a.end_date >= ? AND a.end_date <= ? `
  1556. //parsNew = append(parsNew, startDate, endDate)
  1557. //conditionNew += ` AND a.rai_contract_type = ? `
  1558. //parsNew = append(parsNew, "新签合同")
  1559. //totalNew, err := company.GetIncrementalRenewalCompanyProductMergeCount(conditionNew, parsNew)
  1560. //if err != nil && err.Error() != utils.ErrNoRow() {
  1561. // br.Msg = "获取失败"
  1562. // br.ErrMsg = "获取失败,Err:" + err.Error()
  1563. // return
  1564. //}
  1565. //totalMoneyNew, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(conditionNew, parsNew)
  1566. //if err != nil {
  1567. // br.Msg = "获取失败"
  1568. // br.ErrMsg = "获取失败,Err:" + err.Error()
  1569. // return
  1570. //} // 新签合同查询条件 end
  1571. //fmt.Println("totalNew", totalNew)
  1572. //fmt.Println("totalRenewalEnd", totalRenewalEnd)
  1573. condition1 := condition
  1574. pars1 := pars
  1575. //condition1 += ` AND a.start_date >= ? AND a.start_date <= ? `
  1576. condition1 += ` AND a.end_date >= ? AND a.end_date <= ? `
  1577. pars1 = append(pars1, startDate, endDate)
  1578. //condition1 += ` AND a.rai_contract_type = ? `
  1579. //pars1 = append(pars1, "续约合同")
  1580. condition1 += ` AND a.company_ascribe_id > 0 AND a.company_ascribe_id !=9 ` // 已确认未续约
  1581. totalNo, err := company.GetIncrementalRenewalCompanyProductMergeCount(condition1, pars1)
  1582. if err != nil && err.Error() != utils.ErrNoRow() {
  1583. br.Msg = "获取失败"
  1584. br.ErrMsg = "获取失败,Err:" + err.Error()
  1585. return
  1586. }
  1587. totalMoney, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(condition1, pars1)
  1588. if err != nil && err.Error() != utils.ErrNoRow() {
  1589. br.Msg = "获取失败"
  1590. br.ErrMsg = "获取失败,Err:" + err.Error()
  1591. return
  1592. }
  1593. renewalContractTotal = totalNo
  1594. renewalContractMoney = int(totalMoney)
  1595. if contractDataType == "续约合同" || contractDataType == "确认不续约合同" {
  1596. //列表页数据
  1597. tmpList, err := models.GetIncrementalCompanyMergeListEnd(condition1, pars1, startSize, pageSize)
  1598. if err != nil {
  1599. br.Msg = "获取失败"
  1600. br.ErrMsg = "获取失败,Err:" + err.Error()
  1601. return
  1602. }
  1603. list = tmpList
  1604. dataTotal = totalNo
  1605. }
  1606. conditionEnd1 := condition
  1607. parsEnd1 := pars
  1608. //判断结束时间是否晚于当前时间,如果晚于当前时间,那么就把当前时间作为截止时间。并且当前这天的到期的合同,不查询
  1609. //endDateTime, _ := time.Parse(utils.FormatDate, initendDate)
  1610. //if endDateTime.After(time.Now()) {
  1611. // endDate = time.Now().Format(utils.FormatDate)
  1612. // condition1 += ` AND a.end_date >= ? AND a.end_date < ? `
  1613. //} else {
  1614. // condition1 += ` AND a.end_date >= ? AND a.end_date <= ? `
  1615. //}
  1616. conditionEnd1 += ` AND a.end_date >= ? AND a.end_date <= ? AND a.company_ascribe_id !=9 `
  1617. parsEnd1 = append(parsEnd1, startDate, endDate)
  1618. //condition1 += ` AND a.start_date < ? `
  1619. //pars1 = append(pars1, startDateTime)
  1620. totalEnd, err := company.GetIncrementalRenewalCompanyProductMergeCount(conditionEnd1, parsEnd1)
  1621. if err != nil && err.Error() != utils.ErrNoRow() {
  1622. br.Msg = "获取失败"
  1623. br.ErrMsg = "获取失败,Err:" + err.Error()
  1624. return
  1625. }
  1626. totalMoneyEnd, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(conditionEnd1, parsEnd1)
  1627. if err != nil && err.Error() != utils.ErrNoRow() {
  1628. br.Msg = "获取失败"
  1629. br.ErrMsg = "获取失败,Err:" + err.Error()
  1630. return
  1631. }
  1632. expireRenewalContractTotal = totalEnd
  1633. expireRenewalContractMoney = int(totalMoneyEnd)
  1634. if contractDataType == "到期合同" {
  1635. //列表页数据
  1636. tmpList, err := models.GetIncrementalCompanyMergeListEnd(conditionEnd1, parsEnd1, startSize, pageSize)
  1637. if err != nil {
  1638. br.Msg = "获取失败"
  1639. br.ErrMsg = "获取失败,Err:" + err.Error()
  1640. return
  1641. }
  1642. list = tmpList
  1643. dataTotal = totalEnd
  1644. }
  1645. //分子或者分母为零的时候,不做计算
  1646. if totalNo == 0 || totalEnd == 0 {
  1647. renewalRateMoney = "0%"
  1648. renewalRateTotal = "0%"
  1649. } else {
  1650. renewalRateMoney = fmt.Sprint(utils.SubFloatToString(float64(totalMoney)/float64(totalMoneyEnd)*100, 2), "%")
  1651. renewalRateTotal = fmt.Sprint(utils.SubFloatToString(float64(totalNo)/float64(totalEnd)*100, 2), "%")
  1652. }
  1653. renewalRateTotalContent = fmt.Sprint(int(totalMoney), "/", int(totalMoneyEnd))
  1654. renewalRateMoneyContent = fmt.Sprint(totalNo, "份/", totalEnd, "份")
  1655. listLen := len(list)
  1656. if listLen == 0 {
  1657. list = make([]*models.IncrementalList, 0)
  1658. }
  1659. var companyContractIds []int
  1660. for i := 0; i < listLen; i++ {
  1661. item := list[i]
  1662. companyContractIds = append(companyContractIds, item.CompanyContractId)
  1663. }
  1664. //合同归因标签
  1665. mapGetCompanyAscribeContent, mapContent := services.GetCompanyContractAscribeContentMap(companyContractIds)
  1666. //合并合同所对应的权限
  1667. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  1668. if err != nil {
  1669. br.Msg = "获取失败"
  1670. br.ErrMsg = "获取失败,Err:" + err.Error()
  1671. return
  1672. }
  1673. for _, v := range list {
  1674. v.PermissionName = mappermissionName[v.CompanyContractId]
  1675. if mapGetCompanyAscribeContent[v.CompanyContractId] != "" {
  1676. v.Content = mapContent[v.CompanyContractId]
  1677. v.AscribeContent = mapGetCompanyAscribeContent[v.CompanyContractId]
  1678. v.IsShowNoRenewedNote = true
  1679. }
  1680. }
  1681. page := paging.GetPaging(currentIndex, pageSize, dataTotal)
  1682. resp := response.IncrementalCompanyPercentageListResp{
  1683. Paging: page,
  1684. RenewalContractTotal: renewalContractTotal,
  1685. RenewalContractMoney: renewalContractMoney,
  1686. ExpireRenewalContractTotal: expireRenewalContractTotal,
  1687. ExpireRenewalContractMoney: expireRenewalContractMoney,
  1688. RenewalRateMoney: renewalRateMoney,
  1689. RenewalRateMoneyContent: renewalRateMoneyContent,
  1690. RenewalRateTotal: renewalRateTotal,
  1691. RenewalRateTotalContent: renewalRateTotalContent,
  1692. List: list,
  1693. }
  1694. br.Ret = 200
  1695. br.Success = true
  1696. br.Msg = "获取成功"
  1697. br.Data = resp
  1698. }
  1699. // @Title 权益客户未续约率下载
  1700. // @Description 权益客户未续约率下载接口
  1701. // @Param AdminId query string true "销售id,多个用英文逗号隔开,空字符串为全部"
  1702. // @Param StartDate query string false "开始日期"
  1703. // @Param EndDate query string false "结束日期"
  1704. // @Param ExportType query int true "下载类型 1:下载当前销售的合同明细数据;2:下载所有销售未续约数据列表"
  1705. // @Success 200 {object} response.IncrementalCompanyListResp
  1706. // @router /merge_company/company_contract_percentage/list_export [get]
  1707. func (this *StatisticCompanyMergerController) CompanyContractPercentageListExport() {
  1708. br := new(models.BaseResponse).Init()
  1709. defer func() {
  1710. this.Data["json"] = br
  1711. this.ServeJSON()
  1712. }()
  1713. sysUser := this.SysUser
  1714. if sysUser == nil {
  1715. br.Msg = "请登录"
  1716. br.ErrMsg = "请登录,SysUser Is Empty"
  1717. br.Ret = 408
  1718. return
  1719. }
  1720. pageSize, _ := this.GetInt("PageSize")
  1721. currentIndex, _ := this.GetInt("CurrentIndex")
  1722. exportType, _ := this.GetInt("ExportType")
  1723. adminId := this.GetString("AdminId")
  1724. contractDataType := this.GetString("ContractDataType")
  1725. startDate := this.GetString("StartDate")
  1726. endDate := this.GetString("EndDate")
  1727. //initendDate := this.GetString("EndDate")
  1728. if startDate == "" || endDate == "" {
  1729. br.Msg = "开始时间或结束时间不能为空"
  1730. return
  1731. }
  1732. //判断结束时间是否晚于当前时间,如果晚于当前时间,那么就把当前时间作为截止时间。
  1733. endDateTime, _ := time.Parse(utils.FormatDate, endDate)
  1734. if endDateTime.After(time.Now()) {
  1735. endDate = time.Now().Format(utils.FormatDate)
  1736. }
  1737. //fmt.Println("endDate", endDate)
  1738. if contractDataType == "" {
  1739. contractDataType = "续约合同"
  1740. }
  1741. var startSize int
  1742. if pageSize <= 0 {
  1743. pageSize = 10000
  1744. }
  1745. if currentIndex <= 0 {
  1746. currentIndex = 1
  1747. }
  1748. startSize = utils.StartIndex(currentIndex, pageSize)
  1749. var condition string
  1750. var pars []interface{}
  1751. if exportType == 1 {
  1752. //条件
  1753. if adminId != "" {
  1754. condition += ` AND a.seller_id_init in (` + adminId + `) `
  1755. }
  1756. }
  1757. //权益有效合同
  1758. condition += ` AND c.product_id = 2 AND a.status = 1 `
  1759. condition += ` AND c.status not in ("永续","正式","关闭") `
  1760. //var list []*models.IncrementalList
  1761. condition1 := condition
  1762. pars1 := pars
  1763. condition1 += ` AND a.end_date >= ? AND a.end_date <= ? `
  1764. pars1 = append(pars1, startDate, endDate)
  1765. condition1 += ` AND a.company_ascribe_id > 0 AND a.company_ascribe_id !=9 ` // 已确认未续约
  1766. //if contractDataType == "续约合同" || contractDataType == "确认不续约合同" {
  1767. //不续约列表数据
  1768. listRenewal, err := models.GetIncrementalCompanyMergeListEnd(condition1, pars1, startSize, pageSize)
  1769. if err != nil {
  1770. br.Msg = "获取失败"
  1771. br.ErrMsg = "获取失败,Err:" + err.Error()
  1772. return
  1773. }
  1774. conditionEnd1 := condition
  1775. parsEnd1 := pars
  1776. conditionEnd1 += ` AND a.end_date >= ? AND a.end_date <= ? AND a.company_ascribe_id !=9 `
  1777. parsEnd1 = append(parsEnd1, startDate, endDate)
  1778. //if contractDataType == "到期合同" {
  1779. //到期列表数据
  1780. listEndDate, err := models.GetIncrementalCompanyMergeListEnd(conditionEnd1, parsEnd1, startSize, pageSize)
  1781. if err != nil {
  1782. br.Msg = "获取失败"
  1783. br.ErrMsg = "获取失败,Err:" + err.Error()
  1784. return
  1785. }
  1786. var companyContractIds []int
  1787. for _, v := range listRenewal {
  1788. companyContractIds = append(companyContractIds, v.CompanyContractId)
  1789. v.SellerId = v.SellerIdInit
  1790. }
  1791. for _, v := range listEndDate {
  1792. companyContractIds = append(companyContractIds, v.CompanyContractId)
  1793. v.SellerId = v.SellerIdInit
  1794. }
  1795. //合同归因标签
  1796. mapGetCompanyAscribeContent, mapContent := services.GetCompanyContractAscribeContentMap(companyContractIds)
  1797. //合并合同所对应的权限
  1798. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  1799. if err != nil {
  1800. br.Msg = "获取失败"
  1801. br.ErrMsg = "获取失败,Err:" + err.Error()
  1802. return
  1803. }
  1804. for _, v := range listRenewal {
  1805. v.PermissionName = mappermissionName[v.CompanyContractId]
  1806. if mapGetCompanyAscribeContent[v.CompanyContractId] != "" {
  1807. v.Content = mapContent[v.CompanyContractId]
  1808. v.AscribeContent = mapGetCompanyAscribeContent[v.CompanyContractId]
  1809. v.IsShowNoRenewedNote = true
  1810. }
  1811. }
  1812. for _, v := range listEndDate {
  1813. v.PermissionName = mappermissionName[v.CompanyContractId]
  1814. if mapGetCompanyAscribeContent[v.CompanyContractId] != "" {
  1815. v.Content = mapContent[v.CompanyContractId]
  1816. v.AscribeContent = mapGetCompanyAscribeContent[v.CompanyContractId]
  1817. v.IsShowNoRenewedNote = true
  1818. }
  1819. }
  1820. //创建excel
  1821. dir, err := os.Executable()
  1822. exPath := filepath.Dir(dir)
  1823. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1824. xlsxFile := xlsx.NewFile()
  1825. if err != nil {
  1826. br.Msg = "生成文件失败"
  1827. br.ErrMsg = "生成文件失败"
  1828. return
  1829. }
  1830. style := xlsx.NewStyle()
  1831. alignment := xlsx.Alignment{
  1832. Horizontal: "center",
  1833. Vertical: "center",
  1834. WrapText: true,
  1835. }
  1836. style.Alignment = alignment
  1837. style.ApplyAlignment = true
  1838. if exportType == 1 {
  1839. for pidIndex := 0; pidIndex <= 1; pidIndex++ {
  1840. var sheetName string
  1841. var listDate []*models.IncrementalList
  1842. if pidIndex == 0 {
  1843. sheetName = "确认不续约合同"
  1844. listDate = listRenewal
  1845. } else {
  1846. sheetName = "到期合同"
  1847. listDate = listEndDate
  1848. }
  1849. sheet, err := xlsxFile.AddSheet(sheetName)
  1850. if err != nil {
  1851. br.Msg = "新增Sheet失败"
  1852. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1853. return
  1854. }
  1855. //标头
  1856. rowTitle := sheet.AddRow()
  1857. cellA := rowTitle.AddCell()
  1858. cellA.Value = "客户名称"
  1859. cellB := rowTitle.AddCell()
  1860. cellB.Value = "客户状态"
  1861. cellC := rowTitle.AddCell()
  1862. cellC.Value = "所属销售"
  1863. cellCSeller := rowTitle.AddCell()
  1864. cellCSeller.Value = "服务销售"
  1865. cellD := rowTitle.AddCell()
  1866. cellD.Value = "合同期限"
  1867. cellE := rowTitle.AddCell()
  1868. cellE.Value = "签约套餐"
  1869. cellF := rowTitle.AddCell()
  1870. cellF.Value = "合同金额"
  1871. if pidIndex == 0 {
  1872. cellG := rowTitle.AddCell()
  1873. cellG.Value = "不续约归因"
  1874. }
  1875. for _, item := range listDate {
  1876. row := sheet.AddRow()
  1877. cellAData := row.AddCell()
  1878. cellAData.Value = item.CompanyName
  1879. cellBData := row.AddCell()
  1880. cellBData.Value = item.ProductStatus
  1881. cellCData := row.AddCell()
  1882. cellCData.Value = item.SellerName
  1883. cellCSellerData := row.AddCell()
  1884. cellCSellerData.Value = item.ShareSeller
  1885. cellDData := row.AddCell()
  1886. cellDData.Value = item.StartDate + "~" + item.EndDate
  1887. cellEData := row.AddCell()
  1888. cellEData.Value = item.PermissionName
  1889. cellFData := row.AddCell()
  1890. cellFData.Value = fmt.Sprint(item.Money)
  1891. if pidIndex == 0 {
  1892. cellGData := row.AddCell()
  1893. cellGData.Value = item.Content
  1894. }
  1895. }
  1896. }
  1897. err = xlsxFile.Save(downLoadnFilePath)
  1898. if err != nil {
  1899. br.Msg = "保存文件失败"
  1900. br.ErrMsg = "保存文件失败"
  1901. return
  1902. }
  1903. } else {
  1904. var sheetName string
  1905. //var listDate []*models.IncrementalList
  1906. sheetName = "未续约率"
  1907. sheet, err := xlsxFile.AddSheet(sheetName)
  1908. if err != nil {
  1909. br.Msg = "新增Sheet失败"
  1910. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1911. return
  1912. }
  1913. //标头
  1914. rowTitle := sheet.AddRow()
  1915. cellA := rowTitle.AddCell()
  1916. cellA.Value = "姓名"
  1917. cellB := rowTitle.AddCell()
  1918. cellB.Value = "确认不续约合同数"
  1919. cellC := rowTitle.AddCell()
  1920. cellC.Value = "确认不续约合同金额"
  1921. cellD := rowTitle.AddCell()
  1922. cellD.Value = "到期合同数"
  1923. cellE := rowTitle.AddCell()
  1924. cellE.Value = "到期合同金额"
  1925. cellF := rowTitle.AddCell()
  1926. cellF.Value = "确认不续约率"
  1927. listRaiSeller, err := system.GetSysuserRaiListNoServer()
  1928. if err != nil {
  1929. br.Msg = "获取失败"
  1930. br.ErrMsg = "获取权益销售信息失败,Err:" + err.Error()
  1931. return
  1932. }
  1933. //NoRenewalContractTotal int `description:"确认不续约合同数"`
  1934. //NoRenewalContractMoney int `description:"确认不续约合同金额"`
  1935. //ExpireRenewalContractTotal int `description:"到期合同数量"`
  1936. //ExpireRenewalContractMoney int `description:"到期合同总金额"`
  1937. //NoRenewalContractPercentage string `description:"确认不续约率"`
  1938. mapNoRenewalContractTotal := make(map[int]int)
  1939. mapNoRenewalContractMoney := make(map[int]float64)
  1940. mapExpireRenewalContractTotal := make(map[int]int)
  1941. mapExpireRenewalContractMoney := make(map[int]float64)
  1942. //mapData := make(map[int]*response.IncrementalCompanyPercentageExportResp)
  1943. for _, v := range listRenewal {
  1944. mapNoRenewalContractTotal[v.SellerId]++
  1945. mapNoRenewalContractMoney[v.SellerId] += v.Money
  1946. }
  1947. for _, v := range listEndDate {
  1948. mapExpireRenewalContractTotal[v.SellerId]++
  1949. mapExpireRenewalContractMoney[v.SellerId] += v.Money
  1950. }
  1951. for _, item := range listRaiSeller {
  1952. row := sheet.AddRow()
  1953. cellAData := row.AddCell()
  1954. cellAData.Value = item.RealName
  1955. cellBData := row.AddCell()
  1956. cellBData.Value = fmt.Sprint(mapNoRenewalContractTotal[item.AdminId])
  1957. cellCData := row.AddCell()
  1958. cellCData.Value = utils.SubFloatToString(mapNoRenewalContractMoney[item.AdminId], 2)
  1959. cellEData := row.AddCell()
  1960. cellEData.Value = fmt.Sprint(mapExpireRenewalContractTotal[item.AdminId])
  1961. cellFData := row.AddCell()
  1962. cellFData.Value = utils.SubFloatToString(mapExpireRenewalContractMoney[item.AdminId], 2)
  1963. cellGData := row.AddCell()
  1964. //分子或者分母为零的时候,不做计算
  1965. if mapNoRenewalContractMoney[item.AdminId] == 0 || mapExpireRenewalContractMoney[item.AdminId] == 0 {
  1966. cellGData.Value = "0%"
  1967. } else {
  1968. cellGData.Value = fmt.Sprint(utils.SubFloatToString(float64(mapNoRenewalContractMoney[item.AdminId])/float64(mapExpireRenewalContractMoney[item.AdminId])*100, 2), "%")
  1969. }
  1970. }
  1971. err = xlsxFile.Save(downLoadnFilePath)
  1972. if err != nil {
  1973. br.Msg = "保存文件失败"
  1974. br.ErrMsg = "保存文件失败"
  1975. return
  1976. }
  1977. }
  1978. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1979. downloadFileName := "未续约率明细" + randStr + ".xlsx"
  1980. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  1981. defer func() {
  1982. os.Remove(downLoadnFilePath)
  1983. }()
  1984. br.Ret = 200
  1985. br.Success = true
  1986. br.Msg = "导出成功"
  1987. }
  1988. // @Title 获取上一年的合同列表
  1989. // @Description 获取上一年的合同列表接口
  1990. // @Param CompanyContractId query int true "合同ID"
  1991. // @Success Ret=200 {object} company.GetCompanyContractDetailListResp
  1992. // @router /merge_company_previous/last_year [get]
  1993. func (this *StatisticCompanyMergerController) CompanyPreviousLastYear() {
  1994. br := new(models.BaseResponse).Init()
  1995. defer func() {
  1996. this.Data["json"] = br
  1997. this.ServeJSON()
  1998. }()
  1999. AdminUser := this.SysUser
  2000. if AdminUser == nil {
  2001. br.Msg = "请登录"
  2002. br.ErrMsg = "请登录,用户信息为空"
  2003. br.Ret = 408
  2004. return
  2005. }
  2006. resp := new(company.GetCompanyContractDetailListResp)
  2007. companyContractId, _ := this.GetInt("CompanyContractId")
  2008. if companyContractId < 1 {
  2009. br.Msg = "请输入合同ID"
  2010. return
  2011. }
  2012. var condition string
  2013. var pars []interface{}
  2014. //获取最新的一条到期的合同信息
  2015. condition = " AND company_contract_id = ? "
  2016. pars = append(pars, companyContractId)
  2017. detail, err := company.GetCompanyContracDetail(condition, pars)
  2018. if err != nil {
  2019. br.Msg = "详情不存在"
  2020. br.ErrMsg = "获取失败,Err:" + err.Error()
  2021. return
  2022. }
  2023. startDateContrac := detail.StartDate // 合同开始时间
  2024. startDateYear := utils.StrDateToDate(startDateContrac).AddDate(-1, 0, 0).Year() //所要查询的年份
  2025. startDate := fmt.Sprint(startDateYear, "-01-01")
  2026. endDate := fmt.Sprint(startDateYear, "-12-31")
  2027. //获取前一份合同的信息
  2028. pars = make([]interface{}, 0)
  2029. condition = " AND company_id = ? AND status = 1 AND start_date >= ? AND start_date <= ? AND product_id = 2 ORDER BY start_date DESC "
  2030. pars = append(pars, detail.CompanyId, startDate, endDate)
  2031. detailPreviousList, err := company.GetCompanyContracList(condition, pars)
  2032. if err != nil {
  2033. br.Msg = "详情不存在"
  2034. br.ErrMsg = "获取失败,Err:" + err.Error()
  2035. return
  2036. }
  2037. var companyContractIds []int
  2038. for _, v := range detailPreviousList {
  2039. companyContractIds = append(companyContractIds, v.CompanyContractId)
  2040. }
  2041. //合并合同所对应的权限
  2042. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  2043. if err != nil {
  2044. br.Msg = "获取失败"
  2045. br.ErrMsg = "获取失败,Err:" + err.Error()
  2046. return
  2047. }
  2048. for _, v := range detailPreviousList {
  2049. v.PermissionName = mappermissionName[v.CompanyContractId]
  2050. }
  2051. resp.List = detailPreviousList
  2052. br.Ret = 200
  2053. br.Success = true
  2054. br.Msg = "获取成功"
  2055. br.Data = resp
  2056. }
  2057. func init213() {
  2058. var condition string
  2059. var pars []interface{}
  2060. condition = " AND rai_contract_type = '续约合同' AND product_id = 2 "
  2061. var companyContractIds []string
  2062. companyContractList, e := company.GetCompanyContractList(condition, pars)
  2063. if e != nil && e.Error() != utils.ErrNoRow() {
  2064. fmt.Println(e)
  2065. return
  2066. }
  2067. for _, v := range companyContractList {
  2068. fmt.Println(v.CompanyContractId)
  2069. startDateTime, _ := time.Parse(utils.FormatDate, v.StartDate)
  2070. startDateTime = startDateTime.AddDate(-1, 0, 0)
  2071. totalLastYear, err := company.GetCompanyContractCountRaiByLastYear(v.CompanyId, startDateTime.Format(utils.FormatDate))
  2072. if err != nil {
  2073. fmt.Println(err)
  2074. return
  2075. }
  2076. if totalLastYear > 0 {
  2077. companyContractIds = append(companyContractIds, strconv.Itoa(v.CompanyContractId))
  2078. }
  2079. }
  2080. fmt.Println("companyContractIds", len(companyContractIds))
  2081. fmt.Println(strings.Join(companyContractIds, ","))
  2082. err := company.UpdateCompanyContractRaiContractTypeInit(strings.Join(companyContractIds, ","))
  2083. fmt.Println(err)
  2084. return
  2085. }
  2086. func init323() {
  2087. var conditionConfirm string
  2088. var parsConfirm []interface{}
  2089. companyConfirmList, err := company.GetCompanyContractNoRenewedAscribeList(conditionConfirm, parsConfirm, 0, 0)
  2090. if err != nil && err.Error() != utils.ErrNoRow() {
  2091. fmt.Println(err)
  2092. return
  2093. }
  2094. for _, v := range companyConfirmList {
  2095. err = company.UpdateCompanyContractCompanyAscribeId(v.CompanyAscribeId, v.CompanyContractId)
  2096. if err != nil && err.Error() != utils.ErrNoRow() {
  2097. fmt.Println(err)
  2098. return
  2099. }
  2100. }
  2101. }
  2102. //func init() {
  2103. // init16_1_06()
  2104. //}
  2105. func init16_1_01() {
  2106. var condition string
  2107. var pars []interface{}
  2108. condition = " AND product_id = 2 "
  2109. var companyIds []int
  2110. companyContractList, e := company.GetCompanyContractList(condition, pars)
  2111. if e != nil && e.Error() != utils.ErrNoRow() {
  2112. fmt.Println(e)
  2113. return
  2114. }
  2115. for _, v := range companyContractList {
  2116. if utils.InArrayByInt(companyIds, v.CompanyId) {
  2117. continue
  2118. }
  2119. companyIds = append(companyIds, v.CompanyId)
  2120. }
  2121. condition = " AND company_id IN (" + (utils.GetOrmInReplace(len(companyIds))) + ") AND product_id = 2 AND share_seller_id > 0 "
  2122. pars = append(pars, companyIds)
  2123. listCompanyProduct, e := company.GetCompanyProductList(condition, pars)
  2124. if e != nil {
  2125. fmt.Println(e)
  2126. return
  2127. }
  2128. for _, v := range listCompanyProduct {
  2129. fmt.Println(v.ShareSellerId, v.ShareSeller)
  2130. e = company.UpdateCompanyContractTypeinit16_1_01(v.ShareSeller, v.ShareSellerId, v.CompanyId)
  2131. if e != nil {
  2132. fmt.Println(e)
  2133. return
  2134. }
  2135. }
  2136. }
  2137. func init16_1_02() {
  2138. var condition string
  2139. var pars []interface{}
  2140. condition = " AND product_id = 2 AND company_contract_id > 0 "
  2141. CompanyApprovalList, e := company.GetCompanyApprovalList(condition, pars)
  2142. if e != nil && e.Error() != utils.ErrNoRow() {
  2143. fmt.Println(e)
  2144. return
  2145. }
  2146. for _, v := range CompanyApprovalList {
  2147. fmt.Println(v.CompanyId, v.CompanyContractId, "__", v.CompanyApprovalId)
  2148. e = company.UpdateCompanyContractTypeinit16_1_02(v.ApplyRealName, v.ApplyUserId, v.CompanyContractId)
  2149. if e != nil {
  2150. fmt.Println(e)
  2151. return
  2152. }
  2153. }
  2154. }
  2155. func init16_1_03() {
  2156. CompanyApprovalList, e := company.GetCompanyOperationRecordListinitcrm16_1()
  2157. if e != nil && e.Error() != utils.ErrNoRow() {
  2158. fmt.Println(e)
  2159. return
  2160. }
  2161. for _, v := range CompanyApprovalList {
  2162. fmt.Println(v.CompanyId, v.SellerId, "__", v.SysRealName)
  2163. e = company.UpdateCompanyProductsellerInit(v.SellerId, v.SysRealName, v.CompanyId, 2)
  2164. if e != nil {
  2165. fmt.Println(e)
  2166. return
  2167. }
  2168. }
  2169. }
  2170. func init16_1_04() {
  2171. var condition string
  2172. var pars []interface{}
  2173. condition = " AND product_id = 2 AND seller_id_init = 0 "
  2174. CompanyApprovalList, e := company.GetCompanyContracList(condition, pars)
  2175. if e != nil && e.Error() != utils.ErrNoRow() {
  2176. fmt.Println(e)
  2177. return
  2178. }
  2179. for _, v := range CompanyApprovalList {
  2180. productItem, e := company.GetCompanyProductByCompanyIdAndProductId(v.CompanyId, 2)
  2181. if e != nil && e.Error() != utils.ErrNoRow() {
  2182. fmt.Println(e)
  2183. return
  2184. }
  2185. if productItem == nil {
  2186. continue
  2187. }
  2188. e = company.UpdateCompanyContractTypeinit16_1_02(productItem.SellerName, productItem.SellerId, v.CompanyContractId)
  2189. if e != nil {
  2190. fmt.Println(e)
  2191. return
  2192. }
  2193. }
  2194. }
  2195. //func init16_1_05() {
  2196. // var condition string
  2197. // var pars []interface{}
  2198. //
  2199. // condition = " AND product_id = 2 "
  2200. //
  2201. // listProduct, e := company.GetCompanyProductList(condition, pars)
  2202. // if e != nil {
  2203. // fmt.Println(e)
  2204. // return
  2205. // }
  2206. // for _, v := range listProduct {
  2207. // e = company.UpdateCompanyProductSellerUnexpiredInitinit16_1_05(v.SellerId, v.SellerName, v.CompanyId)
  2208. // if e != nil {
  2209. // fmt.Println(e)
  2210. // return
  2211. // }
  2212. // }
  2213. // fmt.Println("end")
  2214. //}
  2215. func init16_1_06() {
  2216. var condition string
  2217. var pars []interface{}
  2218. listContractMax, e := company.GetCompanyContracListInit(condition, pars)
  2219. if e != nil {
  2220. fmt.Println(e)
  2221. return
  2222. }
  2223. maxId := make(map[int]int)
  2224. for _, v := range listContractMax {
  2225. maxId[v.MaxCompanyContractId] = v.MaxCompanyContractId
  2226. }
  2227. listContract, e := company.GetCompanyContracListInit_CRM_16_1(condition, pars)
  2228. if e != nil {
  2229. fmt.Println(e)
  2230. return
  2231. }
  2232. var updateId []string
  2233. for _, v := range listContract {
  2234. if maxId[v.CompanyContractId] > 0 {
  2235. continue
  2236. }
  2237. updateId = append(updateId, strconv.Itoa(v.CompanyContractId))
  2238. }
  2239. err := company.UpdateCompanyContracthide(updateId)
  2240. fmt.Println((err))
  2241. fmt.Println((updateId))
  2242. }
  2243. //更新权益销客户后一个正式的销售为当前销售
  2244. //func init(){
  2245. // SELECT * FROM company_product WHERE 1= 1 AND product_id =2 AND share_seller_id > 0 ;
  2246. //}