statistic_company_merge.go 81 KB

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