company.go 78 KB

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