company.go 79 KB

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