statistic_company_merge.go 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  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/services"
  10. cygxService "hongze/hz_crm_api/services/cygx"
  11. "os"
  12. "path/filepath"
  13. "strconv"
  14. "strings"
  15. //"hongze/hz_crm_api/services"
  16. "hongze/hz_crm_api/utils"
  17. //"strings"
  18. "time"
  19. )
  20. // 统计报表模块下权益合同合并
  21. // StatisticReportController 统计报告基类
  22. type StatisticCompanyMergerController struct {
  23. BaseAuthController
  24. }
  25. // MergeCompanyList
  26. // @Title 权益客户统计列表(合同进行合并之后的)
  27. // @Description 权益客户统计列表接口
  28. // @Param PageSize query int true "每页数据条数"
  29. // @Param CurrentIndex query int true "当前页页码,从1开始"
  30. // @Param Keyword query string true "客户名称"
  31. // @Param CompanyType query string true "客户类型:传空字符串或者不传为全部,'ficc','权益'"
  32. // @Param AdminId query string true "销售id,多个用英文逗号隔开,空字符串为全部"
  33. // @Param RegionType query string false "所属区域:传空字符串或者不传为全部,'国内','海外'"
  34. // @Param StartDate query string false "开始日期"
  35. // @Param EndDate query string false "结束日期"
  36. // @Param DataType query string false "报表类型,枚举值:`新增试用`,`新签客户`,`续约客户`,`未续约客户`"
  37. // @Param TryOutType query string false " '试用', '非试用' 非试用即为冻结/流失"
  38. // @Param IsExport query bool false "是否导出excel,默认是false"
  39. // @Param IsConfirm query int false "是否确认续约: -1-默认全部; 0-待确认; 1-已确认"
  40. // @Param CompanyAscribeId query int false "归因ID"
  41. // @Param PackageDifference query string false "和上一份合同的区别,枚举值:`增加套餐`,`减少套餐`,`维持套餐`"
  42. // @Success 200 {object} response.IncrementalCompanyListResp
  43. // @router /merge_company_list [get]
  44. func (this *StatisticCompanyMergerController) MergeCompanyList() {
  45. br := new(models.BaseResponse).Init()
  46. defer func() {
  47. this.Data["json"] = br
  48. this.ServeJSON()
  49. }()
  50. sysUser := this.SysUser
  51. if sysUser == nil {
  52. br.Msg = "请登录"
  53. br.ErrMsg = "请登录,SysUser Is Empty"
  54. br.Ret = 408
  55. return
  56. }
  57. pageSize, _ := this.GetInt("PageSize")
  58. currentIndex, _ := this.GetInt("CurrentIndex")
  59. adminId := this.GetString("AdminId")
  60. regionType := this.GetString("RegionType")
  61. //companyType := this.GetString("CompanyType")
  62. dataType := this.GetString("DataType")
  63. tryOutType := this.GetString("TryOutType")
  64. keyword := this.GetString("Keyword")
  65. packageDifference := this.GetString("PackageDifference")
  66. startDate := this.GetString("StartDate")
  67. endDate := this.GetString("EndDate")
  68. isConfirm, _ := this.GetInt("IsConfirm", -1)
  69. companyAscribeId, _ := this.GetInt("CompanyAscribeId", -1)
  70. if dataType != "续约客户" {
  71. packageDifference = "" // 只有续约客户才会有值,过滤前端传过来的脏数据
  72. }
  73. if startDate == "" {
  74. startDate = "2015-01-01"
  75. }
  76. if endDate == "" {
  77. endDate = time.Now().Format(utils.FormatDate)
  78. }
  79. var startSize int
  80. if pageSize <= 0 {
  81. pageSize = utils.PageSize20
  82. }
  83. if currentIndex <= 0 {
  84. currentIndex = 1
  85. }
  86. startSize = utils.StartIndex(currentIndex, pageSize)
  87. //是否导出报表
  88. isExport, _ := this.GetBool("IsExport")
  89. if isExport {
  90. pageSize = 10000
  91. currentIndex = 1
  92. }
  93. var condition string
  94. var pars []interface{}
  95. //条件
  96. if adminId != "" {
  97. condition += ` AND c.seller_id in (` + adminId + `) `
  98. //pars = append(pars, adminId)
  99. } else {
  100. //根据当前角色来获取查询条件
  101. condition, pars = getQueryParams(condition, pars, sysUser, "c.")
  102. }
  103. if regionType != "" {
  104. condition += ` AND b.region_type = ? `
  105. pars = append(pars, regionType)
  106. }
  107. //关键字搜索
  108. if keyword != "" {
  109. condition += ` and b.company_name like "%` + keyword + `%" `
  110. }
  111. var conditionAscribRai string // 处理权益未续约客户检索列表SQL查询条件
  112. var conditionAscribRaiTotal string // 处理权益未续约客户总量查询条件
  113. var conditionAscribRaiToBeTotal string // 处理权益未续约客户待确认总量查询条件
  114. var parsAscribeRai []interface{}
  115. var parsAscribeRaiTotal []interface{} // 处理权益未续约客户总量查询条件
  116. var parsAscribeRaiTobeTotal []interface{} // 处理权益未续约客户待确认总量查询条件
  117. var conditionConfirm string
  118. var parsConfirm []interface{}
  119. companyConfirmList, err := company.GetCompanyContractNoRenewedAscribeList(conditionConfirm, parsConfirm, 0, 0)
  120. if err != nil && err.Error() != utils.ErrNoRow() {
  121. br.Msg = "获取失败"
  122. br.ErrMsg = "获取失败,GetCompanyNoRenewedAscribeList Err:" + err.Error()
  123. return
  124. }
  125. var noRenewedcompanyContractIds []int //已经确定未续约的公司ID
  126. if len(companyConfirmList) == 0 {
  127. noRenewedcompanyContractIds = append(noRenewedcompanyContractIds, 0) // 给一个不存在的ID
  128. } else {
  129. for _, v := range companyConfirmList {
  130. noRenewedcompanyContractIds = append(noRenewedcompanyContractIds, v.CompanyContractId)
  131. }
  132. }
  133. //是否确认续约 CRM 13.9
  134. conditionAscribRaiTotal += ` AND a.company_contract_id IN (` + utils.GetOrmInReplace(len(noRenewedcompanyContractIds)) + `) ` // 已确认
  135. parsAscribeRaiTotal = append(parsAscribeRaiTotal, noRenewedcompanyContractIds)
  136. conditionAscribRaiToBeTotal += ` AND a.company_contract_id NOT IN (` + utils.GetOrmInReplace(len(noRenewedcompanyContractIds)) + `) ` // 待确认
  137. parsAscribeRaiTobeTotal = append(parsAscribeRaiTobeTotal, noRenewedcompanyContractIds)
  138. if isConfirm != -1 {
  139. if isConfirm == 0 {
  140. conditionAscribRai += ` AND a.company_contract_id NOT IN (` + utils.GetOrmInReplace(len(noRenewedcompanyContractIds)) + `) ` // 待确认
  141. } else {
  142. conditionAscribRai += ` AND a.company_contract_id IN (` + utils.GetOrmInReplace(len(noRenewedcompanyContractIds)) + `) ` // 已确认
  143. }
  144. parsAscribeRai = append(parsAscribeRai, noRenewedcompanyContractIds)
  145. }
  146. //归因ID CRM 13.9
  147. if companyAscribeId > 0 {
  148. var conditionAscribe string
  149. var parsAscribe []interface{}
  150. conditionAscribe = " AND company_ascribe_id = ? "
  151. parsAscribe = append(parsAscribe, companyAscribeId)
  152. companyNoRenewedAscribeList, err := company.GetCompanyContractNoRenewedAscribeList(conditionAscribe, parsAscribe, 0, 0)
  153. if err != nil && err.Error() != utils.ErrNoRow() {
  154. br.Msg = "获取失败"
  155. br.ErrMsg = "获取失败,GetCompanyNoRenewedAscribeList Err:" + err.Error()
  156. return
  157. }
  158. var noRenewedcompanyContractIds []int
  159. if len(companyNoRenewedAscribeList) == 0 {
  160. noRenewedcompanyContractIds = append(noRenewedcompanyContractIds, 0) // 给一个不存在的ID
  161. } else {
  162. for _, v := range companyNoRenewedAscribeList {
  163. noRenewedcompanyContractIds = append(noRenewedcompanyContractIds, v.CompanyContractId)
  164. }
  165. }
  166. conditionAscribRai += ` AND a.company_contract_id IN (` + utils.GetOrmInReplace(len(noRenewedcompanyContractIds)) + `)`
  167. parsAscribeRai = append(parsAscribeRai, noRenewedcompanyContractIds)
  168. }
  169. condition += ` AND c.product_id = ? AND a.status = 1 `
  170. pars = append(pars, 2)
  171. var list []*models.IncrementalList
  172. //moreListMap := make(map[int][]*models.IncrementalList)
  173. var newCompanyTotal int //新签合同数量
  174. var notRenewalCompanyTotal int //未续约客户数已确认
  175. var notRenewalCompanyToBeConfirmTotal int //未续约客户数待确认
  176. var dataTotal, trialTotal, renewalCompanyTotal int
  177. var notRenewalTryOut, notRenewalNotTryOut int
  178. //新签客户数
  179. {
  180. condition1 := condition
  181. pars1 := pars
  182. condition1 += ` AND a.start_date >= ? AND a.start_date <= ? `
  183. pars1 = append(pars1, startDate, endDate)
  184. condition1 += ` AND a.rai_contract_type = ? `
  185. pars1 = append(pars1, "新签合同")
  186. newCompanyTotal, err = company.GetIncrementalNewCompanyProductMergeCount(condition1, pars1)
  187. if err != nil && err.Error() != utils.ErrNoRow() {
  188. br.Msg = "获取失败"
  189. br.ErrMsg = "获取失败,Err:" + err.Error()
  190. return
  191. }
  192. if dataType == "新签客户" {
  193. //列表数据数量
  194. total, err := company.GetIncrementalNewCompanyProductMergeCount(condition1, pars1)
  195. if err != nil && err.Error() != utils.ErrNoRow() {
  196. br.Msg = "获取失败"
  197. br.ErrMsg = "获取失败,Err:" + err.Error()
  198. return
  199. }
  200. //列表页数据
  201. tmpList, err := models.GetIncrementalCompanyMergeList(condition1, pars1, startSize, pageSize)
  202. if err != nil {
  203. br.Msg = "获取失败"
  204. br.ErrMsg = "获取失败,Err:" + err.Error()
  205. return
  206. }
  207. list = tmpList
  208. dataTotal = total
  209. }
  210. }
  211. //续约客户数
  212. {
  213. condition1 := condition
  214. pars1 := pars
  215. condition1 += ` AND a.start_date >= ? AND a.start_date <= ? `
  216. pars1 = append(pars1, startDate, endDate)
  217. condition1 += ` AND a.rai_contract_type = ? `
  218. pars1 = append(pars1, "续约合同")
  219. ////额外条件(续约合同的起始日期包含在所选时间段内且不包含在新签合同存续期内的客户)
  220. //pars1 = append(pars1, endDate)
  221. total, err := company.GetIncrementalNewCompanyProductMergeCount(condition1, pars1)
  222. if err != nil && err.Error() != utils.ErrNoRow() {
  223. br.Msg = "获取失败"
  224. br.ErrMsg = "获取失败,Err:" + err.Error()
  225. return
  226. }
  227. renewalCompanyTotal = total
  228. if dataType == "续约客户" {
  229. //续约的客户才会查询
  230. if packageDifference != "" {
  231. condition1 += ` AND a.package_difference = ? `
  232. pars1 = append(pars1, packageDifference)
  233. }
  234. //列表数据数量
  235. total, err := company.GetIncrementalRenewalCompanyProductMergeCount(condition1, pars1)
  236. if err != nil && err.Error() != utils.ErrNoRow() {
  237. br.Msg = "获取失败"
  238. br.ErrMsg = "获取失败,Err:" + err.Error()
  239. return
  240. }
  241. //列表页数据
  242. tmpList, err := models.GetIncrementalCompanyMergeList(condition1, pars1, startSize, pageSize)
  243. if err != nil {
  244. br.Msg = "获取失败"
  245. br.ErrMsg = "获取失败,Err:" + err.Error()
  246. return
  247. }
  248. list = tmpList
  249. dataTotal = total
  250. }
  251. }
  252. //未续约客户数
  253. {
  254. condition1 := condition
  255. pars1 := pars
  256. condition1 += ` AND a.end_date >= ? AND a.end_date <= ? `
  257. pars1 = append(pars1, startDate, endDate)
  258. //condition1 += ` AND a.operation = ? `
  259. //pars1 = append(pars1, "try_out")
  260. condition1 += ` AND c.status not in ("永续","正式","关闭") `
  261. //未续约已确认数量
  262. notRenewalCondition := condition1
  263. notRenewalPars := pars1
  264. notRenewalCondition += conditionAscribRaiTotal
  265. notRenewalPars = append(notRenewalPars, parsAscribeRaiTotal)
  266. total, err := company.GetIncrementalRenewalCompanyProductMergeCount(notRenewalCondition, notRenewalPars)
  267. if err != nil && err.Error() != utils.ErrNoRow() {
  268. br.Msg = "获取失败"
  269. br.ErrMsg = "获取失败,Err:" + err.Error()
  270. return
  271. }
  272. notRenewalCompanyTotal = total
  273. //未续约待确认数量
  274. notRenewalToBeCondition := condition1
  275. notRenewalToBePars := pars1
  276. notRenewalToBeCondition += conditionAscribRaiToBeTotal
  277. notRenewalToBePars = append(notRenewalToBePars, parsAscribeRaiTobeTotal)
  278. notRenewalCompanyToBeConfirmTotal, err = company.GetIncrementalRenewalCompanyProductMergeCount(notRenewalToBeCondition, notRenewalToBePars)
  279. if err != nil && err.Error() != utils.ErrNoRow() {
  280. br.Msg = "获取失败"
  281. br.ErrMsg = "获取失败,Err:" + err.Error()
  282. return
  283. }
  284. condition1 += conditionAscribRai
  285. pars1 = append(pars1, parsAscribeRai)
  286. if dataType == "未续约客户" {
  287. //统计数据
  288. for _, v := range []string{"试用", "非试用"} {
  289. totalCondition1 := condition1
  290. totalPars1 := pars1
  291. var tmpTotal int
  292. if v == "试用" {
  293. totalCondition1 += ` AND c.status = "试用" `
  294. tmpTotal, err = company.GetIncrementalRenewalCompanyProductMergeCount(totalCondition1, totalPars1)
  295. if err != nil && err.Error() != utils.ErrNoRow() {
  296. br.Msg = "获取失败"
  297. br.ErrMsg = "获取失败,Err:" + err.Error()
  298. return
  299. }
  300. notRenewalTryOut = tmpTotal
  301. } else if v == "非试用" {
  302. totalCondition1 += ` AND c.status IN ("冻结","流失") `
  303. tmpTotal, err = company.GetIncrementalRenewalCompanyProductMergeCount(totalCondition1, totalPars1)
  304. if err != nil && err.Error() != utils.ErrNoRow() {
  305. br.Msg = "获取失败"
  306. br.ErrMsg = "获取失败,Err:" + err.Error()
  307. return
  308. }
  309. notRenewalNotTryOut = tmpTotal
  310. }
  311. }
  312. //列表数据数量
  313. if tryOutType == "试用" {
  314. condition1 += ` AND c.status = "试用" `
  315. total = notRenewalTryOut
  316. } else if tryOutType == "非试用" {
  317. condition1 += ` AND c.status IN ("冻结","流失") `
  318. total = notRenewalNotTryOut
  319. }
  320. //分页total单独计算
  321. total, err = company.GetIncrementalRenewalCompanyProductMergeCount(condition1, pars1)
  322. if err != nil && err.Error() != utils.ErrNoRow() {
  323. br.Msg = "获取失败"
  324. br.ErrMsg = "获取失败,Err:" + err.Error()
  325. return
  326. }
  327. //return
  328. //列表页数据
  329. tmpList, err := models.GetIncrementalCompanyListByOperationRecordMerge(condition1, pars1, startSize, pageSize)
  330. if err != nil {
  331. br.Msg = "获取失败"
  332. br.ErrMsg = "获取失败,Err:" + err.Error()
  333. return
  334. }
  335. var ascribecompanyIds []int
  336. var companyContractIds []int
  337. for _, item := range tmpList {
  338. //endDateTime, _ := time.Parse(utils.FormatDateTime, item.CreateTime)
  339. //item.EndDate = endDateTime.Format(utils.FormatDate)
  340. ascribecompanyIds = append(ascribecompanyIds, item.CompanyId)
  341. companyContractIds = append(companyContractIds, item.CompanyContractId)
  342. }
  343. //合同归因标签
  344. mapGetCompanyAscribeContent, mapContent := services.GetCompanyContractAscribeContentMap(companyContractIds)
  345. mapNoRenewedNote := services.GetCompanyNoRenewedNoteMap(ascribecompanyIds)
  346. for _, item := range tmpList {
  347. item.AscribeContent = mapGetCompanyAscribeContent[item.CompanyContractId]
  348. item.Content = mapContent[item.CompanyContractId]
  349. item.IsShowNoRenewedNote = mapNoRenewedNote[fmt.Sprint("CID_", item.CompanyId, "PID_", item.ProductId)]
  350. }
  351. list = tmpList
  352. dataTotal = total
  353. }
  354. }
  355. listLen := len(list)
  356. if listLen == 0 {
  357. list = make([]*models.IncrementalList, 0)
  358. }
  359. var companyContractIds []int
  360. var companyIds []int
  361. for i := 0; i < listLen; i++ {
  362. item := list[i]
  363. companyContractIds = append(companyContractIds, item.CompanyContractId)
  364. //新增试用不需要计算剩余日期
  365. if dataType != "新增试用" {
  366. //剩余可用天数
  367. endDateTime, _ := time.Parse(utils.FormatDate, item.EndDate)
  368. endDateTime = endDateTime.AddDate(0, 0, 1)
  369. sub := endDateTime.Sub(time.Now())
  370. expireDay := fmt.Sprintf("%v", int(sub.Hours()/24))
  371. list[i].ExpireDay = expireDay
  372. }
  373. companyIds = append(companyIds, item.CompanyId)
  374. }
  375. //合并合同所对应的权限
  376. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  377. if err != nil {
  378. br.Msg = "获取失败"
  379. br.ErrMsg = "获取失败,Err:" + err.Error()
  380. return
  381. }
  382. mapIsUserMaker := cygxService.GetCompanyProductIsUserMakerByCompanyIds(companyIds) //根据公司ID获取近四周之内有决策人互动的客户
  383. for _, v := range list {
  384. v.PermissionName = mappermissionName[v.CompanyContractId]
  385. v.IsUserMaker = mapIsUserMaker[v.CompanyId]
  386. }
  387. page := paging.GetPaging(currentIndex, pageSize, dataTotal)
  388. resp := response.IncrementalCompanyListResp{
  389. Paging: page,
  390. List: list,
  391. TrialTotal: trialTotal,
  392. NewCompanyTotal: newCompanyTotal,
  393. RenewalCompanyTotal: renewalCompanyTotal,
  394. NotRenewalCompanyTotal: notRenewalCompanyTotal,
  395. NotRenewalCompanyToBeConfirmTotal: notRenewalCompanyToBeConfirmTotal,
  396. NotRenewalTryOut: notRenewalTryOut,
  397. NotRenewalNotTryOut: notRenewalNotTryOut,
  398. }
  399. //fmt.Println()
  400. //导出excel
  401. if isExport {
  402. MergeCompanyListListExport(this, dataType, resp, br)
  403. return
  404. }
  405. br.Ret = 200
  406. br.Success = true
  407. br.Msg = "获取成功"
  408. br.Data = resp
  409. }
  410. // IncrementalCompanyListExport 导出增量客户数据报表excel
  411. func MergeCompanyListListExport(this *StatisticCompanyMergerController, dataType string, resp response.IncrementalCompanyListResp, br *models.BaseResponse) {
  412. dir, err := os.Executable()
  413. exPath := filepath.Dir(dir)
  414. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  415. xlsxFile := xlsx.NewFile()
  416. if err != nil {
  417. br.Msg = "生成文件失败"
  418. br.ErrMsg = "生成文件失败"
  419. return
  420. }
  421. style := xlsx.NewStyle()
  422. alignment := xlsx.Alignment{
  423. Horizontal: "center",
  424. Vertical: "center",
  425. WrapText: true,
  426. }
  427. style.Alignment = alignment
  428. style.ApplyAlignment = true
  429. sheel, err := xlsxFile.AddSheet("增量客户数据")
  430. if err != nil {
  431. br.Msg = "新增Sheet失败"
  432. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  433. return
  434. }
  435. sheel.SetColWidth(0, 0, 30)
  436. sheel.SetColWidth(1, 1, 15)
  437. sheel.SetColWidth(2, 2, 15)
  438. sheel.SetColWidth(3, 3, 18)
  439. sheel.SetColWidth(4, 4, 40)
  440. sheel.SetColWidth(5, 5, 18)
  441. //统计数据
  442. statisticRow := sheel.AddRow()
  443. cell1 := statisticRow.AddCell()
  444. cell1.SetStyle(style)
  445. cell1.SetValue(fmt.Sprint("新增试用客户数:", resp.TrialTotal))
  446. cell2 := statisticRow.AddCell()
  447. cell2.SetStyle(style)
  448. cell2.SetValue(fmt.Sprint("新签客户数:", resp.NewCompanyTotal))
  449. cell3 := statisticRow.AddCell()
  450. cell3.SetStyle(style)
  451. cell3.SetValue(fmt.Sprint("续约客户数:", resp.RenewalCompanyTotal))
  452. cell4 := statisticRow.AddCell()
  453. cell4.SetStyle(style)
  454. cell4.SetValue(fmt.Sprint("未续约客户数:", resp.NotRenewalCompanyTotal))
  455. //表头
  456. titleRow := sheel.AddRow()
  457. cellName := titleRow.AddCell()
  458. cellName.SetStyle(style)
  459. cellName.SetValue("客户名称")
  460. cellProName := titleRow.AddCell()
  461. cellProName.SetStyle(style)
  462. cellProName.SetValue("客户类型")
  463. cellSellerName := titleRow.AddCell()
  464. cellSellerName.SetStyle(style)
  465. cellSellerName.SetValue("所属销售")
  466. cellMoney := titleRow.AddCell()
  467. cellMoney.SetStyle(style)
  468. cellMoney.SetValue("合同金额")
  469. cellTime := titleRow.AddCell()
  470. cellTime.SetStyle(style)
  471. switch dataType {
  472. case "新增试用":
  473. cellTime.SetValue("新增时间")
  474. case "新签客户":
  475. cellTime.SetValue("签约时间")
  476. case "续约客户":
  477. cellTime.SetValue("续约时间")
  478. case "未续约客户":
  479. cellTime.SetValue("最近合同到期时间")
  480. cellAscribeContent := titleRow.AddCell()
  481. cellAscribeContent.SetStyle(style)
  482. cellAscribeContent.SetValue("不续约归因")
  483. cellContent := titleRow.AddCell()
  484. cellContent.SetStyle(style)
  485. cellContent.SetValue("详细原因")
  486. }
  487. cellPermissionName := titleRow.AddCell()
  488. cellPermissionName.SetStyle(style)
  489. cellPermissionName.SetValue("签约套餐")
  490. for _, v := range resp.List {
  491. dataRow := sheel.AddRow()
  492. dataRow.SetHeight(20)
  493. cellDataName := dataRow.AddCell()
  494. cellDataName.SetStyle(style)
  495. cellDataName.SetValue(v.CompanyName)
  496. cellDataProName := dataRow.AddCell()
  497. cellDataProName.SetStyle(style)
  498. cellDataProName.SetValue(v.ProductName)
  499. cellDataSellerName := dataRow.AddCell()
  500. cellDataSellerName.SetStyle(style)
  501. cellDataSellerName.SetValue(v.SellerName)
  502. //if dataType == "新签客户" || dataType == "续约客户" {
  503. cellDataMoney := dataRow.AddCell()
  504. cellDataMoney.SetStyle(style)
  505. cellDataMoney.SetValue(v.Money)
  506. //}
  507. cellDataTime := dataRow.AddCell()
  508. cellDataTime.SetStyle(style)
  509. switch dataType {
  510. case "新增试用":
  511. cellDataTime.SetValue(v.CreateTime)
  512. case "新签客户":
  513. cellDataTime.SetValue(v.StartDate)
  514. case "续约客户":
  515. cellDataTime.SetValue(v.StartDate)
  516. case "未续约客户":
  517. cellDataTime.SetValue(v.EndDate)
  518. cellAscribeContent := dataRow.AddCell()
  519. cellAscribeContent.SetStyle(style)
  520. cellAscribeContent.SetValue(v.AscribeContent)
  521. cellContent := dataRow.AddCell()
  522. cellContent.SetStyle(style)
  523. cellContent.SetValue(v.Content)
  524. }
  525. cellDataPermissionName := dataRow.AddCell()
  526. cellDataPermissionName.SetStyle(style)
  527. cellDataPermissionName.SetValue(v.PermissionName)
  528. }
  529. err = xlsxFile.Save(downLoadnFilePath)
  530. if err != nil {
  531. br.Msg = "保存文件失败"
  532. br.ErrMsg = "保存文件失败"
  533. return
  534. }
  535. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  536. downloadFileName := dataType + "数据_" + randStr + ".xlsx"
  537. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  538. defer func() {
  539. os.Remove(downLoadnFilePath)
  540. }()
  541. br.Ret = 200
  542. br.Success = true
  543. br.Msg = "导出成功"
  544. }
  545. // @Title 上一份合同详情
  546. // @Description 上一份合同详情接口
  547. // @Param CompanyContractId query int true "合同ID"
  548. // @Success Ret=200 {object} company.GetCompanyContractMergeDetailResp
  549. // @router /merge_company_previous/detail [get]
  550. func (this *StatisticCompanyMergerController) CompanyPreviousDetail() {
  551. br := new(models.BaseResponse).Init()
  552. defer func() {
  553. this.Data["json"] = br
  554. this.ServeJSON()
  555. }()
  556. AdminUser := this.SysUser
  557. if AdminUser == nil {
  558. br.Msg = "请登录"
  559. br.ErrMsg = "请登录,用户信息为空"
  560. br.Ret = 408
  561. return
  562. }
  563. resp := new(company.GetCompanyContractDetailResp)
  564. companyContractId, _ := this.GetInt("CompanyContractId")
  565. if companyContractId < 1 {
  566. br.Msg = "请输入合同ID"
  567. return
  568. }
  569. var condition string
  570. var pars []interface{}
  571. //获取最新的一条到期的合同信息
  572. condition = " AND company_contract_id = ? "
  573. pars = append(pars, companyContractId)
  574. detail, err := company.GetCompanyContracDetail(condition, pars)
  575. if err != nil {
  576. br.Msg = "详情不存在"
  577. br.ErrMsg = "获取失败,Err:" + err.Error()
  578. return
  579. }
  580. //获取前一份合同的信息
  581. pars = make([]interface{}, 0)
  582. condition = " AND company_id = ? AND company_contract_id < ? AND status = 1 AND product_id = 2 ORDER BY company_contract_id DESC LIMIT 1 "
  583. pars = append(pars, detail.CompanyId, companyContractId)
  584. detailPrevious, err := company.GetCompanyContracDetail(condition, pars)
  585. if err != nil {
  586. br.Msg = "详情不存在"
  587. br.ErrMsg = "获取失败,Err:" + err.Error()
  588. return
  589. }
  590. //合并合同所对应的权限
  591. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById([]int{detailPrevious.CompanyContractId})
  592. if err != nil {
  593. br.Msg = "获取失败"
  594. br.ErrMsg = "获取失败,Err:" + err.Error()
  595. return
  596. }
  597. detailPrevious.PermissionName = mappermissionName[detailPrevious.CompanyContractId]
  598. resp.Detail = detailPrevious
  599. br.Ret = 200
  600. br.Success = true
  601. br.Msg = "获取成功"
  602. br.Data = resp
  603. }
  604. // @Title 权益行业套餐统计
  605. // @Description 权益客户统计列表接口
  606. // @Param PageSize query int true "每页数据条数"
  607. // @Param CurrentIndex query int true "当前页页码,从1开始"
  608. // @Param Keyword query string true "客户名称"
  609. // @Param AdminId query string true "销售id,多个用英文逗号隔开,空字符串为全部"
  610. // @Param StartDate query string false "开始日期"
  611. // @Param EndDate query string false "结束日期"
  612. // @Param DataType query string false "报表类型,枚举值:`行业新签`,`行业续约`,`行业未续约`"
  613. // @Param PermissionName query string false "行业权限名称"
  614. // @Param IsExport query bool false "是否导出excel,默认是false"
  615. // @Success 200 {object} response.IncrementalCompanyListResp
  616. // @router /merge_company/company_contract_permission/list [get]
  617. func (this *StatisticCompanyMergerController) CompanyContractPermissionList() {
  618. br := new(models.BaseResponse).Init()
  619. defer func() {
  620. this.Data["json"] = br
  621. this.ServeJSON()
  622. }()
  623. sysUser := this.SysUser
  624. if sysUser == nil {
  625. br.Msg = "请登录"
  626. br.ErrMsg = "请登录,SysUser Is Empty"
  627. br.Ret = 408
  628. return
  629. }
  630. pageSize, _ := this.GetInt("PageSize")
  631. currentIndex, _ := this.GetInt("CurrentIndex")
  632. adminId := this.GetString("AdminId")
  633. regionType := this.GetString("RegionType")
  634. //companyType := this.GetString("CompanyType")
  635. dataType := this.GetString("DataType")
  636. keyword := this.GetString("Keyword")
  637. startDate := this.GetString("StartDate")
  638. endDate := this.GetString("EndDate")
  639. permissionName := this.GetString("PermissionName")
  640. if permissionName == "" { //默认所筛选行业为医药
  641. permissionName = "医药"
  642. }
  643. //if startDate == "" || endDate == "" {
  644. // br.Msg = "获取失败,开始日期或结束日期未传"
  645. // br.ErrMsg = "获取失败,开始日期或结束日期未传"
  646. // return
  647. //}
  648. if startDate == "" {
  649. startDate = "2015-01-01"
  650. }
  651. if endDate == "" {
  652. endDate = time.Now().Format(utils.FormatDate)
  653. }
  654. var startSize int
  655. if pageSize <= 0 {
  656. pageSize = utils.PageSize20
  657. }
  658. if currentIndex <= 0 {
  659. currentIndex = 1
  660. }
  661. startSize = utils.StartIndex(currentIndex, pageSize)
  662. //是否导出报表
  663. isExport, _ := this.GetBool("IsExport")
  664. if isExport {
  665. pageSize = 10000
  666. currentIndex = 1
  667. permissionName = "" //点击下载的时候不做行业筛选
  668. }
  669. var condition string
  670. var pars []interface{}
  671. //条件
  672. if adminId != "" {
  673. condition += ` AND c.seller_id in (` + adminId + `) `
  674. //pars = append(pars, adminId)
  675. } else {
  676. //根据当前角色来获取查询条件
  677. condition, pars = getQueryParams(condition, pars, sysUser, "c.")
  678. }
  679. if regionType != "" {
  680. condition += ` AND b.region_type = ? `
  681. pars = append(pars, regionType)
  682. }
  683. //关键字搜索
  684. if keyword != "" {
  685. condition += ` and b.company_name like "%` + keyword + `%" `
  686. }
  687. var conditionAscribRai string // 处理权益未续约客户检索列表SQL查询条件
  688. var conditionAscribRaiTotal string // 处理权益未续约客户总量查询条件
  689. var parsAscribeRai []interface{}
  690. var parsAscribeRaiTotal []interface{} // 处理权益未续约客户总量查询条件
  691. condition += ` AND c.product_id = ? AND a.status = 1 `
  692. pars = append(pars, 2)
  693. var list []*models.IncrementalList
  694. var listPessionGroup []*models.CompanyContractPermissionNameGroupCountResp
  695. var newCompanyTotal int //新签合同数量
  696. var notRenewalCompanyTotal int //未续约客户数已确认
  697. var notRenewalCompanyToBeConfirmTotal int //未续约客户数待确认
  698. var dataTotal, trialTotal, renewalCompanyTotal int
  699. var notRenewalTryOut, notRenewalNotTryOut int
  700. //if dataType == "行业未续约" {
  701. // condition += ` AND a.end_date >= ? AND a.end_date <= ? `
  702. // pars = append(pars, startDate, endDate)
  703. //} else {
  704. // condition += ` AND a.start_date >= ? AND a.start_date <= ? `
  705. // pars = append(pars, startDate, endDate)
  706. //}
  707. //
  708. //conditionPerssion := condition
  709. //parsPerssion := pars
  710. //conditionPerssion += ` AND d.permission_name IN ('医药','消费','科技','智造','策略','买方研选')`
  711. //
  712. //listPessionGroup, err := models.GetCompanyContractPermissionNameGroupCount(conditionPerssion, parsPerssion)
  713. //if err != nil && err.Error() != utils.ErrNoRow() {
  714. // br.Msg = "获取失败"
  715. // br.ErrMsg = "获取失败,Err:" + err.Error()
  716. // return
  717. //}
  718. //if permissionName == "" {
  719. condition += ` AND d.permission_name IN ('医药','消费','科技','智造','策略','研选订阅','研选扣点包')`
  720. //}
  721. var err error
  722. //新签客户数
  723. {
  724. condition1 := condition
  725. pars1 := pars
  726. condition1 += ` AND a.start_date >= ? AND a.start_date <= ? `
  727. pars1 = append(pars1, startDate, endDate)
  728. condition1 += ` AND d.contract_type = ? `
  729. pars1 = append(pars1, "行业新签")
  730. newCompanyTotal, err = company.GetIncrementalNewCompanyProductPermissionCount(condition1, pars1)
  731. if err != nil && err.Error() != utils.ErrNoRow() {
  732. br.Msg = "获取失败"
  733. br.ErrMsg = "获取失败,Err:" + err.Error()
  734. return
  735. }
  736. if dataType == "行业新签" {
  737. conditionPerssion := condition1
  738. parsPerssion := pars1
  739. conditionPerssion += ` AND d.permission_name IN ('医药','消费','科技','智造','策略','研选订阅','研选扣点包')`
  740. listPessionGroup, err = models.GetCompanyContractPermissionNameGroupCount(conditionPerssion, parsPerssion)
  741. if err != nil && err.Error() != utils.ErrNoRow() {
  742. br.Msg = "获取失败"
  743. br.ErrMsg = "获取失败,Err:" + err.Error()
  744. return
  745. }
  746. //行业筛选查询
  747. if permissionName != "" {
  748. condition1 += ` AND d.permission_name = ? `
  749. pars1 = append(pars1, permissionName)
  750. }
  751. total, err := company.GetIncrementalNewCompanyProductPermissionCount(condition1, pars1)
  752. if err != nil && err.Error() != utils.ErrNoRow() {
  753. br.Msg = "获取失败"
  754. br.ErrMsg = "获取失败,Err:" + err.Error()
  755. return
  756. }
  757. //列表页数据
  758. tmpList, err := models.GetIncrementalCompanyPermissionList(condition1, pars1, startSize, pageSize)
  759. if err != nil {
  760. br.Msg = "获取失败"
  761. br.ErrMsg = "获取失败,Err:" + err.Error()
  762. return
  763. }
  764. list = tmpList
  765. dataTotal = total
  766. }
  767. }
  768. //续约客户数
  769. {
  770. condition1 := condition
  771. pars1 := pars
  772. condition1 += ` AND a.start_date >= ? AND a.start_date <= ? `
  773. pars1 = append(pars1, startDate, endDate)
  774. condition1 += ` AND d.contract_type = ? `
  775. pars1 = append(pars1, "行业续约")
  776. ////额外条件(续约合同的起始日期包含在所选时间段内且不包含在新签合同存续期内的客户)
  777. //pars1 = append(pars1, endDate)
  778. total, err := company.GetIncrementalNewCompanyProductPermissionCount(condition1, pars1)
  779. if err != nil && err.Error() != utils.ErrNoRow() {
  780. br.Msg = "获取失败"
  781. br.ErrMsg = "获取失败,Err:" + err.Error()
  782. return
  783. }
  784. renewalCompanyTotal = total
  785. if dataType == "行业续约" {
  786. conditionPerssion := condition1
  787. parsPerssion := pars1
  788. conditionPerssion += ` AND d.permission_name IN ('医药','消费','科技','智造','策略','研选订阅','研选扣点包')`
  789. listPessionGroup, err = models.GetCompanyContractPermissionNameGroupCount(conditionPerssion, parsPerssion)
  790. if err != nil && err.Error() != utils.ErrNoRow() {
  791. br.Msg = "获取失败"
  792. br.ErrMsg = "获取失败,Err:" + err.Error()
  793. return
  794. }
  795. //行业筛选查询
  796. if permissionName != "" {
  797. condition1 += ` AND d.permission_name = ? `
  798. pars1 = append(pars1, permissionName)
  799. }
  800. //列表数据数量
  801. total, err := company.GetIncrementalNewCompanyProductPermissionCount(condition1, pars1)
  802. if err != nil && err.Error() != utils.ErrNoRow() {
  803. br.Msg = "获取失败"
  804. br.ErrMsg = "获取失败,Err:" + err.Error()
  805. return
  806. }
  807. //列表页数据
  808. tmpList, err := models.GetIncrementalCompanyPermissionList(condition1, pars1, startSize, pageSize)
  809. if err != nil {
  810. br.Msg = "获取失败"
  811. br.ErrMsg = "获取失败,Err:" + err.Error()
  812. return
  813. }
  814. list = tmpList
  815. dataTotal = total
  816. }
  817. }
  818. //return
  819. //行业未续约
  820. {
  821. condition1 := condition
  822. pars1 := pars
  823. condition1 += ` AND a.end_date >= ? AND a.end_date <= ? `
  824. pars1 = append(pars1, startDate, endDate)
  825. condition1 += ` AND c.status not in ("永续","正式","关闭") `
  826. //未续约数量
  827. notRenewalCondition := condition1
  828. notRenewalPars := pars1
  829. notRenewalCondition += conditionAscribRaiTotal
  830. notRenewalPars = append(notRenewalPars, parsAscribeRaiTotal)
  831. total, err := company.GetIncrementalNewCompanyProductPermissionCount(notRenewalCondition, notRenewalPars)
  832. if err != nil && err.Error() != utils.ErrNoRow() {
  833. br.Msg = "获取失败"
  834. br.ErrMsg = "获取失败,Err:" + err.Error()
  835. return
  836. }
  837. notRenewalCompanyTotal = total
  838. condition1 += conditionAscribRai
  839. pars1 = append(pars1, parsAscribeRai)
  840. if dataType == "行业未续约" {
  841. conditionPerssion := condition1
  842. parsPerssion := pars1
  843. conditionPerssion += ` AND d.permission_name IN ('医药','消费','科技','智造','策略','研选订阅','研选扣点包')`
  844. listPessionGroup, err = models.GetCompanyContractPermissionNameGroupCount(conditionPerssion, parsPerssion)
  845. if err != nil && err.Error() != utils.ErrNoRow() {
  846. br.Msg = "获取失败"
  847. br.ErrMsg = "获取失败,Err:" + err.Error()
  848. return
  849. }
  850. //行业筛选查询
  851. if permissionName != "" {
  852. condition1 += ` AND d.permission_name = ? `
  853. pars1 = append(pars1, permissionName)
  854. }
  855. //分页total单独计算
  856. total, err = company.GetIncrementalNewCompanyProductPermissionCount(condition1, pars1)
  857. if err != nil && err.Error() != utils.ErrNoRow() {
  858. br.Msg = "获取失败"
  859. br.ErrMsg = "获取失败,Err:" + err.Error()
  860. return
  861. }
  862. //return
  863. //列表页数据
  864. tmpList, err := models.GetIncrementalCompanyPermissionList(condition1, pars1, startSize, pageSize)
  865. if err != nil {
  866. br.Msg = "获取失败"
  867. br.ErrMsg = "获取失败,Err:" + err.Error()
  868. return
  869. }
  870. list = tmpList
  871. dataTotal = total
  872. }
  873. }
  874. listLen := len(list)
  875. if listLen == 0 {
  876. list = make([]*models.IncrementalList, 0)
  877. }
  878. var companyContractIds []int
  879. var companyIds []int
  880. for i := 0; i < listLen; i++ {
  881. item := list[i]
  882. companyContractIds = append(companyContractIds, item.CompanyContractId)
  883. companyIds = append(companyIds, item.CompanyId)
  884. //新增试用不需要计算剩余日期
  885. if dataType != "新增试用" {
  886. //剩余可用天数
  887. endDateTime, _ := time.Parse(utils.FormatDate, item.EndDate)
  888. endDateTime = endDateTime.AddDate(0, 0, 1)
  889. sub := endDateTime.Sub(time.Now())
  890. expireDay := fmt.Sprintf("%v", int(sub.Hours()/24))
  891. list[i].ExpireDay = expireDay
  892. }
  893. }
  894. //行业未续约 需要处理公司对应行业权限下的权限状态
  895. if dataType == "行业未续约" {
  896. mappermissionNameStatus, err := cygxService.GetCompanyPermissionNameStatusMapByCompanyId(companyIds)
  897. if err != nil {
  898. br.Msg = "获取失败"
  899. br.ErrMsg = "获取失败,Err:" + err.Error()
  900. return
  901. }
  902. for _, v := range list {
  903. v.PermissionNameStatus = mappermissionNameStatus[fmt.Sprint(v.CompanyId, v.PermissionNameExport)]
  904. }
  905. }
  906. //合并合同所对应的权限
  907. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  908. if err != nil {
  909. br.Msg = "获取失败"
  910. br.ErrMsg = "获取失败,Err:" + err.Error()
  911. return
  912. }
  913. for _, v := range list {
  914. v.PermissionName = mappermissionName[v.CompanyContractId]
  915. }
  916. //处理展示顺序
  917. permissionNameSlice := []string{"医药", "消费", "科技", "智造", "策略", utils.CHART_PERMISSION_NAME_MF_YANXUAN, utils.YAN_XUAN_KOU_DIAN_BAO_NAME}
  918. var itemspermissionNameSlice []*models.CompanyContractPermissionNameGroupCountResp
  919. for _, vName := range permissionNameSlice {
  920. item := new(models.CompanyContractPermissionNameGroupCountResp)
  921. for _, v := range listPessionGroup {
  922. if v.PermissionName != vName {
  923. continue
  924. }
  925. item.Total = v.Total
  926. }
  927. item.PermissionName = vName
  928. itemspermissionNameSlice = append(itemspermissionNameSlice, item)
  929. }
  930. page := paging.GetPaging(currentIndex, pageSize, dataTotal)
  931. resp := response.IncrementalCompanyListResp{
  932. Paging: page,
  933. ListPermissionName: itemspermissionNameSlice,
  934. List: list,
  935. TrialTotal: trialTotal,
  936. NewCompanyTotal: newCompanyTotal,
  937. RenewalCompanyTotal: renewalCompanyTotal,
  938. NotRenewalCompanyTotal: notRenewalCompanyTotal,
  939. NotRenewalCompanyToBeConfirmTotal: notRenewalCompanyToBeConfirmTotal,
  940. NotRenewalTryOut: notRenewalTryOut,
  941. NotRenewalNotTryOut: notRenewalNotTryOut,
  942. }
  943. //fmt.Println()
  944. //导出excel
  945. if isExport {
  946. PermissionCompanyListListExport(this, dataType, resp, br)
  947. return
  948. }
  949. br.Ret = 200
  950. br.Success = true
  951. br.Msg = "获取成功"
  952. br.Data = resp
  953. }
  954. // PermissionCompanyListListExport 导出增量客户数据报表excel
  955. func PermissionCompanyListListExport(this *StatisticCompanyMergerController, dataType string, resp response.IncrementalCompanyListResp, br *models.BaseResponse) {
  956. dir, err := os.Executable()
  957. exPath := filepath.Dir(dir)
  958. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  959. xlsxFile := xlsx.NewFile()
  960. if err != nil {
  961. br.Msg = "生成文件失败"
  962. br.ErrMsg = "生成文件失败"
  963. return
  964. }
  965. style := xlsx.NewStyle()
  966. alignment := xlsx.Alignment{
  967. Horizontal: "center",
  968. Vertical: "center",
  969. WrapText: true,
  970. }
  971. permissionNameSlice := []string{"医药", "消费", "科技", "智造", "策略", "买方研选"}
  972. mapExport := make(map[string][]*models.IncrementalList)
  973. type IncrementalCompanyListExport struct {
  974. PermissionName string `description:"权限名"`
  975. List []*models.IncrementalList
  976. }
  977. for _, v := range resp.List {
  978. if v.PermissionNameExport == "" {
  979. continue
  980. }
  981. mapExport[v.PermissionNameExport] = append(mapExport[v.PermissionNameExport], v)
  982. }
  983. style.Alignment = alignment
  984. style.ApplyAlignment = true
  985. for _, vName := range permissionNameSlice {
  986. sheel, err := xlsxFile.AddSheet(vName)
  987. if err != nil {
  988. br.Msg = "新增Sheet失败"
  989. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  990. return
  991. }
  992. sheel.SetColWidth(0, 0, 30)
  993. sheel.SetColWidth(1, 1, 15)
  994. sheel.SetColWidth(2, 2, 15)
  995. sheel.SetColWidth(3, 3, 18)
  996. sheel.SetColWidth(4, 4, 40)
  997. sheel.SetColWidth(5, 5, 18)
  998. //表头
  999. titleRow := sheel.AddRow()
  1000. cellName := titleRow.AddCell()
  1001. cellName.SetStyle(style)
  1002. cellName.SetValue("客户名称")
  1003. cellSellerName := titleRow.AddCell()
  1004. cellSellerName.SetStyle(style)
  1005. cellSellerName.SetValue("所属销售")
  1006. cellMoney := titleRow.AddCell()
  1007. cellMoney.SetStyle(style)
  1008. cellMoney.SetValue("合同金额")
  1009. cellTime := titleRow.AddCell()
  1010. cellTime.SetStyle(style)
  1011. cellTime.SetValue("合同期限")
  1012. cellPermissionName := titleRow.AddCell()
  1013. cellPermissionName.SetStyle(style)
  1014. cellPermissionName.SetValue("签约套餐")
  1015. if dataType == "行业未续约" {
  1016. cellPermissionNameStatus := titleRow.AddCell()
  1017. cellPermissionNameStatus.SetStyle(style)
  1018. cellPermissionNameStatus.SetValue("当前行业状态")
  1019. cellCompanyProductStatus := titleRow.AddCell()
  1020. cellCompanyProductStatus.SetStyle(style)
  1021. cellCompanyProductStatus.SetValue("当前客户状态")
  1022. }
  1023. for _, v := range mapExport[vName] {
  1024. dataRow := sheel.AddRow()
  1025. dataRow.SetHeight(20)
  1026. cellDataName := dataRow.AddCell()
  1027. cellDataName.SetStyle(style)
  1028. cellDataName.SetValue(v.CompanyName)
  1029. cellDataSellerName := dataRow.AddCell()
  1030. cellDataSellerName.SetStyle(style)
  1031. cellDataSellerName.SetValue(v.SellerName)
  1032. cellDataMoney := dataRow.AddCell()
  1033. cellDataMoney.SetStyle(style)
  1034. cellDataMoney.SetValue(v.Money)
  1035. cellDataTime := dataRow.AddCell()
  1036. cellDataTime.SetStyle(style)
  1037. cellDataTime.SetValue(v.StartDate + "~" + v.EndDate)
  1038. cellDataPermissionName := dataRow.AddCell()
  1039. cellDataPermissionName.SetStyle(style)
  1040. cellDataPermissionName.SetValue(v.PermissionName)
  1041. if dataType == "行业未续约" {
  1042. cellDataPermissionNameStatus := dataRow.AddCell()
  1043. cellDataPermissionNameStatus.SetStyle(style)
  1044. cellDataPermissionNameStatus.SetValue(v.PermissionNameStatus)
  1045. cellDataCompanyProductStatus := dataRow.AddCell()
  1046. cellDataCompanyProductStatus.SetStyle(style)
  1047. cellDataCompanyProductStatus.SetValue(v.CompanyProductStatus)
  1048. }
  1049. }
  1050. }
  1051. err = xlsxFile.Save(downLoadnFilePath)
  1052. if err != nil {
  1053. br.Msg = "保存文件失败"
  1054. br.ErrMsg = "保存文件失败"
  1055. return
  1056. }
  1057. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1058. downloadFileName := dataType + "数据_" + randStr + ".xlsx"
  1059. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  1060. defer func() {
  1061. os.Remove(downLoadnFilePath)
  1062. }()
  1063. br.Ret = 200
  1064. br.Success = true
  1065. br.Msg = "导出成功"
  1066. }
  1067. // @Title 权益客户续约率统计
  1068. // @Description 权益客户续约率统计接口
  1069. // @Param PageSize query int true "每页数据条数"
  1070. // @Param CurrentIndex query int true "当前页页码,从1开始"
  1071. // @Param AdminId query string true "销售id,多个用英文逗号隔开,空字符串为全部"
  1072. // @Param StartDate query string false "开始日期"
  1073. // @Param EndDate query string false "结束日期"
  1074. // @Param ContractDataType query string false "合同类型,枚举值:`续约合同`,`到期合同`"
  1075. // @Success 200 {object} response.IncrementalCompanyListResp
  1076. // @router /merge_company/company_contract_percentage/list [get]
  1077. func (this *StatisticCompanyMergerController) CompanyContractPercentageList() {
  1078. br := new(models.BaseResponse).Init()
  1079. defer func() {
  1080. this.Data["json"] = br
  1081. this.ServeJSON()
  1082. }()
  1083. sysUser := this.SysUser
  1084. if sysUser == nil {
  1085. br.Msg = "请登录"
  1086. br.ErrMsg = "请登录,SysUser Is Empty"
  1087. br.Ret = 408
  1088. return
  1089. }
  1090. pageSize, _ := this.GetInt("PageSize")
  1091. currentIndex, _ := this.GetInt("CurrentIndex")
  1092. adminId := this.GetString("AdminId")
  1093. contractDataType := this.GetString("ContractDataType")
  1094. startDate := this.GetString("StartDate")
  1095. endDate := this.GetString("EndDate")
  1096. initendDate := this.GetString("EndDate")
  1097. if startDate == "" || endDate == "" {
  1098. br.Msg = "开始时间或结束时间不能为空"
  1099. return
  1100. }
  1101. //如果传的查询方式是今年,那么就以当前时间作为截止时间
  1102. //if endDate == strconv.Itoa(time.Now().Year())+"-12-31" {
  1103. // endDate = time.Now().Format(utils.FormatDate)
  1104. //}
  1105. //判断结束时间是否晚于当前时间,如果晚于当前时间,那么就把当前时间作为截止时间。
  1106. endDateTime, _ := time.Parse(utils.FormatDate, endDate)
  1107. if endDateTime.After(time.Now()) {
  1108. endDate = time.Now().Format(utils.FormatDate)
  1109. }
  1110. //fmt.Println("endDate", endDate)
  1111. if contractDataType == "" {
  1112. contractDataType = "续约合同"
  1113. }
  1114. var startSize int
  1115. if pageSize <= 0 {
  1116. pageSize = utils.PageSize20
  1117. }
  1118. if currentIndex <= 0 {
  1119. currentIndex = 1
  1120. }
  1121. startSize = utils.StartIndex(currentIndex, pageSize)
  1122. var condition string
  1123. var pars []interface{}
  1124. //条件
  1125. if adminId != "" {
  1126. condition += ` AND c.seller_id in (` + adminId + `) `
  1127. } else {
  1128. //根据当前角色来获取查询条件
  1129. condition, pars = getQueryParams(condition, pars, sysUser, "c.")
  1130. }
  1131. //权益有效合同
  1132. condition += ` AND c.product_id = 2 AND a.status = 1 `
  1133. var list []*models.IncrementalList
  1134. var renewalRateMoney string //合同金额续约率
  1135. var renewalRateMoneyContent string //合同金额续约率详情
  1136. var renewalRateTotal string //合同数量续约率
  1137. var renewalRateTotalContent string //合同数量续约率详情
  1138. var renewalContractTotal int //续约合同数量
  1139. var renewalContractMoney int //续约合同数量
  1140. var expireRenewalContractTotal int //到期合同数量
  1141. var expireRenewalContractMoney int //到期合同数量
  1142. var dataTotal int //分页数据
  1143. conditionRenewal := condition // 续约合同查询条件查询条件
  1144. parsRenewal := pars
  1145. conditionRenewal += ` AND a.start_date >= ? AND a.start_date <= ? `
  1146. parsRenewal = append(parsRenewal, startDate, endDate)
  1147. conditionRenewal += ` AND a.rai_contract_type = ? `
  1148. parsRenewal = append(parsRenewal, "续约合同")
  1149. totalRenewal, err := company.GetIncrementalRenewalCompanyProductMergeCount(conditionRenewal, parsRenewal)
  1150. if err != nil {
  1151. br.Msg = "获取失败"
  1152. br.ErrMsg = "获取失败,Err:" + err.Error()
  1153. return
  1154. }
  1155. totalMoneyRenewal, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(conditionRenewal, parsRenewal)
  1156. if err != nil {
  1157. br.Msg = "获取失败"
  1158. br.ErrMsg = "获取失败,Err:" + err.Error()
  1159. return
  1160. }
  1161. //续约合同查询条件查询条件 end
  1162. conditionRenewalEnd := condition // 今年结束的续约合同查询条件
  1163. parsRenewalEnd := pars
  1164. conditionRenewalEnd += ` AND a.end_date >= ? AND a.end_date <= ? `
  1165. parsRenewalEnd = append(parsRenewalEnd, startDate, endDate)
  1166. conditionRenewalEnd += ` AND a.rai_contract_type = ? `
  1167. parsRenewalEnd = append(parsRenewalEnd, "续约合同")
  1168. totalRenewalEnd, err := company.GetIncrementalRenewalCompanyProductMergeCount(conditionRenewalEnd, parsRenewalEnd)
  1169. if err != nil && err.Error() != utils.ErrNoRow() {
  1170. br.Msg = "获取失败"
  1171. br.ErrMsg = "获取失败,Err:" + err.Error()
  1172. return
  1173. }
  1174. totalMoneyRenewalEnd, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(conditionRenewalEnd, parsRenewalEnd)
  1175. if err != nil {
  1176. br.Msg = "获取失败"
  1177. br.ErrMsg = "获取失败,Err:" + err.Error()
  1178. return
  1179. } // 今年结束的续约合同查询条件end
  1180. conditionNew := condition // 新签合同查询条件
  1181. parsNew := pars
  1182. conditionNew += ` AND a.end_date >= ? AND a.end_date <= ? `
  1183. parsNew = append(parsNew, startDate, endDate)
  1184. conditionNew += ` AND a.rai_contract_type = ? `
  1185. parsNew = append(parsNew, "新签合同")
  1186. totalNew, err := company.GetIncrementalRenewalCompanyProductMergeCount(conditionNew, parsNew)
  1187. if err != nil && err.Error() != utils.ErrNoRow() {
  1188. br.Msg = "获取失败"
  1189. br.ErrMsg = "获取失败,Err:" + err.Error()
  1190. return
  1191. }
  1192. totalMoneyNew, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(conditionNew, parsNew)
  1193. if err != nil {
  1194. br.Msg = "获取失败"
  1195. br.ErrMsg = "获取失败,Err:" + err.Error()
  1196. return
  1197. } // 新签合同查询条件 end
  1198. //分子或者分母为零的时候,不做计算
  1199. if totalRenewal == 0 || totalNew+totalRenewalEnd == 0 {
  1200. renewalRateMoney = "0%"
  1201. renewalRateTotal = "0%"
  1202. } else {
  1203. renewalRateMoney = fmt.Sprint(utils.SubFloatToString(float64(totalMoneyRenewal)/float64(totalMoneyNew+totalMoneyRenewalEnd)*100, 2), "%")
  1204. renewalRateTotal = fmt.Sprint(utils.SubFloatToString(float64(totalRenewal)/float64(totalNew+totalRenewalEnd)*100, 2), "%")
  1205. }
  1206. renewalRateTotalContent = fmt.Sprint(int(totalMoneyRenewal), "/", int(totalMoneyNew+totalMoneyRenewalEnd))
  1207. renewalRateMoneyContent = fmt.Sprint(totalRenewal, "份/", totalNew+totalRenewalEnd, "份")
  1208. if contractDataType == "续约合同" {
  1209. condition1 := condition
  1210. pars1 := pars
  1211. condition1 += ` AND a.start_date >= ? AND a.start_date <= ? `
  1212. pars1 = append(pars1, startDate, endDate)
  1213. condition1 += ` AND a.rai_contract_type = ? `
  1214. pars1 = append(pars1, "续约合同")
  1215. total, err := company.GetIncrementalRenewalCompanyProductMergeCount(condition1, pars1)
  1216. if err != nil && err.Error() != utils.ErrNoRow() {
  1217. br.Msg = "获取失败"
  1218. br.ErrMsg = "获取失败,Err:" + err.Error()
  1219. return
  1220. }
  1221. totalMoney, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(condition1, pars1)
  1222. if err != nil && err.Error() != utils.ErrNoRow() {
  1223. br.Msg = "获取失败"
  1224. br.ErrMsg = "获取失败,Err:" + err.Error()
  1225. return
  1226. }
  1227. renewalContractTotal = total
  1228. renewalContractMoney = int(totalMoney)
  1229. //列表页数据
  1230. tmpList, err := models.GetIncrementalCompanyMergeList(condition1, pars1, startSize, pageSize)
  1231. if err != nil {
  1232. br.Msg = "获取失败"
  1233. br.ErrMsg = "获取失败,Err:" + err.Error()
  1234. return
  1235. }
  1236. list = tmpList
  1237. dataTotal = total
  1238. }
  1239. if contractDataType == "到期合同" {
  1240. condition1 := condition
  1241. pars1 := pars
  1242. //判断结束时间是否晚于当前时间,如果晚于当前时间,那么就把当前时间作为截止时间。并且当前这天的到期的合同,不查询
  1243. endDateTime, _ := time.Parse(utils.FormatDate, initendDate)
  1244. if endDateTime.After(time.Now()) {
  1245. endDate = time.Now().Format(utils.FormatDate)
  1246. condition1 += ` AND a.end_date >= ? AND a.end_date < ? `
  1247. } else {
  1248. condition1 += ` AND a.end_date >= ? AND a.end_date <= ? `
  1249. }
  1250. //condition1 += ` AND a.end_date >= ? AND a.end_date <= ? `
  1251. pars1 = append(pars1, startDate, endDate)
  1252. total, err := company.GetIncrementalRenewalCompanyProductMergeCount(condition1, pars1)
  1253. if err != nil && err.Error() != utils.ErrNoRow() {
  1254. br.Msg = "获取失败"
  1255. br.ErrMsg = "获取失败,Err:" + err.Error()
  1256. return
  1257. }
  1258. totalMoney, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(condition1, pars1)
  1259. if err != nil && err.Error() != utils.ErrNoRow() {
  1260. br.Msg = "获取失败"
  1261. br.ErrMsg = "获取失败,Err:" + err.Error()
  1262. return
  1263. }
  1264. expireRenewalContractTotal = total
  1265. expireRenewalContractMoney = int(totalMoney)
  1266. //列表页数据
  1267. tmpList, err := models.GetIncrementalCompanyMergeListEnd(condition1, pars1, startSize, pageSize)
  1268. if err != nil {
  1269. br.Msg = "获取失败"
  1270. br.ErrMsg = "获取失败,Err:" + err.Error()
  1271. return
  1272. }
  1273. list = tmpList
  1274. dataTotal = total
  1275. }
  1276. listLen := len(list)
  1277. if listLen == 0 {
  1278. list = make([]*models.IncrementalList, 0)
  1279. }
  1280. var companyContractIds []int
  1281. for i := 0; i < listLen; i++ {
  1282. item := list[i]
  1283. companyContractIds = append(companyContractIds, item.CompanyContractId)
  1284. }
  1285. //合并合同所对应的权限
  1286. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  1287. if err != nil {
  1288. br.Msg = "获取失败"
  1289. br.ErrMsg = "获取失败,Err:" + err.Error()
  1290. return
  1291. }
  1292. for _, v := range list {
  1293. v.PermissionName = mappermissionName[v.CompanyContractId]
  1294. }
  1295. page := paging.GetPaging(currentIndex, pageSize, dataTotal)
  1296. resp := response.IncrementalCompanyPercentageListResp{
  1297. Paging: page,
  1298. RenewalContractTotal: renewalContractTotal,
  1299. RenewalContractMoney: renewalContractMoney,
  1300. ExpireRenewalContractTotal: expireRenewalContractTotal,
  1301. ExpireRenewalContractMoney: expireRenewalContractMoney,
  1302. RenewalRateMoney: renewalRateMoney,
  1303. RenewalRateMoneyContent: renewalRateMoneyContent,
  1304. RenewalRateTotal: renewalRateTotal,
  1305. RenewalRateTotalContent: renewalRateTotalContent,
  1306. List: list,
  1307. }
  1308. br.Ret = 200
  1309. br.Success = true
  1310. br.Msg = "获取成功"
  1311. br.Data = resp
  1312. }
  1313. // @Title 权益客户续约率统计所能查询的年份
  1314. // @Description权益客户续约率统计所能查询的年份接口
  1315. // @Success 200 {object} response.IncrementalCompanyListResp
  1316. // @router /merge_company/get_year_list [get]
  1317. func (this *StatisticCompanyMergerController) GetYearList() {
  1318. br := new(models.BaseResponse).Init()
  1319. defer func() {
  1320. this.Data["json"] = br
  1321. this.ServeJSON()
  1322. }()
  1323. sysUser := this.SysUser
  1324. if sysUser == nil {
  1325. br.Msg = "请登录"
  1326. br.ErrMsg = "请登录,SysUser Is Empty"
  1327. br.Ret = 408
  1328. return
  1329. }
  1330. thisYear := time.Now().Year()
  1331. startYear := 2022 //初始查询年份
  1332. resp := new(company.SearchYearListResp)
  1333. var yearList []int
  1334. for i := startYear; i <= thisYear; i++ {
  1335. yearList = append(yearList, i)
  1336. }
  1337. resp.List = yearList
  1338. br.Ret = 200
  1339. br.Success = true
  1340. br.Msg = "获取成功"
  1341. br.Data = resp
  1342. }
  1343. // @Title 权益客户续约率统计
  1344. // @Description 权益客户续约率统计接口
  1345. // @Param PageSize query int true "每页数据条数"
  1346. // @Param CurrentIndex query int true "当前页页码,从1开始"
  1347. // @Param AdminId query string true "销售id,多个用英文逗号隔开,空字符串为全部"
  1348. // @Param StartDate query string false "开始日期"
  1349. // @Param EndDate query string false "结束日期"
  1350. // @Param ContractDataType query string false "合同类型,枚举值:`续约合同`,`到期合同`"
  1351. // @Success 200 {object} response.IncrementalCompanyListResp
  1352. // @router /merge_company/company_contract_percentage/listV2 [get]
  1353. func (this *StatisticCompanyMergerController) CompanyContractPercentageListV2() {
  1354. br := new(models.BaseResponse).Init()
  1355. defer func() {
  1356. this.Data["json"] = br
  1357. this.ServeJSON()
  1358. }()
  1359. sysUser := this.SysUser
  1360. if sysUser == nil {
  1361. br.Msg = "请登录"
  1362. br.ErrMsg = "请登录,SysUser Is Empty"
  1363. br.Ret = 408
  1364. return
  1365. }
  1366. pageSize, _ := this.GetInt("PageSize")
  1367. currentIndex, _ := this.GetInt("CurrentIndex")
  1368. adminId := this.GetString("AdminId")
  1369. contractDataType := this.GetString("ContractDataType")
  1370. startDate := this.GetString("StartDate")
  1371. endDate := this.GetString("EndDate")
  1372. //initendDate := this.GetString("EndDate")
  1373. if startDate == "" || endDate == "" {
  1374. br.Msg = "开始时间或结束时间不能为空"
  1375. return
  1376. }
  1377. //如果传的查询方式是今年,那么就以当前时间作为截止时间
  1378. //if endDate == strconv.Itoa(time.Now().Year())+"-12-31" {
  1379. // endDate = time.Now().Format(utils.FormatDate)
  1380. //}
  1381. //判断结束时间是否晚于当前时间,如果晚于当前时间,那么就把当前时间作为截止时间。
  1382. endDateTime, _ := time.Parse(utils.FormatDate, endDate)
  1383. if endDateTime.After(time.Now()) {
  1384. endDate = time.Now().Format(utils.FormatDate)
  1385. }
  1386. //fmt.Println("endDate", endDate)
  1387. if contractDataType == "" {
  1388. contractDataType = "续约合同"
  1389. }
  1390. var startSize int
  1391. if pageSize <= 0 {
  1392. pageSize = utils.PageSize20
  1393. }
  1394. if currentIndex <= 0 {
  1395. currentIndex = 1
  1396. }
  1397. startSize = utils.StartIndex(currentIndex, pageSize)
  1398. var condition string
  1399. var pars []interface{}
  1400. //条件
  1401. if adminId != "" {
  1402. condition += ` AND c.seller_id in (` + adminId + `) `
  1403. } else {
  1404. //根据当前角色来获取查询条件
  1405. condition, pars = getQueryParams(condition, pars, sysUser, "c.")
  1406. }
  1407. //权益有效合同
  1408. condition += ` AND c.product_id = 2 AND a.status = 1 `
  1409. var list []*models.IncrementalList
  1410. var renewalRateMoney string //合同金额续约率
  1411. var renewalRateMoneyContent string //合同金额续约率详情
  1412. var renewalRateTotal string //合同数量续约率
  1413. var renewalRateTotalContent string //合同数量续约率详情
  1414. var renewalContractTotal int //续约合同数量
  1415. var renewalContractMoney int //续约合同数量
  1416. var expireRenewalContractTotal int //到期合同数量
  1417. var expireRenewalContractMoney int //到期合同数量
  1418. var dataTotal int //分页数据
  1419. //conditionRenewal := condition // 续约合同查询条件查询条件
  1420. //parsRenewal := pars
  1421. //conditionRenewal += ` AND a.start_date >= ? AND a.start_date <= ? `
  1422. //conditionRenewal += ` AND a.end_date >= ? AND a.end_date <= ? `
  1423. //parsRenewal = append(parsRenewal, startDate, endDate)
  1424. ////conditionRenewal += ` AND a.rai_contract_type = ? `
  1425. ////parsRenewal = append(parsRenewal, "续约合同")
  1426. //
  1427. //conditionRenewal += ` AND a.company_ascribe_id > 0 AND a.company_ascribe_id !=9 ` // 已确认未续约
  1428. //totalRenewal, err := company.GetIncrementalRenewalCompanyProductMergeCount(conditionRenewal, parsRenewal)
  1429. //if err != nil {
  1430. // br.Msg = "获取失败"
  1431. // br.ErrMsg = "获取失败,Err:" + err.Error()
  1432. // return
  1433. //}
  1434. //
  1435. //totalMoneyRenewal, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(conditionRenewal, parsRenewal)
  1436. //if err != nil {
  1437. // br.Msg = "获取失败"
  1438. // br.ErrMsg = "获取失败,Err:" + err.Error()
  1439. // return
  1440. //}
  1441. //续约合同查询条件查询条件 end
  1442. //conditionRenewalEnd := condition // 今年结束的续约合同查询条件
  1443. //parsRenewalEnd := pars
  1444. //
  1445. ////startDateTime, _ := time.Parse(utils.FormatDate, endDate)
  1446. ////startDateTime = startDateTime.AddDate(-1, 0, 2) //合同开始时间,与查询的结束时间,之间相差的天数大于 365天。
  1447. ////fmt.Println("startDateTime", startDateTime)
  1448. //conditionRenewalEnd += ` AND a.end_date >= ? AND a.end_date <= ? `
  1449. //parsRenewalEnd = append(parsRenewalEnd, startDate, endDate)
  1450. //conditionRenewalEnd += ` AND a.rai_contract_type = ? `
  1451. //parsRenewalEnd = append(parsRenewalEnd, "续约合同")
  1452. //
  1453. //totalRenewalEnd, err := company.GetIncrementalRenewalCompanyProductMergeCount(conditionRenewalEnd, parsRenewalEnd)
  1454. //if err != nil && err.Error() != utils.ErrNoRow() {
  1455. // br.Msg = "获取失败"
  1456. // br.ErrMsg = "获取失败,Err:" + err.Error()
  1457. // return
  1458. //}
  1459. ////return
  1460. //totalMoneyRenewalEnd, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(conditionRenewalEnd, parsRenewalEnd)
  1461. //if err != nil {
  1462. // br.Msg = "获取失败"
  1463. // br.ErrMsg = "获取失败,Err:" + err.Error()
  1464. // return
  1465. //} // 今年结束的续约合同查询条件end
  1466. //
  1467. //conditionNew := condition // 新签合同查询条件
  1468. //parsNew := pars
  1469. //conditionNew += ` AND a.end_date >= ? AND a.end_date <= ? `
  1470. //parsNew = append(parsNew, startDate, endDate)
  1471. //conditionNew += ` AND a.rai_contract_type = ? `
  1472. //parsNew = append(parsNew, "新签合同")
  1473. //totalNew, err := company.GetIncrementalRenewalCompanyProductMergeCount(conditionNew, parsNew)
  1474. //if err != nil && err.Error() != utils.ErrNoRow() {
  1475. // br.Msg = "获取失败"
  1476. // br.ErrMsg = "获取失败,Err:" + err.Error()
  1477. // return
  1478. //}
  1479. //totalMoneyNew, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(conditionNew, parsNew)
  1480. //if err != nil {
  1481. // br.Msg = "获取失败"
  1482. // br.ErrMsg = "获取失败,Err:" + err.Error()
  1483. // return
  1484. //} // 新签合同查询条件 end
  1485. //fmt.Println("totalNew", totalNew)
  1486. //fmt.Println("totalRenewalEnd", totalRenewalEnd)
  1487. condition1 := condition
  1488. pars1 := pars
  1489. //condition1 += ` AND a.start_date >= ? AND a.start_date <= ? `
  1490. condition1 += ` AND a.end_date >= ? AND a.end_date <= ? `
  1491. pars1 = append(pars1, startDate, endDate)
  1492. //condition1 += ` AND a.rai_contract_type = ? `
  1493. //pars1 = append(pars1, "续约合同")
  1494. condition1 += ` AND a.company_ascribe_id > 0 AND a.company_ascribe_id !=9 ` // 已确认未续约
  1495. totalNo, err := company.GetIncrementalRenewalCompanyProductMergeCount(condition1, pars1)
  1496. if err != nil && err.Error() != utils.ErrNoRow() {
  1497. br.Msg = "获取失败"
  1498. br.ErrMsg = "获取失败,Err:" + err.Error()
  1499. return
  1500. }
  1501. totalMoney, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(condition1, pars1)
  1502. if err != nil && err.Error() != utils.ErrNoRow() {
  1503. br.Msg = "获取失败"
  1504. br.ErrMsg = "获取失败,Err:" + err.Error()
  1505. return
  1506. }
  1507. renewalContractTotal = totalNo
  1508. renewalContractMoney = int(totalMoney)
  1509. if contractDataType == "续约合同" || contractDataType == "确认不续约合同" {
  1510. //列表页数据
  1511. tmpList, err := models.GetIncrementalCompanyMergeListEnd(condition1, pars1, startSize, pageSize)
  1512. if err != nil {
  1513. br.Msg = "获取失败"
  1514. br.ErrMsg = "获取失败,Err:" + err.Error()
  1515. return
  1516. }
  1517. list = tmpList
  1518. dataTotal = totalNo
  1519. }
  1520. conditionEnd1 := condition
  1521. parsEnd1 := pars
  1522. //判断结束时间是否晚于当前时间,如果晚于当前时间,那么就把当前时间作为截止时间。并且当前这天的到期的合同,不查询
  1523. //endDateTime, _ := time.Parse(utils.FormatDate, initendDate)
  1524. //if endDateTime.After(time.Now()) {
  1525. // endDate = time.Now().Format(utils.FormatDate)
  1526. // condition1 += ` AND a.end_date >= ? AND a.end_date < ? `
  1527. //} else {
  1528. // condition1 += ` AND a.end_date >= ? AND a.end_date <= ? `
  1529. //}
  1530. conditionEnd1 += ` AND a.end_date >= ? AND a.end_date <= ? AND a.company_ascribe_id !=9 `
  1531. parsEnd1 = append(parsEnd1, startDate, endDate)
  1532. //condition1 += ` AND a.start_date < ? `
  1533. //pars1 = append(pars1, startDateTime)
  1534. totalEnd, err := company.GetIncrementalRenewalCompanyProductMergeCount(conditionEnd1, parsEnd1)
  1535. if err != nil && err.Error() != utils.ErrNoRow() {
  1536. br.Msg = "获取失败"
  1537. br.ErrMsg = "获取失败,Err:" + err.Error()
  1538. return
  1539. }
  1540. totalMoneyEnd, err := company.GetIncrementalRenewalCompanyProductMergeSumMoney(conditionEnd1, parsEnd1)
  1541. if err != nil && err.Error() != utils.ErrNoRow() {
  1542. br.Msg = "获取失败"
  1543. br.ErrMsg = "获取失败,Err:" + err.Error()
  1544. return
  1545. }
  1546. expireRenewalContractTotal = totalEnd
  1547. expireRenewalContractMoney = int(totalMoneyEnd)
  1548. if contractDataType == "到期合同" {
  1549. //列表页数据
  1550. tmpList, err := models.GetIncrementalCompanyMergeListEnd(conditionEnd1, parsEnd1, startSize, pageSize)
  1551. if err != nil {
  1552. br.Msg = "获取失败"
  1553. br.ErrMsg = "获取失败,Err:" + err.Error()
  1554. return
  1555. }
  1556. list = tmpList
  1557. dataTotal = totalEnd
  1558. }
  1559. //分子或者分母为零的时候,不做计算
  1560. if totalNo == 0 || totalEnd == 0 {
  1561. renewalRateMoney = "0%"
  1562. renewalRateTotal = "0%"
  1563. } else {
  1564. renewalRateMoney = fmt.Sprint(utils.SubFloatToString(float64(totalMoney)/float64(totalMoneyEnd)*100, 2), "%")
  1565. renewalRateTotal = fmt.Sprint(utils.SubFloatToString(float64(totalNo)/float64(totalEnd)*100, 2), "%")
  1566. }
  1567. renewalRateTotalContent = fmt.Sprint(int(totalMoney), "/", int(totalMoneyEnd))
  1568. renewalRateMoneyContent = fmt.Sprint(totalNo, "份/", totalEnd, "份")
  1569. listLen := len(list)
  1570. if listLen == 0 {
  1571. list = make([]*models.IncrementalList, 0)
  1572. }
  1573. var companyContractIds []int
  1574. for i := 0; i < listLen; i++ {
  1575. item := list[i]
  1576. companyContractIds = append(companyContractIds, item.CompanyContractId)
  1577. }
  1578. //合同归因标签
  1579. mapGetCompanyAscribeContent, mapContent := services.GetCompanyContractAscribeContentMap(companyContractIds)
  1580. //合并合同所对应的权限
  1581. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  1582. if err != nil {
  1583. br.Msg = "获取失败"
  1584. br.ErrMsg = "获取失败,Err:" + err.Error()
  1585. return
  1586. }
  1587. for _, v := range list {
  1588. v.PermissionName = mappermissionName[v.CompanyContractId]
  1589. if mapGetCompanyAscribeContent[v.CompanyContractId] != "" {
  1590. v.Content = mapContent[v.CompanyContractId]
  1591. v.AscribeContent = mapGetCompanyAscribeContent[v.CompanyContractId]
  1592. v.IsShowNoRenewedNote = true
  1593. }
  1594. }
  1595. page := paging.GetPaging(currentIndex, pageSize, dataTotal)
  1596. resp := response.IncrementalCompanyPercentageListResp{
  1597. Paging: page,
  1598. RenewalContractTotal: renewalContractTotal,
  1599. RenewalContractMoney: renewalContractMoney,
  1600. ExpireRenewalContractTotal: expireRenewalContractTotal,
  1601. ExpireRenewalContractMoney: expireRenewalContractMoney,
  1602. RenewalRateMoney: renewalRateMoney,
  1603. RenewalRateMoneyContent: renewalRateMoneyContent,
  1604. RenewalRateTotal: renewalRateTotal,
  1605. RenewalRateTotalContent: renewalRateTotalContent,
  1606. List: list,
  1607. }
  1608. br.Ret = 200
  1609. br.Success = true
  1610. br.Msg = "获取成功"
  1611. br.Data = resp
  1612. }
  1613. func init213() {
  1614. var condition string
  1615. var pars []interface{}
  1616. condition = " AND rai_contract_type = '续约合同' AND product_id = 2 "
  1617. var companyContractIds []string
  1618. companyContractList, e := company.GetCompanyContractList(condition, pars)
  1619. if e != nil && e.Error() != utils.ErrNoRow() {
  1620. fmt.Println(e)
  1621. return
  1622. }
  1623. for _, v := range companyContractList {
  1624. fmt.Println(v.CompanyContractId)
  1625. startDateTime, _ := time.Parse(utils.FormatDate, v.StartDate)
  1626. startDateTime = startDateTime.AddDate(-1, 0, 0)
  1627. totalLastYear, err := company.GetCompanyContractCountRaiByLastYear(v.CompanyId, startDateTime.Format(utils.FormatDate))
  1628. if err != nil {
  1629. fmt.Println(err)
  1630. return
  1631. }
  1632. if totalLastYear > 0 {
  1633. companyContractIds = append(companyContractIds, strconv.Itoa(v.CompanyContractId))
  1634. }
  1635. }
  1636. fmt.Println("companyContractIds", len(companyContractIds))
  1637. fmt.Println(strings.Join(companyContractIds, ","))
  1638. err := company.UpdateCompanyContractRaiContractTypeInit(strings.Join(companyContractIds, ","))
  1639. fmt.Println(err)
  1640. return
  1641. }
  1642. func init323() {
  1643. var conditionConfirm string
  1644. var parsConfirm []interface{}
  1645. companyConfirmList, err := company.GetCompanyContractNoRenewedAscribeList(conditionConfirm, parsConfirm, 0, 0)
  1646. if err != nil && err.Error() != utils.ErrNoRow() {
  1647. fmt.Println(err)
  1648. return
  1649. }
  1650. for _, v := range companyConfirmList {
  1651. err = company.UpdateCompanyContractCompanyAscribeId(v.CompanyAscribeId, v.CompanyContractId)
  1652. if err != nil && err.Error() != utils.ErrNoRow() {
  1653. fmt.Println(err)
  1654. return
  1655. }
  1656. }
  1657. }