statistic_company_merge.go 80 KB

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