statistic_company_merge.go 75 KB

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