company.go 82 KB

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