statistic_company_merge.go 83 KB

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