company.go 80 KB

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