statistic_company_merge.go 86 KB

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