company.go 80 KB

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