statistic_company_merge.go 83 KB

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