company.go 77 KB

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