company.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. package company
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "hongze/hongze_mobile_admin/utils"
  7. "time"
  8. )
  9. type Company struct {
  10. CompanyId int `orm:"column(company_id);pk"`
  11. CompanyName string `description:"客户名称"`
  12. CompanyType int `orm:"column(type)"`
  13. CreditCode string `description:"社会统一信用码"`
  14. CompanyCode string `description:"客户编码"`
  15. Sort int `description:"优先级"`
  16. IsFeeCustomer int `description:"是否付费用户"`
  17. Country string `description:"国家编码"`
  18. Province string `description:"省"`
  19. City string `description:"市"`
  20. Address string `description:"详细地址"`
  21. Enabled int `description:"用户状态"`
  22. CreatedTime time.Time `description:"创建时间"`
  23. LastUpdatedTime time.Time `description:"最后一次阅读时间"`
  24. Seller string `description:"销售员"`
  25. SellsId int `description:"销售员id"`
  26. CompanyBelong string `description:"客户所属,ficc:ficc客户,public_offering:公募客户,partner:合作伙伴"`
  27. StartDate string `description:"合同开始日期"`
  28. EndDate string `description:"合同结束日期"`
  29. LastType int `description:"原客户标签"`
  30. IsVip int `description:"0:普通用户,1:大客户"`
  31. FirstStartDate string `description:"首次设置为试用客户开始时间"`
  32. FirstEndDate string `description:"首次设置为试用客户结束时间"`
  33. DateType int `description:"设置流失类型,1:1个月,2:2个月,3:3个月"`
  34. Remark string `description:"备注信息"`
  35. RegionType string `description:"地区类型,国内,国外"`
  36. }
  37. //新增客户
  38. func AddCompany(item *Company) (lastId int64, err error) {
  39. o := orm.NewOrm()
  40. lastId, err = o.Insert(item)
  41. return
  42. }
  43. //根据企业id获取企业信息
  44. func GetCompanyById(companyId int) (items *Company, err error) {
  45. o := orm.NewOrm()
  46. sql := ` SELECT * FROM company where company_id= ? `
  47. err = o.Raw(sql, companyId).QueryRow(&items)
  48. return
  49. }
  50. type CompanySearchItem struct {
  51. CompanyId int `orm:"column(company_id);pk"`
  52. CompanyName string `description:"客户名称"`
  53. CreditCode string `description:"社会统一信用码"`
  54. CompanyCode string `description:"客户编码"`
  55. StartDate string `description:"合同开始日期"`
  56. EndDate string `description:"合同结束日期"`
  57. LoseReason string `description:"流失原因"`
  58. LossTime string `description:"流失时间"`
  59. Status string `description:"客户状态:'试用','永续','冻结','流失','正式' "`
  60. CompanyType string `description:"客户类型:ficc/权益"`
  61. ApproveStatus string `description:"审批状态:'审批中','通过','驳回' 审批状态为空时,表示没有审批申请"`
  62. SellerName string `description:"销售:吉根龙/颖丹"`
  63. SellerIds string `description:"销售ID集合,包含ficc和权益的销售id" json:"-"`
  64. Duration string `description:"时长"`
  65. FreezeTime string `description:"冻结时间"`
  66. GroupId int `description:"分组id"`
  67. GroupIds string `description:"分组id集合,包含ficc和权益的小组id" json:"-"`
  68. DepartmentId int `description:"部门id"`
  69. IsSuspend int `description:"是否暂停:1:暂停,0:启用 "`
  70. ProductId int `description:"产品id"`
  71. IsMoveShow int `description:"按钮显示控制:1:显示,移动按钮,0:不显示移动按钮"`
  72. SellerId int `description:"销售id"`
  73. BtnItem *ButtonPermission
  74. FormalTime string `description:"转正时间"`
  75. FreezeStartDate string `description:"冻结开始日期"`
  76. FreezeEndDate string `description:"冻结结束日期"`
  77. RegionType string `description:"地区类型,国内,国外"`
  78. }
  79. type CompanySearchListResp struct {
  80. List []*CompanySearchItem
  81. }
  82. func GetCompanySearchList(condition string, pars []interface{}) (items []*CompanySearchItem, err error) {
  83. o := orm.NewOrm()
  84. sql := `SELECT DISTINCT a.company_id,a.company_name,a.credit_code,a.company_code,a.region_type,
  85. GROUP_CONCAT(b.status ORDER BY b.product_id ASC SEPARATOR '/') AS status,
  86. GROUP_CONCAT(DISTINCT b.seller_name ORDER BY b.product_id ASC SEPARATOR '/' ) AS seller_name,
  87. GROUP_CONCAT(DISTINCT b.seller_id ORDER BY b.product_id ASC SEPARATOR '/') AS seller_ids,
  88. GROUP_CONCAT(DISTINCT b.group_id ORDER BY b.product_id ASC SEPARATOR '/' ) AS group_ids,
  89. 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,
  90. GROUP_CONCAT(b.start_date ORDER BY b.product_id ASC SEPARATOR '/') AS start_date,
  91. GROUP_CONCAT(b.end_date ORDER BY b.product_id ASC SEPARATOR '/') AS end_date,
  92. GROUP_CONCAT(DISTINCT b.company_type ORDER BY b.product_id ASC SEPARATOR '/') AS company_type,
  93. GROUP_CONCAT(DISTINCT b.approve_status ORDER BY b.product_id ASC SEPARATOR '/') AS approve_status,
  94. GROUP_CONCAT(DISTINCT b.freeze_start_date ORDER BY b.product_id ASC SEPARATOR '/') AS freeze_start_date,
  95. GROUP_CONCAT(DISTINCT b.freeze_end_date ORDER BY b.product_id ASC SEPARATOR '/') AS freeze_end_date
  96. FROM company AS a
  97. INNER JOIN company_product AS b ON a.company_id=b.company_id
  98. WHERE a.enabled=1 `
  99. if condition != "" {
  100. sql += condition
  101. }
  102. sql += ` GROUP BY a.company_id ORDER BY a.last_updated_time DESC `
  103. _, err = o.Raw(sql, pars).QueryRows(&items)
  104. return
  105. }
  106. type CompanyItem struct {
  107. CompanyId int `orm:"column(company_id);pk"`
  108. CompanyName string `description:"客户名称"`
  109. CreditCode string `description:"社会统一信用码"`
  110. CompanyCode string `description:"客户编码"`
  111. StartDate string `description:"合同开始日期"`
  112. EndDate string `description:"合同结束日期"`
  113. LoseReason string `description:"流失原因"`
  114. LossTime string `description:"流失时间"`
  115. Status string `description:"客户状态:'试用','永续','冻结','流失','正式' "`
  116. CompanyType string `description:"客户类型:ficc/权益"`
  117. ApproveStatus string `description:"审批状态:'待审批','已审批','驳回' 审批状态为空时,表示没有审批申请"`
  118. SellerName string `description:"销售:吉根龙/颖丹"`
  119. SellerId int `description:"销售ID"`
  120. SellerIds string `description:"销售ID集合,包含ficc和权益的销售id" json:"-"`
  121. ExpireDay string `description:"到期天数"`
  122. FreezeTime string `description:"冻结时间"`
  123. GroupId int `description:"分组id"`
  124. GroupIds string `description:"分组id集合,包含ficc和权益的小组id" json:"-"`
  125. DepartmentId int `description:"部门id"`
  126. IndustryName string `description:"所属行业"`
  127. IsSuspend int `description:"是否暂停:1:暂停,0:启用 "`
  128. CreatedTime string `description:"创建时间"`
  129. Source string `description:"客户来源"`
  130. Address string `description:"详细地址"`
  131. Reasons string `description:"新增理由"`
  132. FreezeStartDate string `description:"冻结开始日期"`
  133. FreezeEndDate string `description:"冻结结束日期"`
  134. FreezeExpireDays int `description:"冻结到期天数"`
  135. BtnItem *ButtonPermission
  136. ProductId int `json:"-"`
  137. FormalTime string `description:"转正时间"`
  138. IsShared bool `description:"是否共享客户"`
  139. RegionType string `description:"区域:国内,海外"`
  140. }
  141. type CompanyListResp struct {
  142. Paging *paging.PagingItem
  143. List []*CompanyItem
  144. StatusCount []*CompanyStatus
  145. }
  146. func GetCompanyListCount(condition string, pars []interface{}) (count int, err error) {
  147. o := orm.NewOrm()
  148. sql := `SELECT COUNT(DISTINCT b.company_id) AS count
  149. FROM company AS a
  150. INNER JOIN company_product AS b ON a.company_id=b.company_id
  151. WHERE a.enabled=1 `
  152. if condition != "" {
  153. sql += condition
  154. }
  155. err = o.Raw(sql, pars).QueryRow(&count)
  156. return
  157. }
  158. func GetCompanyList(condition, status, sortStr string, pars []interface{}, startSize, pageSize int) (items []*CompanyItem, err error) {
  159. o := orm.NewOrm()
  160. sql := `SELECT a.company_id,a.company_name,a.credit_code,a.company_code,a.created_time,a.address,a.region_type,b.group_id,
  161. GROUP_CONCAT(b.status ORDER BY b.product_id ASC SEPARATOR '/') AS status,
  162. GROUP_CONCAT(DISTINCT b.seller_name ORDER BY b.product_id ASC SEPARATOR '/') AS seller_name,
  163. GROUP_CONCAT(DISTINCT b.seller_id ORDER BY b.product_id ASC SEPARATOR '/') AS seller_ids,
  164. GROUP_CONCAT(DISTINCT b.industry_name ORDER BY b.product_id ASC SEPARATOR '/') AS industry_name,
  165. GROUP_CONCAT(DISTINCT b.product_name ORDER BY b.product_id ASC SEPARATOR '/') AS company_type,
  166. GROUP_CONCAT(b.start_date ORDER BY b.product_id ASC SEPARATOR '/') AS start_date,
  167. GROUP_CONCAT(b.end_date ORDER BY b.product_id ASC SEPARATOR '/') AS end_date,
  168. GROUP_CONCAT(DISTINCT b.loss_time ORDER BY b.product_id ASC SEPARATOR '/') AS loss_time,
  169. GROUP_CONCAT(DISTINCT b.lose_reason ORDER BY b.product_id ASC SEPARATOR '/') AS lose_reason,
  170. GROUP_CONCAT(DISTINCT b.source ORDER BY b.product_id ASC SEPARATOR '/') AS source,
  171. GROUP_CONCAT(DISTINCT b.reasons ORDER BY b.product_id ASC SEPARATOR '/') AS reasons,
  172. GROUP_CONCAT(DISTINCT b.approve_status ORDER BY b.product_id ASC SEPARATOR '/') AS approve_status,
  173. GROUP_CONCAT(DISTINCT b.group_id ORDER BY b.product_id ASC SEPARATOR '/') AS group_ids,
  174. b.is_suspend,
  175. b.freeze_start_date,
  176. b.freeze_end_date,
  177. b.seller_id,
  178. b.product_id,
  179. b.formal_time,
  180. b.freeze_time
  181. FROM company AS a
  182. INNER JOIN company_product AS b ON a.company_id=b.company_id
  183. WHERE a.enabled=1 `
  184. if condition != "" {
  185. sql += condition
  186. }
  187. //sortStr := ``
  188. //如果没有传入排序字段,那么按照状态排序
  189. if sortStr == "" {
  190. if status == "全部" || status == utils.COMPANY_STATUS_TRY_OUT { //试用
  191. sortStr = " ORDER BY a.created_time DESC "
  192. } else if status == utils.COMPANY_STATUS_FORMAL { //正式
  193. sortStr = " ORDER BY b.formal_time DESC "
  194. } else if status == utils.COMPANY_STATUS_FREEZE { //冻结
  195. sortStr = " ORDER BY b.freeze_time DESC "
  196. } else if status == utils.COMPANY_STATUS_LOSE { //流失
  197. sortStr = " ORDER BY b.loss_time DESC "
  198. } else {
  199. sortStr = " ORDER BY a.created_time DESC "
  200. }
  201. }
  202. sql += ` GROUP BY a.company_id ` + sortStr + ` LIMIT ?,? `
  203. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  204. return
  205. }
  206. type CompanyStatus struct {
  207. Status string `description:"客户状态:'试用','永续','冻结','流失','正式' "`
  208. Count int `description:"数量 "`
  209. }
  210. func GetCompanyStatusCount(condition string, pars []interface{}) (items []*CompanyStatus, err error) {
  211. o := orm.NewOrm()
  212. sql := ` SELECT b.status,COUNT(DISTINCT a.company_id) AS count FROM company AS a
  213. INNER JOIN company_product AS b ON a.company_id=b.company_id
  214. WHERE a.enabled=1 AND b.status IN('正式','试用','冻结','永续') `
  215. if condition != "" {
  216. sql += condition
  217. }
  218. //sql += `GROUP BY b.status `
  219. //_, err = o.Raw(sql, pars).QueryRows(&items)
  220. sql += `GROUP BY b.status `
  221. _, err = o.Raw(sql, pars).QueryRows(&items)
  222. return
  223. }
  224. func GetCompanyStatusLoseCount(condition string, pars []interface{}) (items *CompanyStatus, err error) {
  225. o := orm.NewOrm()
  226. sql := ` SELECT b.status,COUNT(DISTINCT a.company_id) AS count FROM company AS a
  227. INNER JOIN company_product AS b ON a.company_id=b.company_id
  228. WHERE a.enabled=1 AND b.status='流失' `
  229. if condition != "" {
  230. sql += condition
  231. }
  232. err = o.Raw(sql, pars).QueryRow(&items)
  233. return
  234. }
  235. func GetCompanyStatusFreezeCount(condition string, pars []interface{}) (items *CompanyStatus, err error) {
  236. o := orm.NewOrm()
  237. sql := ` SELECT b.status,COUNT(DISTINCT a.company_id) AS count FROM company AS a
  238. INNER JOIN company_product AS b ON a.company_id=b.company_id
  239. WHERE a.enabled=1 AND b.status='冻结' `
  240. if condition != "" {
  241. sql += condition
  242. }
  243. err = o.Raw(sql, pars).QueryRow(&items)
  244. return
  245. }
  246. func GetCompanyStatusForverCount(condition string, pars []interface{}) (items *CompanyStatus, err error) {
  247. o := orm.NewOrm()
  248. sql := ` SELECT b.status,COUNT(DISTINCT a.company_id) AS count FROM company AS a
  249. INNER JOIN company_product AS b ON a.company_id=b.company_id
  250. WHERE a.enabled=1 AND b.status='永续' `
  251. if condition != "" {
  252. sql += condition
  253. }
  254. err = o.Raw(sql, pars).QueryRow(&items)
  255. return
  256. }
  257. //新增客户请求参数
  258. type CompanyAddReq struct {
  259. CompanyName string `description:"客户名称"`
  260. CreditCode string `description:"社会统一信用码"`
  261. CompanyCode string `description:"客户编码"`
  262. Province string `description:"省"`
  263. City string `description:"市"`
  264. Status string `description:"客户状态,'试用','永续','冻结','流失','正式','潜在'"`
  265. CompanyType string `description:"客户类型,ficc/权益"`
  266. IndustryId int `description:"所属行业id"`
  267. Source string `description:"来源"`
  268. SellsId int `description:"销售员id"`
  269. Reasons string `description:"新增理由,备注"`
  270. PermissionIds string `description:"权限id,多个用英文逗号隔开"`
  271. RegionType string `description:"地区类型,国内,海外"`
  272. UserId int `description:"联系人ID"`
  273. }
  274. //新增客户请求参数
  275. type CompanyAddResp struct {
  276. CompanyId int `description:"客户ID"`
  277. }
  278. func GetCompanyCode() (companyCode string, err error) {
  279. var num int
  280. o := orm.NewOrm()
  281. sql := `SELECT COUNT(1) AS num FROM company WHERE created_time >=? `
  282. err = o.Raw(sql, time.Now().Format(utils.FormatDate)).QueryRow(&num)
  283. if err != nil {
  284. return
  285. }
  286. companyCode = "KH" + time.Now().Format("20060102") + fmt.Sprintf("%03d", num)
  287. return
  288. }
  289. type CompanyDetail struct {
  290. CompanyId int `orm:"column(company_id);pk"`
  291. CompanyName string `description:"客户名称"`
  292. CreditCode string `description:"社会统一信用码"`
  293. CompanyCode string `description:"客户编码"`
  294. Province string `description:"省"`
  295. City string `description:"市"`
  296. Address string `description:"详细地址"`
  297. RegionType string `description:"地区类型,1:国内,2:国外"`
  298. }
  299. func GetCompanyDetailById(companyId int) (item *CompanyDetail, err error) {
  300. o := orm.NewOrm()
  301. sql := `SELECT * FROM company WHERE company_id =? `
  302. err = o.Raw(sql, companyId).QueryRow(&item)
  303. return
  304. }
  305. func GetCompanyCountByCompanyName(companyName string) (count int, err error) {
  306. o := orm.NewOrm()
  307. sql := ` SELECT COUNT(1) AS COUNT FROM company AS a WHERE a.company_name=? `
  308. err = o.Raw(sql, companyName).QueryRow(&count)
  309. return
  310. }
  311. func GetCompanyCountByCreditCode(creditCode string) (count int, err error) {
  312. o := orm.NewOrm()
  313. sql := ` SELECT COUNT(1) AS COUNT FROM company AS a WHERE a.credit_code=? `
  314. err = o.Raw(sql, creditCode).QueryRow(&count)
  315. return
  316. }
  317. func GetCompanyCountByCompanyNameAndCompanyId(companyId int, companyName string) (count int, err error) {
  318. o := orm.NewOrm()
  319. sql := ` SELECT COUNT(1) AS COUNT FROM company AS a WHERE a.company_id<>? AND a.company_name=? `
  320. err = o.Raw(sql, companyId, companyName).QueryRow(&count)
  321. return
  322. }
  323. func GetCompanyCountByCreditCodeAndCompanyId(companyId int, creditCode string) (count int, err error) {
  324. o := orm.NewOrm()
  325. sql := ` SELECT COUNT(1) AS COUNT FROM company AS a WHERE a.company_id<>? AND a.credit_code=? `
  326. err = o.Raw(sql, companyId, creditCode).QueryRow(&count)
  327. return
  328. }
  329. //根据社会信用码获取客户信息
  330. func GetCompanyByCreditCode(creditCode string) (companyInfo *Company, err error) {
  331. o := orm.NewOrm()
  332. sql := ` SELECT * FROM company AS a WHERE a.credit_code=? `
  333. err = o.Raw(sql, creditCode).QueryRow(&companyInfo)
  334. return
  335. }
  336. func GetCompanyTryOutCount(status string, sellerId int) (count int, err error) {
  337. o := orm.NewOrm()
  338. sql := ` SELECT COUNT(DISTINCT a.company_id) AS count FROM company AS a
  339. INNER JOIN company_product AS b ON a.company_id=b.company_id
  340. WHERE b.status=?
  341. AND b.seller_id=? `
  342. err = o.Raw(sql, status, sellerId).QueryRow(&count)
  343. return
  344. }
  345. type CompanySearchResp struct {
  346. List []*CompanyItem
  347. }
  348. func GetCompanyBySearch(keyWord string) (item []*CompanyItem, err error) {
  349. o := orm.NewOrm()
  350. var sql string
  351. if keyWord == "##" {
  352. sql = ` SELECT * FROM company ORDER BY RAND() LIMIT 5 `
  353. } else {
  354. sql = `SELECT * FROM company WHERE company_name LIKE '%` + keyWord + `%' LIMIT 5`
  355. }
  356. _, err = o.Raw(sql).QueryRows(&item)
  357. return
  358. }
  359. //编辑客户请求参数
  360. type CompanyEditReq struct {
  361. CompanyId int `description:"客户id"`
  362. CompanyName string `description:"客户名称"`
  363. CreditCode string `description:"社会统一信用码"`
  364. Province string `description:"省"`
  365. City string `description:"市"`
  366. Products []*CompanyProductEditReq `description:"产品信息"`
  367. }
  368. //编辑客户信息请求参数
  369. type CompanyInfoEditReq struct {
  370. CompanyId int `description:"客户id"`
  371. CompanyName string `description:"客户名称"`
  372. CreditCode string `description:"社会统一信用码"`
  373. Province string `description:"省"`
  374. City string `description:"市"`
  375. IndustryId int `description:"所属行业id"`
  376. IndustryName string `description:"所属行业id"`
  377. Source string `description:"来源"`
  378. }
  379. type CompanyProductEditReq struct {
  380. CompanyProductId int `description:"客户产品id"`
  381. Status string `description:"客户状态,试用,永续"`
  382. CompanyType string `description:"客户类型,ficc/权益"`
  383. IndustryId int `description:"所属行业id"`
  384. Source string `description:"来源"`
  385. SellsId int `description:"销售员id"`
  386. Reasons string `description:"新增理由"`
  387. PermissionIds string `description:"权限id,多个用英文逗号隔开"`
  388. }
  389. type CompanyFuzzySearch struct {
  390. CompanyId int `orm:"column(company_id);pk"`
  391. CompanyName string `description:"客户名称"`
  392. CreditCode string `description:"社会统一信用码"`
  393. CompanyCode string `description:"客户编码"`
  394. }
  395. //新增客户请求参数
  396. type CompanyFuzzySearchReq struct {
  397. List []*CompanyFuzzySearch
  398. }
  399. func GetCompanyFuzzySearch(keyWord string) (items []*CompanyFuzzySearch, err error) {
  400. o := orm.NewOrm()
  401. sql := `SELECT * FROM company WHERE company_name LIKE '%` + keyWord + `%' `
  402. _, err = o.Raw(sql).QueryRows(&items)
  403. return
  404. }
  405. //修改客户
  406. func ModifyCompany(item *Company) (err error) {
  407. o := orm.NewOrm()
  408. sql := `UPDATE company
  409. SET
  410. company_name = ?,
  411. credit_code = ?,
  412. address = ?,
  413. last_updated_time = NOW(),
  414. province = ?,
  415. city = ?
  416. WHERE company_id =?`
  417. _, err = o.Raw(sql, item.CompanyName, item.CreditCode, item.Address, item.Province, item.City, item.CompanyId).Exec()
  418. return
  419. }
  420. //更新客户基础信息
  421. func (company *Company) Update(cols []string) (err error) {
  422. o := orm.NewOrm()
  423. _, err = o.Update(company, cols...)
  424. return
  425. }
  426. type DeletePptReq struct {
  427. CompanyId int `description:"客户id"`
  428. }
  429. func DeleteCompanyById(companyId int) (err error) {
  430. o := orm.NewOrm()
  431. tx, err := o.Begin()
  432. if err != nil {
  433. return
  434. }
  435. defer func() {
  436. if err != nil {
  437. _ = tx.Rollback()
  438. } else {
  439. _ = tx.Commit()
  440. }
  441. }()
  442. sql := `DELETE FROM company_product WHERE company_id=? `
  443. _, err = tx.Raw(sql, companyId).Exec()
  444. if err != nil {
  445. return
  446. }
  447. sql = `DELETE FROM company_report_permission WHERE company_id=? `
  448. _, err = tx.Raw(sql, companyId).Exec()
  449. if err != nil {
  450. return
  451. }
  452. //联系人表
  453. sql = `DELETE FROM wx_user WHERE company_id=? `
  454. _, err = tx.Raw(sql, companyId).Exec()
  455. if err != nil {
  456. return
  457. }
  458. //联系人 与 销售 关系表
  459. sql = `DELETE FROM user_seller_relation WHERE company_id=? `
  460. _, err = tx.Raw(sql, companyId).Exec()
  461. if err != nil {
  462. return
  463. }
  464. //sql = `DELETE FROM company_operation_record WHERE company_id=? `
  465. //_, err = tx.Raw(sql, companyId).Exec()
  466. //if err != nil {
  467. // return
  468. //}
  469. sql = `DELETE FROM company_contract WHERE company_id=? `
  470. _, err = tx.Raw(sql, companyId).Exec()
  471. if err != nil {
  472. return
  473. }
  474. sql = `DELETE FROM company_contract_permission WHERE company_id=? `
  475. _, err = tx.Raw(sql, companyId).Exec()
  476. if err != nil {
  477. return
  478. }
  479. sql = `DELETE FROM company WHERE company_id=? `
  480. _, err = tx.Raw(sql, companyId).Exec()
  481. return
  482. }
  483. type CompanyReceiveItem struct {
  484. CompanyId int `orm:"column(company_id);pk"`
  485. CompanyName string `description:"客户名称"`
  486. StartDate string `description:"合同开始日期"`
  487. EndDate string `description:"合同结束日期"`
  488. Status string `description:"客户状态:'试用','永续','冻结','流失','正式' "`
  489. ProductName string `description:"客户类型:ficc/权益"`
  490. SellerName string `description:"销售:吉根龙/颖丹"`
  491. CreatedTime string `description:"创建时间"`
  492. ExpireDay string `description:"到期天数"`
  493. IndustryName string `description:"行业"`
  494. }
  495. type CompanyReceiveListResp struct {
  496. Paging *paging.PagingItem
  497. List []*CompanyReceiveItem
  498. }
  499. func GetCompanyReceiveListCount(condition string, pars []interface{}, productId, receiveProductId int) (count int, err error) {
  500. o := orm.NewOrm()
  501. sql := `SELECT
  502. COUNT(DISTINCT a.company_id ) AS COUNT
  503. FROM company AS a
  504. LEFT JOIN wx_user AS b ON a.company_id=b.company_id
  505. LEFT JOIN company_product AS c ON a.company_id=c.company_id
  506. WHERE a.enabled=1
  507. AND c.product_id=?
  508. AND c.company_id NOT IN (SELECT company_id FROM company_product WHERE product_id=?) `
  509. if condition != "" {
  510. sql += condition
  511. }
  512. err = o.Raw(sql, receiveProductId, productId, pars).QueryRow(&count)
  513. return
  514. }
  515. func GetCompanyReceiveList(condition string, pars []interface{}, productId, receiveProductId, startSize, pageSize int) (items []*CompanyReceiveItem, err error) {
  516. o := orm.NewOrm()
  517. sql := `SELECT
  518. 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
  519. FROM company AS a
  520. LEFT JOIN wx_user AS b ON a.company_id=b.company_id
  521. LEFT JOIN company_product AS c ON a.company_id=c.company_id
  522. WHERE a.enabled=1
  523. AND c.product_id=?
  524. AND c.company_id NOT IN (SELECT company_id FROM company_product WHERE product_id=?) `
  525. if condition != "" {
  526. sql += condition
  527. }
  528. sql += ` GROUP BY a.company_id ORDER BY a.last_updated_time DESC LIMIT ?,? `
  529. _, err = o.Raw(sql, receiveProductId, productId, pars, startSize, pageSize).QueryRows(&items)
  530. return
  531. }
  532. //编辑客户请求参数
  533. type CompanyReceiveReq struct {
  534. CompanyId int `description:"客户id"`
  535. Status string `description:"客户状态,试用,永续"`
  536. IndustryId int `description:"所属行业id"`
  537. Source string `description:"来源"`
  538. SellsId int `description:"销售员id,流失状态下直接领取,请传 0"`
  539. Reasons string `description:"新增理由"`
  540. PermissionIds string `description:"权限id,多个用英文逗号隔开"`
  541. CompanyType string `description:"客户类型"`
  542. }
  543. //移动销售
  544. type MoveSellerReq struct {
  545. CompanyId int `description:"客户id"`
  546. SellsId int `description:"销售员id"`
  547. CompanyType string `description:"客户类型"`
  548. }
  549. func GetExportCompanyIds(condition string, pars []interface{}) (company_id string, err error) {
  550. o := orm.NewOrm()
  551. sql := `SELECT
  552. GROUP_CONCAT(DISTINCT b.company_id SEPARATOR ',') AS company_id
  553. FROM company AS a
  554. INNER JOIN company_product AS b ON a.company_id=b.company_id
  555. LEFT JOIN wx_user AS c ON a.company_id=c.company_id
  556. WHERE 1=1 `
  557. if condition != "" {
  558. sql += condition
  559. }
  560. err = o.Raw(sql, pars).QueryRow(&company_id)
  561. return
  562. }
  563. type ExportUser struct {
  564. RealName string
  565. Mobile string
  566. Email string
  567. Position string
  568. IsMaker int
  569. DepartmentName string
  570. CompanyId int
  571. }
  572. func GetExportUser(companyId string) (items []*ExportUser, err error) {
  573. o := orm.NewOrm()
  574. sql := `SELECT a.real_name,a.mobile,a.email,a.position,a.is_maker,department_name,a.company_id
  575. FROM wx_user AS a
  576. WHERE a.company_id<>1 AND a.company_id IN(` + companyId + `)
  577. ORDER BY a.company_id ASC `
  578. _, err = o.Raw(sql).QueryRows(&items)
  579. return
  580. }
  581. type ExportCompanyProduct struct {
  582. CompanyName string
  583. ProductId int
  584. ProductName string
  585. IndustryName string
  586. SellerName string
  587. Status string
  588. StartDate time.Time
  589. EndDate time.Time
  590. CreateTime string
  591. Source string
  592. CreditCode string
  593. RegionType string
  594. LossTime string
  595. }
  596. func GetExportCompanyProduct(companyId int) (items []*ExportCompanyProduct, err error) {
  597. o := orm.NewOrm()
  598. 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,
  599. b.source,a.credit_code,a.region_type,b.loss_time
  600. FROM company AS a
  601. INNER JOIN company_product AS b ON a.company_id=b.company_id
  602. WHERE a.company_id=?
  603. ORDER BY b.product_id ASC `
  604. _, err = o.Raw(sql, companyId).QueryRows(&items)
  605. return
  606. }
  607. func GetExportPermissionName(companyId, productId int) (chart_permission_name string, err error) {
  608. o := orm.NewOrm()
  609. sql := ` SELECT GROUP_CONCAT(DISTINCT c.permission_name ORDER BY sort ASC SEPARATOR '/') AS chart_permission_name FROM company_product AS a
  610. INNER JOIN company_report_permission AS b ON a.company_id=b.company_id AND a.product_id=b.product_id
  611. INNER JOIN chart_permission AS c ON b.chart_permission_id=c.chart_permission_id AND b.product_id=c.product_id
  612. WHERE a.company_id=? AND a.product_id=? `
  613. err = o.Raw(sql, companyId, productId).QueryRow(&chart_permission_name)
  614. return
  615. }
  616. //移动销售
  617. type SuspendReq struct {
  618. CompanyId int `description:"客户id"`
  619. }
  620. func MoveCompanySeller(companyId, productId, sellerId, groupId, departmentId int, sellerName string) (err error) {
  621. o := orm.NewOrm()
  622. if sellerName != "" {
  623. sql := `UPDATE company_product SET seller_id=?,seller_name=?,group_id=?,department_id=?,modify_time=NOW() WHERE company_id=? AND product_id=? `
  624. _, err = o.Raw(sql, sellerId, sellerName, groupId, departmentId, companyId, productId).Exec()
  625. } else {
  626. sql := `UPDATE company_product SET seller_id=?,group_id=?,department_id=?,modify_time=NOW() WHERE company_id=? AND product_id=? `
  627. _, err = o.Raw(sql, sellerId, groupId, departmentId, companyId, productId).Exec()
  628. }
  629. return
  630. }
  631. //新增客户请求参数
  632. type CheckCompanyInfoResp struct {
  633. Status int `description:"检测状态:1:跳转到领取页面,0:不跳转,继续操作"`
  634. CompanyId int `description:"客户id"`
  635. ProductName string `description:"产品名称"`
  636. }
  637. type ButtonPermission struct {
  638. BtnView bool `description:"查看按钮权限:true显示,false不显示"`
  639. BtnEdit bool `description:"编辑按钮权限:true显示,false不显示"`
  640. BtnDelete bool `description:"删除按钮权限:true显示,false不显示"`
  641. BtnSuspend bool `description:"暂停/启用按钮权限:true显示,false不显示"`
  642. BtnDelay bool `description:"申请延期按钮权限:true显示,false不显示"`
  643. BtnTurnPositive bool `description:"申请转正按钮权限:true显示,false不显示"`
  644. BtnUpdate bool `description:"服务更新按钮权限:true显示,false不显示"`
  645. BtnAddAgreement bool `description:"补充协议按钮权限:true显示,false不显示"`
  646. BtnThaw bool `description:"申请解冻按钮权限:true显示,false不显示"`
  647. BtnApplyReceive bool `description:"申请领取按钮权限:true显示,false不显示"`
  648. BtnReceive bool `description:"领取客户按钮权限:true显示,false不显示"`
  649. BtnModifySeller bool `description:"修改销售按钮权限:true显示,false不显示"`
  650. BtnReceiveOther bool `description:"领取其他部门客户按钮权限:true显示,false不显示"`
  651. BtnFreeze bool `description:"冻结按钮权限:true显示,false不显示"`
  652. BtnTryOut bool `description:"增开试用:true显示,false不显示"`
  653. BtnDetail bool `description:"详情按钮:true显示,false不显示"`
  654. }
  655. type ButtonSearchPermission struct {
  656. BtnView bool `description:"查看按钮权限:true显示,false不显示"`
  657. BtnUpdate bool `description:"服务更新按钮权限:true显示,false不显示"`
  658. BtnEdit bool `description:"编辑按钮权限:true显示,false不显示"`
  659. BtnDelete bool `description:"删除按钮权限:true显示,false不显示"`
  660. BtnSuspend bool `description:"暂停/启用按钮权限:true显示,false不显示"`
  661. BtnDelay bool `description:"申请延期按钮权限:true显示,false不显示"`
  662. BtnTurnPositive bool `description:"申请转正按钮权限:true显示,false不显示"`
  663. BtnThaw bool `description:"申请解冻按钮权限:true显示,false不显示"`
  664. BtnApplyReceive bool `description:"申请领取按钮权限:true显示,false不显示"`
  665. BtnReceive bool `description:"领取客户按钮权限:true显示,false不显示"`
  666. BtnModifySeller bool `description:"修改销售按钮权限:true显示,false不显示"`
  667. BtnReceiveOther bool `description:"领取其他部门客户按钮权限:true显示,false不显示"`
  668. }
  669. type ButtonDetailPermission struct {
  670. BtnEdit bool `description:"编辑按钮权限:true显示,false不显示"`
  671. BtnDelete bool `description:"删除按钮权限:true显示,false不显示"`
  672. BtnHistoryList bool `description:"历史签约按钮权限:true显示,false不显示"`
  673. BaseInfoEdit bool `description:"客户基础信息编辑操作:true可操作,false不可操作"`
  674. FiccEdit bool `description:"ficc类型,编辑按钮权限:true显示,false不显示"`
  675. FiccDelete bool `description:"ficc类型,删除按钮权限:true显示,false不显示"`
  676. RaiEdit bool `description:"权益类型,编辑按钮权限:true显示,false不显示"`
  677. RaiDelete bool `description:"权益类型,删除按钮权限:true显示,false不显示"`
  678. }
  679. func GetCompanyListExport(condition string, pars []interface{}) (items []*CompanyItem, err error) {
  680. o := orm.NewOrm()
  681. sql := `SELECT a.company_id,a.company_name,a.credit_code,a.company_code,a.created_time,a.address,
  682. GROUP_CONCAT(b.status ORDER BY b.product_id ASC SEPARATOR '/') AS status,
  683. GROUP_CONCAT(DISTINCT b.seller_name ORDER BY b.product_id ASC SEPARATOR '/') AS seller_name,
  684. GROUP_CONCAT(DISTINCT b.industry_name ORDER BY b.product_id ASC SEPARATOR '/') AS industry_name,
  685. GROUP_CONCAT(DISTINCT b.product_name ORDER BY b.product_id ASC SEPARATOR '/') AS company_type,
  686. GROUP_CONCAT(b.start_date ORDER BY b.product_id ASC SEPARATOR '/') AS start_date,
  687. GROUP_CONCAT(b.end_date ORDER BY b.product_id ASC SEPARATOR '/') AS end_date,
  688. GROUP_CONCAT(DISTINCT b.loss_time ORDER BY b.product_id ASC SEPARATOR '/') AS loss_time,
  689. GROUP_CONCAT(DISTINCT b.lose_reason ORDER BY b.product_id ASC SEPARATOR '/') AS lose_reason,
  690. GROUP_CONCAT(DISTINCT b.source ORDER BY b.product_id ASC SEPARATOR '/') AS source,
  691. GROUP_CONCAT(DISTINCT b.reasons ORDER BY b.product_id ASC SEPARATOR '/') AS reasons,
  692. GROUP_CONCAT(DISTINCT b.approve_status ORDER BY b.product_id ASC SEPARATOR '/') AS approve_status,
  693. GROUP_CONCAT(DISTINCT b.group_id ORDER BY b.product_id ASC SEPARATOR '/') AS group_id,
  694. b.is_suspend,
  695. b.freeze_start_date,
  696. b.freeze_end_date,
  697. b.seller_id,
  698. b.product_id
  699. FROM company AS a
  700. INNER JOIN company_product AS b ON a.company_id=b.company_id
  701. WHERE a.enabled=1 AND a.company_id<>1 `
  702. if condition != "" {
  703. sql += condition
  704. }
  705. sql += ` GROUP BY a.company_id ORDER BY a.last_updated_time DESC `
  706. _, err = o.Raw(sql, pars).QueryRows(&items)
  707. return
  708. }
  709. //冻结客户请求参数
  710. type CompanyFreezeReq struct {
  711. CompanyId int `description:"客户id"`
  712. CompanyType string `description:"客户类型:ficc/权益"`
  713. Remark string `description:"备注"`
  714. }
  715. //冻结客户
  716. func CompanyFreeze(companyId, productId int) (err error) {
  717. o := orm.NewOrm()
  718. tx, err := o.Begin()
  719. if err != nil {
  720. return
  721. }
  722. defer func() {
  723. if err != nil {
  724. _ = tx.Rollback()
  725. } else {
  726. _ = tx.Commit()
  727. }
  728. }()
  729. freezeStartDate := time.Now().Format(utils.FormatDate)
  730. freezeEndDate := time.Now().AddDate(0, 3, 0).Format(utils.FormatDate)
  731. //修改用户产品状态
  732. sql := `UPDATE company_product SET status='冻结',freeze_time=NOW(),modify_time=NOW(),start_date=?,end_date=?,freeze_start_date=?,freeze_end_date=?,try_stage=1 WHERE company_id=? AND product_id=? `
  733. _, err = tx.Raw(sql, freezeStartDate, freezeEndDate, freezeStartDate, freezeEndDate, companyId, productId).Exec()
  734. if err != nil {
  735. return err
  736. }
  737. //修改产品权限状态为关闭
  738. sql = `UPDATE company_report_permission SET status='关闭',start_date=?,end_date=?,modify_time=NOW() WHERE company_id= ? AND product_id = ? `
  739. _, err = tx.Raw(sql, freezeStartDate, freezeEndDate, companyId, productId).Exec()
  740. if err != nil {
  741. return err
  742. }
  743. //修改客户为关闭
  744. sql = `UPDATE company SET type=3,last_updated_time=NOW(),start_date=?,end_date=? WHERE company_id=? `
  745. _, err = tx.Raw(sql, freezeStartDate, freezeEndDate, companyId).Exec()
  746. if err != nil {
  747. return err
  748. }
  749. return
  750. }
  751. func ModifyCompanyGroupId(sellerId, groupId int) (err error) {
  752. o := orm.NewOrm()
  753. sql := `UPDATE company_product SET group_id=? WHERE seller_id=? `
  754. _, err = o.Raw(sql, groupId, sellerId).Exec()
  755. return
  756. }
  757. func ModifyCompanyDepartmentId(sellerId, departmentId int) (err error) {
  758. o := orm.NewOrm()
  759. sql := `UPDATE company_product SET department_id=? WHERE seller_id=? `
  760. _, err = o.Raw(sql, departmentId, sellerId).Exec()
  761. return
  762. }
  763. type CompanyNameList struct {
  764. CompanyId int `description:"客户ID"`
  765. CompanyName string `description:"客户名称"`
  766. CreditCode string `description:"社会统一信用码"`
  767. }
  768. // GetCompanyNameList 获取客户名称列表
  769. func GetCompanyNameList(condition string, pars []interface{}, startSize, pageSize int) (list []*CompanyNameList, err error) {
  770. o := orm.NewOrm()
  771. sql := ` SELECT company_id,company_name,credit_code FROM company WHERE 1=1 `
  772. sql += condition
  773. sql += ` LIMIT ?,? `
  774. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  775. return
  776. }