company.go 76 KB

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