company.go 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  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. if productId == utils.COMPANY_PRODUCT_RAI_ID { // 权益客户试用期改为1个月。 需求池 1035
  911. endDateTime = time.Now().AddDate(0, 1, 0)
  912. } else {
  913. endDateTime = time.Now().AddDate(0, 2, 0)
  914. }
  915. startDate := startDateTime.Format(utils.FormatDate)
  916. endDate := endDateTime.Format(utils.FormatDate)
  917. //更改 客户状态为 试用
  918. sql := `UPDATE company SET start_date=?,end_date=?,last_updated_time=NOW() WHERE company_id=? `
  919. _, err = to.Raw(sql, startDate, endDate, companyId).Exec()
  920. if err != nil {
  921. return
  922. }
  923. //更改 客户产品 状态为 试用
  924. //重置tryout时间,上次修改未续约说明时间
  925. 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=? `
  926. _, err = to.Raw(sql, startDate, endDate, sellerId, sellerName, groupId, departmentId, companyId, productId).Exec()
  927. if err != nil {
  928. return
  929. }
  930. //修改原有的 客户产品权限 的服务时间
  931. items = make([]*CompanyReportPermission, 0)
  932. sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
  933. _, err = to.Raw(sql, companyId, productId).QueryRows(&items)
  934. for _, pv := range items {
  935. cpLog := new(CompanyPermissionLog)
  936. cpLog.CompanyId = companyId
  937. cpLog.ChartPermissionId = pv.ChartPermissionId
  938. cpLog.CreateTime = time.Now()
  939. cpLog.SysUserId = sellerId
  940. cpLog.SysUserName = sellerName
  941. cpLog.StartDate = pv.StartDate
  942. cpLog.EndDate = pv.EndDate
  943. cpLog.ProductId = productId
  944. cpLog.ProductName = pv.ProductName
  945. go AddCompanyPermissionLog(cpLog)
  946. }
  947. sql = `UPDATE company_report_permission SET status='试用',start_date=?,end_date=?,modify_time=NOW() WHERE company_id=? AND product_id=? `
  948. _, err = to.Raw(sql, startDate, endDate, companyId, productId).Exec()
  949. //修改 该联系人 与 当前销售的关系
  950. sql = `UPDATE user_seller_relation SET seller_id=?,seller = ? ,modify_time=NOW() WHERE company_id = ? AND product_id=?`
  951. _, err = to.Raw(sql, sellerId, sellerName, companyId, productId).Exec()
  952. //如果是权益客户,则把对应的主观客观一起开通
  953. if productId == 2 {
  954. //获取公司现有的权限,并通过现有的权限找出对应的主观客观权限
  955. oldPermissionId, tmpErr := GetCompanyPermissionIdById(companyId)
  956. if tmpErr != nil {
  957. err = tmpErr
  958. return
  959. }
  960. newPermissionId, tmpErr := GetPermissionIdById(oldPermissionId)
  961. if tmpErr != nil {
  962. err = tmpErr
  963. return
  964. }
  965. permissionArr := strings.Split(newPermissionId, ",")
  966. for _, v := range permissionArr {
  967. permissionId, tmpErr := strconv.Atoi(v)
  968. if tmpErr != nil {
  969. err = tmpErr
  970. return
  971. }
  972. //判断这个权限是否存在,如果不存在则新增
  973. count, tmpErr := GetCompanyPermissionCheck(companyId, permissionId)
  974. if tmpErr != nil {
  975. err = tmpErr
  976. return
  977. }
  978. if count == 0 {
  979. permission := new(CompanyReportPermission)
  980. permission.CompanyId = companyId
  981. permission.ReportPermissionId = permissionId
  982. permission.CreatedTime = time.Now()
  983. permission.LastUpdatedTime = time.Now()
  984. permission.ChartPermissionId = permissionId
  985. permission.StartDate = startDate
  986. permission.EndDate = endDate
  987. permission.ProductId = productId
  988. permission.ProductName = "权益"
  989. permission.ModifyTime = time.Now()
  990. permission.Status = "试用"
  991. err = AddCompanyReportPermission(permission)
  992. if err != nil {
  993. return
  994. }
  995. items = append(items, permission)
  996. }
  997. }
  998. }
  999. // 将历史的任务给标记删除掉
  1000. sql = `UPDATE company_todo SET is_delete=1,modify_time=NOW() WHERE company_id=? AND product_id=? AND is_delete = 0 `
  1001. _, err = to.Raw(sql, companyId, productId).Exec()
  1002. if err != nil {
  1003. return
  1004. }
  1005. return
  1006. }
  1007. func GetExportCompanyIds(condition string, pars []interface{}) (company_id string, err error) {
  1008. o := orm.NewOrm()
  1009. sql := `SELECT
  1010. GROUP_CONCAT(DISTINCT b.company_id SEPARATOR ',') AS company_id
  1011. FROM company AS a
  1012. INNER JOIN company_product AS b ON a.company_id=b.company_id
  1013. LEFT JOIN wx_user AS c ON a.company_id=c.company_id
  1014. WHERE 1=1 and a.enabled=1 `
  1015. if condition != "" {
  1016. sql += condition
  1017. }
  1018. err = o.Raw(sql, pars).QueryRow(&company_id)
  1019. return
  1020. }
  1021. type ExportUser struct {
  1022. RealName string
  1023. Mobile string
  1024. Email string
  1025. Position string
  1026. IsMaker int
  1027. DepartmentName string
  1028. CompanyId int
  1029. IsSubscribe int `description:"是否关注了公众号(弘则研究)"`
  1030. }
  1031. func GetExportUser(companyId string) (items []*ExportUser, err error) {
  1032. o := orm.NewOrm()
  1033. sql := `SELECT a.real_name,a.mobile,a.email,a.position,a.is_maker,department_name,a.company_id,b.subscribe AS is_subscribe
  1034. FROM wx_user AS a
  1035. LEFT JOIN (SELECT * FROM user_record WHERE create_platform = 1 GROUP BY user_id) AS b ON a.user_id = b.user_id
  1036. WHERE a.company_id<>1 AND a.company_id IN(` + companyId + `)
  1037. GROUP BY a.user_id ORDER BY a.company_id ASC `
  1038. _, err = o.Raw(sql).QueryRows(&items)
  1039. return
  1040. }
  1041. type ExportCompanyProduct struct {
  1042. CompanyName string
  1043. ProductId int
  1044. ProductName string
  1045. IndustryName string
  1046. SellerName string
  1047. Status string
  1048. StartDate time.Time
  1049. EndDate time.Time
  1050. CreateTime string
  1051. Source string
  1052. CreditCode string
  1053. RegionType string
  1054. LossTime string
  1055. IsSuspend int
  1056. }
  1057. func GetExportCompanyProduct(companyId int) (items []*ExportCompanyProduct, err error) {
  1058. o := orm.NewOrm()
  1059. 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,
  1060. b.source,a.credit_code,a.region_type,b.loss_time, b.is_suspend
  1061. FROM company AS a
  1062. INNER JOIN company_product AS b ON a.company_id=b.company_id
  1063. WHERE a.company_id=?
  1064. ORDER BY b.product_id ASC `
  1065. _, err = o.Raw(sql, companyId).QueryRows(&items)
  1066. return
  1067. }
  1068. func GetExportPermissionName(companyId, productId int) (chart_permission_name string, err error) {
  1069. o := orm.NewOrm()
  1070. sql := ` SELECT GROUP_CONCAT(DISTINCT c.permission_name ORDER BY sort ASC SEPARATOR '/') AS chart_permission_name FROM company_product AS a
  1071. INNER JOIN company_report_permission AS b ON a.company_id=b.company_id AND a.product_id=b.product_id
  1072. INNER JOIN chart_permission AS c ON b.chart_permission_id=c.chart_permission_id AND b.product_id=c.product_id
  1073. WHERE a.company_id=? AND a.product_id=? `
  1074. err = o.Raw(sql, companyId, productId).QueryRow(&chart_permission_name)
  1075. return
  1076. }
  1077. // 移动销售
  1078. type SuspendReq struct {
  1079. CompanyId int `description:"客户id"`
  1080. }
  1081. // MoveCompanySeller 移动客户销售
  1082. func MoveCompanySeller(companyId, productId, sellerId, groupId, departmentId int, sellerName string) (err error) {
  1083. o := orm.NewOrm()
  1084. to, err := o.Begin()
  1085. if err != nil {
  1086. return
  1087. }
  1088. defer func() {
  1089. if err != nil {
  1090. _ = to.Rollback()
  1091. } else {
  1092. _ = to.Commit()
  1093. }
  1094. }()
  1095. if sellerName != "" {
  1096. 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=? `
  1097. _, err = to.Raw(sql, sellerId, sellerName, groupId, departmentId, companyId, productId).Exec()
  1098. if err != nil {
  1099. return
  1100. }
  1101. } else {
  1102. 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=? `
  1103. _, err = to.Raw(sql, sellerId, groupId, departmentId, companyId, productId).Exec()
  1104. if err != nil {
  1105. return
  1106. }
  1107. }
  1108. // 将历史的任务给标记删除掉
  1109. sql := `UPDATE company_todo SET is_delete=1,modify_time=NOW() WHERE company_id=? AND product_id=? AND is_delete = 0 `
  1110. _, err = to.Raw(sql, companyId, productId).Exec()
  1111. if err != nil {
  1112. return
  1113. }
  1114. return
  1115. }
  1116. // UpdateCompanyTryStage 更新试用客户标签
  1117. func UpdateCompanyTryStage(companyId, productId, tryStage int) (err error) {
  1118. o := orm.NewOrm()
  1119. sql := `UPDATE company_product SET try_stage=? WHERE company_id=? AND product_id=? and status="试用"`
  1120. _, err = o.Raw(sql, tryStage, companyId, productId).Exec()
  1121. return
  1122. }
  1123. func CheckCompanyProductByName(companyName 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.company_name=?
  1128. AND b.product_id=? `
  1129. err = o.Raw(sql, companyName, productId).QueryRow(&item)
  1130. return
  1131. }
  1132. func CheckCompanyProductById(companyId, productId int) (item *CompanyProductDetail, err error) {
  1133. o := orm.NewOrm()
  1134. sql := ` SELECT * FROM company AS a
  1135. INNER JOIN company_product AS b ON a.company_id=b.company_id
  1136. WHERE a.company_id=?
  1137. AND b.product_id=? `
  1138. err = o.Raw(sql, companyId, productId).QueryRow(&item)
  1139. return
  1140. }
  1141. func CheckCompanyProductByCreditCode(creditCode string, productId int) (item *CompanyProductDetail, err error) {
  1142. o := orm.NewOrm()
  1143. sql := ` SELECT * FROM company AS a
  1144. INNER JOIN company_product AS b ON a.company_id=b.company_id
  1145. WHERE a.credit_code=?
  1146. AND b.product_id=? `
  1147. err = o.Raw(sql, creditCode, productId).QueryRow(&item)
  1148. return
  1149. }
  1150. // 新增客户请求参数
  1151. type CheckCompanyInfoResp struct {
  1152. Status int `description:"检测状态:1:跳转到领取页面,0:不跳转,继续操作"`
  1153. CompanyId int `description:"客户id"`
  1154. ProductName string `description:"产品名称"`
  1155. }
  1156. type CheckCompanyInfoRepeatResp struct {
  1157. Status int `description:"检测状态:1:跳转到领取页面,0:不跳转,继续操作"`
  1158. RepeatStatus int `description:"检测状态:0:无重复 1:信用码重复 2:公司名重复"`
  1159. CompanyId int `description:"客户id"`
  1160. ProductName string `description:"产品名称"`
  1161. }
  1162. type ButtonPermission struct {
  1163. BtnView bool `description:"查看按钮权限:true显示,false不显示"`
  1164. BtnEdit bool `description:"编辑按钮权限:true显示,false不显示"`
  1165. BtnDelete bool `description:"删除按钮权限:true显示,false不显示"`
  1166. BtnSuspend bool `description:"暂停/启用按钮权限:true显示,false不显示"`
  1167. BtnDelay bool `description:"申请延期按钮权限:true显示,false不显示"`
  1168. BtnTurnPositive bool `description:"申请转正按钮权限:true显示,false不显示"`
  1169. BtnUpdate bool `description:"服务更新按钮权限:true显示,false不显示"`
  1170. BtnAddAgreement bool `description:"补充协议按钮权限:true显示,false不显示"`
  1171. BtnThaw bool `description:"申请解冻按钮权限:true显示,false不显示"`
  1172. BtnApplyReceive bool `description:"申请领取按钮权限:true显示,false不显示"`
  1173. BtnReceive bool `description:"领取客户按钮权限:true显示,false不显示"`
  1174. BtnModifySeller bool `description:"修改销售按钮权限:true显示,false不显示"`
  1175. BtnReceiveOther bool `description:"领取其他部门客户按钮权限:true显示,false不显示"`
  1176. BtnFreeze bool `description:"冻结按钮权限:true显示,false不显示"`
  1177. BtnTryOut bool `description:"增开试用:true显示,false不显示"`
  1178. BtnDetail bool `description:"详情按钮:true显示,false不显示"`
  1179. BtnRemarkEdit bool `description:"编辑备注按钮:true显示,false不显示"`
  1180. BtnRemarkView bool `description:"查看备注按钮:true显示,false不显示"`
  1181. BtnRemarkViewHistory bool `description:"查看历史备注按钮:true显示,false不显示"`
  1182. BtnShare bool `description:"设置共享按钮:true显示,false不显示"`
  1183. BtnServiceRecord bool `description:"查看服务记录按钮:true显示,false不显示"`
  1184. BtnClose bool `description:"关闭客户按钮:true显示,false不显示"`
  1185. BtnLoss bool `description:"转流失按钮:true显示,false不显示"`
  1186. IsResearchShow bool `description:"研选详情展示控制:true显示,false不显示"`
  1187. }
  1188. type ButtonSearchPermission struct {
  1189. BtnView bool `description:"查看按钮权限:true显示,false不显示"`
  1190. BtnUpdate bool `description:"服务更新按钮权限:true显示,false不显示"`
  1191. BtnEdit bool `description:"编辑按钮权限:true显示,false不显示"`
  1192. BtnDelete bool `description:"删除按钮权限:true显示,false不显示"`
  1193. BtnSuspend bool `description:"暂停/启用按钮权限:true显示,false不显示"`
  1194. BtnDelay bool `description:"申请延期按钮权限:true显示,false不显示"`
  1195. BtnTurnPositive bool `description:"申请转正按钮权限:true显示,false不显示"`
  1196. BtnThaw bool `description:"申请解冻按钮权限:true显示,false不显示"`
  1197. BtnApplyReceive bool `description:"申请领取按钮权限:true显示,false不显示"`
  1198. BtnReceive bool `description:"领取客户按钮权限:true显示,false不显示"`
  1199. BtnModifySeller bool `description:"修改销售按钮权限:true显示,false不显示"`
  1200. BtnReceiveOther bool `description:"领取其他部门客户按钮权限:true显示,false不显示"`
  1201. }
  1202. type ButtonDetailPermission struct {
  1203. BtnEdit bool `description:"编辑按钮权限:true显示,false不显示"`
  1204. BtnDelete bool `description:"删除按钮权限:true显示,false不显示"`
  1205. BtnHistoryList bool `description:"历史签约按钮权限:true显示,false不显示"`
  1206. BaseInfoEdit bool `description:"客户基础信息编辑操作:true可操作,false不可操作"`
  1207. FiccEdit bool `description:"ficc类型,编辑按钮权限:true显示,false不显示"`
  1208. FiccDelete bool `description:"ficc类型,删除按钮权限:true显示,false不显示"`
  1209. RaiEdit bool `description:"权益类型,编辑按钮权限:true显示,false不显示"`
  1210. RaiDelete bool `description:"权益类型,删除按钮权限:true显示,false不显示"`
  1211. }
  1212. func GetCompanyListExport(condition string, pars []interface{}) (items []*CompanyItem, err error) {
  1213. o := orm.NewOrm()
  1214. sql := `SELECT a.company_id,a.company_name,a.credit_code,a.company_code,a.created_time,a.address,
  1215. GROUP_CONCAT(b.status ORDER BY b.product_id ASC SEPARATOR '/') AS status,
  1216. GROUP_CONCAT(DISTINCT b.seller_name ORDER BY b.product_id ASC SEPARATOR '/') AS seller_name,
  1217. GROUP_CONCAT(DISTINCT b.industry_name ORDER BY b.product_id ASC SEPARATOR '/') AS industry_name,
  1218. GROUP_CONCAT(DISTINCT b.product_name ORDER BY b.product_id ASC SEPARATOR '/') AS company_type,
  1219. GROUP_CONCAT(b.start_date ORDER BY b.product_id ASC SEPARATOR '/') AS start_date,
  1220. GROUP_CONCAT(b.end_date ORDER BY b.product_id ASC SEPARATOR '/') AS end_date,
  1221. GROUP_CONCAT(DISTINCT b.loss_time ORDER BY b.product_id ASC SEPARATOR '/') AS loss_time,
  1222. GROUP_CONCAT(DISTINCT b.lose_reason ORDER BY b.product_id ASC SEPARATOR '/') AS lose_reason,
  1223. GROUP_CONCAT(DISTINCT b.source ORDER BY b.product_id ASC SEPARATOR '/') AS source,
  1224. GROUP_CONCAT(DISTINCT b.reasons ORDER BY b.product_id ASC SEPARATOR '/') AS reasons,
  1225. GROUP_CONCAT(DISTINCT b.approve_status ORDER BY b.product_id ASC SEPARATOR '/') AS approve_status,
  1226. GROUP_CONCAT(DISTINCT b.group_id ORDER BY b.product_id ASC SEPARATOR '/') AS group_id,
  1227. b.is_suspend,
  1228. b.freeze_start_date,
  1229. b.freeze_end_date,
  1230. b.seller_id,
  1231. b.product_id
  1232. FROM company AS a
  1233. INNER JOIN company_product AS b ON a.company_id=b.company_id
  1234. WHERE a.enabled=1 AND a.company_id<>1 `
  1235. if condition != "" {
  1236. sql += condition
  1237. }
  1238. sql += ` GROUP BY a.company_id ORDER BY a.last_updated_time DESC `
  1239. _, err = o.Raw(sql, pars).QueryRows(&items)
  1240. return
  1241. }
  1242. // 冻结客户请求参数
  1243. type CompanyFreezeReq struct {
  1244. CompanyId int `description:"客户id"`
  1245. CompanyType string `description:"客户类型:ficc/权益"`
  1246. Remark string `description:"备注"`
  1247. }
  1248. // 冻结客户
  1249. func CompanyFreeze(companyId, productId int, freezeReason string) (err error) {
  1250. o := orm.NewOrm()
  1251. to, err := o.Begin()
  1252. if err != nil {
  1253. return
  1254. }
  1255. defer func() {
  1256. if err != nil {
  1257. _ = to.Rollback()
  1258. } else {
  1259. _ = to.Commit()
  1260. }
  1261. }()
  1262. freezeStartDate := time.Now().Format(utils.FormatDate)
  1263. freezeEndDate := time.Now().AddDate(0, 3, 0).Format(utils.FormatDate)
  1264. // ficc是六个月的冻结期
  1265. if productId == 1 {
  1266. freezeEndDate = time.Now().AddDate(0, 6, 0).Format(utils.FormatDate)
  1267. }
  1268. // 权益也调整为 两个月的冻结期
  1269. if productId == 2 {
  1270. freezeEndDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
  1271. }
  1272. //修改用户产品状态
  1273. 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=? `
  1274. _, err = to.Raw(sql, freezeStartDate, freezeEndDate, freezeStartDate, freezeEndDate, freezeReason, companyId, productId).Exec()
  1275. if err != nil {
  1276. return err
  1277. }
  1278. //修改产品权限状态为关闭
  1279. sql = `UPDATE company_report_permission SET status='关闭',modify_time=NOW() WHERE company_id= ? AND product_id = ? `
  1280. _, err = to.Raw(sql, companyId, productId).Exec()
  1281. if err != nil {
  1282. return err
  1283. }
  1284. //修改客户为关闭
  1285. sql = `UPDATE company SET type=3,last_updated_time=NOW(),start_date=?,end_date=? WHERE company_id=? `
  1286. _, err = to.Raw(sql, freezeStartDate, freezeEndDate, companyId).Exec()
  1287. if err != nil {
  1288. return err
  1289. }
  1290. return
  1291. }
  1292. func ModifyCompanyGroupId(sellerId, groupId int) (err error) {
  1293. o := orm.NewOrm()
  1294. sql := `UPDATE company_product SET group_id=? WHERE seller_id=? `
  1295. _, err = o.Raw(sql, groupId, sellerId).Exec()
  1296. return
  1297. }
  1298. func ModifyCompanyDepartmentId(sellerId, departmentId int) (err error) {
  1299. o := orm.NewOrm()
  1300. sql := `UPDATE company_product SET department_id=? WHERE seller_id=? `
  1301. _, err = o.Raw(sql, departmentId, sellerId).Exec()
  1302. return
  1303. }
  1304. // CompanyTryOutReasonReq 编辑客户未续约情况说明请求参数
  1305. type CompanyTryOutReasonReq struct {
  1306. CompanyId int `description:"客户id"`
  1307. ProductId int `description:"客户产品id:ficc:1;权益:2"`
  1308. Remark string `description:"备注"`
  1309. }
  1310. // RenewalReasonReq 新增客户未续约情况说明请求参数
  1311. type RenewalReasonReq struct {
  1312. CompanyId int `description:"客户id"`
  1313. ProductId int `description:"客户产品id:ficc:1;权益:2"`
  1314. Remark string `description:"未续约理由"`
  1315. Renew int `description:"是否勾选不再跟进 1:勾选,0:未勾选"`
  1316. RenewalTodo string `description:"待办事项说明"`
  1317. }
  1318. // TryOutReq 新增客户未续约情况说明请求参数
  1319. type TryOutReq struct {
  1320. CompanyId int `description:"客户id"`
  1321. ProductId int `description:"客户产品id:ficc:1;权益:2"`
  1322. OperationId int `description:"operation record id"`
  1323. Remark string `description:"未续约理由"`
  1324. Renew int `description:"是否勾选不再跟进 1:勾选,0:未勾选"`
  1325. RenewalTodo string `description:"待办事项说明"`
  1326. }
  1327. func GetFiccReportListByMobile(mobiles string) (items []*ViewReportList, err error) {
  1328. dataName := ""
  1329. sql := ``
  1330. if utils.RunMode == "debug" {
  1331. dataName = "test_v2_hongze_rddp"
  1332. } else {
  1333. dataName = "hongze_rddp"
  1334. }
  1335. ficcSql := `SELECT
  1336. rr.research_report_name,
  1337. rr.type AS report_type,
  1338. 'ficc' AS txt_type,
  1339. '--' AS match_type_name,
  1340. '--' AS stop_time,
  1341. uvh.created_time AS created_time
  1342. FROM
  1343. user_view_history uvh
  1344. LEFT JOIN research_report rr ON rr.research_report_id = uvh.research_report_id
  1345. WHERE
  1346. uvh.mobile in (` + mobiles + `)
  1347. UNION ALL
  1348. SELECT
  1349. r.title AS research_report_name,
  1350. 'rddp' AS report_type,
  1351. 'ficc' AS txt_type,
  1352. r.classify_name_first AS match_type_name,
  1353. '--' AS stop_time,
  1354. rvr.create_time AS created_time
  1355. FROM %s.report_view_record rvr
  1356. LEFT JOIN %s.report r ON r.id = rvr.report_id
  1357. WHERE
  1358. rvr.mobile in (` + mobiles + `)
  1359. UNION ALL
  1360. SELECT
  1361. cha.permission_name AS research_report_name,
  1362. 'advisory' AS report_type,
  1363. 'ficc' AS txt_type,
  1364. cha.classify_name AS match_type_name,
  1365. '--' AS stop_time,
  1366. auc.create_time AS created_time
  1367. FROM
  1368. advisory_user_chart_article_record auc
  1369. LEFT JOIN chart_permission cha ON cha.chart_permission_id = auc.chart_permission_id
  1370. WHERE
  1371. auc.mobile in (` + mobiles + `)`
  1372. sql = ` SELECT * FROM ( ` + ficcSql + `
  1373. )AS t ORDER BY t.created_time DESC`
  1374. o := orm.NewOrm()
  1375. sql = fmt.Sprintf(sql, dataName, dataName)
  1376. _, err = o.Raw(sql).QueryRows(&items)
  1377. return
  1378. }
  1379. func GetRightsReportListByMobile(mobiles string) (items []*ViewReportList, err error) {
  1380. rightsSql := `SELECT
  1381. art.title AS research_report_name,
  1382. 'cygx' AS report_type,
  1383. 'rights' AS txt_type,
  1384. art.match_type_name,
  1385. h.stop_time,
  1386. h.create_time AS created_time
  1387. FROM
  1388. cygx_article_history_record_newpv h
  1389. LEFT JOIN cygx_article art ON art.article_id = h.article_id
  1390. WHERE
  1391. h.mobile in (` + mobiles + `) AND h.company_id != 16 `
  1392. sql := ` SELECT * FROM ( ` + rightsSql + `
  1393. )AS t ORDER BY t.created_time DESC`
  1394. o := orm.NewOrm()
  1395. _, err = o.Raw(sql).QueryRows(&items)
  1396. return
  1397. }
  1398. func GetFiccReportListByEmail(emails string) (items []*ViewReportList, err error) {
  1399. dataName := ""
  1400. sql := ``
  1401. if utils.RunMode == "debug" {
  1402. dataName = "test_v2_hongze_rddp"
  1403. } else {
  1404. dataName = "hongze_rddp"
  1405. }
  1406. ficcSql := `SELECT
  1407. rr.research_report_name,
  1408. rr.type AS report_type,
  1409. 'ficc' AS txt_type,
  1410. '--' AS match_type_name,
  1411. '--' AS stop_time,
  1412. uvh.created_time AS created_time
  1413. FROM
  1414. user_view_history uvh
  1415. INNER JOIN research_report rr ON rr.research_report_id = uvh.research_report_id
  1416. WHERE
  1417. uvh.email in (` + emails + `)
  1418. UNION ALL
  1419. SELECT
  1420. r.title AS research_report_name,
  1421. 'rddp' AS report_type,
  1422. 'ficc' AS txt_type,
  1423. r.classify_name_first AS match_type_name,
  1424. '--' AS stop_time,
  1425. rvr.create_time AS created_time
  1426. FROM %s.report_view_record rvr
  1427. INNER JOIN %s.report r ON r.id = rvr.report_id
  1428. WHERE
  1429. rvr.email in (` + emails + `)
  1430. UNION ALL
  1431. SELECT
  1432. cha.permission_name AS research_report_name,
  1433. 'advisory' AS report_type,
  1434. 'ficc' AS txt_type,
  1435. cha.classify_name AS match_type_name,
  1436. '--' AS stop_time,
  1437. auc.create_time AS created_time
  1438. FROM
  1439. advisory_user_chart_article_record auc
  1440. INNER JOIN chart_permission cha ON cha.chart_permission_id = auc.chart_permission_id
  1441. WHERE
  1442. auc.email in (` + emails + `) `
  1443. sql = ` SELECT * FROM ( ` + ficcSql + `
  1444. )AS t ORDER BY t.created_time DESC`
  1445. o := orm.NewOrm()
  1446. sql = fmt.Sprintf(sql, dataName, dataName)
  1447. _, err = o.Raw(sql).QueryRows(&items)
  1448. return
  1449. }
  1450. func GetRightsReportListByEmail(emails string) (items []*ViewReportList, err error) {
  1451. rightsSql := `SELECT
  1452. art.title AS research_report_name,
  1453. 'cygx' AS report_type,
  1454. 'rights' AS txt_type,
  1455. art.match_type_name,
  1456. h.stop_time,
  1457. h.create_time AS created_time
  1458. FROM
  1459. cygx_article_history_record_newpv h
  1460. INNER JOIN cygx_article art ON art.article_id = h.article_id
  1461. WHERE
  1462. h.email in (` + emails + `) AND h.company_id != 16 `
  1463. sql := ` SELECT * FROM ( ` + rightsSql + `
  1464. )AS t ORDER BY t.created_time DESC`
  1465. o := orm.NewOrm()
  1466. _, err = o.Raw(sql).QueryRows(&items)
  1467. return
  1468. }
  1469. // ModifyCompanyProductLastViewData 修改客户产品的总共阅读次数以及最近阅读时间
  1470. func ModifyCompanyProductLastViewData(companyId, productId, viewTotal int, lastViewTime string) (err error) {
  1471. o := orm.NewOrm()
  1472. sql := `UPDATE company_product SET view_total=?,last_view_time=? WHERE company_id=? AND product_id=? `
  1473. _, err = o.Raw(sql, viewTotal, lastViewTime, companyId, productId).Exec()
  1474. if err != nil {
  1475. return err
  1476. }
  1477. return
  1478. }
  1479. // RemarkReq 新增备注请求
  1480. type RemarkReq struct {
  1481. CompanyId int `description:"客户id"`
  1482. Remark string `description:"备注"`
  1483. }
  1484. // RemarkDelReq 删除备注请求
  1485. type RemarkDelReq struct {
  1486. RemarkId int `description:"备注ID"`
  1487. }
  1488. // RemarkResp 备注说明
  1489. type RemarkResp struct {
  1490. RemarkId int `description:"备注ID"`
  1491. Remark string `description:"未续约说明"`
  1492. ProductName string `description:"客户产品:ficc:权益:"`
  1493. CreateTime string `description:"添加时间"`
  1494. ButtonDel bool `description:"删除权限"`
  1495. }
  1496. type RoadShowTotalSlice struct {
  1497. Total int `description:"总路演数"`
  1498. CompanyId int `description:"用户id"`
  1499. }
  1500. // 根据用户id字符串获取企业用户的路演总次数
  1501. func GetRoadShowTotal(companyIds, date, endTime string) (items []*RoadShowTotalSlice, err error) {
  1502. //如果companyIds是空串,那么直接返回
  1503. if companyIds == "" {
  1504. return
  1505. }
  1506. o := orm.NewOrm()
  1507. sql := `SELECT
  1508. COUNT(1) total,
  1509. t.company_id
  1510. FROM
  1511. (
  1512. SELECT
  1513. a.rs_calendar_id,
  1514. a.company_id
  1515. FROM
  1516. rs_calendar AS a
  1517. INNER JOIN rs_calendar_researcher AS b ON a.rs_calendar_id = b.rs_calendar_id
  1518. WHERE
  1519. a.company_id in (` + companyIds + `)
  1520. AND a.activity_type = '路演'
  1521. AND b.status = 2
  1522. AND b.end_date <= ?
  1523. AND b.end_time < ?
  1524. ) AS t
  1525. GROUP BY
  1526. company_id`
  1527. _, err = o.Raw(sql, date, endTime).QueryRows(&items)
  1528. return
  1529. }
  1530. type RoadShowCalendar struct {
  1531. RsCalendarId int `orm:"column(rs_calendar_id);pk"`
  1532. SysUserId int `description:"创建人id"`
  1533. SysUserRealName string `description:"创建人名称"`
  1534. ActivityType string `description:"活动类型"`
  1535. RoadshowType string `description:"路演形式"`
  1536. RoadshowPlatform string `description:"路演平台"`
  1537. CompanyId int `description:"客户id"`
  1538. CompanyName string `description:"客户名称"`
  1539. Province string `description:"省"`
  1540. ProvinceCode string `description:"省编码"`
  1541. City string `description:"市"`
  1542. CityCode string `description:"市编码"`
  1543. Theme string `description:"会议主题"`
  1544. CooperationName string `description:"合作方名称"`
  1545. Title string `description:"展示在日历的标题"`
  1546. Source int8 `description:"来源,0:自系统,1:上海方的"`
  1547. CreateTime time.Time
  1548. ModifyTime time.Time
  1549. ActivityCategory string `description:"活动类别"`
  1550. IsSynced int `description:"是否与上海同步 0:未同步 1:已同步"`
  1551. EndDate time.Time
  1552. }
  1553. func GetRoadShowList(companyId, englishCompany int) (items []*RoadShowCalendar, err error) {
  1554. o := orm.NewOrm()
  1555. sql := `SELECT
  1556. a.*,b.end_date
  1557. FROM
  1558. rs_calendar AS a
  1559. INNER JOIN rs_calendar_researcher AS b ON a.rs_calendar_id = b.rs_calendar_id
  1560. WHERE
  1561. a.source = 0
  1562. AND a.company_id=?
  1563. AND b.status = 2
  1564. AND a.english_company=?
  1565. AND NOW()> DATE_FORMAT( CONCAT( end_date, " ", end_time ), '%Y-%m-%d %H:%i:%S' )
  1566. GROUP BY rs_calendar_id ORDER BY create_time DESC `
  1567. _, err = o.Raw(sql, companyId, englishCompany).QueryRows(&items)
  1568. return
  1569. }
  1570. type RoadShowListResp struct {
  1571. SysUserRealName string `description:"对接销售"`
  1572. RoadshowType string `description:"路演形式"`
  1573. RoadshowPlatform string `description:"路演平台:线上时为平台,线下时为省市"`
  1574. RoadShowTime string `description:"路演时间"`
  1575. Researchers string `description:"研究员"`
  1576. }
  1577. func GetRoadShowResearchers(calendarId int) (items []*string, err error) {
  1578. o := orm.NewOrm()
  1579. 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' ) `
  1580. _, err = o.Raw(sql, calendarId).QueryRows(&items)
  1581. return
  1582. }
  1583. type CompanyNameAndId struct {
  1584. CompanyId int `description:"公司id"`
  1585. CompanyName string `description:"公司名称"`
  1586. }
  1587. type CompanyNameAndIdListResp struct {
  1588. List []*CompanyNameAndId
  1589. }
  1590. // GetCompanyIdListByproductName 获取不同种类不同状态下的公司ID
  1591. func GetCompanyIdListByproductName(condition string, pars []interface{}) (items []*CompanyNameAndId, err error) {
  1592. o := orm.NewOrm()
  1593. sql := `SELECT
  1594. a.company_id,a.company_name
  1595. FROM
  1596. company AS a
  1597. INNER JOIN company_product AS b ON a.company_id = b.company_id
  1598. WHERE
  1599. a.enabled = 1 `
  1600. if condition != `` {
  1601. sql += condition
  1602. }
  1603. _, err = o.Raw(sql, pars).QueryRows(&items)
  1604. return
  1605. }
  1606. // CompanyTryOutDayListResp 客户累计试用天数
  1607. type CompanyTryOutDayListResp struct {
  1608. Paging *paging.PagingItem
  1609. List []*CompanyProductTryOutUpdateLog
  1610. }
  1611. // 共享客户
  1612. type CompanyShareReq struct {
  1613. CompanyId int `description:"客户id"`
  1614. IsShare int `description:"0取消共享 1共享"`
  1615. }
  1616. func GetShareCompanyListCount(condition string, pars []interface{}) (count int, err error) {
  1617. o := orm.NewOrm()
  1618. sql := `SELECT COUNT(DISTINCT b.company_id) AS count
  1619. FROM company AS a
  1620. INNER JOIN company_product AS b ON a.company_id=b.company_id
  1621. WHERE a.enabled=1 `
  1622. if condition != "" {
  1623. sql += condition
  1624. }
  1625. err = o.Raw(sql, pars).QueryRow(&count)
  1626. return
  1627. }
  1628. func GetShareCompanyList(condition, sortStr string, pars []interface{}, startSize, pageSize int) (items []*CompanyItem, err error) {
  1629. o := orm.NewOrm()
  1630. // 七天前的时间-用于查询周阅读活跃, 此处的需求是
  1631. // FICC客户、共享客户: 以FICC最后一次阅读时间作为参考, 七日内有阅读量则为活跃
  1632. // 权益客户: 以权益最后一次阅读时间作为参考
  1633. t := time.Now().Local().AddDate(0, 0, -7).Format(utils.FormatDate)
  1634. sql := `SELECT a.company_id,a.company_name,a.credit_code,a.company_code,a.created_time,a.province,a.city,
  1635. 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,
  1636. CASE
  1637. WHEN GROUP_CONCAT(b.status) LIKE "%永续%" THEN 1
  1638. WHEN GROUP_CONCAT(b.status) LIKE "%正式%" THEN 2
  1639. WHEN GROUP_CONCAT(b.status) LIKE "%试用%" THEN 3
  1640. WHEN GROUP_CONCAT(b.status) LIKE "%冻结%" THEN 4
  1641. WHEN GROUP_CONCAT(b.status) LIKE "%流失%" THEN 5
  1642. ELSE 10 END
  1643. as sort_status,
  1644. CASE
  1645. WHEN GROUP_CONCAT(b.try_stage) LIKE "%2%" THEN 1
  1646. WHEN GROUP_CONCAT(b.try_stage) LIKE "%3%" THEN 2
  1647. WHEN GROUP_CONCAT(b.try_stage) LIKE "%4%" THEN 3
  1648. WHEN GROUP_CONCAT(b.try_stage) LIKE "%1%" THEN 4
  1649. ELSE 10 END
  1650. as sort_try_stage,
  1651. CASE
  1652. WHEN GROUP_CONCAT(b.todo_status) LIKE "%未完成%" AND GROUP_CONCAT(b.todo_end_time) is not null THEN GROUP_CONCAT(b.todo_end_time)
  1653. ELSE "9999-01-01" END
  1654. as sort_todo_end_time,
  1655. GROUP_CONCAT(CASE
  1656. WHEN b.is_share = 1 THEN CONCAT(b.status, '(共享)')
  1657. ELSE b.status
  1658. END ORDER BY b.product_id ASC SEPARATOR '/') AS status,
  1659. GROUP_CONCAT(DISTINCT b.seller_name ORDER BY b.product_id ASC SEPARATOR '/') AS seller_name,
  1660. GROUP_CONCAT(DISTINCT b.seller_id ORDER BY b.product_id ASC SEPARATOR '/') AS seller_ids,
  1661. GROUP_CONCAT(DISTINCT b.package_type ORDER BY b.product_id ASC SEPARATOR '/') AS package_types,
  1662. GROUP_CONCAT(DISTINCT b.industry_name ORDER BY b.product_id ASC SEPARATOR '/') AS industry_name,
  1663. GROUP_CONCAT(DISTINCT b.product_name ORDER BY b.product_id ASC SEPARATOR '/') AS company_type,
  1664. GROUP_CONCAT(DISTINCT b.product_id ORDER BY b.product_id ASC SEPARATOR '/') AS product_ids,
  1665. GROUP_CONCAT(DISTINCT b.view_total ORDER BY b.product_id ASC SEPARATOR '/') AS view_totals,
  1666. GROUP_CONCAT(DISTINCT b.last_view_time ORDER BY b.product_id ASC SEPARATOR '/') AS last_view_times,
  1667. GROUP_CONCAT(b.start_date ORDER BY b.product_id ASC SEPARATOR '/') AS start_date,
  1668. GROUP_CONCAT(b.end_date ORDER BY b.product_id ASC SEPARATOR '/') AS end_date,
  1669. GROUP_CONCAT(DISTINCT b.loss_time ORDER BY b.product_id ASC SEPARATOR '/') AS loss_time,
  1670. GROUP_CONCAT(DISTINCT b.lose_reason ORDER BY b.product_id ASC SEPARATOR '/') AS lose_reason,
  1671. GROUP_CONCAT(DISTINCT b.source ORDER BY b.product_id ASC SEPARATOR '/') AS source,
  1672. GROUP_CONCAT(DISTINCT b.reasons ORDER BY b.product_id ASC SEPARATOR '/') AS reasons,
  1673. GROUP_CONCAT(DISTINCT b.approve_status ORDER BY b.product_id ASC SEPARATOR '/') AS approve_status,
  1674. GROUP_CONCAT(b.try_stage ORDER BY b.product_id ASC SEPARATOR '/') AS try_stage_str,
  1675. GROUP_CONCAT(DISTINCT b.group_id ORDER BY b.product_id ASC SEPARATOR '/') AS group_ids,
  1676. GROUP_CONCAT(DISTINCT b.todo_status ORDER BY b.product_id ASC SEPARATOR '/') AS todo_statuss,
  1677. GROUP_CONCAT(DISTINCT b.todo_end_time ORDER BY b.product_id ASC SEPARATOR '/') AS todo_end_time_str,
  1678. GROUP_CONCAT(DISTINCT b.try_out_day_total ORDER BY b.product_id ASC SEPARATOR '/') AS try_out_day_totals,
  1679. b.is_suspend,
  1680. b.freeze_start_date,
  1681. b.freeze_end_date,
  1682. b.seller_id,
  1683. b.product_id,
  1684. b.formal_time,
  1685. b.freeze_time,b.freeze_reason,b.renewal_reason,
  1686. b.view_total,
  1687. b.last_view_time,
  1688. GROUP_CONCAT(DISTINCT b.share_seller ORDER BY b.product_id ASC SEPARATOR '/') AS share_seller,
  1689. GROUP_CONCAT(DISTINCT b.share_seller_id ORDER BY b.product_id ASC SEPARATOR ',') AS share_seller_ids,
  1690. b.share_seller_id ,
  1691. MAX(b.last_view_time) as max_last_view_time,
  1692. MIN(b.last_view_time) as min_last_view_time,
  1693. MAX(b.end_date) as max_end_date,
  1694. MIN(b.end_date) as min_end_date,
  1695. SUM(b.view_total) as all_view_total,
  1696. SUM(b.try_out_day_total) as all_try_out_day_total,
  1697. IF(LEFT(GROUP_CONCAT(DISTINCT b.last_view_time ORDER BY b.product_id), 19) >= "` + t + `", 1, 0) AS week_view_active,
  1698. (SELECT MAX(create_time) FROM company_service_record WHERE company_id = a.company_id) AS last_service_time,
  1699. (SELECT COUNT(company_service_record_id) FROM company_service_record WHERE company_id = a.company_id) AS service_times
  1700. FROM company AS a
  1701. INNER JOIN company_product AS b ON a.company_id=b.company_id
  1702. WHERE a.enabled=1 `
  1703. if condition != "" {
  1704. sql += condition
  1705. }
  1706. //sortStr := ``
  1707. //如果没有传入排序字段,那么按照状态排序
  1708. if sortStr == "" {
  1709. sortStr = " ORDER BY a.created_time DESC "
  1710. }
  1711. sql += ` GROUP BY a.company_id ` + sortStr + ` LIMIT ?,? `
  1712. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  1713. return
  1714. }
  1715. // 分配共享销售
  1716. type MoveShareSellerReq struct {
  1717. CompanyId int `description:"客户id"`
  1718. SellsId int `description:"销售员id"`
  1719. }
  1720. // ExportUserCompanyStatus 联系人导出客户状态
  1721. type ExportUserCompanyStatus struct {
  1722. CompanyId int `description:"客户ID"`
  1723. CompanyName string `description:"客户名称"`
  1724. CompanyStatus string `description:"客户状态: FICC状态/权益状态"`
  1725. }
  1726. // GetExportUserCompanyStatus 获取联系人导出客户状态
  1727. func GetExportUserCompanyStatus(condition string, pars []interface{}) (items []*ExportUserCompanyStatus, err error) {
  1728. o := orm.NewOrm()
  1729. sql := `SELECT
  1730. a.company_id,
  1731. a.company_name,
  1732. GROUP_CONCAT(b.status SEPARATOR '/') AS company_status
  1733. FROM
  1734. company AS a
  1735. INNER JOIN company_product AS b ON a.company_id = b.company_id
  1736. WHERE 1 = 1 `
  1737. if condition != "" {
  1738. sql += condition
  1739. }
  1740. sql += ` GROUP BY a.company_id `
  1741. _, err = o.Raw(sql, pars).QueryRows(&items)
  1742. return
  1743. }
  1744. func GetExportUserByProductId(companyId string, productId int) (items []*ExportUser, err error) {
  1745. o := orm.NewOrm()
  1746. sql := `SELECT a.real_name,a.mobile,a.email,a.position,a.is_maker,department_name,a.company_id,b.subscribe AS is_subscribe
  1747. FROM wx_user AS a
  1748. LEFT JOIN (SELECT * FROM user_record WHERE create_platform = 1 GROUP BY user_id) AS b ON a.user_id = b.user_id
  1749. LEFT JOIN user_seller_relation AS c ON a.user_id = c.user_id
  1750. WHERE a.company_id<>1 AND a.company_id IN(` + companyId + `) AND c.product_id = ?
  1751. GROUP BY a.user_id ORDER BY a.company_id ASC `
  1752. _, err = o.Raw(sql, productId).QueryRows(&items)
  1753. return
  1754. }
  1755. // CloseCompanyReq 关闭客户请求体
  1756. type CloseCompanyReq struct {
  1757. CompanyId int `description:"客户id"`
  1758. Remark string `description:"备注"`
  1759. }
  1760. // CloseCompany 关闭客户
  1761. func CloseCompany(companyId, productId int, reason string) (err error) {
  1762. o := orm.NewOrm()
  1763. tx, e := o.Begin()
  1764. if e != nil {
  1765. err = fmt.Errorf("orm begin err: %s", e.Error())
  1766. return
  1767. }
  1768. defer func() {
  1769. if err != nil {
  1770. _ = tx.Rollback()
  1771. return
  1772. }
  1773. _ = tx.Commit()
  1774. }()
  1775. // 修改产品状态
  1776. 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 = ?`
  1777. _, e = tx.Raw(sql, reason, companyId, productId).Exec()
  1778. if e != nil {
  1779. err = fmt.Errorf("update company product err: %s", e.Error())
  1780. return
  1781. }
  1782. // 修改产品权限
  1783. sql = `UPDATE company_report_permission SET status='关闭', modify_time=NOW() WHERE company_id = ? AND product_id = ?`
  1784. _, e = tx.Raw(sql, companyId, productId).Exec()
  1785. if e != nil {
  1786. err = fmt.Errorf("update company report permission err: %s", e.Error())
  1787. return
  1788. }
  1789. // 将历史的任务给标记删除掉
  1790. sql = `UPDATE company_todo SET is_delete = 1, modify_time=NOW() WHERE company_id = ? AND product_id = ? AND is_delete = 0`
  1791. _, e = tx.Raw(sql, companyId, productId).Exec()
  1792. if e != nil {
  1793. err = fmt.Errorf("update company todo err: %s", e.Error())
  1794. return
  1795. }
  1796. return
  1797. }
  1798. // Close2LossReq 关闭转流失请求体
  1799. type Close2LossReq struct {
  1800. CompanyId int `description:"客户id"`
  1801. }
  1802. // Close2Loss 关闭转流失
  1803. func Close2Loss(companyId, productId int) (err error) {
  1804. o := orm.NewOrm()
  1805. 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 = ?`
  1806. _, err = o.Raw(sql, companyId, productId).Exec()
  1807. return
  1808. }
  1809. // CompanyMarkReq 标记请求
  1810. type CompanyMarkReq struct {
  1811. CompanyServiceRecordId int `description:"服务记录ID"`
  1812. Status int `description:"0取消标记 1标记"`
  1813. }
  1814. // GetCompanyListByCondition 获取客户列表
  1815. func GetCompanyListByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*Company, err error) {
  1816. o := orm.NewOrm()
  1817. fields := strings.Join(fieldArr, ",")
  1818. if len(fieldArr) == 0 {
  1819. fields = `*`
  1820. }
  1821. order := `ORDER BY created_time DESC`
  1822. if orderRule != "" {
  1823. order = ` ORDER BY ` + orderRule
  1824. }
  1825. sql := fmt.Sprintf(`SELECT %s FROM company WHERE 1=1 %s %s`, fields, condition, order)
  1826. _, err = o.Raw(sql, pars).QueryRows(&items)
  1827. return
  1828. }
  1829. func GetOverseasCompanys() (items []*Company, err error) {
  1830. o := orm.NewOrm()
  1831. sql := ` SELECT * FROM company where region_type= '海外' `
  1832. _, err = o.Raw(sql).QueryRows(&items)
  1833. return
  1834. }
  1835. // GetCompanyListByCompanyId 根据公司ID获取公司信息
  1836. func GetCompanyListByCompanyId(companyId []int) (items []*Company, err error) {
  1837. lenArr := len(companyId)
  1838. if lenArr == 0 {
  1839. return
  1840. }
  1841. o := orm.NewOrm()
  1842. sql := `SELECT interaction_num , company_id FROM company AS a WHERE a.company_id IN (` + utils.GetOrmInReplace(lenArr) + `) `
  1843. _, err = o.Raw(sql, companyId).QueryRows(&items)
  1844. return
  1845. }