company.go 80 KB

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