company.go 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000
  1. package company
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "hongze/hz_crm_api/utils"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type Company struct {
  12. CompanyId int `orm:"column(company_id);pk"`
  13. CompanyName string `description:"客户名称"`
  14. CompanyType int `orm:"column(type)"`
  15. CreditCode string `description:"社会统一信用码"`
  16. CompanyCode string `description:"客户编码"`
  17. Sort int `description:"优先级"`
  18. IsFeeCustomer int `description:"是否付费用户"`
  19. Country string `description:"国家编码"`
  20. Province string `description:"省"`
  21. City string `description:"市"`
  22. Address string `description:"详细地址"`
  23. Enabled int `description:"用户状态"`
  24. CreatedTime time.Time `description:"创建时间"`
  25. LastUpdatedTime time.Time `description:"最后一次阅读时间"`
  26. Seller string `description:"销售员"`
  27. SellsId int `description:"销售员id"`
  28. CompanyBelong string `description:"客户所属,ficc:ficc客户,public_offering:公募客户,partner:合作伙伴"`
  29. StartDate string `description:"合同开始日期"`
  30. EndDate string `description:"合同结束日期"`
  31. LastType int `description:"原客户标签"`
  32. IsVip int `description:"0:普通用户,1:大客户"`
  33. FirstStartDate string `description:"首次设置为试用客户开始时间"`
  34. FirstEndDate string `description:"首次设置为试用客户结束时间"`
  35. DateType int `description:"设置流失类型,1:1个月,2:2个月,3:3个月"`
  36. Remark string `description:"备注信息"`
  37. RegionType string `description:"地区类型,国内,国外"`
  38. OpenCompanyCode string `description:"开放给第三方的客户编码,不让第三方定位我们的客户信息"`
  39. Nation string `description:"所属国家"`
  40. IsHide int `description:"是否隐藏:0:不隐藏,1:隐藏"`
  41. OverseasStatus string `description:"海外客户状态:'正式','试用','关闭'"`
  42. ResetBtn int `description:"转正式和重置按钮:同步过来默认为0:显示转正式为1:显示重置为2"`
  43. InteractionNum int `description:"用户总的互动量"`
  44. }
  45. // 新增客户
  46. func AddCompany(item *Company) (lastId int64, err error) {
  47. o := orm.NewOrm()
  48. lastId, err = o.Insert(item)
  49. return
  50. }
  51. // 根据企业id获取企业信息
  52. func GetCompanyById(companyId int) (items *Company, err error) {
  53. o := orm.NewOrm()
  54. sql := ` SELECT * FROM company where company_id= ? `
  55. err = o.Raw(sql, companyId).QueryRow(&items)
  56. return
  57. }
  58. type CompanySearchItem struct {
  59. CompanyId int `orm:"column(company_id);pk"`
  60. CompanyName string `description:"客户名称"`
  61. CreditCode string `description:"社会统一信用码"`
  62. CompanyCode string `description:"客户编码"`
  63. StartDate string `description:"合同开始日期"`
  64. EndDate string `description:"合同结束日期"`
  65. LoseReason string `description:"流失原因"`
  66. LossTime string `description:"流失时间"`
  67. Status string `description:"客户状态:'试用','永续','冻结','流失','正式' "`
  68. CompanyType string `description:"客户类型:ficc/权益"`
  69. ApproveStatus string `description:"审批状态:'审批中','通过','驳回' 审批状态为空时,表示没有审批申请"`
  70. SellerName string `description:"销售:吉根龙/颖丹"`
  71. SellerIds string `description:"销售ID集合,包含ficc和权益的销售id" json:"-"`
  72. Duration string `description:"时长"`
  73. FreezeTime string `description:"冻结时间"`
  74. GroupId int `description:"分组id"`
  75. GroupIds string `json:"-" description:"分组id集合,包含ficc和权益的小组id"`
  76. DepartmentId int `description:"部门id"`
  77. IsSuspend int `description:"是否暂停:1:暂停,0:启用 "`
  78. ProductId int `description:"产品id"`
  79. ProductIds string `json:"-";description:"分组id集合,包含ficc和权益的小组id"`
  80. IsMoveShow int `description:"按钮显示控制:1:显示,移动按钮,0:不显示移动按钮"`
  81. SellerId int `description:"销售id"`
  82. BtnItem *ButtonPermission
  83. FormalTime string `description:"转正时间"`
  84. FreezeStartDate string `description:"冻结开始日期"`
  85. FreezeEndDate string `description:"冻结结束日期"`
  86. RegionType string `description:"地区类型,国内,国外"`
  87. ShareSeller string `description:"共享销售员"`
  88. ShareSellerId int `description:"共享销售员id"`
  89. ShareSellerIds string `description:"共享销售员id"`
  90. IsShare int `description:"0:非共享用户,1:共享客户"`
  91. IsScrounge int `description:"是否白嫖 0不是 1是"`
  92. Nation string `description:"所属国家"`
  93. }
  94. type CompanySearchListResp struct {
  95. Paging *paging.PagingItem
  96. List []*CompanySearchItem
  97. }
  98. func GetCompanySearchList(condition string, pars []interface{}, limitParam ...int) (items []*CompanySearchItem, err error) {
  99. o := orm.NewOrm()
  100. sql := `SELECT DISTINCT a.company_id,a.company_name,a.credit_code,a.company_code,a.region_type,b.share_seller_id,b.share_seller,b.is_share,
  101. GROUP_CONCAT(b.status ORDER BY b.product_id ASC SEPARATOR '/') AS status,
  102. GROUP_CONCAT(DISTINCT b.seller_name ORDER BY b.product_id ASC SEPARATOR '/' ) AS seller_name,
  103. GROUP_CONCAT(DISTINCT b.seller_id ORDER BY b.product_id ASC SEPARATOR '/') AS seller_ids,
  104. GROUP_CONCAT(DISTINCT b.group_id ORDER BY b.product_id ASC SEPARATOR '/' ) AS group_ids,
  105. GROUP_CONCAT(DISTINCT b.product_id ORDER BY b.product_id ASC SEPARATOR '/' ) AS product_ids,
  106. b.freeze_time,b.loss_time,a.lose_reason,b.is_suspend,b.seller_id,b.product_id,b.group_id,b.formal_time,b.freeze_time,
  107. GROUP_CONCAT(b.start_date ORDER BY b.product_id ASC SEPARATOR '/') AS start_date,
  108. GROUP_CONCAT(b.end_date ORDER BY b.product_id ASC SEPARATOR '/') AS end_date,
  109. GROUP_CONCAT(DISTINCT b.product_name ORDER BY b.product_id ASC SEPARATOR '/') AS company_type,
  110. GROUP_CONCAT(DISTINCT b.approve_status ORDER BY b.product_id ASC SEPARATOR '/') AS approve_status,
  111. GROUP_CONCAT(DISTINCT b.freeze_start_date ORDER BY b.product_id ASC SEPARATOR '/') AS freeze_start_date,
  112. GROUP_CONCAT(DISTINCT b.freeze_end_date ORDER BY b.product_id ASC SEPARATOR '/') AS freeze_end_date,
  113. GROUP_CONCAT(DISTINCT b.share_seller_id ORDER BY b.product_id ASC SEPARATOR ',') AS share_seller_ids
  114. FROM company AS a
  115. INNER JOIN company_product AS b ON a.company_id=b.company_id
  116. WHERE a.enabled=1 `
  117. if condition != "" {
  118. sql += condition
  119. }
  120. sql += ` GROUP BY a.company_id ORDER BY a.last_updated_time DESC LIMIT ?,? `
  121. _, err = o.Raw(sql, pars, limitParam).QueryRows(&items)
  122. return
  123. }
  124. func GetCompanySearchCount(condition string, pars []interface{}) (count int, err error) {
  125. o := orm.NewOrm()
  126. sql := `SELECT COUNT(1) AS count
  127. FROM company AS a
  128. INNER JOIN company_product AS b ON a.company_id=b.company_id
  129. WHERE a.enabled=1 `
  130. if condition != "" {
  131. sql += condition
  132. }
  133. sql += ` GROUP BY a.company_id `
  134. qsql := `SELECT COUNT(1) AS count FROM (` + sql + `) AS t`
  135. err = o.Raw(qsql, pars).QueryRow(&count)
  136. return
  137. }
  138. type CompanyItem struct {
  139. CompanyId int `orm:"column(company_id);pk"`
  140. CompanyName string `description:"客户名称"`
  141. CreditCode string `description:"社会统一信用码"`
  142. CompanyCode string `description:"客户编码"`
  143. StartDate string `description:"合同开始日期"`
  144. EndDate string `description:"合同结束日期"`
  145. LoseReason string `description:"流失原因"`
  146. RenewalReason string `description:"续约说明"`
  147. FreezeReason string `description:"冻结理由"`
  148. LossTime string `description:"流失时间"`
  149. Status string `description:"客户状态:'试用','永续','冻结','流失','正式' "`
  150. StatusStr string `description:"客户状态:'试用','永续','冻结','流失','正式' 如果是共享客户会加上 '(共享)'" `
  151. CompanyType string `description:"客户类型:ficc/权益"`
  152. ApproveStatus string `description:"审批状态:'待审批','已审批','驳回' 审批状态为空时,表示没有审批申请"`
  153. SellerName string `description:"销售:吉根龙/颖丹"`
  154. SellerId int `description:"销售ID"`
  155. SellerIds string `description:"销售ID集合,包含ficc和权益的销售id"`
  156. ShareSeller string `description:"共享销售员"`
  157. ShareSellerId int `description:"共享销售员id"`
  158. ShareSellerIds string `description:"共享销售员ids"`
  159. PackageTypes string `description:"套餐类型集合,包含ficc和权益的套餐类型" json:"-"`
  160. FiccPackageType int `description:"ficc的套餐类型"`
  161. ExpireDay string `description:"到期天数"`
  162. FreezeTime string `description:"冻结时间"`
  163. GroupId int `description:"分组id"`
  164. GroupIds string `description:"分组id集合,包含ficc和权益的小组id" json:"-"`
  165. DepartmentId int `description:"部门id"`
  166. IndustryName string `description:"所属行业"`
  167. IsSuspend int `description:"是否暂停:1:暂停,0:启用 "`
  168. CreatedTime string `description:"创建时间"`
  169. Source string `description:"客户来源"`
  170. Province string `description:"省"`
  171. City string `description:"市"`
  172. Address string `description:"详细地址"`
  173. Reasons string `description:"新增理由"`
  174. FreezeStartDate string `description:"冻结开始日期"`
  175. FreezeEndDate string `description:"冻结结束日期"`
  176. FreezeExpireDays int `description:"冻结到期天数"`
  177. BtnItem *ButtonPermission
  178. ProductId int `json:"-"`
  179. ProductIds string `description:"产品id集合,包含ficc和权益的产品id" json:"-"`
  180. FormalTime string `description:"转正时间"`
  181. IsShared bool `description:"是否共享客户"`
  182. RegionType string `description:"区域:国内,海外"`
  183. FiccLastViewTime string `description:"ficc最近一次阅读时间"`
  184. RaiLastViewTime string `description:"权益最近一次阅读时间"`
  185. ViewTotals string `description:"阅读次数集合,包含ficc和权益的阅读次数" json:"-"`
  186. LastViewTimes string `description:"最近一次阅读时间集合,包含ficc和权益的最近一次阅读时间" json:"-"`
  187. FiccView int `description:"Ficc报告阅读次数" json:"-"`
  188. RaiView int `description:"权益报告阅读次数" json:"-"`
  189. RoadShowTotal int `description:"累计路演次数"`
  190. TodoStatuss string `description:"任务状态"`
  191. TryStageStr string `description:"试用客户子标签"`
  192. TryStageSlice []*TryStageSliceItem `description:"试用状态相关"`
  193. AllViewTotal int `description:"总阅读数"`
  194. Deadline string `description:"未完成的todo任务的截止日期,截止目前还剩余的天数"`
  195. TodoEndTimeStr string `description:"未完成的todo任务的截止日期拼接格式"`
  196. TodoEndTime time.Time `description:"未完成的todo任务的截止日期"`
  197. TryOutDayTotals string `description:"试用天数集合,包含ficc和权益的试用天数" json:"-"`
  198. FiccTryOutDay int `description:"Ficc试用天数" json:"-"`
  199. RaiTryOutDay int `description:"权益试用天数" json:"-"`
  200. WeekViewActive int `description:"周阅读活跃: 0-七日内无阅读; 1-活跃"`
  201. IsShare int `description:"0:非共享用户,1:共享客户"`
  202. LastServiceTime string `description:"最后服务时间"`
  203. ServiceTimes int `description:"服务次数"`
  204. CloseReason string `description:"关闭原因"`
  205. CloseTime string `description:"关闭时间"`
  206. Nation string `description:"所属国家"`
  207. IsUserMaker int `description:"近四周之内是否包含决策人互动过 ,0否,1是"`
  208. LatestServiceRecord time.Time `description:"最近沟通时间"`
  209. FirstDate time.Time `description:"首次服务时间"`
  210. }
  211. type CompanyListItem struct {
  212. CompanyId int `orm:"column(company_id);pk"`
  213. CompanyName string `description:"客户名称"`
  214. CreditCode string `description:"社会统一信用码"`
  215. CompanyCode string `description:"客户编码"`
  216. StartDate string `description:"合同开始日期"`
  217. EndDate string `description:"合同结束日期"`
  218. LoseReason string `description:"流失原因"`
  219. RenewalReason string `description:"续约说明"`
  220. FreezeReason string `description:"冻结理由"`
  221. LossTime string `description:"流失时间"`
  222. Status string `description:"客户状态:'试用','永续','冻结','流失','正式' "`
  223. CompanyType string `description:"客户类型:ficc/权益"`
  224. ApproveStatus string `description:"审批状态:'待审批','已审批','驳回' 审批状态为空时,表示没有审批申请"`
  225. SellerName string `description:"销售:吉根龙/颖丹"`
  226. SellerId int `description:"销售ID"`
  227. SellerIds string `description:"销售ID集合,包含ficc和权益的销售id"`
  228. FiccPackageType int `description:"ficc的套餐类型"`
  229. ExpireDay string `description:"到期天数"`
  230. FreezeTime string `description:"冻结时间"`
  231. GroupId int `description:"分组id"`
  232. GroupIds string `description:"分组id集合,包含ficc和权益的小组id" json:"-"`
  233. DepartmentId int `description:"部门id"`
  234. IndustryName string `description:"所属行业"`
  235. IsSuspend int `description:"是否暂停:1:暂停,0:启用 "`
  236. CreatedTime string `description:"创建时间"`
  237. Source string `description:"客户来源"`
  238. Province string `description:"省"`
  239. City string `description:"市"`
  240. Address string `description:"详细地址"`
  241. Reasons string `description:"新增理由"`
  242. FreezeStartDate string `description:"冻结开始日期"`
  243. FreezeEndDate string `description:"冻结结束日期"`
  244. FreezeExpireDays int `description:"冻结到期天数"`
  245. BtnItem *ButtonPermission
  246. ProductId int `json:"-"`
  247. FormalTime string `description:"转正时间"`
  248. IsShared bool `description:"是否共享客户"`
  249. RegionType string `description:"区域:国内,海外"`
  250. FiccView int `description:"Ficc报告阅读次数"`
  251. RaiView int `description:"权益报告阅读次数"`
  252. FiccLastViewTime string `description:"ficc最近一次阅读时间"`
  253. RaiLastViewTime string `description:"权益最近一次阅读时间"`
  254. RoadShowTotal int `description:"累计路演次数"`
  255. TodoStatus bool `description:"是否存在进行中任务,true:存在待处理的任务"`
  256. CanConfirm bool `description:"是否允许完成任务,true:允许完成任务"`
  257. HiddenConfirm bool `description:"是否隐藏完成任务按钮,true:隐藏完成任务按钮"`
  258. HiddenCreate bool `description:"是否隐藏新增/编辑按钮,true:隐藏新增/编辑按钮"`
  259. TodoButtonColor string `description:"任务按钮颜色:red,green,gray"`
  260. TryStageSlice []*TryStageSliceItem `description:"试用状态相关"`
  261. AllViewTotal int `description:"总阅读数"`
  262. Deadline string `description:"未完成的todo任务的截止日期,截止目前还剩余的天数"`
  263. FiccTryOutDay int `description:"Ficc总试用天数"`
  264. RaiTryOutDay int `description:"权益总试用天数"`
  265. WeekViewActive int `description:"周阅读活跃: 0-七日内无阅读; 1-活跃"`
  266. IsShare int `description:"0:非共享用户,1:共享客户"`
  267. ShareSeller string `description:"共享销售员"`
  268. ShareSellerId int `description:"共享销售员id"`
  269. ShareSellerIds string `description:"共享销售员ids"`
  270. LastServiceTime string `description:"最后服务时间"`
  271. ServiceTimes int `description:"服务次数"`
  272. CloseReason string `description:"关闭客户原因"`
  273. CloseTime string `description:"关闭客户时间"`
  274. IsScrounge int `description:"是否白嫖 0不是 1是"`
  275. IsUserMaker int `description:"近四周之内是否包含决策人互动过 ,0否,1是"`
  276. }
  277. type TryStageSliceItem struct {
  278. ProductId int `description:"产品ID"`
  279. TryStage int `description:"试用客户子标签1未分类、2 推进、3 跟踪、4 预备"`
  280. HasPermission bool `description:"是否有权限设置标签"`
  281. }
  282. type CompanyListResp struct {
  283. Paging *paging.PagingItem
  284. List []*CompanyListItem
  285. StatusCount []*CompanyStatus
  286. TryStageCount []*CompanyTryStageCount
  287. AddCompanyBtn bool
  288. IsShareGroup bool `description:"是否咨询组销售"`
  289. Status int `description:"是否咨询组销售"`
  290. }
  291. func GetCompanyListCount(condition string, pars []interface{}) (count int, err error) {
  292. o := orm.NewOrm()
  293. sql := `SELECT COUNT(DISTINCT b.company_id) AS count
  294. FROM company AS a
  295. INNER JOIN company_product AS b ON a.company_id=b.company_id
  296. WHERE a.enabled=1 `
  297. if condition != "" {
  298. sql += condition
  299. }
  300. err = o.Raw(sql, pars).QueryRow(&count)
  301. return
  302. }
  303. func GetCompanyList(condition, status, sortStr string, pars []interface{}, startSize, pageSize int) (items []*CompanyItem, err error) {
  304. o := orm.NewOrm()
  305. // 七天前的时间-用于查询周阅读活跃, 此处的需求是
  306. // FICC客户、共享客户: 以FICC最后一次阅读时间作为参考, 七日内有阅读量则为活跃
  307. // 权益客户: 以权益最后一次阅读时间作为参考
  308. t := time.Now().Local().AddDate(0, 0, -7).Format(utils.FormatDate)
  309. sql := `SELECT a.company_id,a.company_name,a.credit_code,a.company_code,a.created_time,a.province,a.city,a.address,a.region_type,b.group_id,b.road_show_total,b.is_share,b.is_user_maker,
  310. CASE
  311. WHEN GROUP_CONCAT(b.status) LIKE "%永续%" THEN 1
  312. WHEN GROUP_CONCAT(b.status) LIKE "%正式%" THEN 2
  313. WHEN GROUP_CONCAT(b.status) LIKE "%试用%" THEN 3
  314. WHEN GROUP_CONCAT(b.status) LIKE "%冻结%" THEN 4
  315. WHEN GROUP_CONCAT(b.status) LIKE "%流失%" THEN 5
  316. ELSE 10 END
  317. as sort_status,
  318. CASE
  319. WHEN GROUP_CONCAT(b.try_stage) LIKE "%2%" THEN 1
  320. WHEN GROUP_CONCAT(b.try_stage) LIKE "%3%" THEN 2
  321. WHEN GROUP_CONCAT(b.try_stage) LIKE "%4%" THEN 3
  322. WHEN GROUP_CONCAT(b.try_stage) LIKE "%1%" THEN 4
  323. ELSE 10 END
  324. as sort_try_stage,
  325. CASE
  326. WHEN GROUP_CONCAT(b.todo_status) LIKE "%未完成%" AND GROUP_CONCAT(b.todo_end_time) is not null THEN GROUP_CONCAT(b.todo_end_time)
  327. ELSE "9999-01-01" END
  328. as sort_todo_end_time,
  329. GROUP_CONCAT(CASE
  330. WHEN b.is_share = 1 THEN CONCAT(b.status, '(共享)')
  331. ELSE b.status
  332. END ORDER BY b.product_id ASC SEPARATOR '/') AS status_str,
  333. GROUP_CONCAT(b.status ORDER BY b.product_id ASC SEPARATOR '/') AS status,
  334. GROUP_CONCAT(DISTINCT b.seller_name ORDER BY b.product_id ASC SEPARATOR '/') AS seller_name,
  335. GROUP_CONCAT(DISTINCT b.seller_id ORDER BY b.product_id ASC SEPARATOR '/') AS seller_ids,
  336. GROUP_CONCAT(DISTINCT b.package_type ORDER BY b.product_id ASC SEPARATOR '/') AS package_types,
  337. GROUP_CONCAT(DISTINCT b.industry_name ORDER BY b.product_id ASC SEPARATOR '/') AS industry_name,
  338. GROUP_CONCAT(DISTINCT b.product_name ORDER BY b.product_id ASC SEPARATOR '/') AS company_type,
  339. GROUP_CONCAT(DISTINCT b.product_id ORDER BY b.product_id ASC SEPARATOR '/') AS product_ids,
  340. GROUP_CONCAT(DISTINCT b.view_total ORDER BY b.product_id ASC SEPARATOR '/') AS view_totals,
  341. GROUP_CONCAT(DISTINCT b.last_view_time ORDER BY b.product_id ASC SEPARATOR '/') AS last_view_times,
  342. GROUP_CONCAT(b.start_date ORDER BY b.product_id ASC SEPARATOR '/') AS start_date,
  343. GROUP_CONCAT(b.end_date ORDER BY b.product_id ASC SEPARATOR '/') AS end_date,
  344. GROUP_CONCAT(DISTINCT b.loss_time ORDER BY b.product_id ASC SEPARATOR '/') AS loss_time,
  345. GROUP_CONCAT(DISTINCT b.lose_reason ORDER BY b.product_id ASC SEPARATOR '/') AS lose_reason,
  346. GROUP_CONCAT(DISTINCT b.close_time ORDER BY b.product_id ASC SEPARATOR '/') AS close_time,
  347. GROUP_CONCAT(DISTINCT b.close_reason ORDER BY b.product_id ASC SEPARATOR '/') AS close_reason,
  348. GROUP_CONCAT(DISTINCT b.source ORDER BY b.product_id ASC SEPARATOR '/') AS source,
  349. GROUP_CONCAT(DISTINCT b.reasons ORDER BY b.product_id ASC SEPARATOR '/') AS reasons,
  350. GROUP_CONCAT(DISTINCT b.approve_status ORDER BY b.product_id ASC SEPARATOR '/') AS approve_status,
  351. GROUP_CONCAT(b.try_stage ORDER BY b.product_id ASC SEPARATOR '/') AS try_stage_str,
  352. GROUP_CONCAT(DISTINCT b.group_id ORDER BY b.product_id ASC SEPARATOR '/') AS group_ids,
  353. GROUP_CONCAT(DISTINCT b.todo_status ORDER BY b.product_id ASC SEPARATOR '/') AS todo_statuss,
  354. GROUP_CONCAT(DISTINCT b.todo_end_time ORDER BY b.product_id ASC SEPARATOR '/') AS todo_end_time_str,
  355. GROUP_CONCAT(DISTINCT b.try_out_day_total ORDER BY b.product_id ASC SEPARATOR '/') AS try_out_day_totals,
  356. b.is_suspend,
  357. b.freeze_start_date,
  358. b.freeze_end_date,
  359. b.seller_id,
  360. b.product_id,
  361. b.formal_time,
  362. b.freeze_time,b.freeze_reason,b.renewal_reason,
  363. b.view_total,
  364. b.last_view_time,
  365. GROUP_CONCAT(DISTINCT b.share_seller ORDER BY b.product_id ASC SEPARATOR '/') AS share_seller,
  366. GROUP_CONCAT(DISTINCT b.share_seller_id ORDER BY b.product_id ASC SEPARATOR ',') AS share_seller_ids,
  367. b.share_seller_id ,
  368. MAX(b.last_view_time) as max_last_view_time,
  369. MIN(b.last_view_time) as min_last_view_time,
  370. MAX(b.end_date) as max_end_date,
  371. MIN(b.end_date) as min_end_date,
  372. SUM(b.view_total) as all_view_total,
  373. SUM(b.try_out_day_total) as all_try_out_day_total,
  374. IF(LEFT(GROUP_CONCAT(DISTINCT b.last_view_time ORDER BY b.product_id), 19) >= "` + t + `", 1, 0) AS week_view_active,
  375. a.nation,
  376. c.latest_service_record,
  377. d.first_date
  378. FROM company AS a
  379. INNER JOIN company_product AS b ON a.company_id=b.company_id
  380. LEFT JOIN (
  381. SELECT company_id, MAX(create_time) AS latest_service_record
  382. FROM company_service_record
  383. GROUP BY company_id
  384. ) AS c ON a.company_id = c.company_id
  385. LEFT JOIN (
  386. SELECT company_id,MIN(start_date) AS first_date
  387. FROM company_contract WHERE status=1
  388. GROUP BY company_id
  389. ) AS d ON a.company_id = d.company_id
  390. WHERE a.enabled=1 `
  391. if condition != "" {
  392. sql += condition
  393. }
  394. //sortStr := ``
  395. //如果没有传入排序字段,那么按照状态排序
  396. if sortStr == "" {
  397. if status == "全部" || status == utils.COMPANY_STATUS_TRY_OUT { //试用
  398. sortStr = " ORDER BY a.created_time DESC "
  399. } else if status == utils.COMPANY_STATUS_FORMAL { //正式
  400. // 正式客户默认按照周阅读活跃ASC, 同为不活跃则按照转正时间DESC
  401. sortStr = " ORDER BY week_view_active ASC, b.formal_time DESC"
  402. //sortStr = " ORDER BY b.formal_time DESC "
  403. } else if status == utils.COMPANY_STATUS_FREEZE { //冻结
  404. sortStr = " ORDER BY b.freeze_time DESC "
  405. } else if status == utils.COMPANY_STATUS_LOSE { //流失
  406. sortStr = " ORDER BY b.loss_time DESC "
  407. } else if status == utils.COMPANY_STATUS_CLOSE { //关闭
  408. sortStr = " ORDER BY b.close_time DESC "
  409. } else {
  410. sortStr = " ORDER BY a.created_time DESC "
  411. }
  412. }
  413. sql += ` GROUP BY a.company_id ` + sortStr + ` LIMIT ?,? `
  414. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  415. return
  416. }
  417. // GetCountCygxArticleHistoryRecordByCompanyName 查研观向阅读次数
  418. func GetCountCygxArticleHistoryRecordByCompanyName(companyName string) (count int, err error) {
  419. o := orm.NewOrm()
  420. sql := `SELECT count(1) total FROM cygx_article_history_record WHERE company_name=? `
  421. _ = o.Raw(sql, companyName).QueryRow(&count)
  422. return
  423. }
  424. // GetCountUserViewHistoryByCompanyName 晨报阅读次数
  425. func GetCountUserViewHistoryByCompanyName(companyName string) (count int, err error) {
  426. o := orm.NewOrm()
  427. sql := `SELECT count(1) total FROM user_view_history WHERE company_name=? `
  428. _ = o.Raw(sql, companyName).QueryRow(&count)
  429. return
  430. }
  431. // GetCountReportViewRecordByCompanyName 日评阅读次数
  432. func GetCountReportViewRecordByCompanyName(companyName string) (count int, err error) {
  433. o := orm.NewOrm()
  434. sql := `SELECT count(1) total FROM report_view_record WHERE company_name=? `
  435. _ = o.Raw(sql, companyName).QueryRow(&count)
  436. return
  437. }
  438. // GetCountAdvisoryUserChartArticleRecordByCompanyName 每日资讯阅读次数
  439. func GetCountAdvisoryUserChartArticleRecordByCompanyName(companyName string) (count int, err error) {
  440. o := orm.NewOrm()
  441. sql := `SELECT count(1) total FROM advisory_user_chart_article_record WHERE company_name=? `
  442. _ = o.Raw(sql, companyName).QueryRow(&count)
  443. return
  444. }
  445. type CompanyStatus struct {
  446. Status string `description:"客户状态:'试用','永续','冻结','流失','正式','试用-活跃','试用-非活跃' "`
  447. Count int `description:"数量 "`
  448. }
  449. type CompanyTryStageCount struct {
  450. TryStageStr string `description:"试用客户子标签:未分类、推进、跟踪、预备"`
  451. TryStage int `description:"试用客户子标签:1未分类、2 推进、3 跟踪、4 预备"`
  452. Count int `description:"数量 "`
  453. }
  454. func GetCompanyStatusCount(condition string, pars []interface{}) (items []*CompanyStatus, err error) {
  455. o := orm.NewOrm()
  456. sql := ` SELECT b.status,COUNT(DISTINCT a.company_id) AS count FROM company AS a
  457. INNER JOIN company_product AS b ON a.company_id=b.company_id
  458. WHERE a.enabled=1 AND b.status IN('正式','试用','冻结','永续','关闭') `
  459. if condition != "" {
  460. sql += condition
  461. }
  462. //sql += `GROUP BY b.status `
  463. //_, err = o.Raw(sql, pars).QueryRows(&items)
  464. sql += ` GROUP BY b.status `
  465. _, err = o.Raw(sql, pars).QueryRows(&items)
  466. return
  467. }
  468. // GetCompanyTryStageCount 获取试用客户标签的总数
  469. func GetCompanyTryStageCount(condition string, pars []interface{}) (items []*CompanyTryStageCount, err error) {
  470. o := orm.NewOrm()
  471. sql := ` SELECT b.try_stage,COUNT(DISTINCT a.company_id) AS count FROM company AS a
  472. INNER JOIN company_product AS b ON a.company_id=b.company_id
  473. WHERE a.enabled=1 AND b.status = "试用"`
  474. if condition != "" {
  475. sql += condition
  476. }
  477. //sql += `GROUP BY b.status `
  478. //_, err = o.Raw(sql, pars).QueryRows(&items)
  479. sql += ` GROUP BY b.try_stage `
  480. _, err = o.Raw(sql, pars).QueryRows(&items)
  481. return
  482. }
  483. // GetCompanyTryOutStatusCount 获取试用状态的客户数
  484. func GetCompanyTryOutStatusCount(condition string, pars []interface{}) (item *CompanyStatus, err error) {
  485. o := orm.NewOrm()
  486. sql := ` SELECT b.status,COUNT(DISTINCT a.company_id) AS count FROM company AS a
  487. INNER JOIN company_product AS b ON a.company_id=b.company_id
  488. WHERE a.enabled=1 AND b.status = "试用" `
  489. if condition != "" {
  490. sql += condition
  491. }
  492. //sql += `GROUP BY b.status `
  493. //_, err = o.Raw(sql, pars).QueryRows(&items)
  494. err = o.Raw(sql, pars).QueryRow(&item)
  495. return
  496. }
  497. func GetCompanyStatusLoseCount(condition string, pars []interface{}) (items *CompanyStatus, err error) {
  498. o := orm.NewOrm()
  499. sql := ` SELECT b.status,COUNT(DISTINCT a.company_id) AS count FROM company AS a
  500. INNER JOIN company_product AS b ON a.company_id=b.company_id
  501. WHERE a.enabled=1 AND b.status='流失' `
  502. if condition != "" {
  503. sql += condition
  504. }
  505. err = o.Raw(sql, pars).QueryRow(&items)
  506. return
  507. }
  508. func GetCompanyStatusFreezeCount(condition string, pars []interface{}) (items *CompanyStatus, err error) {
  509. o := orm.NewOrm()
  510. sql := ` SELECT b.status,COUNT(DISTINCT a.company_id) AS count FROM company AS a
  511. INNER JOIN company_product AS b ON a.company_id=b.company_id
  512. WHERE a.enabled=1 AND b.status='冻结' `
  513. if condition != "" {
  514. sql += condition
  515. }
  516. err = o.Raw(sql, pars).QueryRow(&items)
  517. return
  518. }
  519. func GetCompanyStatusForverCount(condition string, pars []interface{}) (items *CompanyStatus, err error) {
  520. o := orm.NewOrm()
  521. sql := ` SELECT b.status,COUNT(DISTINCT a.company_id) AS count FROM company AS a
  522. INNER JOIN company_product AS b ON a.company_id=b.company_id
  523. WHERE a.enabled=1 AND b.status='永续' `
  524. if condition != "" {
  525. sql += condition
  526. }
  527. err = o.Raw(sql, pars).QueryRow(&items)
  528. return
  529. }
  530. // 新增客户请求参数
  531. type CompanyAddReq struct {
  532. CompanyName string `description:"客户名称"`
  533. CreditCode string `description:"社会统一信用码"`
  534. CompanyCode string `description:"客户编码"`
  535. Province string `description:"省"`
  536. City string `description:"市"`
  537. Status string `description:"客户状态,'试用','永续','冻结','流失','正式','潜在'"`
  538. CompanyType string `description:"客户类型,ficc/权益"`
  539. IndustryId int `description:"所属行业id"`
  540. Source string `description:"来源"`
  541. SellsId int `description:"销售员id"`
  542. Reasons string `description:"新增理由,备注"`
  543. PermissionIds string `description:"权限id,多个用英文逗号隔开"`
  544. RegionType string `description:"地区类型,国内,海外"`
  545. UserId int `description:"联系人ID"`
  546. Scale string `description:"管理规模,空不填,1::50亿以下,2:50~100亿,3:100亿以上。"`
  547. Nation string `description:"所属国家"`
  548. }
  549. // 新增客户请求参数
  550. type CompanyAddResp struct {
  551. CompanyId int `description:"客户ID"`
  552. }
  553. func GetCompanyCode() (companyCode string, err error) {
  554. var num int
  555. o := orm.NewOrm()
  556. sql := `SELECT COUNT(1) AS num FROM company WHERE created_time >=? `
  557. err = o.Raw(sql, time.Now().Format(utils.FormatDate)).QueryRow(&num)
  558. if err != nil {
  559. return
  560. }
  561. fmt.Println("num", num)
  562. companyCode = "KH" + time.Now().Format("20060102") + fmt.Sprintf("%03d", num)
  563. return
  564. }
  565. type CompanyDetail struct {
  566. CompanyId int `orm:"column(company_id);pk"`
  567. CompanyName string `description:"客户名称"`
  568. CreditCode string `description:"社会统一信用码"`
  569. CompanyCode string `description:"客户编码"`
  570. Province string `description:"省"`
  571. City string `description:"市"`
  572. Address string `description:"详细地址"`
  573. RegionType string `description:"地区类型,1:国内,2:国外"`
  574. OpenCompanyCode string `description:"开放给第三方的客户编码,不让第三方定位我们的客户信息"`
  575. IsScrounge int `description:"是否白嫖 0不是 1是"`
  576. Nation string `description:"所属国家"`
  577. }
  578. type CompanyDetailResp struct {
  579. Item *CompanyDetail `description:"客户信息"`
  580. FiccItem *CompanyProductDetail `description:"Ficc客户产品详情"`
  581. RaiItem *CompanyProductDetail `description:"权益客户产品详情"`
  582. CreateAuth int `description:"创建人:1:ficc,2:权益"`
  583. ProductName string `description:"部门名称:ficc/权益/admin"`
  584. IsButtonShow int `description:"1:显示编辑和删除按钮,0:不显示"`
  585. IsContractButtonShow bool `description:"true:显示联系人操作按钮,新增、编辑、导入和删除按钮,false:不显示"`
  586. IsUserYanXuanButtonShow bool `description:"true:显示联系人开通研选试用按钮,false:不显示"`
  587. BtnItem *ButtonDetailPermission
  588. }
  589. // 客户权限列表
  590. type CompanyPermissionResp struct {
  591. FiccPermissionList []*PermissionLookList `description:"Ficc权限列表"`
  592. PermissionList []*PermissionLookList `description:"权益权限列表"`
  593. }
  594. func GetCompanyDetailById(companyId int) (item *CompanyDetail, err error) {
  595. o := orm.NewOrm()
  596. sql := `SELECT * FROM company WHERE company_id =? `
  597. err = o.Raw(sql, companyId).QueryRow(&item)
  598. return
  599. }
  600. func GetCompanyCountByCompanyName(companyName string) (count int, err error) {
  601. o := orm.NewOrm()
  602. sql := ` SELECT COUNT(1) AS COUNT FROM company AS a WHERE a.company_name=? `
  603. err = o.Raw(sql, companyName).QueryRow(&count)
  604. return
  605. }
  606. func GetCompanyCountByCreditCode(creditCode string) (count int, err error) {
  607. o := orm.NewOrm()
  608. sql := ` SELECT COUNT(1) AS COUNT FROM company AS a WHERE a.credit_code=? `
  609. err = o.Raw(sql, creditCode).QueryRow(&count)
  610. return
  611. }
  612. func GetCompanyCountByCompanyNameAndCompanyId(companyId int, companyName string) (count int, err error) {
  613. o := orm.NewOrm()
  614. sql := ` SELECT COUNT(1) AS COUNT FROM company AS a WHERE a.company_id<>? AND a.company_name=? `
  615. err = o.Raw(sql, companyId, companyName).QueryRow(&count)
  616. return
  617. }
  618. func GetCompanyCountByCreditCodeAndCompanyId(companyId int, creditCode string) (count int, err error) {
  619. o := orm.NewOrm()
  620. sql := ` SELECT COUNT(1) AS COUNT FROM company AS a WHERE a.company_id<>? AND a.credit_code=? `
  621. err = o.Raw(sql, companyId, creditCode).QueryRow(&count)
  622. return
  623. }
  624. // GetCompanyByCreditCode 根据社会信用码获取客户信息
  625. func GetCompanyByCreditCode(creditCode string) (companyInfo *Company, err error) {
  626. o := orm.NewOrm()
  627. sql := ` SELECT * FROM company AS a WHERE a.credit_code=? `
  628. err = o.Raw(sql, creditCode).QueryRow(&companyInfo)
  629. return
  630. }
  631. // GetCompanyDetailByCreditCode 根据社会信用码获取客户详情接口
  632. func GetCompanyDetailByCreditCode(creditCode string) (companyDetail *CompanyDetail, err error) {
  633. o := orm.NewOrm()
  634. sql := ` SELECT * FROM company AS a WHERE a.credit_code=? `
  635. err = o.Raw(sql, creditCode).QueryRow(&companyDetail)
  636. return
  637. }
  638. func GetCompanyTryOutCount(status string, sellerId int) (count int, err error) {
  639. o := orm.NewOrm()
  640. sql := ` SELECT COUNT(DISTINCT a.company_id) AS count FROM company AS a
  641. INNER JOIN company_product AS b ON a.company_id=b.company_id
  642. WHERE b.status=?
  643. AND b.seller_id=? `
  644. err = o.Raw(sql, status, sellerId).QueryRow(&count)
  645. return
  646. }
  647. // 获取权益销售开通的非研选试用客户数量
  648. func GetRaiCompanyTryOutCountNoResearch(sellerId int) (count int, err error) {
  649. o := orm.NewOrm()
  650. sql := `SELECT
  651. COUNT( DISTINCT a.company_id ) AS count
  652. FROM
  653. company AS a
  654. INNER JOIN company_product AS b ON a.company_id = b.company_id
  655. INNER JOIN company_report_permission AS p ON p.company_id = a.company_id
  656. WHERE
  657. b.status = '试用'
  658. AND p.status = '试用'
  659. AND b.seller_id = ?
  660. AND p.chart_permission_id NOT IN(31,52) `
  661. err = o.Raw(sql, sellerId).QueryRow(&count)
  662. return
  663. }
  664. type CompanySearchResp struct {
  665. List []*CompanyItem
  666. }
  667. func GetCompanyBySearch(keyWord string) (item []*CompanyItem, err error) {
  668. o := orm.NewOrm()
  669. var sql string
  670. if keyWord == "##" {
  671. sql = ` SELECT * FROM company ORDER BY RAND() LIMIT 5 `
  672. } else {
  673. sql = `SELECT * FROM company WHERE company_name LIKE '%` + keyWord + `%' LIMIT 50`
  674. }
  675. _, err = o.Raw(sql).QueryRows(&item)
  676. return
  677. }
  678. // 编辑客户请求参数
  679. type CompanyEditReq struct {
  680. CompanyId int `description:"客户id"`
  681. CompanyName string `description:"客户名称"`
  682. CreditCode string `description:"社会统一信用码"`
  683. Province string `description:"省"`
  684. City string `description:"市"`
  685. Nation string `description:"所属国家"`
  686. Products []*CompanyProductEditReq `description:"产品信息"`
  687. }
  688. // 编辑客户信息请求参数
  689. type CompanyInfoEditReq struct {
  690. CompanyId int `description:"客户id"`
  691. CompanyName string `description:"客户名称"`
  692. CreditCode string `description:"社会统一信用码"`
  693. Province string `description:"省"`
  694. City string `description:"市"`
  695. IndustryId int `description:"所属行业id"`
  696. IndustryName string `description:"所属行业id"`
  697. Source string `description:"来源"`
  698. }
  699. type CompanyProductEditReq struct {
  700. CompanyProductId int `description:"客户产品id"`
  701. Status string `description:"客户状态,试用,永续"`
  702. CompanyType string `description:"客户类型,ficc/权益"`
  703. IndustryId int `description:"所属行业id"`
  704. Source string `description:"来源"`
  705. SellsId int `description:"销售员id"`
  706. Reasons string `description:"新增理由"`
  707. PermissionIds string `description:"权限id,多个用英文逗号隔开"`
  708. Scale string `description:"管理规模,空不填,1::50亿以下,2:50~100亿,3:100亿以上。"`
  709. }
  710. type CompanyFuzzySearch struct {
  711. CompanyId int `orm:"column(company_id);pk"`
  712. CompanyName string `description:"客户名称"`
  713. CreditCode string `description:"社会统一信用码"`
  714. CompanyCode string `description:"客户编码"`
  715. }
  716. // 新增客户请求参数
  717. type CompanyFuzzySearchReq struct {
  718. List []*CompanyFuzzySearch
  719. }
  720. func GetCompanyFuzzySearch(keyWord string) (items []*CompanyFuzzySearch, err error) {
  721. o := orm.NewOrm()
  722. sql := `SELECT * FROM company WHERE company_name LIKE '%` + keyWord + `%' `
  723. _, err = o.Raw(sql).QueryRows(&items)
  724. return
  725. }
  726. // 修改客户
  727. func ModifyCompany(item *Company) (err error) {
  728. o := orm.NewOrm()
  729. sql := `UPDATE company
  730. SET
  731. company_name = ?,
  732. credit_code = ?,
  733. address = ?,
  734. last_updated_time = NOW(),
  735. province = ?,
  736. city = ?,
  737. nation=?
  738. WHERE company_id =?`
  739. _, err = o.Raw(sql, item.CompanyName, item.CreditCode, item.Address, item.Province, item.City, item.Nation, item.CompanyId).Exec()
  740. return
  741. }
  742. // 更新客户基础信息
  743. func (company *Company) Update(cols []string) (err error) {
  744. o := orm.NewOrm()
  745. _, err = o.Update(company, cols...)
  746. return
  747. }
  748. type DeletePptReq struct {
  749. CompanyId int `description:"客户id"`
  750. }
  751. func DeleteCompanyById(companyId int) (err error) {
  752. o := orm.NewOrm()
  753. to, err := o.Begin()
  754. if err != nil {
  755. return
  756. }
  757. defer func() {
  758. if err != nil {
  759. _ = to.Rollback()
  760. } else {
  761. _ = to.Commit()
  762. }
  763. }()
  764. sql := `DELETE FROM company_product WHERE company_id=? `
  765. _, err = to.Raw(sql, companyId).Exec()
  766. if err != nil {
  767. return
  768. }
  769. sql = `DELETE FROM company_report_permission WHERE company_id=? `
  770. _, err = to.Raw(sql, companyId).Exec()
  771. if err != nil {
  772. return
  773. }
  774. //联系人表
  775. sql = `DELETE FROM wx_user WHERE company_id=? `
  776. _, err = to.Raw(sql, companyId).Exec()
  777. if err != nil {
  778. return
  779. }
  780. //联系人 与 销售 关系表
  781. sql = `DELETE FROM user_seller_relation WHERE company_id=? `
  782. _, err = to.Raw(sql, companyId).Exec()
  783. if err != nil {
  784. return
  785. }
  786. //sql = `DELETE FROM company_operation_record WHERE company_id=? `
  787. //_, err = to.Raw(sql, companyId).Exec()
  788. //if err != nil {
  789. // return
  790. //}
  791. sql = `DELETE FROM company_contract WHERE company_id=? `
  792. _, err = to.Raw(sql, companyId).Exec()
  793. if err != nil {
  794. return
  795. }
  796. sql = `DELETE FROM company_contract_permission WHERE company_id=? `
  797. _, err = to.Raw(sql, companyId).Exec()
  798. if err != nil {
  799. return
  800. }
  801. sql = `DELETE FROM company WHERE company_id=? `
  802. _, err = to.Raw(sql, companyId).Exec()
  803. return
  804. }
  805. type CompanyReceiveItem struct {
  806. CompanyId int `orm:"column(company_id);pk"`
  807. CompanyName string `description:"客户名称"`
  808. StartDate string `description:"合同开始日期"`
  809. EndDate string `description:"合同结束日期"`
  810. Status string `description:"客户状态:'试用','永续','冻结','流失','正式' "`
  811. ProductName string `description:"客户类型:ficc/权益"`
  812. SellerName string `description:"销售:吉根龙/颖丹"`
  813. CreatedTime string `description:"创建时间"`
  814. ExpireDay string `description:"到期天数"`
  815. IndustryName string `description:"行业"`
  816. }
  817. type CompanyReceiveListResp struct {
  818. Paging *paging.PagingItem
  819. List []*CompanyReceiveItem
  820. }
  821. func GetCompanyReceiveListCount(condition string, pars []interface{}, productId, receiveProductId int) (count int, err error) {
  822. o := orm.NewOrm()
  823. sql := `SELECT
  824. COUNT(DISTINCT a.company_id ) AS COUNT
  825. FROM company AS a
  826. LEFT JOIN wx_user AS b ON a.company_id=b.company_id
  827. LEFT JOIN company_product AS c ON a.company_id=c.company_id
  828. WHERE a.enabled=1
  829. AND c.product_id=?
  830. AND c.company_id NOT IN (SELECT company_id FROM company_product WHERE product_id=?) `
  831. if condition != "" {
  832. sql += condition
  833. }
  834. err = o.Raw(sql, receiveProductId, productId, pars).QueryRow(&count)
  835. return
  836. }
  837. func GetCompanyReceiveList(condition string, pars []interface{}, productId, receiveProductId, startSize, pageSize int) (items []*CompanyReceiveItem, err error) {
  838. o := orm.NewOrm()
  839. sql := `SELECT
  840. c.company_id,a.company_name,c.product_name,c.industry_name,c.seller_name,c.status,c.start_date,c.end_date,a.created_time
  841. FROM company AS a
  842. LEFT JOIN wx_user AS b ON a.company_id=b.company_id
  843. LEFT JOIN company_product AS c ON a.company_id=c.company_id
  844. WHERE a.enabled=1
  845. AND c.product_id=?
  846. AND c.company_id NOT IN (SELECT company_id FROM company_product WHERE product_id=?) `
  847. if condition != "" {
  848. sql += condition
  849. }
  850. sql += ` GROUP BY a.company_id ORDER BY a.last_updated_time DESC LIMIT ?,? `
  851. _, err = o.Raw(sql, receiveProductId, productId, pars, startSize, pageSize).QueryRows(&items)
  852. return
  853. }
  854. // 编辑客户请求参数
  855. type CompanyReceiveReq struct {
  856. CompanyId int `description:"客户id"`
  857. Status string `description:"客户状态,试用,永续"`
  858. IndustryId int `description:"所属行业id"`
  859. Source string `description:"来源"`
  860. SellsId int `description:"销售员id,流失状态下直接领取,请传 0"`
  861. Reasons string `description:"新增理由"`
  862. PermissionIds string `description:"权限id,多个用英文逗号隔开"`
  863. CompanyType string `description:"客户类型"`
  864. }
  865. // 移动销售
  866. type MoveSellerReq struct {
  867. CompanyId int `description:"客户id"`
  868. SellsId int `description:"销售员id"`
  869. CompanyType string `description:"客户类型"`
  870. }
  871. // 修改试用客户标签
  872. type UpdateTryStageReq struct {
  873. CompanyId int `description:"客户id"`
  874. ProductId int `description:"产品ID"`
  875. TryStage int `description:"试用客户子标签:1未分类、2 推进、3 跟踪、4 预备"`
  876. }
  877. // MoveSeller 移动客户到另外的销售(销售领取客户时使用该接口)
  878. func MoveSeller(companyId, productId, sellerId, groupId, departmentId int, sellerName string) (items []*CompanyReportPermission, startDateTime, endDateTime time.Time, err error) {
  879. o := orm.NewOrm()
  880. to, err := o.Begin()
  881. if err != nil {
  882. return
  883. }
  884. defer func() {
  885. if err != nil {
  886. _ = to.Rollback()
  887. } else {
  888. _ = to.Commit()
  889. }
  890. }()
  891. startDateTime = time.Now()
  892. if productId == utils.COMPANY_PRODUCT_RAI_ID { // 权益客户试用期改为1个月。 需求池 1035
  893. endDateTime = time.Now().AddDate(0, 1, 0)
  894. } else {
  895. endDateTime = time.Now().AddDate(0, 2, 0)
  896. }
  897. startDate := startDateTime.Format(utils.FormatDate)
  898. endDate := endDateTime.Format(utils.FormatDate)
  899. //更改 客户状态为 试用
  900. sql := `UPDATE company SET start_date=?,end_date=?,last_updated_time=NOW() WHERE company_id=? `
  901. _, err = to.Raw(sql, startDate, endDate, companyId).Exec()
  902. if err != nil {
  903. return
  904. }
  905. //更改 客户产品 状态为 试用
  906. //重置tryout时间,上次修改未续约说明时间
  907. sql = `UPDATE company_product SET status='试用',try_out_time=NULL,last_description_time=NULL,is_suspend=0,start_date=?,end_date=?,seller_id=?,seller_name=?,group_id=?,department_id=?,modify_time=NOW(),try_stage=1,todo_status="无任务",todo_create_time=null,todo_approve_time=null,todo_modify_time=null,todo_end_time=null WHERE company_id=? AND product_id=? `
  908. _, err = to.Raw(sql, startDate, endDate, sellerId, sellerName, groupId, departmentId, companyId, productId).Exec()
  909. if err != nil {
  910. return
  911. }
  912. //修改原有的 客户产品权限 的服务时间
  913. items = make([]*CompanyReportPermission, 0)
  914. sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
  915. _, err = to.Raw(sql, companyId, productId).QueryRows(&items)
  916. for _, pv := range items {
  917. cpLog := new(CompanyPermissionLog)
  918. cpLog.CompanyId = companyId
  919. cpLog.ChartPermissionId = pv.ChartPermissionId
  920. cpLog.CreateTime = time.Now()
  921. cpLog.SysUserId = sellerId
  922. cpLog.SysUserName = sellerName
  923. cpLog.StartDate = pv.StartDate
  924. cpLog.EndDate = pv.EndDate
  925. cpLog.ProductId = productId
  926. cpLog.ProductName = pv.ProductName
  927. go AddCompanyPermissionLog(cpLog)
  928. }
  929. sql = `UPDATE company_report_permission SET status='试用',start_date=?,end_date=?,modify_time=NOW() WHERE company_id=? AND product_id=? `
  930. _, err = to.Raw(sql, startDate, endDate, companyId, productId).Exec()
  931. //修改 该联系人 与 当前销售的关系
  932. sql = `UPDATE user_seller_relation SET seller_id=?,seller = ? ,modify_time=NOW() WHERE company_id = ? AND product_id=?`
  933. _, err = to.Raw(sql, sellerId, sellerName, companyId, productId).Exec()
  934. //如果是权益客户,则把对应的主观客观一起开通
  935. if productId == 2 {
  936. //获取公司现有的权限,并通过现有的权限找出对应的主观客观权限
  937. oldPermissionId, tmpErr := GetCompanyPermissionIdById(companyId)
  938. if tmpErr != nil {
  939. err = tmpErr
  940. return
  941. }
  942. newPermissionId, tmpErr := GetPermissionIdById(oldPermissionId)
  943. if tmpErr != nil {
  944. err = tmpErr
  945. return
  946. }
  947. permissionArr := strings.Split(newPermissionId, ",")
  948. for _, v := range permissionArr {
  949. permissionId, tmpErr := strconv.Atoi(v)
  950. if tmpErr != nil {
  951. err = tmpErr
  952. return
  953. }
  954. //判断这个权限是否存在,如果不存在则新增
  955. count, tmpErr := GetCompanyPermissionCheck(companyId, permissionId)
  956. if tmpErr != nil {
  957. err = tmpErr
  958. return
  959. }
  960. if count == 0 {
  961. permission := new(CompanyReportPermission)
  962. permission.CompanyId = companyId
  963. permission.ReportPermissionId = permissionId
  964. permission.CreatedTime = time.Now()
  965. permission.LastUpdatedTime = time.Now()
  966. permission.ChartPermissionId = permissionId
  967. permission.StartDate = startDate
  968. permission.EndDate = endDate
  969. permission.ProductId = productId
  970. permission.ProductName = "权益"
  971. permission.ModifyTime = time.Now()
  972. permission.Status = "试用"
  973. err = AddCompanyReportPermission(permission)
  974. if err != nil {
  975. return
  976. }
  977. items = append(items, permission)
  978. }
  979. }
  980. }
  981. // 将历史的任务给标记删除掉
  982. sql = `UPDATE company_todo SET is_delete=1,modify_time=NOW() WHERE company_id=? AND product_id=? AND is_delete = 0 `
  983. _, err = to.Raw(sql, companyId, productId).Exec()
  984. if err != nil {
  985. return
  986. }
  987. return
  988. }
  989. func GetExportCompanyIds(condition string, pars []interface{}) (company_id string, err error) {
  990. o := orm.NewOrm()
  991. sql := `SELECT
  992. GROUP_CONCAT(DISTINCT b.company_id SEPARATOR ',') AS company_id
  993. FROM company AS a
  994. INNER JOIN company_product AS b ON a.company_id=b.company_id
  995. LEFT JOIN wx_user AS c ON a.company_id=c.company_id
  996. WHERE 1=1 and a.enabled=1 `
  997. if condition != "" {
  998. sql += condition
  999. }
  1000. err = o.Raw(sql, pars).QueryRow(&company_id)
  1001. return
  1002. }
  1003. type ExportUser struct {
  1004. RealName string
  1005. Mobile string
  1006. Email string
  1007. Position string
  1008. IsMaker int
  1009. DepartmentName string
  1010. CompanyId int
  1011. IsSubscribe int `description:"是否关注了公众号(弘则研究)"`
  1012. }
  1013. func GetExportUser(companyId string) (items []*ExportUser, err error) {
  1014. o := orm.NewOrm()
  1015. sql := `SELECT a.real_name,a.mobile,a.email,a.position,a.is_maker,department_name,a.company_id,b.subscribe AS is_subscribe
  1016. FROM wx_user AS a
  1017. LEFT JOIN (SELECT * FROM user_record WHERE create_platform = 1 GROUP BY user_id) AS b ON a.user_id = b.user_id
  1018. WHERE a.company_id<>1 AND a.company_id IN(` + companyId + `)
  1019. GROUP BY a.user_id ORDER BY a.company_id ASC `
  1020. _, err = o.Raw(sql).QueryRows(&items)
  1021. return
  1022. }
  1023. type ExportCompanyProduct struct {
  1024. CompanyName string
  1025. ProductId int
  1026. ProductName string
  1027. IndustryName string
  1028. SellerName string
  1029. Status string
  1030. StartDate time.Time
  1031. EndDate time.Time
  1032. CreateTime string
  1033. Source string
  1034. CreditCode string
  1035. RegionType string
  1036. LossTime string
  1037. IsSuspend int
  1038. }
  1039. func GetExportCompanyProduct(companyId int) (items []*ExportCompanyProduct, err error) {
  1040. o := orm.NewOrm()
  1041. sql := ` SELECT a.company_name,b.product_id,b.product_name,b.industry_name,b.seller_name,b.status,b.start_date,b.end_date,b.create_time,
  1042. b.source,a.credit_code,a.region_type,b.loss_time, b.is_suspend
  1043. FROM company AS a
  1044. INNER JOIN company_product AS b ON a.company_id=b.company_id
  1045. WHERE a.company_id=?
  1046. ORDER BY b.product_id ASC `
  1047. _, err = o.Raw(sql, companyId).QueryRows(&items)
  1048. return
  1049. }
  1050. func GetExportPermissionName(companyId, productId int) (chart_permission_name string, err error) {
  1051. o := orm.NewOrm()
  1052. sql := ` SELECT GROUP_CONCAT(DISTINCT c.permission_name ORDER BY sort ASC SEPARATOR '/') AS chart_permission_name FROM company_product AS a
  1053. INNER JOIN company_report_permission AS b ON a.company_id=b.company_id AND a.product_id=b.product_id
  1054. INNER JOIN chart_permission AS c ON b.chart_permission_id=c.chart_permission_id AND b.product_id=c.product_id
  1055. WHERE a.company_id=? AND a.product_id=? `
  1056. err = o.Raw(sql, companyId, productId).QueryRow(&chart_permission_name)
  1057. return
  1058. }
  1059. // 移动销售
  1060. type SuspendReq struct {
  1061. CompanyId int `description:"客户id"`
  1062. }
  1063. // MoveCompanySeller 移动客户销售
  1064. func MoveCompanySeller(companyId, productId, sellerId, groupId, departmentId int, sellerName string) (err error) {
  1065. o := orm.NewOrm()
  1066. to, err := o.Begin()
  1067. if err != nil {
  1068. return
  1069. }
  1070. defer func() {
  1071. if err != nil {
  1072. _ = to.Rollback()
  1073. } else {
  1074. _ = to.Commit()
  1075. }
  1076. }()
  1077. if sellerName != "" {
  1078. sql := `UPDATE company_product SET seller_id=?,seller_name=?,group_id=?,department_id=?,modify_time=NOW(),todo_status="无任务",todo_create_time=null,todo_approve_time=null,todo_modify_time=null,todo_end_time=null WHERE company_id=? AND product_id=? `
  1079. _, err = to.Raw(sql, sellerId, sellerName, groupId, departmentId, companyId, productId).Exec()
  1080. if err != nil {
  1081. return
  1082. }
  1083. } else {
  1084. sql := `UPDATE company_product SET seller_id=?,group_id=?,department_id=?,modify_time=NOW(),todo_status="无任务",todo_create_time=null,todo_approve_time=null,todo_modify_time=null,todo_end_time=null WHERE company_id=? AND product_id=? `
  1085. _, err = to.Raw(sql, sellerId, groupId, departmentId, companyId, productId).Exec()
  1086. if err != nil {
  1087. return
  1088. }
  1089. }
  1090. // 将历史的任务给标记删除掉
  1091. sql := `UPDATE company_todo SET is_delete=1,modify_time=NOW() WHERE company_id=? AND product_id=? AND is_delete = 0 `
  1092. _, err = to.Raw(sql, companyId, productId).Exec()
  1093. if err != nil {
  1094. return
  1095. }
  1096. return
  1097. }
  1098. // UpdateCompanyTryStage 更新试用客户标签
  1099. func UpdateCompanyTryStage(companyId, productId, tryStage int) (err error) {
  1100. o := orm.NewOrm()
  1101. sql := `UPDATE company_product SET try_stage=? WHERE company_id=? AND product_id=? and status="试用"`
  1102. _, err = o.Raw(sql, tryStage, companyId, productId).Exec()
  1103. return
  1104. }
  1105. func CheckCompanyProductByName(companyName string, productId int) (item *CompanyProductDetail, err error) {
  1106. o := orm.NewOrm()
  1107. sql := ` SELECT * FROM company AS a
  1108. INNER JOIN company_product AS b ON a.company_id=b.company_id
  1109. WHERE a.company_name=?
  1110. AND b.product_id=? `
  1111. err = o.Raw(sql, companyName, productId).QueryRow(&item)
  1112. return
  1113. }
  1114. func CheckCompanyProductById(companyId, productId int) (item *CompanyProductDetail, err error) {
  1115. o := orm.NewOrm()
  1116. sql := ` SELECT * FROM company AS a
  1117. INNER JOIN company_product AS b ON a.company_id=b.company_id
  1118. WHERE a.company_id=?
  1119. AND b.product_id=? `
  1120. err = o.Raw(sql, companyId, productId).QueryRow(&item)
  1121. return
  1122. }
  1123. func CheckCompanyProductByCreditCode(creditCode string, productId int) (item *CompanyProductDetail, err error) {
  1124. o := orm.NewOrm()
  1125. sql := ` SELECT * FROM company AS a
  1126. INNER JOIN company_product AS b ON a.company_id=b.company_id
  1127. WHERE a.credit_code=?
  1128. AND b.product_id=? `
  1129. err = o.Raw(sql, creditCode, productId).QueryRow(&item)
  1130. return
  1131. }
  1132. // 新增客户请求参数
  1133. type CheckCompanyInfoResp struct {
  1134. Status int `description:"检测状态:1:跳转到领取页面,0:不跳转,继续操作"`
  1135. CompanyId int `description:"客户id"`
  1136. ProductName string `description:"产品名称"`
  1137. }
  1138. type CheckCompanyInfoRepeatResp struct {
  1139. Status int `description:"检测状态:1:跳转到领取页面,0:不跳转,继续操作"`
  1140. RepeatStatus int `description:"检测状态:0:无重复 1:信用码重复 2:公司名重复"`
  1141. CompanyId int `description:"客户id"`
  1142. ProductName string `description:"产品名称"`
  1143. }
  1144. type ButtonPermission struct {
  1145. BtnView bool `description:"查看按钮权限:true显示,false不显示"`
  1146. BtnEdit bool `description:"编辑按钮权限:true显示,false不显示"`
  1147. BtnDelete bool `description:"删除按钮权限:true显示,false不显示"`
  1148. BtnSuspend bool `description:"暂停/启用按钮权限:true显示,false不显示"`
  1149. BtnDelay bool `description:"申请延期按钮权限:true显示,false不显示"`
  1150. BtnTurnPositive bool `description:"申请转正按钮权限:true显示,false不显示"`
  1151. BtnUpdate bool `description:"服务更新按钮权限:true显示,false不显示"`
  1152. BtnAddAgreement bool `description:"补充协议按钮权限:true显示,false不显示"`
  1153. BtnThaw bool `description:"申请解冻按钮权限:true显示,false不显示"`
  1154. BtnApplyReceive bool `description:"申请领取按钮权限:true显示,false不显示"`
  1155. BtnReceive bool `description:"领取客户按钮权限:true显示,false不显示"`
  1156. BtnModifySeller bool `description:"修改销售按钮权限:true显示,false不显示"`
  1157. BtnReceiveOther bool `description:"领取其他部门客户按钮权限:true显示,false不显示"`
  1158. BtnFreeze bool `description:"冻结按钮权限:true显示,false不显示"`
  1159. BtnTryOut bool `description:"增开试用:true显示,false不显示"`
  1160. BtnDetail bool `description:"详情按钮:true显示,false不显示"`
  1161. BtnRemarkEdit bool `description:"编辑备注按钮:true显示,false不显示"`
  1162. BtnRemarkView bool `description:"查看备注按钮:true显示,false不显示"`
  1163. BtnRemarkViewHistory bool `description:"查看历史备注按钮:true显示,false不显示"`
  1164. BtnShare bool `description:"设置共享按钮:true显示,false不显示"`
  1165. BtnServiceRecord bool `description:"查看服务记录按钮:true显示,false不显示"`
  1166. BtnClose bool `description:"关闭客户按钮:true显示,false不显示"`
  1167. BtnLoss bool `description:"转流失按钮:true显示,false不显示"`
  1168. IsResearchShow bool `description:"研选详情展示控制:true显示,false不显示"`
  1169. }
  1170. type ButtonSearchPermission struct {
  1171. BtnView bool `description:"查看按钮权限:true显示,false不显示"`
  1172. BtnUpdate bool `description:"服务更新按钮权限:true显示,false不显示"`
  1173. BtnEdit bool `description:"编辑按钮权限:true显示,false不显示"`
  1174. BtnDelete bool `description:"删除按钮权限:true显示,false不显示"`
  1175. BtnSuspend bool `description:"暂停/启用按钮权限:true显示,false不显示"`
  1176. BtnDelay bool `description:"申请延期按钮权限:true显示,false不显示"`
  1177. BtnTurnPositive bool `description:"申请转正按钮权限:true显示,false不显示"`
  1178. BtnThaw bool `description:"申请解冻按钮权限:true显示,false不显示"`
  1179. BtnApplyReceive bool `description:"申请领取按钮权限:true显示,false不显示"`
  1180. BtnReceive bool `description:"领取客户按钮权限:true显示,false不显示"`
  1181. BtnModifySeller bool `description:"修改销售按钮权限:true显示,false不显示"`
  1182. BtnReceiveOther bool `description:"领取其他部门客户按钮权限:true显示,false不显示"`
  1183. }
  1184. type ButtonDetailPermission struct {
  1185. BtnEdit bool `description:"编辑按钮权限:true显示,false不显示"`
  1186. BtnDelete bool `description:"删除按钮权限:true显示,false不显示"`
  1187. BtnHistoryList bool `description:"历史签约按钮权限:true显示,false不显示"`
  1188. BaseInfoEdit bool `description:"客户基础信息编辑操作:true可操作,false不可操作"`
  1189. FiccEdit bool `description:"ficc类型,编辑按钮权限:true显示,false不显示"`
  1190. FiccDelete bool `description:"ficc类型,删除按钮权限:true显示,false不显示"`
  1191. RaiEdit bool `description:"权益类型,编辑按钮权限:true显示,false不显示"`
  1192. RaiDelete bool `description:"权益类型,删除按钮权限:true显示,false不显示"`
  1193. }
  1194. func GetCompanyListExport(condition string, pars []interface{}) (items []*CompanyItem, err error) {
  1195. o := orm.NewOrm()
  1196. sql := `SELECT a.company_id,a.company_name,a.credit_code,a.company_code,a.created_time,a.address,
  1197. GROUP_CONCAT(b.status ORDER BY b.product_id ASC SEPARATOR '/') AS status,
  1198. GROUP_CONCAT(DISTINCT b.seller_name ORDER BY b.product_id ASC SEPARATOR '/') AS seller_name,
  1199. GROUP_CONCAT(DISTINCT b.industry_name ORDER BY b.product_id ASC SEPARATOR '/') AS industry_name,
  1200. GROUP_CONCAT(DISTINCT b.product_name ORDER BY b.product_id ASC SEPARATOR '/') AS company_type,
  1201. GROUP_CONCAT(b.start_date ORDER BY b.product_id ASC SEPARATOR '/') AS start_date,
  1202. GROUP_CONCAT(b.end_date ORDER BY b.product_id ASC SEPARATOR '/') AS end_date,
  1203. GROUP_CONCAT(DISTINCT b.loss_time ORDER BY b.product_id ASC SEPARATOR '/') AS loss_time,
  1204. GROUP_CONCAT(DISTINCT b.lose_reason ORDER BY b.product_id ASC SEPARATOR '/') AS lose_reason,
  1205. GROUP_CONCAT(DISTINCT b.source ORDER BY b.product_id ASC SEPARATOR '/') AS source,
  1206. GROUP_CONCAT(DISTINCT b.reasons ORDER BY b.product_id ASC SEPARATOR '/') AS reasons,
  1207. GROUP_CONCAT(DISTINCT b.approve_status ORDER BY b.product_id ASC SEPARATOR '/') AS approve_status,
  1208. GROUP_CONCAT(DISTINCT b.group_id ORDER BY b.product_id ASC SEPARATOR '/') AS group_id,
  1209. b.is_suspend,
  1210. b.freeze_start_date,
  1211. b.freeze_end_date,
  1212. b.seller_id,
  1213. b.product_id
  1214. FROM company AS a
  1215. INNER JOIN company_product AS b ON a.company_id=b.company_id
  1216. WHERE a.enabled=1 AND a.company_id<>1 `
  1217. if condition != "" {
  1218. sql += condition
  1219. }
  1220. sql += ` GROUP BY a.company_id ORDER BY a.last_updated_time DESC `
  1221. _, err = o.Raw(sql, pars).QueryRows(&items)
  1222. return
  1223. }
  1224. // 冻结客户请求参数
  1225. type CompanyFreezeReq struct {
  1226. CompanyId int `description:"客户id"`
  1227. CompanyType string `description:"客户类型:ficc/权益"`
  1228. Remark string `description:"备注"`
  1229. }
  1230. // 冻结客户
  1231. func CompanyFreeze(companyId, productId int, freezeReason string) (err error) {
  1232. o := orm.NewOrm()
  1233. to, err := o.Begin()
  1234. if err != nil {
  1235. return
  1236. }
  1237. defer func() {
  1238. if err != nil {
  1239. _ = to.Rollback()
  1240. } else {
  1241. _ = to.Commit()
  1242. }
  1243. }()
  1244. freezeStartDate := time.Now().Format(utils.FormatDate)
  1245. freezeEndDate := time.Now().AddDate(0, 3, 0).Format(utils.FormatDate)
  1246. // ficc是六个月的冻结期
  1247. if productId == 1 {
  1248. freezeEndDate = time.Now().AddDate(0, 6, 0).Format(utils.FormatDate)
  1249. }
  1250. // 权益也调整为 两个月的冻结期
  1251. if productId == 2 {
  1252. freezeEndDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
  1253. }
  1254. //修改用户产品状态
  1255. sql := `UPDATE company_product SET status='冻结',is_formal=0,is_suspend=0,freeze_time=NOW(),modify_time=NOW(),start_date=?,end_date=?,freeze_start_date=?,freeze_end_date=?,freeze_reason=?,try_stage=1 WHERE company_id=? AND product_id=? `
  1256. _, err = to.Raw(sql, freezeStartDate, freezeEndDate, freezeStartDate, freezeEndDate, freezeReason, companyId, productId).Exec()
  1257. if err != nil {
  1258. return err
  1259. }
  1260. //修改产品权限状态为关闭
  1261. sql = `UPDATE company_report_permission SET status='关闭',modify_time=NOW() WHERE company_id= ? AND product_id = ? `
  1262. _, err = to.Raw(sql, companyId, productId).Exec()
  1263. if err != nil {
  1264. return err
  1265. }
  1266. //修改客户为关闭
  1267. sql = `UPDATE company SET type=3,last_updated_time=NOW(),start_date=?,end_date=? WHERE company_id=? `
  1268. _, err = to.Raw(sql, freezeStartDate, freezeEndDate, companyId).Exec()
  1269. if err != nil {
  1270. return err
  1271. }
  1272. return
  1273. }
  1274. func ModifyCompanyGroupId(sellerId, groupId int) (err error) {
  1275. o := orm.NewOrm()
  1276. sql := `UPDATE company_product SET group_id=? WHERE seller_id=? `
  1277. _, err = o.Raw(sql, groupId, sellerId).Exec()
  1278. return
  1279. }
  1280. func ModifyCompanyDepartmentId(sellerId, departmentId int) (err error) {
  1281. o := orm.NewOrm()
  1282. sql := `UPDATE company_product SET department_id=? WHERE seller_id=? `
  1283. _, err = o.Raw(sql, departmentId, sellerId).Exec()
  1284. return
  1285. }
  1286. // CompanyTryOutReasonReq 编辑客户未续约情况说明请求参数
  1287. type CompanyTryOutReasonReq struct {
  1288. CompanyId int `description:"客户id"`
  1289. ProductId int `description:"客户产品id:ficc:1;权益:2"`
  1290. Remark string `description:"备注"`
  1291. }
  1292. // RenewalReasonReq 新增客户未续约情况说明请求参数
  1293. type RenewalReasonReq struct {
  1294. CompanyId int `description:"客户id"`
  1295. ProductId int `description:"客户产品id:ficc:1;权益:2"`
  1296. Remark string `description:"未续约理由"`
  1297. Renew int `description:"是否勾选不再跟进 1:勾选,0:未勾选"`
  1298. RenewalTodo string `description:"待办事项说明"`
  1299. }
  1300. // TryOutReq 新增客户未续约情况说明请求参数
  1301. type TryOutReq struct {
  1302. CompanyId int `description:"客户id"`
  1303. ProductId int `description:"客户产品id:ficc:1;权益:2"`
  1304. OperationId int `description:"operation record id"`
  1305. Remark string `description:"未续约理由"`
  1306. Renew int `description:"是否勾选不再跟进 1:勾选,0:未勾选"`
  1307. RenewalTodo string `description:"待办事项说明"`
  1308. }
  1309. func GetFiccReportListByMobile(mobiles string) (items []*ViewReportList, err error) {
  1310. dataName := ""
  1311. sql := ``
  1312. if utils.RunMode == "debug" {
  1313. dataName = "test_v2_hongze_rddp"
  1314. } else {
  1315. dataName = "hongze_rddp"
  1316. }
  1317. ficcSql := `SELECT
  1318. rr.research_report_name,
  1319. rr.type AS report_type,
  1320. 'ficc' AS txt_type,
  1321. '--' AS match_type_name,
  1322. '--' AS stop_time,
  1323. uvh.created_time AS created_time
  1324. FROM
  1325. user_view_history uvh
  1326. LEFT JOIN research_report rr ON rr.research_report_id = uvh.research_report_id
  1327. WHERE
  1328. uvh.mobile in (` + mobiles + `)
  1329. UNION ALL
  1330. SELECT
  1331. r.title AS research_report_name,
  1332. 'rddp' AS report_type,
  1333. 'ficc' AS txt_type,
  1334. r.classify_name_first AS match_type_name,
  1335. '--' AS stop_time,
  1336. rvr.create_time AS created_time
  1337. FROM %s.report_view_record rvr
  1338. LEFT JOIN %s.report r ON r.id = rvr.report_id
  1339. WHERE
  1340. rvr.mobile in (` + mobiles + `)
  1341. UNION ALL
  1342. SELECT
  1343. cha.permission_name AS research_report_name,
  1344. 'advisory' AS report_type,
  1345. 'ficc' AS txt_type,
  1346. cha.classify_name AS match_type_name,
  1347. '--' AS stop_time,
  1348. auc.create_time AS created_time
  1349. FROM
  1350. advisory_user_chart_article_record auc
  1351. LEFT JOIN chart_permission cha ON cha.chart_permission_id = auc.chart_permission_id
  1352. WHERE
  1353. auc.mobile in (` + mobiles + `)`
  1354. sql = ` SELECT * FROM ( ` + ficcSql + `
  1355. )AS t ORDER BY t.created_time DESC`
  1356. o := orm.NewOrm()
  1357. sql = fmt.Sprintf(sql, dataName, dataName)
  1358. _, err = o.Raw(sql).QueryRows(&items)
  1359. return
  1360. }
  1361. func GetRightsReportListByMobile(mobiles string) (items []*ViewReportList, err error) {
  1362. rightsSql := `SELECT
  1363. art.title AS research_report_name,
  1364. 'cygx' AS report_type,
  1365. 'rights' AS txt_type,
  1366. art.match_type_name,
  1367. h.stop_time,
  1368. h.create_time AS created_time
  1369. FROM
  1370. cygx_article_history_record_newpv h
  1371. LEFT JOIN cygx_article art ON art.article_id = h.article_id
  1372. WHERE
  1373. h.mobile in (` + mobiles + `) AND h.company_id != 16 `
  1374. sql := ` SELECT * FROM ( ` + rightsSql + `
  1375. )AS t ORDER BY t.created_time DESC`
  1376. o := orm.NewOrm()
  1377. _, err = o.Raw(sql).QueryRows(&items)
  1378. return
  1379. }
  1380. func GetFiccReportListByEmail(emails string) (items []*ViewReportList, err error) {
  1381. dataName := ""
  1382. sql := ``
  1383. if utils.RunMode == "debug" {
  1384. dataName = "test_v2_hongze_rddp"
  1385. } else {
  1386. dataName = "hongze_rddp"
  1387. }
  1388. ficcSql := `SELECT
  1389. rr.research_report_name,
  1390. rr.type AS report_type,
  1391. 'ficc' AS txt_type,
  1392. '--' AS match_type_name,
  1393. '--' AS stop_time,
  1394. uvh.created_time AS created_time
  1395. FROM
  1396. user_view_history uvh
  1397. INNER JOIN research_report rr ON rr.research_report_id = uvh.research_report_id
  1398. WHERE
  1399. uvh.email in (` + emails + `)
  1400. UNION ALL
  1401. SELECT
  1402. r.title AS research_report_name,
  1403. 'rddp' AS report_type,
  1404. 'ficc' AS txt_type,
  1405. r.classify_name_first AS match_type_name,
  1406. '--' AS stop_time,
  1407. rvr.create_time AS created_time
  1408. FROM %s.report_view_record rvr
  1409. INNER JOIN %s.report r ON r.id = rvr.report_id
  1410. WHERE
  1411. rvr.email in (` + emails + `)
  1412. UNION ALL
  1413. SELECT
  1414. cha.permission_name AS research_report_name,
  1415. 'advisory' AS report_type,
  1416. 'ficc' AS txt_type,
  1417. cha.classify_name AS match_type_name,
  1418. '--' AS stop_time,
  1419. auc.create_time AS created_time
  1420. FROM
  1421. advisory_user_chart_article_record auc
  1422. INNER JOIN chart_permission cha ON cha.chart_permission_id = auc.chart_permission_id
  1423. WHERE
  1424. auc.email in (` + emails + `) `
  1425. sql = ` SELECT * FROM ( ` + ficcSql + `
  1426. )AS t ORDER BY t.created_time DESC`
  1427. o := orm.NewOrm()
  1428. sql = fmt.Sprintf(sql, dataName, dataName)
  1429. _, err = o.Raw(sql).QueryRows(&items)
  1430. return
  1431. }
  1432. func GetRightsReportListByEmail(emails string) (items []*ViewReportList, err error) {
  1433. rightsSql := `SELECT
  1434. art.title AS research_report_name,
  1435. 'cygx' AS report_type,
  1436. 'rights' AS txt_type,
  1437. art.match_type_name,
  1438. h.stop_time,
  1439. h.create_time AS created_time
  1440. FROM
  1441. cygx_article_history_record_newpv h
  1442. INNER JOIN cygx_article art ON art.article_id = h.article_id
  1443. WHERE
  1444. h.email in (` + emails + `) AND h.company_id != 16 `
  1445. sql := ` SELECT * FROM ( ` + rightsSql + `
  1446. )AS t ORDER BY t.created_time DESC`
  1447. o := orm.NewOrm()
  1448. _, err = o.Raw(sql).QueryRows(&items)
  1449. return
  1450. }
  1451. // ModifyCompanyProductLastViewData 修改客户产品的总共阅读次数以及最近阅读时间
  1452. func ModifyCompanyProductLastViewData(companyId, productId, viewTotal int, lastViewTime string) (err error) {
  1453. o := orm.NewOrm()
  1454. sql := `UPDATE company_product SET view_total=?,last_view_time=? WHERE company_id=? AND product_id=? `
  1455. _, err = o.Raw(sql, viewTotal, lastViewTime, companyId, productId).Exec()
  1456. if err != nil {
  1457. return err
  1458. }
  1459. return
  1460. }
  1461. // RemarkReq 新增备注请求
  1462. type RemarkReq struct {
  1463. CompanyId int `description:"客户id"`
  1464. Remark string `description:"备注"`
  1465. }
  1466. // RemarkDelReq 删除备注请求
  1467. type RemarkDelReq struct {
  1468. RemarkId int `description:"备注ID"`
  1469. }
  1470. // RemarkResp 备注说明
  1471. type RemarkResp struct {
  1472. RemarkId int `description:"备注ID"`
  1473. Remark string `description:"未续约说明"`
  1474. ProductName string `description:"客户产品:ficc:权益:"`
  1475. CreateTime string `description:"添加时间"`
  1476. ButtonDel bool `description:"删除权限"`
  1477. }
  1478. type RoadShowTotalSlice struct {
  1479. Total int `description:"总路演数"`
  1480. CompanyId int `description:"用户id"`
  1481. }
  1482. // 根据用户id字符串获取企业用户的路演总次数
  1483. func GetRoadShowTotal(companyIds, date, endTime string) (items []*RoadShowTotalSlice, err error) {
  1484. //如果companyIds是空串,那么直接返回
  1485. if companyIds == "" {
  1486. return
  1487. }
  1488. o := orm.NewOrm()
  1489. sql := `SELECT
  1490. COUNT(1) total,
  1491. t.company_id
  1492. FROM
  1493. (
  1494. SELECT
  1495. a.rs_calendar_id,
  1496. a.company_id
  1497. FROM
  1498. rs_calendar AS a
  1499. INNER JOIN rs_calendar_researcher AS b ON a.rs_calendar_id = b.rs_calendar_id
  1500. WHERE
  1501. a.company_id in (` + companyIds + `)
  1502. AND a.activity_type = '路演'
  1503. AND b.status = 2
  1504. AND b.end_date <= ?
  1505. AND b.end_time < ?
  1506. ) AS t
  1507. GROUP BY
  1508. company_id`
  1509. _, err = o.Raw(sql, date, endTime).QueryRows(&items)
  1510. return
  1511. }
  1512. type RoadShowCalendar struct {
  1513. RsCalendarId int `orm:"column(rs_calendar_id);pk"`
  1514. SysUserId int `description:"创建人id"`
  1515. SysUserRealName string `description:"创建人名称"`
  1516. ActivityType string `description:"活动类型"`
  1517. RoadshowType string `description:"路演形式"`
  1518. RoadshowPlatform string `description:"路演平台"`
  1519. CompanyId int `description:"客户id"`
  1520. CompanyName string `description:"客户名称"`
  1521. Province string `description:"省"`
  1522. ProvinceCode string `description:"省编码"`
  1523. City string `description:"市"`
  1524. CityCode string `description:"市编码"`
  1525. Theme string `description:"会议主题"`
  1526. CooperationName string `description:"合作方名称"`
  1527. Title string `description:"展示在日历的标题"`
  1528. Source int8 `description:"来源,0:自系统,1:上海方的"`
  1529. CreateTime time.Time
  1530. ModifyTime time.Time
  1531. ActivityCategory string `description:"活动类别"`
  1532. IsSynced int `description:"是否与上海同步 0:未同步 1:已同步"`
  1533. EndDate time.Time
  1534. }
  1535. func GetRoadShowList(companyId, englishCompany int) (items []*RoadShowCalendar, err error) {
  1536. o := orm.NewOrm()
  1537. sql := `SELECT
  1538. a.*,b.end_date
  1539. FROM
  1540. rs_calendar AS a
  1541. INNER JOIN rs_calendar_researcher AS b ON a.rs_calendar_id = b.rs_calendar_id
  1542. WHERE
  1543. a.source = 0
  1544. AND a.company_id=?
  1545. AND b.status = 2
  1546. AND a.english_company=?
  1547. AND NOW()> DATE_FORMAT( CONCAT( end_date, " ", end_time ), '%Y-%m-%d %H:%i:%S' )
  1548. GROUP BY rs_calendar_id ORDER BY create_time DESC `
  1549. _, err = o.Raw(sql, companyId, englishCompany).QueryRows(&items)
  1550. return
  1551. }
  1552. type RoadShowListResp struct {
  1553. SysUserRealName string `description:"对接销售"`
  1554. RoadshowType string `description:"路演形式"`
  1555. RoadshowPlatform string `description:"路演平台:线上时为平台,线下时为省市"`
  1556. RoadShowTime string `description:"路演时间"`
  1557. Researchers string `description:"研究员"`
  1558. }
  1559. func GetRoadShowResearchers(calendarId int) (items []*string, err error) {
  1560. o := orm.NewOrm()
  1561. sql := `SELECT researcher_name FROM rs_calendar_researcher WHERE rs_calendar_id=? AND status=2 AND NOW()> DATE_FORMAT( CONCAT( end_date, " ", end_time ), '%Y-%m-%d %H:%i:%S' ) `
  1562. _, err = o.Raw(sql, calendarId).QueryRows(&items)
  1563. return
  1564. }
  1565. type CompanyNameAndId struct {
  1566. CompanyId int `description:"公司id"`
  1567. CompanyName string `description:"公司名称"`
  1568. }
  1569. type CompanyNameAndIdListResp struct {
  1570. List []*CompanyNameAndId
  1571. }
  1572. // GetCompanyIdListByproductName 获取不同种类不同状态下的公司ID
  1573. func GetCompanyIdListByproductName(condition string, pars []interface{}) (items []*CompanyNameAndId, err error) {
  1574. o := orm.NewOrm()
  1575. sql := `SELECT
  1576. a.company_id,a.company_name
  1577. FROM
  1578. company AS a
  1579. INNER JOIN company_product AS b ON a.company_id = b.company_id
  1580. WHERE
  1581. a.enabled = 1 `
  1582. if condition != `` {
  1583. sql += condition
  1584. }
  1585. _, err = o.Raw(sql, pars).QueryRows(&items)
  1586. return
  1587. }
  1588. // CompanyTryOutDayListResp 客户累计试用天数
  1589. type CompanyTryOutDayListResp struct {
  1590. Paging *paging.PagingItem
  1591. List []*CompanyProductTryOutUpdateLog
  1592. }
  1593. // 共享客户
  1594. type CompanyShareReq struct {
  1595. CompanyId int `description:"客户id"`
  1596. IsShare int `description:"0取消共享 1共享"`
  1597. }
  1598. func GetShareCompanyListCount(condition string, pars []interface{}) (count int, err error) {
  1599. o := orm.NewOrm()
  1600. sql := `SELECT COUNT(DISTINCT b.company_id) AS count
  1601. FROM company AS a
  1602. INNER JOIN company_product AS b ON a.company_id=b.company_id
  1603. WHERE a.enabled=1 `
  1604. if condition != "" {
  1605. sql += condition
  1606. }
  1607. err = o.Raw(sql, pars).QueryRow(&count)
  1608. return
  1609. }
  1610. func GetShareCompanyList(condition, sortStr string, pars []interface{}, startSize, pageSize int) (items []*CompanyItem, err error) {
  1611. o := orm.NewOrm()
  1612. // 七天前的时间-用于查询周阅读活跃, 此处的需求是
  1613. // FICC客户、共享客户: 以FICC最后一次阅读时间作为参考, 七日内有阅读量则为活跃
  1614. // 权益客户: 以权益最后一次阅读时间作为参考
  1615. t := time.Now().Local().AddDate(0, 0, -7).Format(utils.FormatDate)
  1616. sql := `SELECT a.company_id,a.company_name,a.credit_code,a.company_code,a.created_time,a.province,a.city,
  1617. a.address,a.region_type,b.group_id,b.road_show_total,b.is_share,b.share_seller_id,b.share_seller,b.is_user_maker,
  1618. CASE
  1619. WHEN GROUP_CONCAT(b.status) LIKE "%永续%" THEN 1
  1620. WHEN GROUP_CONCAT(b.status) LIKE "%正式%" THEN 2
  1621. WHEN GROUP_CONCAT(b.status) LIKE "%试用%" THEN 3
  1622. WHEN GROUP_CONCAT(b.status) LIKE "%冻结%" THEN 4
  1623. WHEN GROUP_CONCAT(b.status) LIKE "%流失%" THEN 5
  1624. ELSE 10 END
  1625. as sort_status,
  1626. CASE
  1627. WHEN GROUP_CONCAT(b.try_stage) LIKE "%2%" THEN 1
  1628. WHEN GROUP_CONCAT(b.try_stage) LIKE "%3%" THEN 2
  1629. WHEN GROUP_CONCAT(b.try_stage) LIKE "%4%" THEN 3
  1630. WHEN GROUP_CONCAT(b.try_stage) LIKE "%1%" THEN 4
  1631. ELSE 10 END
  1632. as sort_try_stage,
  1633. CASE
  1634. WHEN GROUP_CONCAT(b.todo_status) LIKE "%未完成%" AND GROUP_CONCAT(b.todo_end_time) is not null THEN GROUP_CONCAT(b.todo_end_time)
  1635. ELSE "9999-01-01" END
  1636. as sort_todo_end_time,
  1637. GROUP_CONCAT(CASE
  1638. WHEN b.is_share = 1 THEN CONCAT(b.status, '(共享)')
  1639. ELSE b.status
  1640. END ORDER BY b.product_id ASC SEPARATOR '/') AS status,
  1641. GROUP_CONCAT(DISTINCT b.seller_name ORDER BY b.product_id ASC SEPARATOR '/') AS seller_name,
  1642. GROUP_CONCAT(DISTINCT b.seller_id ORDER BY b.product_id ASC SEPARATOR '/') AS seller_ids,
  1643. GROUP_CONCAT(DISTINCT b.package_type ORDER BY b.product_id ASC SEPARATOR '/') AS package_types,
  1644. GROUP_CONCAT(DISTINCT b.industry_name ORDER BY b.product_id ASC SEPARATOR '/') AS industry_name,
  1645. GROUP_CONCAT(DISTINCT b.product_name ORDER BY b.product_id ASC SEPARATOR '/') AS company_type,
  1646. GROUP_CONCAT(DISTINCT b.product_id ORDER BY b.product_id ASC SEPARATOR '/') AS product_ids,
  1647. GROUP_CONCAT(DISTINCT b.view_total ORDER BY b.product_id ASC SEPARATOR '/') AS view_totals,
  1648. GROUP_CONCAT(DISTINCT b.last_view_time ORDER BY b.product_id ASC SEPARATOR '/') AS last_view_times,
  1649. GROUP_CONCAT(b.start_date ORDER BY b.product_id ASC SEPARATOR '/') AS start_date,
  1650. GROUP_CONCAT(b.end_date ORDER BY b.product_id ASC SEPARATOR '/') AS end_date,
  1651. GROUP_CONCAT(DISTINCT b.loss_time ORDER BY b.product_id ASC SEPARATOR '/') AS loss_time,
  1652. GROUP_CONCAT(DISTINCT b.lose_reason ORDER BY b.product_id ASC SEPARATOR '/') AS lose_reason,
  1653. GROUP_CONCAT(DISTINCT b.source ORDER BY b.product_id ASC SEPARATOR '/') AS source,
  1654. GROUP_CONCAT(DISTINCT b.reasons ORDER BY b.product_id ASC SEPARATOR '/') AS reasons,
  1655. GROUP_CONCAT(DISTINCT b.approve_status ORDER BY b.product_id ASC SEPARATOR '/') AS approve_status,
  1656. GROUP_CONCAT(b.try_stage ORDER BY b.product_id ASC SEPARATOR '/') AS try_stage_str,
  1657. GROUP_CONCAT(DISTINCT b.group_id ORDER BY b.product_id ASC SEPARATOR '/') AS group_ids,
  1658. GROUP_CONCAT(DISTINCT b.todo_status ORDER BY b.product_id ASC SEPARATOR '/') AS todo_statuss,
  1659. GROUP_CONCAT(DISTINCT b.todo_end_time ORDER BY b.product_id ASC SEPARATOR '/') AS todo_end_time_str,
  1660. GROUP_CONCAT(DISTINCT b.try_out_day_total ORDER BY b.product_id ASC SEPARATOR '/') AS try_out_day_totals,
  1661. b.is_suspend,
  1662. b.freeze_start_date,
  1663. b.freeze_end_date,
  1664. b.seller_id,
  1665. b.product_id,
  1666. b.formal_time,
  1667. b.freeze_time,b.freeze_reason,b.renewal_reason,
  1668. b.view_total,
  1669. b.last_view_time,
  1670. GROUP_CONCAT(DISTINCT b.share_seller ORDER BY b.product_id ASC SEPARATOR '/') AS share_seller,
  1671. GROUP_CONCAT(DISTINCT b.share_seller_id ORDER BY b.product_id ASC SEPARATOR ',') AS share_seller_ids,
  1672. b.share_seller_id ,
  1673. MAX(b.last_view_time) as max_last_view_time,
  1674. MIN(b.last_view_time) as min_last_view_time,
  1675. MAX(b.end_date) as max_end_date,
  1676. MIN(b.end_date) as min_end_date,
  1677. SUM(b.view_total) as all_view_total,
  1678. SUM(b.try_out_day_total) as all_try_out_day_total,
  1679. IF(LEFT(GROUP_CONCAT(DISTINCT b.last_view_time ORDER BY b.product_id), 19) >= "` + t + `", 1, 0) AS week_view_active,
  1680. (SELECT MAX(create_time) FROM company_service_record WHERE company_id = a.company_id) AS last_service_time,
  1681. (SELECT COUNT(company_service_record_id) FROM company_service_record WHERE company_id = a.company_id) AS service_times
  1682. FROM company AS a
  1683. INNER JOIN company_product AS b ON a.company_id=b.company_id
  1684. WHERE a.enabled=1 `
  1685. if condition != "" {
  1686. sql += condition
  1687. }
  1688. //sortStr := ``
  1689. //如果没有传入排序字段,那么按照状态排序
  1690. if sortStr == "" {
  1691. sortStr = " ORDER BY a.created_time DESC "
  1692. }
  1693. sql += ` GROUP BY a.company_id ` + sortStr + ` LIMIT ?,? `
  1694. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  1695. return
  1696. }
  1697. // 分配共享销售
  1698. type MoveShareSellerReq struct {
  1699. CompanyId int `description:"客户id"`
  1700. SellsId int `description:"销售员id"`
  1701. }
  1702. // ExportUserCompanyStatus 联系人导出客户状态
  1703. type ExportUserCompanyStatus struct {
  1704. CompanyId int `description:"客户ID"`
  1705. CompanyName string `description:"客户名称"`
  1706. CompanyStatus string `description:"客户状态: FICC状态/权益状态"`
  1707. }
  1708. // GetExportUserCompanyStatus 获取联系人导出客户状态
  1709. func GetExportUserCompanyStatus(condition string, pars []interface{}) (items []*ExportUserCompanyStatus, err error) {
  1710. o := orm.NewOrm()
  1711. sql := `SELECT
  1712. a.company_id,
  1713. a.company_name,
  1714. GROUP_CONCAT(b.status SEPARATOR '/') AS company_status
  1715. FROM
  1716. company AS a
  1717. INNER JOIN company_product AS b ON a.company_id = b.company_id
  1718. WHERE 1 = 1 `
  1719. if condition != "" {
  1720. sql += condition
  1721. }
  1722. sql += ` GROUP BY a.company_id `
  1723. _, err = o.Raw(sql, pars).QueryRows(&items)
  1724. return
  1725. }
  1726. func GetExportUserByProductId(companyId string, productId int) (items []*ExportUser, err error) {
  1727. o := orm.NewOrm()
  1728. sql := `SELECT a.real_name,a.mobile,a.email,a.position,a.is_maker,department_name,a.company_id,b.subscribe AS is_subscribe
  1729. FROM wx_user AS a
  1730. LEFT JOIN (SELECT * FROM user_record WHERE create_platform = 1 GROUP BY user_id) AS b ON a.user_id = b.user_id
  1731. LEFT JOIN user_seller_relation AS c ON a.user_id = c.user_id
  1732. WHERE a.company_id<>1 AND a.company_id IN(` + companyId + `) AND c.product_id = ?
  1733. GROUP BY a.user_id ORDER BY a.company_id ASC `
  1734. _, err = o.Raw(sql, productId).QueryRows(&items)
  1735. return
  1736. }
  1737. // CloseCompanyReq 关闭客户请求体
  1738. type CloseCompanyReq struct {
  1739. CompanyId int `description:"客户id"`
  1740. Remark string `description:"备注"`
  1741. }
  1742. // CloseCompany 关闭客户
  1743. func CloseCompany(companyId, productId int, reason string) (err error) {
  1744. o := orm.NewOrm()
  1745. tx, e := o.Begin()
  1746. if e != nil {
  1747. err = fmt.Errorf("orm begin err: %s", e.Error())
  1748. return
  1749. }
  1750. defer func() {
  1751. if err != nil {
  1752. _ = tx.Rollback()
  1753. return
  1754. }
  1755. _ = tx.Commit()
  1756. }()
  1757. // 修改产品状态
  1758. sql := `UPDATE company_product SET status = '关闭', is_formal = 0, close_reason = ?, close_time = NOW(), modify_time=NOW(), try_stage = 1, todo_status = "无任务", todo_create_time = null, todo_approve_time = null, todo_modify_time = null, todo_end_time = null WHERE company_id = ? AND product_id = ?`
  1759. _, e = tx.Raw(sql, reason, companyId, productId).Exec()
  1760. if e != nil {
  1761. err = fmt.Errorf("update company product err: %s", e.Error())
  1762. return
  1763. }
  1764. // 修改产品权限
  1765. sql = `UPDATE company_report_permission SET status='关闭', modify_time=NOW() WHERE company_id = ? AND product_id = ?`
  1766. _, e = tx.Raw(sql, companyId, productId).Exec()
  1767. if e != nil {
  1768. err = fmt.Errorf("update company report permission err: %s", e.Error())
  1769. return
  1770. }
  1771. // 将历史的任务给标记删除掉
  1772. sql = `UPDATE company_todo SET is_delete = 1, modify_time=NOW() WHERE company_id = ? AND product_id = ? AND is_delete = 0`
  1773. _, e = tx.Raw(sql, companyId, productId).Exec()
  1774. if e != nil {
  1775. err = fmt.Errorf("update company todo err: %s", e.Error())
  1776. return
  1777. }
  1778. return
  1779. }
  1780. // Close2LossReq 关闭转流失请求体
  1781. type Close2LossReq struct {
  1782. CompanyId int `description:"客户id"`
  1783. }
  1784. // Close2Loss 关闭转流失
  1785. func Close2Loss(companyId, productId int) (err error) {
  1786. o := orm.NewOrm()
  1787. sql := `UPDATE company_product SET status = '流失', is_formal=0, loss_time = NOW(), modify_time = NOW(), lose_reason = '关闭转流失', try_stage = 1, todo_status = "无任务", todo_create_time = null, todo_approve_time = null, todo_modify_time = null, todo_end_time = null, close_reason = '', close_time = null WHERE company_id = ? AND product_id = ?`
  1788. _, err = o.Raw(sql, companyId, productId).Exec()
  1789. return
  1790. }
  1791. // CompanyMarkReq 标记请求
  1792. type CompanyMarkReq struct {
  1793. CompanyServiceRecordId int `description:"服务记录ID"`
  1794. Status int `description:"0取消标记 1标记"`
  1795. }
  1796. // GetCompanyListByCondition 获取客户列表
  1797. func GetCompanyListByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*Company, err error) {
  1798. o := orm.NewOrm()
  1799. fields := strings.Join(fieldArr, ",")
  1800. if len(fieldArr) == 0 {
  1801. fields = `*`
  1802. }
  1803. order := `ORDER BY created_time DESC`
  1804. if orderRule != "" {
  1805. order = ` ORDER BY ` + orderRule
  1806. }
  1807. sql := fmt.Sprintf(`SELECT %s FROM company WHERE 1=1 %s %s`, fields, condition, order)
  1808. _, err = o.Raw(sql, pars).QueryRows(&items)
  1809. return
  1810. }
  1811. func GetOverseasCompanys() (items []*Company, err error) {
  1812. o := orm.NewOrm()
  1813. sql := ` SELECT * FROM company where region_type= '海外' `
  1814. _, err = o.Raw(sql).QueryRows(&items)
  1815. return
  1816. }
  1817. // GetCompanyListByCompanyId 根据公司ID获取公司信息
  1818. func GetCompanyListByCompanyId(companyId []int) (items []*Company, err error) {
  1819. lenArr := len(companyId)
  1820. if lenArr == 0 {
  1821. return
  1822. }
  1823. o := orm.NewOrm()
  1824. sql := `SELECT interaction_num , company_id FROM company AS a WHERE a.company_id IN (` + utils.GetOrmInReplace(lenArr) + `) `
  1825. _, err = o.Raw(sql, companyId).QueryRows(&items)
  1826. return
  1827. }