company.go 77 KB

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