full_company.go 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798
  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/company_todo"
  9. "hongze/hz_crm_api/services"
  10. cygxService "hongze/hz_crm_api/services/cygx"
  11. "hongze/hz_crm_api/utils"
  12. "os"
  13. "path/filepath"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // FullCompanyController 全量客户管理
  19. type FullCompanyController struct {
  20. BaseAuthController
  21. }
  22. // List
  23. // @Title 客户列表
  24. // @Description 客户列表接口
  25. // @Param PageSize query int true "每页数据条数"
  26. // @Param CurrentIndex query int true "当前页页码,从1开始"
  27. // @Param Status query string true "状态:'试用','永续','冻结','流失','正式'"
  28. // @Param CompanyType query string true "客户类型:传空字符串或者不传为全部,'ficc','权益'"
  29. // @Param IndustryId query int true "行业id,0为全部"
  30. // @Param AdminId query string true "销售id,多个用英文逗号隔开,空字符串为全部"
  31. // @Param ChartPermissionIds query string false "阅读权限品种ID,多个用英文逗号分开,空字符串为全部"
  32. // @Param ContractPermissionIds query string false "已购品种ID,多个用英文逗号分开,空字符串为全部"
  33. // @Param StartDate query string true "开始时间"
  34. // @Param EndDate query string true "结束时间"
  35. // @Param CreateStartDate query string true "创建开始时间"
  36. // @Param CreateEndDate query string true "创建结束时间"
  37. // @Param KeyWord query string true "搜索关键词"
  38. // @Param Address query string true "地址 , 多个用英文逗号分开 "
  39. // @Param Province query string true "省 , 多个用英文逗号分开 "
  40. // @Param City query string true "市 , 多个用英文逗号分开 "
  41. // @Param PackageType query int true "套餐类型,0:无,1:大套餐,2:小套餐"
  42. // @Param TodoStatus query string true "任务处理状态;枚举值:'无任务','未完成','已完成'"
  43. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'viewTotal':总阅读次数 、 'viewTime':阅读时间 、 'roadShowTotal':累计路演次数 、`expireDay:到期时间` 、 `createTime:创建时间` 、 'formalTime': 转正时间 、 'freezeTime':冻结时间 、'lossTime':流失时间 、'tryStage':试用标签 、'deadline':距离未完成的任务的截止日期的天数、'tryOutDay':试用天数"
  44. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  45. // @Param SortByTodo query bool true "是否根据待办排序,true或false"
  46. // @Param TryStage query int false "试用客户子标签:0全部、1未分类、2 推进、3 跟踪、4 预备"
  47. // @Success 200 {object} company.CompanyListResp
  48. // @router /full/list [get]
  49. func (this *FullCompanyController) List() {
  50. br := new(models.BaseResponse).Init()
  51. defer func() {
  52. this.Data["json"] = br
  53. this.ServeJSON()
  54. }()
  55. sysUser := this.SysUser
  56. if sysUser == nil {
  57. br.Msg = "请登录"
  58. br.ErrMsg = "请登录,SysUser Is Empty"
  59. br.Ret = 408
  60. return
  61. }
  62. roleTypeCode := sysUser.RoleTypeCode
  63. pageSize, _ := this.GetInt("PageSize")
  64. currentIndex, _ := this.GetInt("CurrentIndex")
  65. status := this.GetString("Status")
  66. companyType := this.GetString("CompanyType")
  67. industryId, _ := this.GetInt("IndustryId")
  68. adminId := this.GetString("AdminId")
  69. chartPermissionIds := this.GetString("ChartPermissionIds")
  70. contractPermissionIds := this.GetString("ContractPermissionIds")
  71. startDate := this.GetString("StartDate")
  72. endDate := this.GetString("EndDate")
  73. createStartDate := this.GetString("CreateStartDate")
  74. createEndDate := this.GetString("CreateEndDate")
  75. keyWord := this.GetString("KeyWord")
  76. address := this.GetString("Address")
  77. province := this.GetString("Province")
  78. city := this.GetString("City")
  79. keyWord = strings.Trim(keyWord, " ")
  80. keyWord = strings.Replace(keyWord, "'", "", -1)
  81. todoStatus := this.GetString("TodoStatus") //任务状态
  82. tryStage, _ := this.GetInt("TryStage") //试用客户标签
  83. // 套餐类型
  84. packageType, _ := this.GetInt("PackageType")
  85. //排序参数
  86. sortParam := this.GetString("SortParam")
  87. sortType := this.GetString("SortType")
  88. sortByTodo, _ := this.GetBool("SortByTodo")
  89. var total int
  90. page := paging.GetPaging(currentIndex, pageSize, total)
  91. resp := new(company.CompanyListResp)
  92. statusItems := make([]*company.CompanyStatus, 0)
  93. var statusArr = [...]string{"全部", "试用", "正式", "冻结", "流失", "关闭", "永续"}
  94. //var statusArr = [...]string{"全部", "试用-活跃", "试用-非活跃", "正式", "冻结", "流失", "永续"}
  95. var startSize int
  96. if pageSize <= 0 {
  97. pageSize = utils.PageSize20
  98. }
  99. if currentIndex <= 0 {
  100. currentIndex = 1
  101. }
  102. startSize = paging.StartIndex(currentIndex, pageSize)
  103. var condition string
  104. var pars []interface{}
  105. //流失
  106. var lossCondition string
  107. var lossPars []interface{}
  108. //状态分类查询条件
  109. var permissionCondition string
  110. var permissionPars []interface{}
  111. // 角色为销售主管时, 可见所有, 其余角色依旧走原来的判断
  112. if companyType != "" && sysUser.RoleName != utils.ROLE_NAME_FICC_DIRECTOR {
  113. condition += ` AND b.product_name=? `
  114. pars = append(pars, companyType)
  115. }
  116. if industryId > 0 {
  117. condition += ` AND b.industry_id=? `
  118. pars = append(pars, industryId)
  119. }
  120. if adminId != "" {
  121. condition += ` AND b.seller_id IN (` + adminId + `) `
  122. }
  123. if startDate != "" {
  124. condition += ` AND b.start_date>=? `
  125. pars = append(pars, startDate)
  126. }
  127. if endDate != "" {
  128. condition += ` AND b.end_date<=? `
  129. pars = append(pars, endDate)
  130. }
  131. if createStartDate != "" {
  132. condition += ` AND a.created_time>=? `
  133. pars = append(pars, createStartDate)
  134. }
  135. if createEndDate != "" {
  136. condition += ` AND a.created_time<=? `
  137. pars = append(pars, createEndDate)
  138. }
  139. if address != "" {
  140. var addressSql string
  141. slice := strings.Split(address, ",")
  142. for _, v := range slice {
  143. addressSql += "'" + v + "'" + ","
  144. }
  145. addressSql = strings.TrimRight(addressSql, ",")
  146. condition += ` AND a.address IN (` + addressSql + `) `
  147. }
  148. if province != "" {
  149. var provinceSql string
  150. slice := strings.Split(province, ",")
  151. for _, v := range slice {
  152. provinceSql += "'" + v + "'" + ","
  153. }
  154. provinceSql = strings.TrimRight(provinceSql, ",")
  155. condition += ` AND a.province IN (` + provinceSql + `) `
  156. }
  157. if city != "" {
  158. var citySql string
  159. slice := strings.Split(city, ",")
  160. for _, v := range slice {
  161. citySql += "'" + v + "'" + ","
  162. }
  163. citySql = strings.TrimRight(citySql, ",")
  164. condition += ` AND a.city IN (` + citySql + `) `
  165. }
  166. if keyWord != "" {
  167. companyIdStr, err := company.GetCompanyIdByKeyWord(keyWord)
  168. if err != nil {
  169. br.Msg = "获取客户信息失败"
  170. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  171. return
  172. }
  173. if companyIdStr != "" {
  174. condition += ` AND (a.company_name LIKE '%` + keyWord + `%' OR a.credit_code LIKE '%` + keyWord + `%' OR a.company_id IN(` + companyIdStr + `)) `
  175. } else {
  176. condition += ` AND (a.company_name LIKE '%` + keyWord + `%' OR a.credit_code LIKE '%` + keyWord + `%' ) `
  177. }
  178. }
  179. //任务状态
  180. if todoStatus != "" {
  181. condition += ` AND b.todo_status =? `
  182. pars = append(pars, todoStatus)
  183. }
  184. // 阅读权限/已购品种筛选
  185. cond, emp, e := services.FilterReadAndContractAuth(chartPermissionIds, contractPermissionIds, condition)
  186. if e != nil {
  187. br.Msg = "获取失败"
  188. br.ErrMsg = "FilterReadAndContractAuth err: " + e.Error()
  189. return
  190. }
  191. if emp {
  192. for _, v := range statusArr {
  193. statusCount := new(company.CompanyStatus)
  194. if v == "全部" {
  195. statusCount.Status = "全部"
  196. statusCount.Count = 0
  197. } else {
  198. statusCount.Status = v
  199. statusCount.Count = 0
  200. }
  201. statusItems = append(statusItems, statusCount)
  202. }
  203. items := make([]*company.CompanyListItem, 0)
  204. resp.List = items
  205. resp.Paging = page
  206. resp.StatusCount = statusItems
  207. br.Msg = "获取成功"
  208. br.Ret = 200
  209. br.Data = resp
  210. return
  211. }
  212. condition = cond
  213. //套餐类型
  214. if packageType > 0 {
  215. condition += ` AND b.package_type=? `
  216. pars = append(pars, packageType)
  217. }
  218. lossCondition = condition
  219. lossPars = pars
  220. permissionCondition = condition
  221. permissionPars = pars
  222. //流失客户统计
  223. lossCondition += ` AND status='流失' `
  224. productId := services.GetProductId(roleTypeCode)
  225. //权限控制
  226. {
  227. //如果不是超级管理员或者财务或者销售主管
  228. if roleTypeCode != utils.ROLE_TYPE_CODE_ADMIN && roleTypeCode != utils.ROLE_TYPE_CODE_FINANCE && sysUser.RoleName != utils.ROLE_NAME_FICC_DIRECTOR {
  229. //如果是部门管理员
  230. if roleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || roleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  231. condition += ` AND b.product_id=? `
  232. pars = append(pars, productId)
  233. lossCondition += ` AND b.product_id=? `
  234. lossPars = append(lossPars, productId)
  235. permissionCondition += ` AND b.product_id=? `
  236. permissionPars = append(permissionPars, productId)
  237. //}
  238. //else if roleTypeCode == utils.ROLE_TYPE_CODE_FICC_DIRECTOR {
  239. // condition += ` AND b.product_id=? `
  240. // pars = append(pars, productId)
  241. //
  242. // lossCondition += ` AND b.product_id=? `
  243. // lossPars = append(lossPars, productId)
  244. //
  245. // permissionCondition += ` AND b.product_id=? `
  246. // permissionPars = append(permissionPars, productId)
  247. } else {
  248. condition += ` AND b.product_id=? `
  249. pars = append(pars, productId)
  250. lossCondition += ` AND b.product_id=? `
  251. lossPars = append(lossPars, productId)
  252. permissionCondition += ` AND b.product_id=? `
  253. permissionPars = append(permissionPars, productId)
  254. if status == "全部" {
  255. ////如果是筛选了流失客户状态
  256. if sysUser.Authority <= 0 {
  257. condition += ` AND (b.seller_id=? OR status='流失') `
  258. pars = append(pars, sysUser.AdminId)
  259. permissionCondition += ` AND b.seller_id=? `
  260. permissionPars = append(permissionPars, sysUser.AdminId)
  261. } else {
  262. if sysUser.Authority == 1 {
  263. condition += ` AND (b.department_id=? OR status='流失')`
  264. pars = append(pars, sysUser.DepartmentId)
  265. permissionCondition += ` AND b.department_id=? `
  266. permissionPars = append(permissionPars, sysUser.DepartmentId)
  267. }
  268. if sysUser.Authority == 2 && sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  269. if status != utils.COMPANY_STATUS_LOSE {
  270. condition += ` AND (b.group_id=? OR status='流失') `
  271. pars = append(pars, sysUser.GroupId)
  272. }
  273. permissionCondition += ` AND b.group_id=? `
  274. permissionPars = append(permissionPars, sysUser.GroupId)
  275. }
  276. if sysUser.Authority == 2 && sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  277. pid, err := company.GetParentIdFromGroup(sysUser.GroupId)
  278. if err != nil {
  279. br.Msg = "获取失败"
  280. br.ErrMsg = "查询父级id异常" + err.Error()
  281. return
  282. }
  283. var ids []*string
  284. if pid != nil {
  285. if *pid != 0 {
  286. //销售主管放在三级列表中
  287. ids, err = company.GetGroupIdsByParentId(*pid)
  288. if err != nil {
  289. fmt.Println(err.Error())
  290. }
  291. } else {
  292. //销售主管放在二级列表中
  293. ids, err = company.GetGroupIdsByParentId(sysUser.GroupId)
  294. if err != nil {
  295. fmt.Println(err.Error())
  296. }
  297. }
  298. }
  299. var idSlice []string
  300. var sid string
  301. for _, id := range ids {
  302. idSlice = append(idSlice, *id)
  303. }
  304. //加入父级groupId
  305. if *pid > 0 {
  306. idSlice = append(idSlice, strconv.Itoa(*pid))
  307. } else {
  308. idSlice = append(idSlice, strconv.Itoa(sysUser.GroupId))
  309. }
  310. sid = strings.Join(idSlice, ",")
  311. if len(sid) > 0 {
  312. if status != utils.COMPANY_STATUS_LOSE {
  313. condition += ` AND (b.group_id IN (` + sid + `) OR status='流失') `
  314. //pars = append(pars, sysUser.GroupId)
  315. }
  316. permissionCondition += ` AND b.group_id IN (` + sid + `) `
  317. } else {
  318. if status != utils.COMPANY_STATUS_LOSE {
  319. condition += ` AND (b.group_id IN (` + strconv.Itoa(sysUser.GroupId) + `) OR status='流失') `
  320. //pars = append(pars, sysUser.GroupId)
  321. }
  322. permissionCondition += ` AND b.group_id IN (` + strconv.Itoa(sysUser.GroupId) + `) `
  323. }
  324. //permissionPars = append(permissionPars, sysUser.GroupId)
  325. }
  326. if sysUser.Authority == 4 {
  327. //pid, err := company.GetParentIdFromGroup(sysUser.GroupId)
  328. //if err != nil {
  329. // br.Msg = "获取失败"
  330. // br.ErrMsg = "查询父级id异常" + err.Error()
  331. // return
  332. //}
  333. //ids,err := company.GetGroupIdsByParentId(*pid)
  334. //if err != nil {
  335. // br.Msg = "获取失败"
  336. // br.ErrMsg = "查询父级id异常" + err.Error()
  337. // return
  338. //}
  339. //var idSlice []string
  340. //var sid string
  341. //for _, id := range ids {
  342. // idSlice = append(idSlice, *id)
  343. //}
  344. //sid = strings.Join(idSlice,",")
  345. if status != utils.COMPANY_STATUS_LOSE {
  346. condition += ` AND (b.group_id =? OR status='流失') `
  347. pars = append(pars, sysUser.GroupId)
  348. }
  349. permissionCondition += ` AND b.group_id=? `
  350. permissionPars = append(permissionPars, sysUser.GroupId)
  351. }
  352. }
  353. } else {
  354. //如果没有筛选了流失客户状态,那么需要加客户归属条件加入到查询中去
  355. if sysUser.Authority <= 0 {
  356. if status != utils.COMPANY_STATUS_LOSE {
  357. condition += ` AND b.seller_id=? `
  358. pars = append(pars, sysUser.AdminId)
  359. }
  360. permissionCondition += ` AND b.seller_id=? `
  361. permissionPars = append(permissionPars, sysUser.AdminId)
  362. } else {
  363. if sysUser.Authority == 1 {
  364. if status != utils.COMPANY_STATUS_LOSE {
  365. condition += ` AND b.department_id=?`
  366. pars = append(pars, sysUser.DepartmentId)
  367. }
  368. permissionCondition += ` AND b.department_id=? `
  369. permissionPars = append(permissionPars, sysUser.DepartmentId)
  370. }
  371. if sysUser.Authority == 2 && sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  372. if status != utils.COMPANY_STATUS_LOSE {
  373. condition += ` AND b.group_id=? `
  374. pars = append(pars, sysUser.GroupId)
  375. }
  376. permissionCondition += ` AND b.group_id=? `
  377. permissionPars = append(permissionPars, sysUser.GroupId)
  378. }
  379. if sysUser.Authority == 2 && sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  380. pid, err := company.GetParentIdFromGroup(sysUser.GroupId)
  381. if err != nil {
  382. br.Msg = "获取失败"
  383. br.ErrMsg = "查询父级id异常" + err.Error()
  384. return
  385. }
  386. var ids []*string
  387. if pid != nil {
  388. if *pid != 0 {
  389. //销售主管放在三级列表中
  390. ids, err = company.GetGroupIdsByParentId(*pid)
  391. if err != nil {
  392. fmt.Println(err.Error())
  393. }
  394. } else {
  395. //销售主管放在二级列表中
  396. ids, err = company.GetGroupIdsByParentId(sysUser.GroupId)
  397. if err != nil {
  398. fmt.Println(err.Error())
  399. }
  400. }
  401. }
  402. var idSlice []string
  403. var sid string
  404. for _, id := range ids {
  405. idSlice = append(idSlice, *id)
  406. }
  407. //加入父级groupId
  408. if *pid > 0 {
  409. idSlice = append(idSlice, strconv.Itoa(*pid))
  410. } else {
  411. idSlice = append(idSlice, strconv.Itoa(sysUser.GroupId))
  412. }
  413. sid = strings.Join(idSlice, ",")
  414. if len(sid) > 0 {
  415. if status != utils.COMPANY_STATUS_LOSE {
  416. condition += ` AND (b.group_id IN (` + sid + `) OR status='流失') `
  417. //pars = append(pars, sysUser.GroupId)
  418. }
  419. permissionCondition += ` AND b.group_id IN (` + sid + `) `
  420. } else {
  421. if status != utils.COMPANY_STATUS_LOSE {
  422. condition += ` AND (b.group_id IN (` + strconv.Itoa(sysUser.GroupId) + `) OR status='流失') `
  423. //pars = append(pars, sysUser.GroupId)
  424. }
  425. permissionCondition += ` AND b.group_id IN (` + strconv.Itoa(sysUser.GroupId) + `) `
  426. }
  427. }
  428. if sysUser.Authority == 4 {
  429. if status != utils.COMPANY_STATUS_LOSE {
  430. condition += ` AND b.group_id =? `
  431. pars = append(pars, sysUser.GroupId)
  432. }
  433. permissionCondition += ` AND b.group_id=? `
  434. permissionPars = append(permissionPars, sysUser.GroupId)
  435. }
  436. }
  437. }
  438. }
  439. }
  440. }
  441. //状态控制
  442. if status != "" && status != "全部" {
  443. //switch status {
  444. //case "试用-活跃":
  445. // condition += ` AND b.status="试用" and b.view_total>= ? `
  446. // pars = append(pars, statistic_report.ActiveViewNum)
  447. //case "试用-非活跃":
  448. // condition += ` AND b.status="试用" and b.view_total < ? `
  449. // pars = append(pars, statistic_report.ActiveViewNum)
  450. //default:
  451. // condition += ` AND b.status=? `
  452. // pars = append(pars, status)
  453. //}
  454. condition += ` AND b.status=? `
  455. pars = append(pars, status)
  456. // 试用客户标签
  457. if status == "试用" && tryStage > 0 {
  458. condition += ` AND b.try_stage=? `
  459. pars = append(pars, tryStage)
  460. }
  461. }
  462. total, err := company.GetCompanyListCount(condition, pars)
  463. if err != nil {
  464. br.Msg = "获取失败"
  465. br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
  466. return
  467. }
  468. //
  469. sortStr := ``
  470. if !sortByTodo {
  471. if sortParam != "" && sortType != "" {
  472. if sortParam == "expireDay" {
  473. if roleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  474. if sortType == "asc" {
  475. sortStr = " ORDER BY min_end_date"
  476. } else {
  477. sortStr = " ORDER BY max_end_date"
  478. }
  479. } else {
  480. sortStr = " ORDER BY b.end_date "
  481. }
  482. } else if sortParam == "createTime" {
  483. sortStr = " ORDER BY a.created_time "
  484. } else if sortParam == "viewTotal" {
  485. if roleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  486. sortStr = " ORDER BY all_view_total"
  487. } else {
  488. sortStr = " ORDER BY b.view_total "
  489. }
  490. } else if sortParam == "viewTime" {
  491. if roleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  492. if sortType == "asc" {
  493. sortStr = " ORDER BY min_last_view_time"
  494. } else {
  495. sortStr = " ORDER BY max_last_view_time"
  496. }
  497. } else {
  498. sortStr = " ORDER BY b.last_view_time "
  499. }
  500. } else if sortParam == "roadShowTotal" {
  501. sortStr = " ORDER BY b.road_show_total "
  502. } else if sortParam == "formalTime" {
  503. sortStr = " ORDER BY b.formal_time "
  504. } else if sortParam == "freezeTime" {
  505. sortStr = " ORDER BY b.freeze_time "
  506. } else if sortParam == "lossTime" {
  507. sortStr = " ORDER BY b.loss_time "
  508. } else if sortParam == "todoCreateTime" {
  509. sortStr = " ORDER BY b.todo_create_time "
  510. } else if sortParam == "todoApproveTime" {
  511. sortStr = " ORDER BY b.todo_approve_time "
  512. } else if sortParam == "tryStage" && status == "试用" {
  513. if roleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  514. sortStr = " ORDER BY sort_try_stage asc, all_view_total desc, a.created_time desc"
  515. } else {
  516. sortStr = " ORDER BY sort_try_stage asc, b.view_total desc, a.created_time desc"
  517. }
  518. } else if sortParam == "deadline" {
  519. if roleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  520. sortStr = " ORDER BY sort_todo_end_time asc, all_view_total desc, a.created_time desc"
  521. } else {
  522. sortStr = " ORDER BY sort_todo_end_time asc, b.view_total desc, a.created_time desc"
  523. }
  524. } else if sortParam == "tryOutDay" {
  525. if roleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  526. sortStr = " ORDER BY all_try_out_day_total "
  527. } else {
  528. sortStr = " ORDER BY b.try_out_day_total "
  529. }
  530. } else {
  531. br.Msg = "获取失败"
  532. br.ErrMsg = "排序字段传入数据异常:" + sortParam
  533. return
  534. }
  535. if sortParam != "tryStage" && sortParam != "deadline" {
  536. if sortType == "asc" {
  537. sortStr += " ASC "
  538. } else if sortType == "desc" {
  539. sortStr += " DESC "
  540. } else {
  541. br.Msg = "获取失败"
  542. br.ErrMsg = "排序类型传入数据异常:" + sortType
  543. return
  544. }
  545. }
  546. } else {
  547. //2、客户列表选择“全部”时,支持按照“状态和阅读数”对“全部”分类下所有客户列表进行升降序排列;选择“正式”时按照“转正时间”、选择“冻结”时按照“冻结时间”、选择“流失”时按照“流失时间”、选择“永续”时按照“创建时间”;
  548. if status == "全部" {
  549. if roleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  550. sortStr = " ORDER BY sort_status asc, all_view_total desc, a.created_time "
  551. } else {
  552. sortStr = " ORDER BY sort_status asc, b.view_total desc, a.created_time "
  553. }
  554. } else if status == "永续" {
  555. sortStr = " ORDER BY a.created_time "
  556. } else if status == "正式" {
  557. // 正式客户默认按照周阅读活跃ASC, 同为不活跃则按照转正时间DESC
  558. sortStr = " ORDER BY week_view_active ASC, b.formal_time "
  559. //sortStr = " ORDER BY b.formal_time "
  560. } else if status == "冻结" {
  561. sortStr = " ORDER BY b.freeze_time "
  562. } else if status == "流失" {
  563. sortStr = " ORDER BY b.loss_time "
  564. } else if status == "试用" {
  565. if roleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  566. sortStr = " ORDER BY all_view_total desc, a.created_time "
  567. } else {
  568. sortStr = " ORDER BY b.view_total desc, a.created_time "
  569. }
  570. } else if status == utils.COMPANY_STATUS_CLOSE {
  571. // 关闭客户按照关闭时间DESC
  572. sortStr = " ORDER BY b.close_time "
  573. }
  574. sortStr += " DESC "
  575. }
  576. } else {
  577. sortStr = " ORDER BY FIELD (todo_status,'未完成','已完成','无任务'), todo_modify_time DESC "
  578. }
  579. //任务状态
  580. if todoStatus != "" {
  581. //排序,筛选出的未完成、已完成客户列表排序分别按照 最近一个任务的创建时间、最近一个任务的审核时间排序;
  582. switch todoStatus {
  583. case "未完成":
  584. if sortStr != "" {
  585. sortStr += " ,b.todo_create_time desc "
  586. } else {
  587. sortStr = " ORDER BY b.todo_create_time desc "
  588. }
  589. case "已完成":
  590. if sortStr != "" {
  591. sortStr += " ,b.todo_approve_time desc "
  592. } else {
  593. sortStr = " ORDER BY b.todo_approve_time desc "
  594. }
  595. }
  596. }
  597. list, err := company.GetCompanyList(condition, status, sortStr, pars, startSize, pageSize)
  598. if err != nil {
  599. br.Msg = "获取失败"
  600. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  601. return
  602. }
  603. //企业用户数组切片
  604. companyIdSlice := make([]string, 0)
  605. companyIds := make([]int, 0)
  606. for _, v := range list {
  607. companyIdSlice = append(companyIdSlice, strconv.Itoa(v.CompanyId))
  608. companyIds = append(companyIds, v.CompanyId)
  609. }
  610. //企业用户产品开通数
  611. companyIdStr := strings.Join(companyIdSlice, ",")
  612. companyProductTotalList, _ := company.GetCountProductByCompanyIds(companyIdStr)
  613. companyProductTotalMap := make(map[int]*company.CompanyProductTotalSlice)
  614. for _, companyProductTotal := range companyProductTotalList {
  615. companyProductTotalMap[companyProductTotal.CompanyId] = companyProductTotal
  616. }
  617. lenList := len(list)
  618. //companyFiccViewTimeMap := make(map[int]time.Time)
  619. //companyRaiViewTimeMap := make(map[int]time.Time)
  620. //companyFiccViewTotal := make(map[int]int)
  621. //companyRaiViewTotal := make(map[int]int)
  622. //获取最近阅读时间 和 累计阅读次数
  623. //if lenList > 0 {
  624. // //ficc 或者 全品种
  625. // if productId == 1 || productId == 0 {
  626. // companyFiccViewTimeMap, err = services.GetFiccLastUserViewHistoryByCompanyIds(companyIdStr)
  627. // if err != nil {
  628. // br.Msg = "获取失败"
  629. // br.ErrMsg = "获取数据失败,Err:" + err.Error()
  630. // return
  631. // }
  632. // }
  633. //
  634. // if productId == 2 || productId == 0 {
  635. // companyRaiViewTimeMap, err = services.GetRaiLastUserViewHistoryByCompanyIds(companyIdStr)
  636. // if err != nil {
  637. // br.Msg = "获取失败"
  638. // br.ErrMsg = "获取数据失败,Err:" + err.Error()
  639. // return
  640. // }
  641. // }
  642. //
  643. // companyFiccViewTotal, err = services.GetFiccCountUserViewHistoryByCompanyIds(companyIdStr)
  644. // if err != nil {
  645. // br.Msg = "获取失败"
  646. // br.ErrMsg = "获取ficc阅读数据失败,Err:" + err.Error()
  647. // return
  648. // }
  649. // companyRaiViewTotal, err = services.GetRaiCountUserViewHistoryByCompanyIds(companyIdStr)
  650. // if err != nil {
  651. // br.Msg = "获取失败"
  652. // br.ErrMsg = "获取权益阅读数据失败,Err:" + err.Error()
  653. // return
  654. // }
  655. //}
  656. // 客户产品详细信息
  657. companyProductMap := make(map[string]*company.CompanyProduct)
  658. var companyProductCondition string
  659. var companyProductPars []interface{}
  660. switch companyType {
  661. case "ficc":
  662. companyProductCondition += ` AND product_id=? `
  663. companyProductPars = append(companyProductPars, 1)
  664. case "权益":
  665. companyProductCondition += ` AND product_id=? `
  666. companyProductPars = append(companyProductPars, 2)
  667. default:
  668. }
  669. companyProductList, err := company.GetCompanyProductsByCompanyIds(companyIdStr, companyProductCondition, companyProductPars)
  670. if err != nil {
  671. br.Msg = "获取失败"
  672. br.ErrMsg = "获取客户产品数据失败,Err:" + err.Error()
  673. return
  674. }
  675. for _, companyProduct := range companyProductList {
  676. key := fmt.Sprint(companyProduct.CompanyId, "_", companyProduct.ProductId)
  677. companyProductMap[key] = companyProduct
  678. }
  679. //任务待办
  680. companyTodoMap := make(map[string]*company_todo.CompanyTodo)
  681. {
  682. var companyTodoCondition string
  683. var companyTodoPars []interface{}
  684. switch companyType {
  685. case "ficc":
  686. companyTodoCondition += ` AND product_id=? `
  687. companyTodoPars = append(companyTodoPars, 1)
  688. case "权益":
  689. companyTodoCondition += ` AND product_id=? `
  690. companyTodoPars = append(companyTodoPars, 2)
  691. default:
  692. }
  693. companyTodoList, err := company_todo.GetDoingCompanyTodoByCompanyIds(companyIdStr, companyTodoCondition, companyTodoPars)
  694. if err != nil {
  695. br.Msg = "获取失败"
  696. br.ErrMsg = "获取客户产品数据失败,Err:" + err.Error()
  697. return
  698. }
  699. for _, companyTodo := range companyTodoList {
  700. key := fmt.Sprint(companyTodo.CompanyId)
  701. companyTodoMap[key] = companyTodo
  702. }
  703. }
  704. // 查询是否有服务记录
  705. btnServiceRecordMap := make(map[int]bool)
  706. if len(companyIds) > 0 {
  707. serRecOB := new(company.CompanyServiceRecord)
  708. serRecCond := ` AND company_id IN (` + utils.GetOrmInReplace(len(companyIds)) + `) AND company_type = ? `
  709. serRecPars := make([]interface{}, 0)
  710. serRecPars = append(serRecPars, companyIds, 2)
  711. serRecs, e := serRecOB.GetItemsByCondition(serRecCond, serRecPars, []string{"company_service_record_id", "company_id"}, "")
  712. if e != nil {
  713. br.Msg = "获取失败"
  714. br.ErrMsg = "获取客户服务记录失败, Err: " + e.Error()
  715. return
  716. }
  717. for _, s := range serRecs {
  718. btnServiceRecordMap[s.CompanyId] = true
  719. }
  720. }
  721. // 查询当前销售是否有领取客户的权限
  722. receiveEnabled, _ := services.CheckCompanyReceiveButton(sysUser.AdminId)
  723. for i := 0; i < lenList; i++ {
  724. item := list[i]
  725. //校验该客户是否存在多个产品,如果是,那么代表是共享客户
  726. if companyProductTotal, ok := companyProductTotalMap[int(item.CompanyId)]; ok {
  727. if companyProductTotal.Total > 1 {
  728. list[i].IsShared = true
  729. }
  730. }
  731. var expireDay string
  732. itemStatus := item.Status
  733. if itemStatus != "" {
  734. if !strings.Contains(item.Status, "/") {
  735. if itemStatus == utils.COMPANY_STATUS_FOREVER { //永续
  736. expireDay = "-"
  737. } else if itemStatus == utils.COMPANY_STATUS_TRY_OUT || itemStatus == utils.COMPANY_STATUS_FORMAL { //试用/正式
  738. if item.StartDate != "" && item.EndDate != "" {
  739. endDateTime, _ := time.Parse(utils.FormatDate, item.EndDate)
  740. endDateTime = endDateTime.AddDate(0, 0, 1)
  741. sub := endDateTime.Sub(time.Now())
  742. if sub < 0 {
  743. sub = 0
  744. }
  745. expireDay = fmt.Sprintf("%v", int(sub.Hours()/24))
  746. } else {
  747. expireDay = "-"
  748. }
  749. } else if itemStatus == utils.COMPANY_STATUS_LOSE {
  750. expireDay = "-"
  751. } else if itemStatus == utils.COMPANY_STATUS_FREEZE {
  752. if item.StartDate != "" && item.EndDate != "" {
  753. endDateTime, _ := time.Parse(utils.FormatDate, item.EndDate)
  754. endDateTime = endDateTime.AddDate(0, 0, 0)
  755. sub := endDateTime.Sub(time.Now())
  756. if sub < 0 {
  757. sub = 0
  758. }
  759. expireDay = fmt.Sprintf("%v", int(sub.Hours()/24))
  760. } else {
  761. expireDay = "-"
  762. }
  763. } else {
  764. expireDay = "-"
  765. }
  766. } else {
  767. tmpStatusArr := strings.Split(item.Status, "/")
  768. ficcStatus := tmpStatusArr[0]
  769. raiStatus := tmpStatusArr[1]
  770. var ficcEndDate, tryEndDate string
  771. var ficcExpireDay, tryExpireDay string
  772. if item.EndDate != "" {
  773. endDateArr := strings.Split(item.EndDate, "/")
  774. if len(endDateArr) == 1 {
  775. ficcEndDate = endDateArr[0]
  776. } else if len(endDateArr) > 1 {
  777. ficcEndDate = endDateArr[0]
  778. tryEndDate = endDateArr[1]
  779. }
  780. if ficcStatus == utils.COMPANY_STATUS_FOREVER { //永续
  781. ficcExpireDay = "-"
  782. } else if ficcStatus == utils.COMPANY_STATUS_TRY_OUT || ficcStatus == utils.COMPANY_STATUS_FORMAL { //试用/正式
  783. if ficcEndDate != "" {
  784. endDateTime, _ := time.Parse(utils.FormatDate, ficcEndDate)
  785. endDateTime = endDateTime.AddDate(0, 0, 1)
  786. sub := endDateTime.Sub(time.Now())
  787. if sub < 0 {
  788. sub = 0
  789. }
  790. ficcExpireDay = fmt.Sprintf("%v", int(sub.Hours()/24))
  791. } else {
  792. ficcExpireDay = "-"
  793. }
  794. } else if ficcStatus == utils.COMPANY_STATUS_LOSE { //流失
  795. ficcExpireDay = "-"
  796. } else if ficcStatus == utils.COMPANY_STATUS_FREEZE { //冻结
  797. if item.StartDate != "" && item.EndDate != "" {
  798. endDateTime, _ := time.Parse(utils.FormatDate, ficcEndDate)
  799. endDateTime = endDateTime.AddDate(0, 0, 0)
  800. sub := endDateTime.Sub(time.Now())
  801. if sub < 0 {
  802. sub = 0
  803. }
  804. ficcExpireDay = fmt.Sprintf("%v", int(sub.Hours()/24))
  805. } else {
  806. ficcExpireDay = "-"
  807. }
  808. } else {
  809. ficcExpireDay = "-"
  810. }
  811. if raiStatus == utils.COMPANY_STATUS_FOREVER { //永续
  812. tryExpireDay = "-"
  813. } else if raiStatus == utils.COMPANY_STATUS_TRY_OUT || raiStatus == utils.COMPANY_STATUS_FORMAL { //试用/正式
  814. if tryEndDate != "" {
  815. endDateTime, _ := time.Parse(utils.FormatDate, tryEndDate)
  816. endDateTime = endDateTime.AddDate(0, 0, 1)
  817. sub := endDateTime.Sub(time.Now())
  818. if sub < 0 {
  819. sub = 0
  820. }
  821. tryExpireDay = fmt.Sprintf("%v", int(sub.Hours()/24))
  822. } else {
  823. tryExpireDay = "-"
  824. }
  825. } else if raiStatus == utils.COMPANY_STATUS_LOSE { //流失
  826. tryExpireDay = "-"
  827. } else if raiStatus == utils.COMPANY_STATUS_FREEZE { //冻结
  828. if item.StartDate != "" && item.EndDate != "" {
  829. endDateTime, _ := time.Parse(utils.FormatDate, tryEndDate)
  830. endDateTime = endDateTime.AddDate(0, 0, 0)
  831. sub := endDateTime.Sub(time.Now())
  832. if sub < 0 {
  833. sub = 0
  834. }
  835. tryExpireDay = fmt.Sprintf("%v", int(sub.Hours()/24))
  836. } else {
  837. tryExpireDay = "-"
  838. }
  839. } else {
  840. tryExpireDay = "-"
  841. }
  842. }
  843. expireDay = ficcExpireDay + "/" + tryExpireDay
  844. }
  845. }
  846. list[i].ExpireDay = expireDay
  847. // 按钮控制, 仅显示查看权限
  848. btnItem := new(company.ButtonPermission)
  849. btnItem.BtnView = true
  850. list[i].BtnItem = btnItem
  851. //按钮显示控制
  852. //btnItem := new(company.ButtonPermission)
  853. //if item.CompanyId != 1 {
  854. // *btnItem = services.GetCompanyPermissionButton(roleTypeCode, itemStatus, item.GroupIds, item.GroupIds, item.GroupId, sysUser.GroupId, item.SellerId, sysUser.AdminId, sysUser.Authority, item.ProductId, item.ShareSellerId)
  855. // //根据销售"领取客户"的权限对部分按钮做限制
  856. // btnItem = services.GetCompanyPermissionButtonByReceiveEnabled(btnItem, receiveEnabled)
  857. //}
  858. //if list[i].IsShare == 1 && btnItem.BtnModifySeller == true {
  859. // //如果已经是共享客户了,取消共享按钮全都显示,和修改销售权限一样
  860. // btnItem.BtnShare = true
  861. //}
  862. //list[i].BtnItem = btnItem
  863. if item.FreezeStartDate != "" && item.FreezeEndDate != "" && item.Status == utils.COMPANY_STATUS_FREEZE {
  864. freezeEndDate, _ := time.Parse(utils.FormatDate, item.FreezeEndDate)
  865. if time.Now().Before(freezeEndDate) {
  866. expireDays := freezeEndDate.Sub(time.Now()).Hours() / 24
  867. list[i].FreezeExpireDays = int(expireDays) + 1
  868. }
  869. }
  870. //btnItem.BtnServiceRecord = btnServiceRecordMap[list[i].CompanyId]
  871. ficcKey := fmt.Sprint(item.CompanyId, "_", 1)
  872. //ficc
  873. if tmpCompanyProduct, ok := companyProductMap[ficcKey]; ok {
  874. //套餐类型
  875. item.FiccPackageType = tmpCompanyProduct.PackageType
  876. //总试用天数
  877. item.FiccTryOutDay = tmpCompanyProduct.TryOutDayTotal
  878. //总阅读次数
  879. item.FiccView = tmpCompanyProduct.ViewTotal
  880. if !tmpCompanyProduct.LastViewTime.IsZero() {
  881. //最近一次阅读时间
  882. item.FiccLastViewTime = tmpCompanyProduct.LastViewTime.Format(utils.FormatDateTime)
  883. }
  884. }
  885. //权益
  886. raiKey := fmt.Sprint(item.CompanyId, "_", 2)
  887. if tmpCompanyProduct, ok := companyProductMap[raiKey]; ok {
  888. //总阅读次数
  889. item.RaiView = tmpCompanyProduct.ViewTotal
  890. //总试用天数
  891. item.RaiTryOutDay = tmpCompanyProduct.TryOutDayTotal
  892. if !tmpCompanyProduct.LastViewTime.IsZero() {
  893. //最近一次阅读时间
  894. item.RaiLastViewTime = tmpCompanyProduct.LastViewTime.Format(utils.FormatDateTime)
  895. }
  896. }
  897. // 试用客户子标签
  898. if strings.Contains(item.Status, "试用") {
  899. statusSlice := strings.Split(item.Status, "/")
  900. stageItemSlice := strings.Split(item.TryStageStr, "/")
  901. productIdSlice := strings.Split(item.ProductIds, "/")
  902. for sk, sv := range statusSlice {
  903. if sv != "试用" {
  904. continue
  905. }
  906. tryItem := new(company.TryStageSliceItem)
  907. if len(productIdSlice) < (sk+1) || len(stageItemSlice) < (sk+1) {
  908. continue
  909. }
  910. key := fmt.Sprint(item.CompanyId, "_", productIdSlice[sk])
  911. if pro, ok := companyProductMap[key]; ok {
  912. tryItem.ProductId, _ = strconv.Atoi(productIdSlice[sk])
  913. if pro.SellerId == sysUser.AdminId { //产品销售ID和登录ID相等
  914. tryItem.HasPermission = true
  915. }
  916. tryItem.TryStage, _ = strconv.Atoi(stageItemSlice[sk])
  917. list[i].TryStageSlice = append(list[i].TryStageSlice, tryItem)
  918. }
  919. }
  920. }
  921. // To-Do任务状态为未完成的对应任务的截止日期-当前日期;
  922. if strings.Contains(item.TodoStatuss, "未完成") {
  923. if strings.Contains(item.TodoEndTimeStr, "/") {
  924. edtSlice := strings.Split(item.TodoEndTimeStr, "/")
  925. item.TodoEndTime, _ = time.ParseInLocation(utils.FormatDateTime, edtSlice[0], time.Local)
  926. } else {
  927. item.TodoEndTime, _ = time.ParseInLocation(utils.FormatDateTime, item.TodoEndTimeStr, time.Local)
  928. }
  929. tmpEndTime := item.TodoEndTime.Format(utils.FormatDate)
  930. if tmpEndTime == "0001-01-01" {
  931. tmpEndTime = ""
  932. }
  933. if tmpEndTime != "" {
  934. dlTime1 := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local)
  935. dlTime2 := time.Date(item.TodoEndTime.Year(), item.TodoEndTime.Month(), item.TodoEndTime.Day(), 0, 0, 0, 0, time.Local)
  936. dldays := int(dlTime2.Sub(dlTime1).Hours() / 24)
  937. list[i].Deadline = strconv.Itoa(dldays)
  938. }
  939. }
  940. }
  941. statusCount, err := company.GetCompanyStatusCount(permissionCondition, permissionPars)
  942. if err != nil {
  943. br.Msg = "获取失败"
  944. br.ErrMsg = "获取状态数量失败,Err:" + err.Error()
  945. return
  946. }
  947. statusMap := make(map[string]int, 0)
  948. var totalCount int
  949. for _, sv := range statusCount {
  950. totalCount = totalCount + sv.Count
  951. statusMap[sv.Status] = sv.Count
  952. }
  953. // 统计试用客户的子标签数量,只在试用状态页面统计
  954. var tryStageCountResp []*company.CompanyTryStageCount
  955. if status == "试用" {
  956. tryStageCount, err := company.GetCompanyTryStageCount(permissionCondition, permissionPars)
  957. if err != nil {
  958. br.Msg = "获取失败"
  959. br.ErrMsg = "获取试用状态标签数量失败,Err:" + err.Error()
  960. return
  961. }
  962. tryStageMap := make(map[int]int)
  963. for _, v := range tryStageCount {
  964. tryStageMap[0] += v.Count
  965. tryStageMap[v.TryStage] = v.Count
  966. }
  967. tryStageAllMap := map[int]string{
  968. 0: "全部",
  969. 2: "推进",
  970. 3: "跟踪",
  971. 4: "预备",
  972. 1: "未分类",
  973. }
  974. for k, v := range tryStageAllMap {
  975. item := new(company.CompanyTryStageCount)
  976. item.TryStageStr = v
  977. item.TryStage = k
  978. if count, ok := tryStageMap[k]; ok {
  979. item.Count = count
  980. }
  981. tryStageCountResp = append(tryStageCountResp, item)
  982. }
  983. }
  984. //
  985. ////试用-活跃
  986. //{
  987. // tmpPermissionCondition := permissionCondition
  988. // tmpPermissionPars := permissionPars
  989. // tmpPermissionCondition += ` and b.view_total>=? `
  990. // tmpPermissionPars = append(tmpPermissionPars, statistic_report.ActiveViewNum)
  991. //
  992. // tryOutActiveCount, err := company.GetCompanyTryOutStatusCount(tmpPermissionCondition, tmpPermissionPars)
  993. // if err != nil {
  994. // br.Msg = "获取失败"
  995. // br.ErrMsg = "获取状态数量失败,Err:" + err.Error()
  996. // return
  997. // }
  998. // statusMap["试用-活跃"] = tryOutActiveCount.Count
  999. //}
  1000. //
  1001. ////试用-非活跃
  1002. //{
  1003. // tmpPermissionCondition := permissionCondition
  1004. // tmpPermissionPars := permissionPars
  1005. // tmpPermissionCondition += ` and b.view_total <? `
  1006. // tmpPermissionPars = append(tmpPermissionPars, statistic_report.ActiveViewNum)
  1007. //
  1008. // tryOutActiveCount, err := company.GetCompanyTryOutStatusCount(tmpPermissionCondition, tmpPermissionPars)
  1009. // if err != nil {
  1010. // br.Msg = "获取失败"
  1011. // br.ErrMsg = "获取状态数量失败,Err:" + err.Error()
  1012. // return
  1013. // }
  1014. // statusMap["试用-非活跃"] = tryOutActiveCount.Count
  1015. //}
  1016. lossItem, err := company.GetCompanyStatusLoseCount(lossCondition, lossPars)
  1017. if err != nil {
  1018. br.Msg = "获取失败"
  1019. br.ErrMsg = "获取流失状态数量失败,Err:" + err.Error()
  1020. return
  1021. }
  1022. totalCount = totalCount + lossItem.Count
  1023. for _, v := range statusArr {
  1024. s := new(company.CompanyStatus)
  1025. if v == "全部" {
  1026. s.Status = "全部"
  1027. s.Count = totalCount
  1028. } else if v == utils.COMPANY_STATUS_LOSE {
  1029. s.Status = v
  1030. s.Count = lossItem.Count
  1031. } else {
  1032. s.Status = v
  1033. if val, ok := statusMap[v]; ok {
  1034. s.Count = val
  1035. }
  1036. }
  1037. statusItems = append(statusItems, s)
  1038. }
  1039. companyLists := make([]*company.CompanyListItem, 0)
  1040. //查询阅读次数
  1041. if list == nil {
  1042. companyLists = make([]*company.CompanyListItem, 0)
  1043. } else {
  1044. for _, v := range list {
  1045. //viewTotal:=GetviewTotal(v.CompanyId)
  1046. //活跃(客户状态)
  1047. if v.IsShare != 1 {
  1048. if v.BtnItem.BtnModifySeller == true {
  1049. v.BtnItem.BtnModifySeller = false
  1050. }
  1051. }
  1052. //productIdSlice := strings.Split(v.ProductIds, "/")
  1053. //statusSlice := strings.Split(v.Status, "/")
  1054. //for productIndex, tmpProductId := range productIdSlice {
  1055. // tmpStatusIndex := 0
  1056. // if len(statusSlice) > 1 {
  1057. // tmpStatusIndex = productIndex
  1058. // }
  1059. // tmpProductStatus := statusSlice[tmpStatusIndex]
  1060. // if strings.Contains(tmpProductStatus, "试用") {
  1061. // activeType := "非活跃"
  1062. // viewNum := v.FiccView
  1063. // if tmpProductId == "2" {
  1064. // viewNum = v.RaiView
  1065. // }
  1066. // if viewNum >= statistic_report.ActiveViewNum {
  1067. // activeType = "活跃"
  1068. // }
  1069. // statusSlice[tmpStatusIndex] = fmt.Sprint(tmpProductStatus, "(", activeType, ")")
  1070. //
  1071. // tmpStatus = strings.Join(statusSlice, "/")
  1072. // }
  1073. //}
  1074. //ficcViewTotal, ok := companyFiccViewTotal[v.CompanyId]
  1075. //if !ok {
  1076. // ficcViewTotal = 0
  1077. //}
  1078. //raiViewTotal, ok := companyRaiViewTotal[v.CompanyId]
  1079. //if !ok {
  1080. // raiViewTotal = 0
  1081. //}
  1082. // 是否存在进行中的任务
  1083. var todoStatus bool
  1084. if _, ok := companyTodoMap[fmt.Sprint(v.CompanyId)]; ok {
  1085. todoStatus = true
  1086. }
  1087. //是否支持确认完成待办任务
  1088. var canConfirm bool
  1089. var hiddenConfirm bool //是否隐藏确定完成按钮
  1090. var hiddenCreate bool //是否隐藏新增/编辑按钮
  1091. hiddenConfirm = true
  1092. hiddenCreate = true
  1093. for _, vProductId := range []int{1, 2} {
  1094. ficcKey := fmt.Sprint(v.CompanyId, "_", vProductId)
  1095. //ficc
  1096. if tmpCompanyProduct, ok := companyProductMap[ficcKey]; ok {
  1097. if tmpCompanyProduct.Status != utils.COMPANY_STATUS_LOSE && tmpCompanyProduct.Status != utils.COMPANY_STATUS_CLOSE { //非流失
  1098. hiddenCreate = false
  1099. switch this.SysUser.RoleTypeCode {
  1100. case utils.ROLE_TYPE_CODE_FICC_ADMIN, utils.ROLE_TYPE_CODE_RAI_ADMIN, utils.ROLE_TYPE_CODE_FICC_GROUP, utils.ROLE_TYPE_CODE_RAI_GROUP:
  1101. //部门管理员、ficc主管、权益组长
  1102. hiddenConfirm = false //不展示
  1103. if todoStatus { //有任务,需要展示
  1104. canConfirm = true
  1105. }
  1106. case utils.ROLE_TYPE_CODE_FICC_TEAM: //ficc组长
  1107. if todoStatus { //有任务,需要展示
  1108. canConfirm = true
  1109. }
  1110. if v.SellerId != this.SysUser.AdminId { //不是自己的客户
  1111. hiddenConfirm = false //展示
  1112. }
  1113. case utils.ROLE_TYPE_CODE_RAI_SELLER: //权益销售
  1114. if v.SellerId == this.SysUser.AdminId { //是自己的客户
  1115. has := services.CheckRaiApprovePermission(sysUser.AdminId) //这些个权益销售是有权限的
  1116. if has {
  1117. if todoStatus { //有任务,需要展示
  1118. canConfirm = true
  1119. }
  1120. hiddenConfirm = false //展示
  1121. }
  1122. }
  1123. }
  1124. }
  1125. }
  1126. }
  1127. todoButtonColor := `green`
  1128. //'无任务','未完成','已完成'
  1129. if strings.Contains(v.TodoStatuss, "未完成") {
  1130. todoButtonColor = "red"
  1131. } else if strings.Contains(v.TodoStatuss, "已完成") {
  1132. todoButtonColor = "green"
  1133. } else if strings.Contains(v.TodoStatuss, "无任务") {
  1134. todoButtonColor = "gray"
  1135. }
  1136. //if str
  1137. companyList := &company.CompanyListItem{
  1138. CompanyId: v.CompanyId,
  1139. CompanyName: v.CompanyName,
  1140. CreditCode: v.CreditCode,
  1141. CompanyCode: v.CompanyCode,
  1142. StartDate: v.StartDate,
  1143. EndDate: v.EndDate,
  1144. LoseReason: v.LoseReason,
  1145. RenewalReason: v.RenewalReason,
  1146. FreezeReason: v.FreezeReason,
  1147. LossTime: v.LossTime,
  1148. Status: v.StatusStr,
  1149. CompanyType: v.CompanyType,
  1150. ApproveStatus: v.ApproveStatus,
  1151. SellerName: v.SellerName,
  1152. SellerId: v.SellerId,
  1153. SellerIds: v.SellerIds,
  1154. ExpireDay: v.ExpireDay,
  1155. FreezeTime: v.FreezeTime,
  1156. GroupId: v.GroupId,
  1157. GroupIds: v.GroupIds,
  1158. DepartmentId: v.DepartmentId,
  1159. IndustryName: v.IndustryName,
  1160. IsSuspend: v.IsSuspend,
  1161. CreatedTime: v.CreatedTime,
  1162. Source: v.Source,
  1163. Province: v.Province,
  1164. City: v.City,
  1165. Address: v.Address,
  1166. Reasons: v.Reasons,
  1167. FreezeStartDate: v.FreezeStartDate,
  1168. FreezeEndDate: v.FreezeEndDate,
  1169. FreezeExpireDays: v.FreezeExpireDays,
  1170. BtnItem: v.BtnItem,
  1171. ProductId: v.ProductId,
  1172. FormalTime: v.FormalTime,
  1173. IsShared: v.IsShared,
  1174. RegionType: v.RegionType,
  1175. FiccPackageType: v.FiccPackageType,
  1176. FiccLastViewTime: v.FiccLastViewTime,
  1177. RaiLastViewTime: v.RaiLastViewTime,
  1178. //FiccView: viewTotal[0],
  1179. //RaiView: viewTotal[1],
  1180. //FiccView: ficcViewTotal,
  1181. //RaiView: raiViewTotal,
  1182. FiccView: v.FiccView,
  1183. RaiView: v.RaiView,
  1184. FiccTryOutDay: v.FiccTryOutDay,
  1185. RaiTryOutDay: v.RaiTryOutDay,
  1186. AllViewTotal: v.AllViewTotal,
  1187. RoadShowTotal: v.RoadShowTotal,
  1188. TodoStatus: todoStatus,
  1189. CanConfirm: canConfirm,
  1190. HiddenConfirm: hiddenConfirm,
  1191. HiddenCreate: hiddenCreate,
  1192. TodoButtonColor: todoButtonColor,
  1193. TryStageSlice: v.TryStageSlice,
  1194. Deadline: v.Deadline,
  1195. WeekViewActive: v.WeekViewActive,
  1196. IsShare: v.IsShare,
  1197. CloseTime: v.CloseTime,
  1198. CloseReason: v.CloseReason,
  1199. ShareSellerId: v.ShareSellerId,
  1200. }
  1201. companyLists = append(companyLists, companyList)
  1202. }
  1203. }
  1204. if companyLists == nil {
  1205. companyLists = make([]*company.CompanyListItem, 0)
  1206. }
  1207. page = paging.GetPaging(currentIndex, pageSize, total)
  1208. resp.List = companyLists
  1209. resp.StatusCount = statusItems
  1210. resp.TryStageCount = tryStageCountResp
  1211. resp.Paging = page
  1212. resp.AddCompanyBtn = receiveEnabled
  1213. br.Ret = 200
  1214. br.Success = true
  1215. br.Msg = "获取成功"
  1216. br.Data = resp
  1217. }
  1218. // Export
  1219. // @Title 客户导出
  1220. // @Description 客户导出接口
  1221. // @Param Status query string true "状态:'试用','永续','冻结','流失','正式'"
  1222. // @Param CompanyType query string true "客户类型:传空字符串或者不传为全部,'ficc','权益'"
  1223. // @Param IndustryId query int true "行业id,0为全部"
  1224. // @Param AdminId query string true "销售id,多个用英文逗号隔开,空字符串为全部"
  1225. // @Param ChartPermissionIds query string false "阅读权限品种ID,多个用英文逗号分开,空字符串为全部"
  1226. // @Param ContractPermissionIds query string false "已购品种ID,多个用英文逗号分开,空字符串为全部"
  1227. // @Param StartDate query string true "开始时间"
  1228. // @Param EndDate query string true "结束时间"
  1229. // @Param CreateStartDate query string true "创建开始时间"
  1230. // @Param CreateEndDate query string true "创建结束时间"
  1231. // @Param KeyWord query string true "搜索关键词"
  1232. // @Param Address query string true "地址 , 多个用英文逗号分开 "
  1233. // @Param Province query string true "省 , 多个用英文逗号分开 "
  1234. // @Param City query string true "市 , 多个用英文逗号分开 "
  1235. // @Param PackageType query int true "套餐类型,0:无,1:大套餐,2:小套餐"
  1236. // @Success 200 {object} company.CompanyListResp
  1237. // @router /full/export [get]
  1238. func (this *FullCompanyController) Export() {
  1239. br := new(models.BaseResponse).Init()
  1240. defer func() {
  1241. this.Data["json"] = br
  1242. this.ServeJSON()
  1243. }()
  1244. sysUser := this.SysUser
  1245. if sysUser == nil {
  1246. br.Msg = "请登录"
  1247. br.ErrMsg = "请登录,SysUser Is Empty"
  1248. br.Ret = 408
  1249. return
  1250. }
  1251. roleTypeCode := sysUser.RoleTypeCode
  1252. status := this.GetString("Status")
  1253. companyType := this.GetString("CompanyType")
  1254. industryId, _ := this.GetInt("IndustryId")
  1255. adminId := this.GetString("AdminId")
  1256. chartPermissionIds := this.GetString("ChartPermissionIds")
  1257. contractPermissionIds := this.GetString("ContractPermissionIds")
  1258. startDate := this.GetString("StartDate")
  1259. endDate := this.GetString("EndDate")
  1260. createStartDate := this.GetString("CreateStartDate")
  1261. createEndDate := this.GetString("CreateEndDate")
  1262. keyWord := this.GetString("KeyWord")
  1263. keyWord = strings.Trim(keyWord, " ")
  1264. address := this.GetString("Address")
  1265. province := this.GetString("Province")
  1266. city := this.GetString("City")
  1267. // 套餐类型
  1268. packageType, _ := this.GetInt("PackageType")
  1269. resp := new(company.CompanyListResp)
  1270. statusItems := make([]*company.CompanyStatus, 0)
  1271. var statusArr = [...]string{"全部", "试用", "正式", "冻结", "流失", "永续"}
  1272. var condition string
  1273. var pars []interface{}
  1274. //状态分类查询条件
  1275. var permissionCondition string
  1276. var permissionPars []interface{}
  1277. if companyType != "" && sysUser.RoleName != utils.ROLE_NAME_FICC_DIRECTOR {
  1278. condition += ` AND b.product_name=? `
  1279. pars = append(pars, companyType)
  1280. }
  1281. if industryId > 0 {
  1282. condition += ` AND b.industry_id=? `
  1283. pars = append(pars, industryId)
  1284. }
  1285. if adminId != "" {
  1286. condition += ` AND b.seller_id IN (` + adminId + `) `
  1287. }
  1288. if startDate != "" {
  1289. condition += ` AND b.start_date>=? `
  1290. pars = append(pars, startDate)
  1291. }
  1292. if endDate != "" {
  1293. condition += ` AND b.end_date<=? `
  1294. pars = append(pars, endDate)
  1295. }
  1296. if createStartDate != "" {
  1297. condition += ` AND a.created_time>=? `
  1298. pars = append(pars, createStartDate)
  1299. }
  1300. if createEndDate != "" {
  1301. condition += ` AND a.created_time<=? `
  1302. pars = append(pars, createEndDate)
  1303. }
  1304. if address != "" {
  1305. var addressSql string
  1306. slice := strings.Split(address, ",")
  1307. for _, v := range slice {
  1308. addressSql += "'" + v + "'" + ","
  1309. }
  1310. addressSql = strings.TrimRight(addressSql, ",")
  1311. condition += ` AND a.address IN (` + addressSql + `) `
  1312. }
  1313. if province != "" {
  1314. var provinceSql string
  1315. slice := strings.Split(province, ",")
  1316. for _, v := range slice {
  1317. provinceSql += "'" + v + "'" + ","
  1318. }
  1319. provinceSql = strings.TrimRight(provinceSql, ",")
  1320. condition += ` AND a.province IN (` + provinceSql + `) `
  1321. }
  1322. if city != "" {
  1323. var citySql string
  1324. slice := strings.Split(city, ",")
  1325. for _, v := range slice {
  1326. citySql += "'" + v + "'" + ","
  1327. }
  1328. citySql = strings.TrimRight(citySql, ",")
  1329. condition += ` AND a.city IN (` + citySql + `) `
  1330. }
  1331. if keyWord != "" {
  1332. companyIdStr, err := company.GetCompanyIdByKeyWord(keyWord)
  1333. if err != nil {
  1334. br.Msg = "获取客户信息失败"
  1335. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  1336. return
  1337. }
  1338. if companyIdStr != "" {
  1339. condition += ` AND (a.company_name LIKE '%` + keyWord + `%' OR a.company_id IN(` + companyIdStr + `)) `
  1340. } else {
  1341. condition += ` AND (a.company_name LIKE '%` + keyWord + `%' ) `
  1342. }
  1343. }
  1344. //if chartPermissionIds != "" && chartPermissionIds != "1" {
  1345. // companyIdStr, err := company.GetCompanyListCompanyIdByPermissionIdStr(chartPermissionIds)
  1346. // if err != nil {
  1347. // br.Msg = "获取失败"
  1348. // br.ErrMsg = "根据权限获取公司ID失败,Err:" + err.Error()
  1349. // return
  1350. // }
  1351. // if companyIdStr != "" {
  1352. // condition += ` AND a.company_id IN (` + companyIdStr + `) `
  1353. // } else {
  1354. // for _, v := range statusArr {
  1355. // statusCount := new(company.CompanyStatus)
  1356. // if v == "全部" {
  1357. // statusCount.Status = "全部"
  1358. // statusCount.Count = 0
  1359. // } else {
  1360. // statusCount.Status = v
  1361. // statusCount.Count = 0
  1362. // }
  1363. // statusItems = append(statusItems, statusCount)
  1364. // }
  1365. // items := make([]*company.CompanyListItem, 0)
  1366. // resp.List = items
  1367. // resp.StatusCount = statusItems
  1368. // br.Msg = "获取成功"
  1369. // br.Ret = 200
  1370. // br.Data = resp
  1371. // return
  1372. // }
  1373. //}
  1374. // 阅读权限/已购品种筛选
  1375. cond, emp, e := services.FilterReadAndContractAuth(chartPermissionIds, contractPermissionIds, condition)
  1376. if e != nil {
  1377. br.Msg = "获取失败"
  1378. br.ErrMsg = "FilterReadAndContractAuth err: " + e.Error()
  1379. return
  1380. }
  1381. if emp {
  1382. for _, v := range statusArr {
  1383. statusCount := new(company.CompanyStatus)
  1384. if v == "全部" {
  1385. statusCount.Status = "全部"
  1386. statusCount.Count = 0
  1387. } else {
  1388. statusCount.Status = v
  1389. statusCount.Count = 0
  1390. }
  1391. statusItems = append(statusItems, statusCount)
  1392. }
  1393. items := make([]*company.CompanyListItem, 0)
  1394. resp.List = items
  1395. resp.StatusCount = statusItems
  1396. br.Msg = "获取成功"
  1397. br.Ret = 200
  1398. br.Data = resp
  1399. return
  1400. }
  1401. condition = cond
  1402. //套餐类型
  1403. if packageType > 0 {
  1404. condition += ` AND b.package_type=? `
  1405. pars = append(pars, packageType)
  1406. }
  1407. permissionCondition = condition
  1408. permissionPars = pars
  1409. productId := services.GetProductId(roleTypeCode)
  1410. //权限控制
  1411. {
  1412. //如果不是超级管理员/ficc销售经理
  1413. if roleTypeCode != utils.ROLE_TYPE_CODE_ADMIN && sysUser.RoleName != utils.ROLE_NAME_FICC_DIRECTOR {
  1414. //如果是部门管理员
  1415. if roleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || roleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  1416. condition += ` AND b.product_id=? `
  1417. pars = append(pars, productId)
  1418. permissionCondition += ` AND b.product_id=? `
  1419. permissionPars = append(permissionPars, productId)
  1420. } else {
  1421. condition += ` AND b.product_id=? `
  1422. pars = append(pars, productId)
  1423. permissionCondition += ` AND b.product_id=? `
  1424. permissionPars = append(permissionPars, productId)
  1425. if status == "全部" {
  1426. ////如果是筛选了流失客户状态
  1427. if sysUser.Authority <= 0 {
  1428. condition += ` AND (b.seller_id=? OR status='流失') `
  1429. pars = append(pars, sysUser.AdminId)
  1430. permissionCondition += ` AND b.seller_id=? `
  1431. permissionPars = append(permissionPars, sysUser.AdminId)
  1432. } else {
  1433. if sysUser.Authority == 1 {
  1434. condition += ` AND (b.department_id=? OR status='流失')`
  1435. pars = append(pars, sysUser.DepartmentId)
  1436. permissionCondition += ` AND b.department_id=? `
  1437. permissionPars = append(permissionPars, sysUser.DepartmentId)
  1438. }
  1439. if sysUser.Authority == 2 && sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  1440. if status != utils.COMPANY_STATUS_LOSE {
  1441. condition += ` AND (b.group_id=? OR status='流失') `
  1442. pars = append(pars, sysUser.GroupId)
  1443. }
  1444. permissionCondition += ` AND b.group_id=? `
  1445. permissionPars = append(permissionPars, sysUser.GroupId)
  1446. }
  1447. if sysUser.Authority == 2 && sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  1448. pid, err := company.GetParentIdFromGroup(sysUser.GroupId)
  1449. if err != nil {
  1450. br.Msg = "获取失败"
  1451. br.ErrMsg = "查询父级id异常" + err.Error()
  1452. return
  1453. }
  1454. var ids []*string
  1455. if pid != nil && *pid != 0 {
  1456. ids, err = company.GetGroupIdsByParentId(*pid)
  1457. if err != nil {
  1458. fmt.Println(err.Error())
  1459. }
  1460. } else {
  1461. ids, err = company.GetGroupIdsByParentId(sysUser.GroupId)
  1462. if err != nil {
  1463. fmt.Println(err.Error())
  1464. }
  1465. }
  1466. var idSlice []string
  1467. var sid string
  1468. for _, id := range ids {
  1469. idSlice = append(idSlice, *id)
  1470. }
  1471. //加入父级groupId
  1472. if *pid > 0 {
  1473. idSlice = append(idSlice, strconv.Itoa(*pid))
  1474. } else {
  1475. idSlice = append(idSlice, strconv.Itoa(sysUser.GroupId))
  1476. }
  1477. sid = strings.Join(idSlice, ",")
  1478. if len(sid) > 0 {
  1479. if status != utils.COMPANY_STATUS_LOSE {
  1480. condition += ` AND (b.group_id IN (` + sid + `) OR status='流失') `
  1481. //pars = append(pars, sysUser.GroupId)
  1482. }
  1483. permissionCondition += ` AND b.group_id IN (` + sid + `) `
  1484. } else {
  1485. if status != utils.COMPANY_STATUS_LOSE {
  1486. condition += ` AND (b.group_id IN (` + strconv.Itoa(sysUser.GroupId) + `) OR status='流失') `
  1487. //pars = append(pars, sysUser.GroupId)
  1488. }
  1489. permissionCondition += ` AND b.group_id IN (` + strconv.Itoa(sysUser.GroupId) + `) `
  1490. }
  1491. }
  1492. }
  1493. } else {
  1494. ////如果没有筛选了流失客户状态
  1495. if sysUser.Authority <= 0 {
  1496. condition += ` AND b.seller_id=? `
  1497. pars = append(pars, sysUser.AdminId)
  1498. permissionCondition += ` AND b.seller_id=? `
  1499. permissionPars = append(permissionPars, sysUser.AdminId)
  1500. } else {
  1501. if sysUser.Authority == 1 {
  1502. condition += ` AND b.department_id=?`
  1503. pars = append(pars, sysUser.DepartmentId)
  1504. permissionCondition += ` AND b.department_id=? `
  1505. permissionPars = append(permissionPars, sysUser.DepartmentId)
  1506. }
  1507. if sysUser.Authority == 2 && sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  1508. if status != utils.COMPANY_STATUS_LOSE {
  1509. condition += ` AND b.group_id=? `
  1510. pars = append(pars, sysUser.GroupId)
  1511. }
  1512. permissionCondition += ` AND b.group_id=? `
  1513. permissionPars = append(permissionPars, sysUser.GroupId)
  1514. }
  1515. if sysUser.Authority == 2 && sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  1516. pid, err := company.GetParentIdFromGroup(sysUser.GroupId)
  1517. if err != nil {
  1518. br.Msg = "获取失败"
  1519. br.ErrMsg = "查询父级id异常" + err.Error()
  1520. return
  1521. }
  1522. var ids []*string
  1523. if pid != nil && *pid != 0 {
  1524. ids, err = company.GetGroupIdsByParentId(*pid)
  1525. if err != nil {
  1526. fmt.Println(err.Error())
  1527. }
  1528. } else {
  1529. ids, err = company.GetGroupIdsByParentId(sysUser.GroupId)
  1530. if err != nil {
  1531. fmt.Println(err.Error())
  1532. }
  1533. }
  1534. var idSlice []string
  1535. var sid string
  1536. for _, id := range ids {
  1537. idSlice = append(idSlice, *id)
  1538. }
  1539. //加入父级groupId
  1540. if *pid > 0 {
  1541. idSlice = append(idSlice, strconv.Itoa(*pid))
  1542. } else {
  1543. idSlice = append(idSlice, strconv.Itoa(sysUser.GroupId))
  1544. }
  1545. sid = strings.Join(idSlice, ",")
  1546. if len(sid) > 0 {
  1547. if status != utils.COMPANY_STATUS_LOSE {
  1548. condition += ` AND (b.group_id IN (` + sid + `) OR status='流失') `
  1549. //pars = append(pars, sysUser.GroupId)
  1550. }
  1551. permissionCondition += ` AND b.group_id IN (` + sid + `) `
  1552. } else {
  1553. if status != utils.COMPANY_STATUS_LOSE {
  1554. condition += ` AND (b.group_id IN (` + strconv.Itoa(sysUser.GroupId) + `) OR status='流失') `
  1555. //pars = append(pars, sysUser.GroupId)
  1556. }
  1557. permissionCondition += ` AND b.group_id IN (` + strconv.Itoa(sysUser.GroupId) + `) `
  1558. }
  1559. }
  1560. if sysUser.Authority == 4 {
  1561. if status != utils.COMPANY_STATUS_LOSE {
  1562. condition += ` AND b.group_id =? `
  1563. pars = append(pars, sysUser.GroupId)
  1564. }
  1565. permissionCondition += ` AND b.group_id=? `
  1566. permissionPars = append(permissionPars, sysUser.GroupId)
  1567. }
  1568. }
  1569. }
  1570. }
  1571. }
  1572. }
  1573. //状态控制
  1574. if status != "" && status != "全部" && status != utils.COMPANY_STATUS_LOSE {
  1575. condition += ` AND b.status=? `
  1576. pars = append(pars, status)
  1577. }
  1578. dir, err := os.Executable()
  1579. exPath := filepath.Dir(dir)
  1580. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1581. xlsxFile := xlsx.NewFile()
  1582. if err != nil {
  1583. br.Msg = "生成文件失败"
  1584. br.ErrMsg = "生成文件失败"
  1585. return
  1586. }
  1587. style := xlsx.NewStyle()
  1588. alignment := xlsx.Alignment{
  1589. Horizontal: "center",
  1590. Vertical: "center",
  1591. WrapText: true,
  1592. }
  1593. style.Alignment = alignment
  1594. style.ApplyAlignment = true
  1595. sheel, err := xlsxFile.AddSheet("客户数据")
  1596. if err != nil {
  1597. br.Msg = "新增Sheet失败"
  1598. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1599. return
  1600. }
  1601. titleArr := []string{"客户名称", "客户地址", "客户类型", "所属行业", "所属销售", "客户状态", "服务期限", "到期天数", "创建时间", "权限详情"}
  1602. titleLen := len(titleArr)
  1603. titleRow := sheel.AddRow()
  1604. remarkRow := sheel.AddRow() // 备注行
  1605. // 填充空白至Z列
  1606. emptyColNum := 0
  1607. for i := 0; i < 26; i++ {
  1608. remarkRow.AddCell().SetValue("")
  1609. if i >= titleLen {
  1610. emptyColNum += 1
  1611. titleRow.AddCell().SetValue("")
  1612. continue
  1613. }
  1614. titleRow.AddCell().SetValue(titleArr[i])
  1615. }
  1616. // 第二行, 在品种权限开始列加一行标红备注
  1617. styleRed := xlsx.NewStyle()
  1618. styleRed.Font.Color = "ff0000"
  1619. remarkCell := remarkRow.AddCell()
  1620. remarkCell.SetStyle(styleRed)
  1621. remarkCell.SetString(`第一位数字表示是否已购买该品种,1表示已购,0表示未购;第二位数字表示是否有阅读权限,1表示有权限,0表示没有`)
  1622. // AA列开始为品种权限
  1623. ficcPermissions := make([]int, 0) // 用于后续判断试用暂停客户是否需要暂停这部分权限
  1624. raiPermissions := make([]int, 0)
  1625. permissions, e := models.GetChartPermissionList()
  1626. if e != nil {
  1627. br.Msg = "获取失败"
  1628. br.ErrMsg = "获取品种数据失败,Err:" + err.Error()
  1629. return
  1630. }
  1631. raiArr := []string{"医药", "消费", "科技", "智造"}
  1632. for _, p := range permissions {
  1633. if p.ProductId == 1 {
  1634. ficcPermissions = append(ficcPermissions, p.ChartPermissionId)
  1635. }
  1636. if p.ProductId == 2 {
  1637. raiPermissions = append(raiPermissions, p.ChartPermissionId)
  1638. }
  1639. if utils.InArrayByStr(raiArr, p.PermissionName) {
  1640. titleRow.AddCell().SetValue(p.Remark)
  1641. continue
  1642. }
  1643. titleRow.AddCell().SetValue(p.PermissionName)
  1644. }
  1645. list, err := company.GetCompanyListExport(condition, pars)
  1646. if err != nil {
  1647. br.Msg = "获取失败"
  1648. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1649. return
  1650. }
  1651. companyIds := make([]int, 0)
  1652. for _, c := range list {
  1653. companyIds = append(companyIds, c.CompanyId)
  1654. }
  1655. // 获取客户对应的阅读和合同权限集合
  1656. contractPermissionMap := make(map[int][]int)
  1657. contractPermissions, e := company.GetContractPermissionIdsByCompanyIds(companyIds)
  1658. if e != nil {
  1659. br.Msg = "获取失败"
  1660. br.ErrMsg = "获取已购权限失败, Err: " + e.Error()
  1661. return
  1662. }
  1663. for _, p := range contractPermissions {
  1664. ids := make([]int, 0)
  1665. if p.ChartPermissionIds != "" {
  1666. idArr := strings.Split(p.ChartPermissionIds, ",")
  1667. for _, i := range idArr {
  1668. id, _ := strconv.Atoi(i)
  1669. ids = append(ids, id)
  1670. }
  1671. }
  1672. contractPermissionMap[p.CompanyId] = ids
  1673. }
  1674. reportPermissionMap := make(map[int][]int)
  1675. reportPermissions, e := company.GetReportPermissionIdsByCompanyIds(companyIds)
  1676. if e != nil {
  1677. br.Msg = "获取失败"
  1678. br.ErrMsg = "获取阅读权限失败, Err: " + e.Error()
  1679. return
  1680. }
  1681. for _, p := range reportPermissions {
  1682. ids := make([]int, 0)
  1683. if p.ChartPermissionIds != "" {
  1684. idArr := strings.Split(p.ChartPermissionIds, ",")
  1685. for _, i := range idArr {
  1686. id, _ := strconv.Atoi(i)
  1687. ids = append(ids, id)
  1688. }
  1689. }
  1690. reportPermissionMap[p.CompanyId] = ids
  1691. }
  1692. cpMap := make(map[int][]*company.ExportCompanyProduct)
  1693. permissionMap := make(map[string]string)
  1694. for _, v := range list {
  1695. cpList := make([]*company.ExportCompanyProduct, 1)
  1696. if cps, ok := cpMap[v.CompanyId]; ok {
  1697. cpList = cps
  1698. } else {
  1699. companyProduct, err := company.GetExportCompanyProduct(v.CompanyId)
  1700. if err != nil {
  1701. br.Msg = "导出失败"
  1702. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  1703. return
  1704. }
  1705. cpMap[v.CompanyId] = companyProduct
  1706. cpList = companyProduct
  1707. }
  1708. var companyName, companyType, industryName, sellerName, status, timeLimt, createTime, source, permissionStr, expireDay string
  1709. isFiccSuspend := 0
  1710. isRaiSuspend := 0
  1711. for ck, cv := range cpList {
  1712. companyName = cv.CompanyName
  1713. if ck >= 1 {
  1714. companyType = companyType + "/" + cv.ProductName
  1715. status = status + "/" + cv.Status
  1716. } else {
  1717. companyType = cv.ProductName
  1718. status = cv.Status
  1719. }
  1720. if cv.ProductId == 1 {
  1721. industryName = "ficc:" + cv.IndustryName
  1722. sellerName = "ficc:" + cv.SellerName
  1723. startDateStr := cv.StartDate.Format("2006/01/02")
  1724. endDateStr := cv.EndDate.Format("2006/01/02")
  1725. timeLimt = "ficc:" + startDateStr + "~" + endDateStr
  1726. createTime = "ficc:" + cv.CreateTime
  1727. source = "ficc:" + cv.Source
  1728. sub := cv.EndDate.Sub(time.Now())
  1729. if sub < 0 {
  1730. sub = 0
  1731. }
  1732. expireDay = "ficc:" + fmt.Sprintf("%v", int(sub.Hours()/24))
  1733. permissionKey := strconv.Itoa(v.CompanyId) + "_" + strconv.Itoa(cv.ProductId)
  1734. permissionStr = "ficc:"
  1735. if newPermissionStr, ok := permissionMap[permissionKey]; ok {
  1736. permissionStr += newPermissionStr
  1737. } else {
  1738. getPermissionStr, err := company.GetExportPermissionName(v.CompanyId, cv.ProductId)
  1739. if err != nil {
  1740. br.Msg = "导出失败"
  1741. br.ErrMsg = "获取客户权限信息失败,Err:" + err.Error()
  1742. return
  1743. }
  1744. permissionMap[permissionKey] = getPermissionStr
  1745. permissionStr += getPermissionStr
  1746. }
  1747. isFiccSuspend = cv.IsSuspend
  1748. } else {
  1749. industryName = industryName + "\n" + "权益:" + cv.IndustryName
  1750. sellerName = sellerName + "\n" + "权益:" + cv.SellerName
  1751. startDateStr := cv.StartDate.Format("2006/01/02")
  1752. endDateStr := cv.EndDate.Format("2006/01/02")
  1753. timeLimt = timeLimt + "\n" + "权益:" + startDateStr + "~" + endDateStr
  1754. createTime = createTime + "\n" + "权益:" + cv.CreateTime
  1755. source = source + "\n" + "权益:" + cv.Source
  1756. sub := cv.EndDate.Sub(time.Now())
  1757. if sub < 0 {
  1758. sub = 0
  1759. }
  1760. expireDay = "\n" + "权益:" + fmt.Sprintf("%v", int(sub.Hours()/24))
  1761. permissionKey := strconv.Itoa(v.CompanyId) + "_" + strconv.Itoa(cv.ProductId)
  1762. if permissionStr != "" {
  1763. permissionStr = permissionStr + "\n" + "权益:"
  1764. } else {
  1765. permissionStr = "权益:"
  1766. }
  1767. if newPermissionStr, ok := permissionMap[permissionKey]; ok {
  1768. permissionStr += newPermissionStr
  1769. } else {
  1770. getPermissionStr, err := company.GetExportPermissionName(v.CompanyId, cv.ProductId)
  1771. if err != nil {
  1772. br.Msg = "导出失败"
  1773. br.ErrMsg = "获取客户权限信息失败,Err:" + err.Error()
  1774. return
  1775. }
  1776. permissionMap[permissionKey] = getPermissionStr
  1777. permissionStr += getPermissionStr
  1778. }
  1779. isRaiSuspend = cv.IsSuspend
  1780. }
  1781. }
  1782. dataRow := sheel.AddRow()
  1783. dataRow.SetHeight(20)
  1784. dataRow.AddCell().SetString(companyName)
  1785. dataRow.AddCell().SetString(v.Address)
  1786. dataRow.AddCell().SetString(companyType)
  1787. dataRow.AddCell().SetString(v.IndustryName)
  1788. dataRow.AddCell().SetString(v.SellerName)
  1789. dataRow.AddCell().SetString(v.Status)
  1790. dataRow.AddCell().SetString(timeLimt)
  1791. dataRow.AddCell().SetString(expireDay)
  1792. dataRow.AddCell().SetString(v.CreatedTime)
  1793. //dataRow.AddCell().SetString(v.Source)
  1794. dataRow.AddCell().SetString(permissionStr)
  1795. // 填充空白至Z列, AA列开始为权限
  1796. for i := 0; i < emptyColNum; i++ {
  1797. dataRow.AddCell().SetString("")
  1798. }
  1799. cpIds := contractPermissionMap[v.CompanyId]
  1800. rpIds := reportPermissionMap[v.CompanyId]
  1801. for _, p := range permissions {
  1802. // 数字含义:11-表示已购且有阅读权限, 01-表示未购买但有阅读权限
  1803. first := 0
  1804. if utils.InArrayByInt(cpIds, p.ChartPermissionId) {
  1805. first = 1
  1806. }
  1807. second := 0
  1808. if utils.InArrayByInt(rpIds, p.ChartPermissionId) {
  1809. second = 1
  1810. // 如果客户是试用暂停, 即使有这部分权限, 也标记为0
  1811. if isFiccSuspend == 1 && utils.InArrayByInt(ficcPermissions, p.ChartPermissionId) {
  1812. second = 0
  1813. }
  1814. if isRaiSuspend == 1 && utils.InArrayByInt(raiPermissions, p.ChartPermissionId) {
  1815. second = 0
  1816. }
  1817. }
  1818. dataRow.AddCell().SetString(fmt.Sprintf("%d%d", first, second))
  1819. }
  1820. }
  1821. err = xlsxFile.Save(downLoadnFilePath)
  1822. if err != nil {
  1823. br.Msg = "保存文件失败"
  1824. br.ErrMsg = "保存文件失败"
  1825. return
  1826. }
  1827. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1828. downloadFileName := "客户列表_" + randStr + ".xlsx"
  1829. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  1830. defer func() {
  1831. os.Remove(downLoadnFilePath)
  1832. }()
  1833. br.Ret = 200
  1834. br.Success = true
  1835. br.Msg = "导出成功"
  1836. }
  1837. // CompanyUserExport
  1838. // @Title 导出联系人
  1839. // @Description 导出接口
  1840. // @Param Status query string true "状态:”试用','永续','冻结','流失','正式'"
  1841. // @Param CompanyType query string true "客户类型:传空字符串或者不传为全部,'ficc','权益'"
  1842. // @Param IndustryId query int true "行业id,0为全部"
  1843. // @Param AdminId query string true "销售id,多个用英文逗号隔开,空字符串为全部"
  1844. // @Param ChartPermissionIds query string false "阅读权限品种ID,多个用英文逗号分开,空字符串为全部"
  1845. // @Param ContractPermissionIds query string false "已购品种ID,多个用英文逗号分开,空字符串为全部"
  1846. // @Param StartDate query string true "开始时间"
  1847. // @Param EndDate query string true "结束时间"
  1848. // @Param KeyWord query string true "搜索关键词"
  1849. // @Param Address query string true "地址 , 多个用英文逗号分开 "
  1850. // @Param Province query string true "省 , 多个用英文逗号分开 "
  1851. // @Param City query string true "市 , 多个用英文逗号分开 "
  1852. // @Param PackageType query int true "套餐类型,0:无,1:大套餐,2:小套餐"
  1853. // @Success Ret=200 移动成功
  1854. // @router /full/user/export [get]
  1855. func (this *FullCompanyController) CompanyUserExport() {
  1856. br := new(models.BaseResponse).Init()
  1857. defer func() {
  1858. this.Data["json"] = br
  1859. this.ServeJSON()
  1860. }()
  1861. sysUser := this.SysUser
  1862. if sysUser == nil {
  1863. br.Msg = "请登录"
  1864. br.ErrMsg = "请登录,SysUser Is Empty"
  1865. br.Ret = 408
  1866. return
  1867. }
  1868. roleTypeCode := sysUser.RoleTypeCode
  1869. status := this.GetString("Status")
  1870. companyType := this.GetString("CompanyType")
  1871. industryId, _ := this.GetInt("IndustryId")
  1872. adminId := this.GetString("AdminId")
  1873. chartPermissionIds := this.GetString("ChartPermissionIds")
  1874. contractPermissionIds := this.GetString("ContractPermissionIds")
  1875. startDate := this.GetString("StartDate")
  1876. endDate := this.GetString("EndDate")
  1877. createStartDate := this.GetString("CreateStartDate")
  1878. createEndDate := this.GetString("CreateEndDate")
  1879. keyWord := this.GetString("KeyWord")
  1880. address := this.GetString("Address")
  1881. province := this.GetString("Province")
  1882. city := this.GetString("City")
  1883. // 套餐类型
  1884. packageType, _ := this.GetInt("PackageType")
  1885. var condition string
  1886. var pars []interface{}
  1887. if status != "" && status != "全部" {
  1888. condition += ` AND b.status=? `
  1889. pars = append(pars, status)
  1890. }
  1891. if companyType != "" && sysUser.RoleName != utils.ROLE_NAME_FICC_DIRECTOR {
  1892. condition += ` AND b.product_name=? `
  1893. pars = append(pars, companyType)
  1894. }
  1895. if industryId > 0 {
  1896. condition += ` AND b.industry_id=? `
  1897. pars = append(pars, industryId)
  1898. }
  1899. if adminId != "" {
  1900. condition += ` AND b.seller_id IN (` + adminId + `) `
  1901. }
  1902. if startDate != "" {
  1903. condition += ` AND b.start_date>=? `
  1904. pars = append(pars, startDate)
  1905. }
  1906. if endDate != "" {
  1907. condition += ` AND b.end_date<=? `
  1908. pars = append(pars, endDate)
  1909. }
  1910. if createStartDate != "" {
  1911. condition += ` AND a.created_time>=? `
  1912. pars = append(pars, createStartDate)
  1913. }
  1914. if createEndDate != "" {
  1915. condition += ` AND a.created_time<=? `
  1916. pars = append(pars, createEndDate)
  1917. }
  1918. if keyWord != "" {
  1919. condition += ` AND (a.company_name LIKE '%` + keyWord + `%' OR c.mobile LIKE '%` + keyWord + `%' OR c.email LIKE '%` + keyWord + `%') `
  1920. }
  1921. // 阅读权限/已购品种筛选
  1922. cond, emp, e := services.FilterReadAndContractAuth(chartPermissionIds, contractPermissionIds, condition)
  1923. if e != nil {
  1924. br.Msg = "获取失败"
  1925. br.ErrMsg = "FilterReadAndContractAuth err: " + e.Error()
  1926. return
  1927. }
  1928. if emp {
  1929. condition += ` AND 1 = 2`
  1930. } else {
  1931. condition = cond
  1932. }
  1933. if address != "" {
  1934. var addressSql string
  1935. slice := strings.Split(address, ",")
  1936. for _, v := range slice {
  1937. addressSql += "'" + v + "'" + ","
  1938. }
  1939. addressSql = strings.TrimRight(addressSql, ",")
  1940. condition += ` AND a.address IN (` + addressSql + `) `
  1941. }
  1942. if province != "" {
  1943. var provinceSql string
  1944. slice := strings.Split(province, ",")
  1945. for _, v := range slice {
  1946. provinceSql += "'" + v + "'" + ","
  1947. }
  1948. provinceSql = strings.TrimRight(provinceSql, ",")
  1949. condition += ` AND a.province IN (` + provinceSql + `) `
  1950. }
  1951. if city != "" {
  1952. var citySql string
  1953. slice := strings.Split(city, ",")
  1954. for _, v := range slice {
  1955. citySql += "'" + v + "'" + ","
  1956. }
  1957. citySql = strings.TrimRight(citySql, ",")
  1958. condition += ` AND a.city IN (` + citySql + `) `
  1959. }
  1960. //套餐类型
  1961. if packageType > 0 {
  1962. condition += ` AND b.package_type=? `
  1963. pars = append(pars, packageType)
  1964. }
  1965. //权限控制
  1966. {
  1967. // 角色不为ficc销售经理时走权限控制
  1968. if sysUser.RoleName != utils.ROLE_NAME_FICC_DIRECTOR {
  1969. if roleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER ||
  1970. roleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER {
  1971. if status != utils.COMPANY_STATUS_LOSE && status != utils.COMPANY_STATUS_FOREVER {
  1972. condition += ` AND b.seller_id=? `
  1973. pars = append(pars, sysUser.AdminId)
  1974. }
  1975. }
  1976. if sysUser.Authority == 1 {
  1977. condition += ` AND b.department_id=? `
  1978. pars = append(pars, sysUser.DepartmentId)
  1979. }
  1980. if sysUser.Authority == 2 {
  1981. condition += ` AND b.group_id=? `
  1982. pars = append(pars, sysUser.GroupId)
  1983. }
  1984. }
  1985. }
  1986. var statusCond string
  1987. var statusPars []interface{}
  1988. // 根据当前用户角色
  1989. productId := services.GetProductId(roleTypeCode)
  1990. if sysUser.RoleName == utils.ROLE_NAME_FICC_DIRECTOR {
  1991. productId = 0
  1992. }
  1993. if productId > 0 {
  1994. condition += ` AND b.product_id = ? `
  1995. pars = append(pars, productId)
  1996. statusCond += ` AND b.product_id = ? `
  1997. statusPars = append(statusPars, productId)
  1998. }
  1999. companyIds, err := company.GetExportCompanyIds(condition, pars)
  2000. if err != nil {
  2001. br.Msg = "导出失败"
  2002. br.ErrMsg = "导出失败,Err:" + err.Error()
  2003. return
  2004. }
  2005. dir, err := os.Executable()
  2006. exPath := filepath.Dir(dir)
  2007. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  2008. xlsxFile := xlsx.NewFile()
  2009. if err != nil {
  2010. br.Msg = "生成文件失败"
  2011. br.ErrMsg = "生成文件失败"
  2012. return
  2013. }
  2014. style := xlsx.NewStyle()
  2015. alignment := xlsx.Alignment{
  2016. Horizontal: "center",
  2017. Vertical: "center",
  2018. WrapText: true,
  2019. }
  2020. style.Alignment = alignment
  2021. style.ApplyAlignment = true
  2022. sheel, err := xlsxFile.AddSheet("联系人数据")
  2023. if err != nil {
  2024. br.Msg = "新增Sheet失败"
  2025. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  2026. return
  2027. }
  2028. sheel.SetColWidth(7, 7, 10)
  2029. sheel.SetColWidth(8, 10, 16)
  2030. sheel.SetColWidth(9, 11, 20)
  2031. sheel.SetColWidth(12, 12, 35)
  2032. sheel.SetColWidth(13, 13, 15)
  2033. sheel.SetColWidth(14, 14, 80)
  2034. titleRow := sheel.AddRow()
  2035. titleRow.AddCell().SetValue("联系人")
  2036. titleRow.AddCell().SetValue("是否关注公众号")
  2037. titleRow.AddCell().SetValue("手机号")
  2038. titleRow.AddCell().SetValue("邮箱")
  2039. titleRow.AddCell().SetValue("是否决策人")
  2040. titleRow.AddCell().SetValue("客户名称")
  2041. titleRow.AddCell().SetValue("客户状态")
  2042. if companyIds != "" {
  2043. companyIds = strings.Trim(companyIds, ",")
  2044. users, err := company.GetExportUser(companyIds)
  2045. if err != nil {
  2046. br.Msg = "导出失败"
  2047. br.ErrMsg = "获取联系人信息失败,Err:" + err.Error()
  2048. return
  2049. }
  2050. // 获取客户状态
  2051. companyIntIds := make([]int, 0)
  2052. companyIdArr := strings.Split(companyIds, ",")
  2053. for _, c := range companyIdArr {
  2054. id, e := strconv.Atoi(c)
  2055. if e != nil {
  2056. br.Msg = "导出失败"
  2057. br.ErrMsg = "客户ID有误"
  2058. return
  2059. }
  2060. companyIntIds = append(companyIntIds, id)
  2061. }
  2062. if len(companyIntIds) > 0 {
  2063. statusCond += fmt.Sprintf(` AND a.company_id IN (%s) `, utils.GetOrmInReplace(len(companyIntIds)))
  2064. statusPars = append(statusPars, companyIntIds)
  2065. }
  2066. statusMap := make(map[int]string)
  2067. nameMap := make(map[int]string)
  2068. companyStatusList, e := company.GetExportUserCompanyStatus(statusCond, statusPars)
  2069. if e != nil {
  2070. br.Msg = "导出失败"
  2071. br.ErrMsg = "获取客户状态失败, Err: " + e.Error()
  2072. return
  2073. }
  2074. for _, s := range companyStatusList {
  2075. statusMap[s.CompanyId] = s.CompanyStatus
  2076. nameMap[s.CompanyId] = s.CompanyName
  2077. }
  2078. for _, v := range users {
  2079. dataRow := sheel.AddRow()
  2080. dataRow.SetHeight(20)
  2081. dataRow.AddCell().SetString(v.RealName)
  2082. if v.IsSubscribe == 1 {
  2083. dataRow.AddCell().SetString("是") // 是否关注公众号
  2084. } else {
  2085. dataRow.AddCell().SetString("")
  2086. }
  2087. dataRow.AddCell().SetString(v.Mobile)
  2088. dataRow.AddCell().SetString(v.Email)
  2089. if v.IsMaker == 1 {
  2090. dataRow.AddCell().SetString("是")
  2091. } else {
  2092. dataRow.AddCell().SetString("否")
  2093. }
  2094. dataRow.AddCell().SetString(nameMap[v.CompanyId])
  2095. dataRow.AddCell().SetString(statusMap[v.CompanyId]) // 客户状态
  2096. }
  2097. }
  2098. err = xlsxFile.Save(downLoadnFilePath)
  2099. if err != nil {
  2100. br.Msg = "保存文件失败"
  2101. br.ErrMsg = "保存文件失败"
  2102. return
  2103. }
  2104. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  2105. downloadFileName := "联系人列表" + randStr + ".xlsx"
  2106. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  2107. defer func() {
  2108. os.Remove(downLoadnFilePath)
  2109. }()
  2110. br.Ret = 200
  2111. br.Success = true
  2112. br.Msg = "导出成功"
  2113. }
  2114. // Detail
  2115. // @Title 获取客户详情
  2116. // @Description 获取客户详情接口
  2117. // @Param CompanyId query int true "客户id"
  2118. // @Success 200 {object} company.CompanyDetailResp
  2119. // @router /full/detail [get]
  2120. func (this *FullCompanyController) Detail() {
  2121. br := new(models.BaseResponse).Init()
  2122. defer func() {
  2123. this.Data["json"] = br
  2124. this.ServeJSON()
  2125. }()
  2126. sysUser := this.SysUser
  2127. if sysUser == nil {
  2128. br.Msg = "请登录"
  2129. br.ErrMsg = "请登录,SysUser Is Empty"
  2130. br.Ret = 408
  2131. return
  2132. }
  2133. companyId, _ := this.GetInt("CompanyId")
  2134. if companyId <= 0 {
  2135. br.Msg = "参数错误"
  2136. br.ErrMsg = "客户参数小于等于0"
  2137. return
  2138. }
  2139. // CRM8.8-是否为编辑页的客户详情
  2140. isEdit, _ := this.GetBool("IsEdit")
  2141. item, err := company.GetCompanyDetailById(companyId)
  2142. if err != nil {
  2143. br.Msg = "获取信息失败"
  2144. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  2145. return
  2146. }
  2147. var productName string
  2148. productId := services.GetProductId(sysUser.RoleTypeCode)
  2149. // 销售经理赋予admin查看权限
  2150. if sysUser.RoleName == utils.ROLE_NAME_FICC_DIRECTOR {
  2151. productId = 0
  2152. }
  2153. if productId == 1 {
  2154. productName = utils.COMPANY_PRODUCT_FICC_NAME
  2155. } else if productId == 2 {
  2156. productName = utils.COMPANY_PRODUCT_RAI_NAME
  2157. } else {
  2158. productName = "admin"
  2159. }
  2160. resp := new(company.CompanyDetailResp)
  2161. resp.ProductName = productName
  2162. //获取客户产品
  2163. productList, err := company.GetCompanyProductsByCompanyId(companyId)
  2164. if err != nil {
  2165. br.Msg = "获取信息失败"
  2166. br.ErrMsg = "获取产品信息失败,Err:" + err.Error()
  2167. return
  2168. }
  2169. roleTypeCode := sysUser.RoleTypeCode
  2170. itemLen := len(productList)
  2171. btnDetailItem := new(company.ButtonDetailPermission)
  2172. //产品权限map
  2173. statusMap := make(map[int]string)
  2174. for i := 0; i < itemLen; i++ {
  2175. item := productList[i]
  2176. //添加产品权限map
  2177. statusMap[item.ProductId] = item.Status
  2178. if i == 0 {
  2179. if item.ProductId == 1 {
  2180. resp.CreateAuth = 1
  2181. } else {
  2182. resp.CreateAuth = 2
  2183. }
  2184. }
  2185. if item.ProductId == 1 {
  2186. if strings.Contains(sysUser.RoleTypeCode, "ficc") {
  2187. if item.Status == utils.COMPANY_STATUS_FREEZE || item.Status == utils.COMPANY_STATUS_LOSE {
  2188. btnDetailItem.BtnHistoryList = true
  2189. } else {
  2190. if item.SellerId == sysUser.AdminId || item.ShareSellerId == sysUser.AdminId {
  2191. btnDetailItem.BtnHistoryList = true
  2192. }
  2193. if sysUser.Authority > 0 {
  2194. if item.GroupId == sysUser.GroupId {
  2195. btnDetailItem.BtnHistoryList = true
  2196. }
  2197. if item.DepartmentId == sysUser.DepartmentId {
  2198. btnDetailItem.BtnHistoryList = true
  2199. }
  2200. }
  2201. }
  2202. }
  2203. //子权限切片集合
  2204. allFiccPermissions, permissionMap, e := services.GetBasePermissionLookItem(utils.COMPANY_PRODUCT_FICC_ID)
  2205. if e != nil {
  2206. br.Msg = "查询基础权限失败"
  2207. br.ErrMsg = "查询基础权限失败,Err:" + e.Error()
  2208. return
  2209. }
  2210. //遍历获取
  2211. for _, v := range allFiccPermissions {
  2212. if v.ParentId == 0 {
  2213. checkList := make([]int, 0)
  2214. plist := new(company.PermissionLookList)
  2215. items, ok := permissionMap[v.ChartPermissionId]
  2216. if !ok {
  2217. br.Msg = "获取权限信息失败"
  2218. return
  2219. }
  2220. for itemK, n := range items {
  2221. permission, err := company.GetCompanyPermissionCheckItem(companyId, n.ChartPermissionId)
  2222. if err != nil && err.Error() != utils.ErrNoRow() {
  2223. br.Msg = "获取失败"
  2224. br.ErrMsg = "获取权限信息失败,Err:" + err.Error()
  2225. return
  2226. }
  2227. if permission != nil && permission.ChartPermissionId > 0 {
  2228. checkList = append(checkList, n.ChartPermissionId)
  2229. items[itemK].StartDate = permission.StartDate
  2230. items[itemK].EndDate = permission.EndDate
  2231. items[itemK].Status = permission.Status
  2232. endDateTime, _ := time.Parse(utils.FormatDate, permission.EndDate)
  2233. endDateTime = endDateTime.AddDate(0, 0, 1)
  2234. sub := endDateTime.Sub(time.Now())
  2235. if sub < 0 {
  2236. sub = 0
  2237. }
  2238. expireDay := fmt.Sprintf("%v", int(sub.Hours()/24))
  2239. items[itemK].ExpireDay = expireDay
  2240. }
  2241. //count, err := company.GetCompanyPermissionCheck(companyId, n.ChartPermissionId)
  2242. //if err != nil {
  2243. // br.Msg = "获取失败"
  2244. // br.ErrMsg = "获取权限信息失败,Err:" + err.Error()
  2245. // return
  2246. //}
  2247. //if count > 0 {
  2248. // checkList = append(checkList, n.ChartPermissionId)
  2249. //}
  2250. }
  2251. plist.Items = items
  2252. plist.ClassifyName = v.PermissionName
  2253. plist.CheckList = checkList
  2254. item.PermissionList = append(item.PermissionList, plist)
  2255. }
  2256. }
  2257. resp.FiccItem = item
  2258. if (roleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER ||
  2259. roleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP || roleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT) &&
  2260. item.Status == utils.COMPANY_STATUS_TRY_OUT && item.ApproveStatus != "待审批" {
  2261. resp.IsButtonShow = 1
  2262. }
  2263. } else {
  2264. if strings.Contains(sysUser.RoleTypeCode, "rai") {
  2265. if item.Status == utils.COMPANY_STATUS_FREEZE || item.Status == utils.COMPANY_STATUS_LOSE {
  2266. btnDetailItem.BtnHistoryList = true
  2267. } else {
  2268. if item.SellerId == sysUser.AdminId {
  2269. btnDetailItem.BtnHistoryList = true
  2270. }
  2271. if sysUser.Authority > 0 {
  2272. if item.GroupId == sysUser.GroupId {
  2273. btnDetailItem.BtnHistoryList = true
  2274. }
  2275. if item.DepartmentId == sysUser.DepartmentId {
  2276. btnDetailItem.BtnHistoryList = true
  2277. }
  2278. }
  2279. }
  2280. }
  2281. // CRM8.8-权益权限列表调用较多、统一进行调整
  2282. plist, e := services.RaiCompanyPermissionAndCheckList(companyId, isEdit, []string{"调研"})
  2283. if e != nil {
  2284. br.Msg = "获取客户权益权限列表信息失败"
  2285. br.ErrMsg = "获取客户权益权限列表信息失败,Err:" + e.Error()
  2286. return
  2287. }
  2288. // cygx8.2专项产业调研展示
  2289. item.SpecialSurplus, err = cygxService.GetSpecialSurplusByCompanyNew(companyId)
  2290. if err != nil {
  2291. br.Msg = "获取失败"
  2292. br.ErrMsg = "获取专项调研信息失败,Err:" + err.Error()
  2293. return
  2294. }
  2295. item.PermissionList = append(item.PermissionList, plist)
  2296. resp.RaiItem = item
  2297. if (roleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER ||
  2298. roleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP ||
  2299. roleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT) &&
  2300. item.Status == utils.COMPANY_STATUS_TRY_OUT && item.ApproveStatus != "待审批" {
  2301. resp.IsButtonShow = 1
  2302. }
  2303. }
  2304. btnItem := services.GetCompanyPermissionButton(roleTypeCode, item.Status, strconv.Itoa(item.SellerId), strconv.Itoa(item.GroupId),
  2305. item.GroupId, sysUser.GroupId, item.SellerId, sysUser.AdminId, sysUser.Authority, item.ProductId, item.ShareSellerId, "")
  2306. //if itemLen > 1 {
  2307. // btnDetailItem.BtnEdit = false
  2308. // btnDetailItem.BtnDelete = false
  2309. //} else {
  2310. // btnDetailItem.BtnEdit = btnItem.BtnEdit
  2311. // btnDetailItem.BtnDelete = btnItem.BtnDelete
  2312. //}
  2313. switch item.ProductId {
  2314. case 1:
  2315. btnDetailItem.FiccEdit = btnItem.BtnEdit
  2316. btnDetailItem.FiccDelete = btnItem.BtnDelete
  2317. case 2:
  2318. btnDetailItem.RaiEdit = btnItem.BtnEdit
  2319. btnDetailItem.RaiDelete = btnItem.BtnDelete
  2320. }
  2321. if !btnDetailItem.BtnEdit {
  2322. btnDetailItem.BtnEdit = btnItem.BtnEdit
  2323. }
  2324. if !btnDetailItem.BtnDelete {
  2325. btnDetailItem.BtnDelete = btnItem.BtnDelete
  2326. }
  2327. //联系人操作权限
  2328. if resp.IsContractButtonShow == false {
  2329. resp.IsContractButtonShow = services.CheckCompanyUserButton(sysUser.RoleTypeCode, item.SellerId, sysUser.AdminId, item.ProductId, item.ShareSellerId, item.Status)
  2330. }
  2331. }
  2332. if roleTypeCode == utils.ROLE_TYPE_CODE_ADMIN ||
  2333. roleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN ||
  2334. roleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  2335. resp.IsButtonShow = 1
  2336. btnDetailItem.BtnHistoryList = true
  2337. }
  2338. //ficc权限
  2339. if ficcStatus, ok := statusMap[1]; ok {
  2340. if ficcStatus == utils.COMPANY_STATUS_TRY_OUT {
  2341. if raiStatus, ok := statusMap[2]; ok {
  2342. //另一个权限也是 试用 、冻结、流失
  2343. if raiStatus == utils.COMPANY_STATUS_TRY_OUT || raiStatus == utils.COMPANY_STATUS_FREEZE || raiStatus == utils.COMPANY_STATUS_LOSE {
  2344. //那么超管、部门管理员、销售可以编辑
  2345. if roleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER || roleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || roleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  2346. btnDetailItem.BaseInfoEdit = true
  2347. }
  2348. }
  2349. //权益是永续时,超管可编辑基础信息
  2350. if raiStatus == utils.COMPANY_STATUS_FOREVER {
  2351. if roleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  2352. btnDetailItem.BaseInfoEdit = true
  2353. }
  2354. }
  2355. } else {
  2356. //权限未开,有编辑基础信息权限
  2357. btnDetailItem.BaseInfoEdit = true
  2358. }
  2359. }
  2360. if ficcStatus == utils.COMPANY_STATUS_FOREVER {
  2361. if raiStatus, ok := statusMap[2]; ok {
  2362. //另一个权限也是 永续 、冻结、流失
  2363. if raiStatus == utils.COMPANY_STATUS_TRY_OUT || raiStatus == utils.COMPANY_STATUS_FOREVER || raiStatus == utils.COMPANY_STATUS_FREEZE || raiStatus == utils.COMPANY_STATUS_LOSE {
  2364. //超管可编辑基础信息
  2365. if roleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  2366. btnDetailItem.BaseInfoEdit = true
  2367. }
  2368. }
  2369. } else {
  2370. //权限未开,有编辑基础信息权限
  2371. btnDetailItem.BaseInfoEdit = true
  2372. }
  2373. }
  2374. }
  2375. //权益销售
  2376. if raiStatus, ok := statusMap[2]; ok {
  2377. if raiStatus == utils.COMPANY_STATUS_TRY_OUT {
  2378. if ficcStatus, ok := statusMap[1]; ok {
  2379. //另一个权限也是 试用 、冻结、流失
  2380. if ficcStatus == utils.COMPANY_STATUS_TRY_OUT || ficcStatus == utils.COMPANY_STATUS_FREEZE || ficcStatus == utils.COMPANY_STATUS_LOSE {
  2381. //那么超管、部门管理员、销售可以编辑
  2382. if roleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER || roleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN || roleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  2383. btnDetailItem.BaseInfoEdit = true
  2384. }
  2385. }
  2386. //ficc是永续时,超管可编辑基础信息
  2387. if ficcStatus == utils.COMPANY_STATUS_FOREVER {
  2388. if roleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  2389. btnDetailItem.BaseInfoEdit = true
  2390. }
  2391. }
  2392. } else {
  2393. //权限未开,有编辑基础信息权限
  2394. btnDetailItem.BaseInfoEdit = true
  2395. }
  2396. }
  2397. if raiStatus == utils.COMPANY_STATUS_FOREVER {
  2398. if ficcStatus, ok := statusMap[1]; ok {
  2399. //另一个权限也是 试用、永续 、冻结、流失
  2400. if ficcStatus == utils.COMPANY_STATUS_TRY_OUT || ficcStatus == utils.COMPANY_STATUS_FOREVER || ficcStatus == utils.COMPANY_STATUS_FREEZE || ficcStatus == utils.COMPANY_STATUS_LOSE {
  2401. //超管可编辑基础信息
  2402. if roleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  2403. btnDetailItem.BaseInfoEdit = true
  2404. }
  2405. }
  2406. } else {
  2407. //权限未开,有编辑基础信息权限
  2408. btnDetailItem.BaseInfoEdit = true
  2409. }
  2410. }
  2411. }
  2412. // 隐藏销售列表相关操作按钮, 仅可查看基础内容及历史签约
  2413. resp.IsContractButtonShow = false
  2414. btn := new(company.ButtonDetailPermission)
  2415. btn.BtnHistoryList = true
  2416. btnDetailItem = btn
  2417. resp.Item = item
  2418. resp.BtnItem = btnDetailItem
  2419. br.Ret = 200
  2420. br.Success = true
  2421. br.Msg = "获取成功"
  2422. br.Data = resp
  2423. }
  2424. // PermissionLook
  2425. // @Title 查看客户权限
  2426. // @Description 查看客户权限接口
  2427. // @Param CompanyId query int true "客户id"
  2428. // @Param LookType query int true "调用类型:1增开试用,0其他"
  2429. // @Success 200 {object} company.PermissionLookResp
  2430. // @router /full/permission/look [get]
  2431. func (this *FullCompanyController) PermissionLook() {
  2432. br := new(models.BaseResponse).Init()
  2433. defer func() {
  2434. this.Data["json"] = br
  2435. this.ServeJSON()
  2436. }()
  2437. sysUser := this.SysUser
  2438. if sysUser == nil {
  2439. br.Msg = "请登录"
  2440. br.ErrMsg = "请登录,SysUser Is Empty"
  2441. br.Ret = 408
  2442. return
  2443. }
  2444. roleTypeCode := sysUser.RoleTypeCode
  2445. companyId, _ := this.GetInt("CompanyId")
  2446. if companyId <= 0 {
  2447. br.Msg = "参数错误"
  2448. br.ErrMsg = "参数错误,CompanyId 小于等于0 "
  2449. return
  2450. }
  2451. lookType, _ := this.GetInt("LookType")
  2452. productId := services.GetProductId(roleTypeCode)
  2453. // ficc销售经理可查看ficc和权益的
  2454. if sysUser.RoleName == utils.ROLE_NAME_FICC_DIRECTOR {
  2455. productId = 0
  2456. }
  2457. resp := new(company.PermissionLookResp)
  2458. permissionArr := make([]*models.ChartPermission, 0)
  2459. var err error
  2460. if lookType == 1 {
  2461. permissionArr, err = services.GetPermissionFirstArr(productId)
  2462. } else {
  2463. permissionArr, err = services.GetPermissionFirstArr(0)
  2464. }
  2465. if err != nil {
  2466. br.Msg = "获取权限失败"
  2467. br.ErrMsg = "获取权限失败 err:" + err.Error()
  2468. return
  2469. }
  2470. /*if lookType == 1 {
  2471. if productId == 1 {
  2472. for _, permission := range utils.PermissionFiccClassifyArr {
  2473. permissionArr = append(permissionArr, permission)
  2474. }
  2475. } else if productId == 2 {
  2476. permissionArr = []string{"权益"}
  2477. } else {
  2478. for _, permission := range utils.PermissionAllClassifyArr {
  2479. permissionArr = append(permissionArr, permission)
  2480. }
  2481. }
  2482. } else {
  2483. for _, permission := range utils.PermissionAllClassifyArr {
  2484. permissionArr = append(permissionArr, permission)
  2485. }
  2486. }*/
  2487. //ficc权限类目
  2488. for _, v := range permissionArr {
  2489. checkList := make([]int, 0)
  2490. p := new(company.PermissionLookList)
  2491. p.Items = make([]*company.PermissionLookItem, 0)
  2492. var productId int
  2493. //if v == "权益" {
  2494. // productId = 2
  2495. //} else {
  2496. // productId = 1
  2497. //}
  2498. if v.PermissionName != "权益" {
  2499. productId = 1
  2500. //mapPermissionName := make(map[string]int)
  2501. items, err := company.GetPermissionLookItemsByParentId(productId, v.ChartPermissionId)
  2502. if err != nil {
  2503. br.Msg = "获取失败"
  2504. br.ErrMsg = "获取权限信息失败,Err:" + err.Error()
  2505. return
  2506. }
  2507. itemLen := len(items)
  2508. permissionList := make([]*company.PermissionLookItem, 0)
  2509. for i := 0; i < itemLen; i++ {
  2510. var isAdd bool
  2511. item := items[i]
  2512. permission, err := company.GetCompanyPermissionCheckItem(companyId, item.ChartPermissionId)
  2513. if err != nil && err.Error() != utils.ErrNoRow() {
  2514. br.Msg = "获取失败"
  2515. br.ErrMsg = "获取权限信息失败,Err:" + err.Error()
  2516. return
  2517. }
  2518. if permission != nil && permission.ChartPermissionId > 0 {
  2519. items[i].StartDate = permission.StartDate
  2520. items[i].EndDate = permission.EndDate
  2521. items[i].Status = permission.Status
  2522. //判断主观、客观
  2523. if items[i].PermissionType == 1 {
  2524. items[i].PermissionTypeName = "主观"
  2525. } else if items[i].PermissionType == 2 {
  2526. items[i].PermissionTypeName = "客观"
  2527. }
  2528. checkList = append(checkList, item.ChartPermissionId)
  2529. isAdd = true
  2530. ////如果主观客观 都有,则不显示类型,如果只有一种就显示主观,或者客观
  2531. //if mapPermissionName[items[i].PermissionName] == 0 {
  2532. // checkList = append(checkList, item.ChartPermissionId)
  2533. // mapPermissionName[items[i].PermissionName] = i + 1
  2534. //
  2535. //} else {
  2536. // items[mapPermissionName[items[i].PermissionName]-1].PermissionTypeName = ""
  2537. //}
  2538. }
  2539. endDateTime, _ := time.Parse(utils.FormatDate, item.EndDate)
  2540. endDateTime = endDateTime.AddDate(0, 0, 1)
  2541. sub := endDateTime.Sub(time.Now())
  2542. if sub < 0 {
  2543. sub = 0
  2544. }
  2545. expireDay := fmt.Sprintf("%v", int(sub.Hours()/24))
  2546. items[i].ExpireDay = expireDay
  2547. items[i].ClassifyName = v.PermissionName
  2548. if lookType == 1 {
  2549. permissionList = append(permissionList, items[i])
  2550. } else {
  2551. if isAdd {
  2552. permissionList = append(permissionList, items[i])
  2553. }
  2554. }
  2555. }
  2556. //合并主观客观
  2557. mapPermissionNameList := make(map[string]int)
  2558. for _, v := range permissionList {
  2559. if mapPermissionNameList[v.PermissionName] == 0 {
  2560. p.Items = append(p.Items, v)
  2561. mapPermissionNameList[v.PermissionName] = v.ChartPermissionId
  2562. }
  2563. }
  2564. p.ClassifyName = v.PermissionName
  2565. //p.Items = permissionList
  2566. p.CheckList = checkList
  2567. if lookType == 1 {
  2568. resp.List = append(resp.List, p)
  2569. } else {
  2570. if len(checkList) > 0 {
  2571. resp.List = append(resp.List, p)
  2572. }
  2573. }
  2574. }
  2575. }
  2576. //权益权限类目
  2577. for _, v := range permissionArr {
  2578. if v.PermissionName == "权益" {
  2579. // CRM8.8-权益权限列表调用较多、统一进行调整
  2580. unify := false
  2581. if lookType == 1 {
  2582. unify = true
  2583. }
  2584. p, e := services.RaiCompanyPermissionAndCheckList(companyId, unify, []string{"调研"})
  2585. if e != nil {
  2586. br.Msg = "获取客户权益权限列表信息失败"
  2587. br.ErrMsg = "获取客户权益权限列表信息失败,Err:" + e.Error()
  2588. return
  2589. }
  2590. if lookType == 1 {
  2591. resp.ListRai = append(resp.ListRai, p)
  2592. } else {
  2593. if len(p.CheckList) > 0 {
  2594. resp.ListRai = append(resp.ListRai, p)
  2595. }
  2596. }
  2597. }
  2598. }
  2599. br.Ret = 200
  2600. br.Success = true
  2601. br.Msg = "获取成功"
  2602. br.Data = resp
  2603. }