company.go 82 KB

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