english_company.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. package english_report
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "hongze/hz_crm_api/controllers"
  7. "hongze/hz_crm_api/models"
  8. "hongze/hz_crm_api/models/system"
  9. "hongze/hz_crm_api/services"
  10. "hongze/hz_crm_api/services/alarm_msg"
  11. "hongze/hz_crm_api/utils"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // EnglishCompanyController 英文客户
  17. type EnglishCompanyController struct {
  18. controllers.BaseAuthController
  19. }
  20. // List
  21. // @Title 英文客户列表
  22. // @Description 英文客户列表
  23. // @Param Keywords query string false "关键词:客户名称/联系人邮箱/联系人手机号"
  24. // @Param SortType query int false "点击量排序:1-降序; 2-升序"
  25. // @Param EnPermissionIds query string false "品种权限IDs(字符串)"
  26. // @Success 200 {object} models.EnglishCompanyPageListResp
  27. // @router /company/list [get]
  28. func (this *EnglishCompanyController) List() {
  29. br := new(models.BaseResponse).Init()
  30. br.IsSendEmail = false
  31. defer func() {
  32. this.Data["json"] = br
  33. this.ServeJSON()
  34. }()
  35. sysUser := this.SysUser
  36. if sysUser == nil {
  37. br.Msg = "请登录"
  38. br.ErrMsg = "请登录,SysUser Is Empty"
  39. br.Ret = 408
  40. return
  41. }
  42. var startSize int
  43. pageSize, _ := this.GetInt("PageSize")
  44. currentIndex, _ := this.GetInt("CurrentIndex")
  45. if pageSize <= 0 {
  46. pageSize = utils.PageSize20
  47. }
  48. if currentIndex <= 0 {
  49. currentIndex = 1
  50. }
  51. startSize = paging.StartIndex(currentIndex, pageSize)
  52. keywords := this.GetString("Keywords", "")
  53. sortType, _ := this.GetInt("SortType", 0)
  54. sortParam := this.GetString("SortParam", "")
  55. strPermissionIds := this.GetString("EnPermissionIds", "")
  56. sellers := this.GetString("Sellers", "")
  57. var cond, order string
  58. var pars []interface{}
  59. if keywords != "" {
  60. k := "%" + keywords + "%"
  61. companyIds, e := models.GetEnCompanyIdsByKeyword(k)
  62. if e != nil {
  63. br.Msg = "获取失败"
  64. br.ErrMsg = "关键词获取英文客户IDs失败, Err: " + e.Error()
  65. return
  66. }
  67. if len(companyIds) == 0 {
  68. page := paging.GetPaging(currentIndex, pageSize, 0)
  69. resp := &models.EnglishCompanyPageListResp{
  70. Paging: page,
  71. List: make([]*models.EnglishCompanyResp, 0),
  72. }
  73. br.Ret = 200
  74. br.Success = true
  75. br.Msg = "获取成功"
  76. br.Data = resp
  77. return
  78. }
  79. cond += fmt.Sprintf(` AND c.company_id IN (%s) `, utils.GetOrmInReplace(len(companyIds)))
  80. pars = append(pars, companyIds)
  81. }
  82. if sellers != "" {
  83. sellerIds := strings.Split(sellers, ",")
  84. cond += fmt.Sprintf(` AND c.seller_id IN (%s) `, utils.GetOrmInReplace(len(sellerIds)))
  85. pars = append(pars, sellerIds)
  86. }
  87. // 品种权限
  88. if strPermissionIds != "" {
  89. permissionIdArr := strings.Split(strPermissionIds, ",")
  90. permissionIds := make([]int, 0)
  91. for _, s := range permissionIdArr {
  92. p, e := strconv.Atoi(s)
  93. if e != nil {
  94. br.Msg = "品种权限有误"
  95. br.ErrMsg = "品种权限筛选有误"
  96. return
  97. }
  98. permissionIds = append(permissionIds, p)
  99. }
  100. if len(permissionIds) == 0 {
  101. br.Msg = "品种权限有误"
  102. br.ErrMsg = "品种权限筛选ID为空"
  103. return
  104. }
  105. queryCond := fmt.Sprintf(` AND %s IN (%s)`, models.EnCompanyPermissionColumns.EnPermissionId, utils.GetOrmInReplace(len(permissionIds)))
  106. queryPars := make([]interface{}, 0)
  107. queryPars = append(queryPars, permissionIds)
  108. queryOB := new(models.EnCompanyPermission)
  109. queryList, e := queryOB.GetItemsByCondition(queryCond, queryPars, []string{"DISTINCT en_company_id"}, "")
  110. if e != nil {
  111. br.Msg = "品种筛选有误"
  112. br.ErrMsg = "品种筛选失败, Err: " + e.Error()
  113. return
  114. }
  115. companyIds := make([]int, 0)
  116. for _, q := range queryList {
  117. companyIds = append(companyIds, q.EnCompanyId)
  118. }
  119. if len(companyIds) == 0 {
  120. page := paging.GetPaging(currentIndex, pageSize, 0)
  121. resp := &models.EnglishCompanyPageListResp{
  122. Paging: page,
  123. List: make([]*models.EnglishCompanyResp, 0),
  124. }
  125. br.Ret = 200
  126. br.Success = true
  127. br.Msg = "获取成功"
  128. br.Data = resp
  129. return
  130. }
  131. cond += fmt.Sprintf(` AND c.company_id IN (%s) `, utils.GetOrmInReplace(len(companyIds)))
  132. pars = append(pars, companyIds)
  133. }
  134. if sortParam == "" {
  135. if sortType == 1 {
  136. order = ` ORDER BY c.view_total DESC`
  137. }
  138. if sortType == 2 {
  139. order = ` ORDER BY c.view_total ASC`
  140. }
  141. } else if sortParam == "deadLine" {
  142. order = ` ORDER BY todo_end_time asc, c.view_total desc, c.company_id desc`
  143. } else if sortParam == "todoStatusStr" {
  144. order = ` ORDER BY FIELD (todo_status_str,'进行中','已完成','无任务'), c.view_total desc, c.company_id desc`
  145. }
  146. total, list, e := models.GetEnglishCompanyPageList(cond, pars, order, startSize, pageSize)
  147. if e != nil {
  148. br.Msg = "获取失败"
  149. br.ErrMsg = "获取英文客户列表失败, Err: " + e.Error()
  150. return
  151. }
  152. // 客户列表TODO信息
  153. todoMap := services.GetEnglishCompanyListTodoMap(list, sysUser)
  154. // 品种权限
  155. permissionMap := make(map[int][]int)
  156. {
  157. companyIds := make([]int, 0)
  158. for _, v := range list {
  159. companyIds = append(companyIds, v.CompanyId)
  160. }
  161. if len(companyIds) > 0 {
  162. cond := fmt.Sprintf(` AND %s IN (%s)`, models.EnCompanyPermissionColumns.EnCompanyId, utils.GetOrmInReplace(len(companyIds)))
  163. pars := make([]interface{}, 0)
  164. pars = append(pars, companyIds)
  165. ob := new(models.EnCompanyPermission)
  166. items, e := ob.GetItemsByCondition(cond, pars, []string{}, "")
  167. if e != nil {
  168. br.Msg = "获取失败"
  169. br.ErrMsg = "获取客户权限列表失败, Err: " + e.Error()
  170. return
  171. }
  172. for _, v := range items {
  173. if permissionMap[v.EnCompanyId] == nil {
  174. permissionMap[v.EnCompanyId] = make([]int, 0)
  175. }
  176. permissionMap[v.EnCompanyId] = append(permissionMap[v.EnCompanyId], v.EnPermissionId)
  177. }
  178. }
  179. }
  180. respList := make([]*models.EnglishCompanyResp, 0)
  181. for i := range list {
  182. respList = append(respList, &models.EnglishCompanyResp{
  183. CompanyId: list[i].CompanyId,
  184. CompanyName: list[i].CompanyName,
  185. CountryCode: list[i].CountryCode,
  186. Country: list[i].Country,
  187. SellerId: list[i].SellerId,
  188. SellerName: list[i].SellerName,
  189. ViewTotal: list[i].ViewTotal,
  190. CreateTime: list[i].CreateTime.Format(utils.FormatDateTime),
  191. Enabled: list[i].Enabled,
  192. TodoInfo: todoMap[list[i].CompanyId],
  193. EnPermissions: permissionMap[list[i].CompanyId],
  194. })
  195. }
  196. page := paging.GetPaging(currentIndex, pageSize, total)
  197. resp := &models.EnglishCompanyPageListResp{
  198. Paging: page,
  199. List: respList,
  200. }
  201. br.Ret = 200
  202. br.Success = true
  203. br.Msg = "获取成功"
  204. br.Data = resp
  205. }
  206. // Save
  207. // @Title 保存英文客户
  208. // @Description 保存英文客户
  209. // @Param request body models.EnglishCompanySaveReq true "type json string"
  210. // @Success 200 string "操作成功"
  211. // @router /company/save [post]
  212. func (this *EnglishCompanyController) Save() {
  213. br := new(models.BaseResponse).Init()
  214. br.IsSendEmail = false
  215. defer func() {
  216. this.Data["json"] = br
  217. this.ServeJSON()
  218. }()
  219. sysUser := this.SysUser
  220. if sysUser == nil {
  221. br.Msg = "请登录"
  222. br.ErrMsg = "请登录,SysUser Is Empty"
  223. br.Ret = 408
  224. return
  225. }
  226. var req models.EnglishCompanySaveReq
  227. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  228. br.Msg = "参数解析异常!"
  229. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  230. return
  231. }
  232. req.CompanyName = strings.TrimSpace(req.CompanyName)
  233. if req.CompanyName == "" {
  234. br.Msg = "请输入客户名称"
  235. return
  236. }
  237. if req.CountryCode == "" || req.Country == "" {
  238. br.Msg = "请选择国家"
  239. return
  240. }
  241. if req.SellerId <= 0 {
  242. br.Msg = "请选择销售"
  243. return
  244. }
  245. if len(req.EnPermissions) <= 0 {
  246. br.Msg = "请至少选择一个品种权限"
  247. return
  248. }
  249. seller, e := system.GetSysAdminById(req.SellerId)
  250. if e != nil {
  251. br.Msg = "销售信息有误"
  252. br.ErrMsg = "获取销售信息失败, Err:" + e.Error()
  253. return
  254. }
  255. if seller.Enabled != 1 {
  256. br.Msg = "该销售已被禁用"
  257. return
  258. }
  259. // 重名校验
  260. exist, e := models.GetEnglishCompanyByName(req.CompanyName)
  261. if e != nil && e.Error() != utils.ErrNoRow() {
  262. br.Msg = "保存失败"
  263. br.ErrMsg = "获取客户信息失败, Err:" + e.Error()
  264. return
  265. }
  266. item := new(models.EnglishCompany)
  267. nowTime := time.Now().Local()
  268. opType := 0
  269. if req.CompanyId <= 0 {
  270. if exist != nil {
  271. br.Msg = "客户名称已存在"
  272. return
  273. }
  274. opType = models.EnglishCompanyLogOptionTypeAdd
  275. // 新增
  276. item.CompanyName = req.CompanyName
  277. item.CountryCode = req.CountryCode
  278. item.Country = req.Country
  279. item.SellerId = req.SellerId
  280. item.SellerName = seller.RealName
  281. item.CreateTime = nowTime
  282. item.ModifyTime = nowTime
  283. item.Status = 1
  284. item.Enabled = 1
  285. if req.Country != "" && req.Nation == "" {
  286. item.Nation = req.Country
  287. } else {
  288. item.Nation = req.Nation
  289. }
  290. item.OverseasLabel = 1
  291. item.OverseasStatus = "试用"
  292. item.ResetBtn = 1
  293. if e = item.Create(); e != nil {
  294. br.Msg = "保存失败"
  295. br.ErrMsg = "新增英文客户失败, Err:" + e.Error()
  296. return
  297. }
  298. } else {
  299. if exist != nil && exist.CompanyId != req.CompanyId {
  300. br.Msg = "客户名称已存在"
  301. return
  302. }
  303. opType = models.EnglishCompanyLogOptionTypeEdit
  304. // 编辑
  305. ec, e := models.GetEnglishCompanyById(req.CompanyId)
  306. if e != nil {
  307. br.Msg = "保存失败"
  308. br.ErrMsg = "获取英文客户信息失败, Err:" + e.Error()
  309. return
  310. }
  311. item = ec
  312. item.CompanyName = req.CompanyName
  313. item.CountryCode = req.CountryCode
  314. item.Country = req.Country
  315. item.SellerId = req.SellerId
  316. item.SellerName = seller.RealName
  317. item.ModifyTime = nowTime
  318. item.Nation = req.Nation
  319. updateCols := []string{"CompanyName", "CountryCode", "Country", "SellerId", "SellerName", "ModifyTime", "Nation"}
  320. if e = item.Update(updateCols); e != nil {
  321. br.Msg = "保存失败"
  322. br.ErrMsg = "更新英文客户信息失败, Err:" + e.Error()
  323. return
  324. }
  325. }
  326. // 品种权限
  327. permissions := make([]*models.EnCompanyPermission, 0)
  328. for _, p := range req.EnPermissions {
  329. v := new(models.EnCompanyPermission)
  330. v.EnPermissionId = p
  331. v.EnCompanyId = item.CompanyId
  332. v.CreateTime = nowTime
  333. permissions = append(permissions, v)
  334. }
  335. if e = models.ClearAndCreateEnCompanyPermissions(item.CompanyId, permissions); e != nil {
  336. br.Msg = "保存失败"
  337. br.ErrMsg = "更新客户品种权限失败, Err: " + e.Error()
  338. return
  339. }
  340. // 记录日志
  341. go func() {
  342. opByte, e := json.Marshal(req)
  343. if e != nil {
  344. return
  345. }
  346. opData := string(opByte)
  347. opLog := &models.EnglishCompanyLog{
  348. CompanyId: item.CompanyId,
  349. AdminId: sysUser.AdminId,
  350. OptionData: opData,
  351. OptionType: opType,
  352. CreateTime: nowTime,
  353. }
  354. if e = opLog.Create(); e != nil {
  355. return
  356. }
  357. }()
  358. br.Ret = 200
  359. br.Success = true
  360. br.Msg = "操作成功"
  361. }
  362. // Del
  363. // @Title 删除英文客户及联系人
  364. // @Description 删除英文客户及联系人
  365. // @Param request body models.EnglishCompanyDelReq true "type json string"
  366. // @Success 200 string "操作成功"
  367. // @router /company/del [post]
  368. func (this *EnglishCompanyController) Del() {
  369. br := new(models.BaseResponse).Init()
  370. br.IsSendEmail = false
  371. defer func() {
  372. this.Data["json"] = br
  373. this.ServeJSON()
  374. }()
  375. sysUser := this.SysUser
  376. if sysUser == nil {
  377. br.Msg = "请登录"
  378. br.ErrMsg = "请登录,SysUser Is Empty"
  379. br.Ret = 408
  380. return
  381. }
  382. var req models.EnglishCompanyDelReq
  383. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  384. br.Msg = "参数解析异常!"
  385. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  386. return
  387. }
  388. if req.CompanyId <= 0 {
  389. br.Msg = "参数有误"
  390. return
  391. }
  392. item, e := models.GetEnglishCompanyById(req.CompanyId)
  393. if e != nil {
  394. br.Msg = "删除失败"
  395. br.ErrMsg = "获取英文客户信息失败, Err:" + e.Error()
  396. return
  397. }
  398. if e := models.DeleteEnglishCompanyAndEmails(req.CompanyId); e != nil {
  399. br.Msg = "删除失败"
  400. br.ErrMsg = "删除英文客户及联系人失败, Err: " + e.Error()
  401. return
  402. }
  403. // 删除权限
  404. if e = models.ClearAndCreateEnCompanyPermissions(item.CompanyId, make([]*models.EnCompanyPermission, 0)); e != nil {
  405. br.Msg = "删除失败"
  406. br.ErrMsg = "删除英文客户权限失败, Err: " + e.Error()
  407. return
  408. }
  409. // 记录日志
  410. go func() {
  411. nowTime := time.Now().Local()
  412. opByte, e := json.Marshal(req)
  413. if e != nil {
  414. return
  415. }
  416. opData := string(opByte)
  417. opLog := &models.EnglishCompanyLog{
  418. CompanyId: item.CompanyId,
  419. AdminId: sysUser.AdminId,
  420. OptionData: opData,
  421. OptionType: models.EnglishCompanyLogOptionTypeDel,
  422. CreateTime: nowTime,
  423. }
  424. if e = opLog.Create(); e != nil {
  425. return
  426. }
  427. }()
  428. // 同步删除客户TODO
  429. go func() {
  430. if e = models.DeleteEnglishCompanyTodoByCompanyId(req.CompanyId); e != nil {
  431. alarm_msg.SendAlarmMsg("删除英文客户同步删除TODO失败, ErrMsg: "+e.Error(), 3)
  432. }
  433. }()
  434. br.Ret = 200
  435. br.Success = true
  436. br.Msg = "操作成功"
  437. }
  438. // Detail
  439. // @Title 英文客户详情
  440. // @Description 英文客户详情
  441. // @Param CompanyId query int true "客户ID"
  442. // @Success 200 {object} models.EnglishCompanyResp
  443. // @router /company/detail [get]
  444. func (this *EnglishCompanyController) Detail() {
  445. br := new(models.BaseResponse).Init()
  446. br.IsSendEmail = false
  447. defer func() {
  448. this.Data["json"] = br
  449. this.ServeJSON()
  450. }()
  451. sysUser := this.SysUser
  452. if sysUser == nil {
  453. br.Msg = "请登录"
  454. br.ErrMsg = "请登录,SysUser Is Empty"
  455. br.Ret = 408
  456. return
  457. }
  458. companyId, _ := this.GetInt("CompanyId", 0)
  459. if companyId <= 0 {
  460. br.Msg = "参数有误"
  461. return
  462. }
  463. item, e := models.GetEnglishCompanyById(companyId)
  464. if e != nil {
  465. br.Msg = "获取失败, 客户或已被删除"
  466. return
  467. }
  468. // 品种权限
  469. permissions := make([]int, 0)
  470. {
  471. cond := fmt.Sprintf(` AND %s = ?`, models.EnCompanyPermissionColumns.EnCompanyId)
  472. pars := make([]interface{}, 0)
  473. pars = append(pars, item.CompanyId)
  474. ob := new(models.EnCompanyPermission)
  475. list, e := ob.GetItemsByCondition(cond, pars, []string{}, "")
  476. if e != nil {
  477. br.Msg = "获取失败"
  478. br.ErrMsg = "获取英文客户权限失败, Err: " + e.Error()
  479. return
  480. }
  481. for _, v := range list {
  482. permissions = append(permissions, v.EnPermissionId)
  483. }
  484. }
  485. resp := &models.EnglishCompanyResp{
  486. CompanyId: item.CompanyId,
  487. CompanyName: item.CompanyName,
  488. CountryCode: item.CountryCode,
  489. Country: item.Country,
  490. SellerId: item.SellerId,
  491. SellerName: item.SellerName,
  492. ViewTotal: item.ViewTotal,
  493. CreateTime: item.CreateTime.Format(utils.FormatDateTime),
  494. EnPermissions: permissions,
  495. }
  496. br.Ret = 200
  497. br.Success = true
  498. br.Msg = "获取成功"
  499. br.Data = resp
  500. }
  501. // ViewList
  502. // @Title 英文客户-点击量详情列表
  503. // @Description 英文客户-点击量详情列表
  504. // @Param CompanyId query int false "客户ID"
  505. // @Param SortParam query int false "排序字段:1-点击量;2-点击时间"
  506. // @Param SortType query int false "排序方式:1-倒序;2-正序"
  507. // @Success 200 {object} models.GetEnglishCompanyViewPageListResp
  508. // @router /company/view_list [get]
  509. func (this *EnglishCompanyController) ViewList() {
  510. br := new(models.BaseResponse).Init()
  511. br.IsSendEmail = false
  512. defer func() {
  513. this.Data["json"] = br
  514. this.ServeJSON()
  515. }()
  516. sysUser := this.SysUser
  517. if sysUser == nil {
  518. br.Msg = "请登录"
  519. br.ErrMsg = "请登录,SysUser Is Empty"
  520. br.Ret = 408
  521. return
  522. }
  523. // 默认点击时间降序
  524. companyId, _ := this.GetInt("CompanyId", 0)
  525. sortParam, _ := this.GetInt("SortParam", 2)
  526. sortType, _ := this.GetInt("SortType", 1)
  527. var cond, order string
  528. var pars []interface{}
  529. if companyId > 0 {
  530. cond += ` AND company_id = ?`
  531. pars = append(pars, companyId)
  532. }
  533. sortArr := []int{1, 2}
  534. if utils.InArrayByInt(sortArr, sortParam) && utils.InArrayByInt(sortArr, sortType) {
  535. pMap := map[int]string{1: "view_total", 2: "last_view_time"}
  536. tMap := map[int]string{1: "DESC", 2: "ASC"}
  537. order = fmt.Sprintf(` ORDER BY %s %s`, pMap[sortParam], tMap[sortType])
  538. }
  539. var startSize int
  540. pageSize, _ := this.GetInt("PageSize")
  541. currentIndex, _ := this.GetInt("CurrentIndex")
  542. if pageSize <= 0 {
  543. pageSize = utils.PageSize20
  544. }
  545. if currentIndex <= 0 {
  546. currentIndex = 1
  547. }
  548. startSize = paging.StartIndex(currentIndex, pageSize)
  549. // 注:点击量列表是包含被删除的联系人的历史点击量
  550. total, list, e := models.GetEnglishCompanyViewPageList(cond, pars, order, startSize, pageSize)
  551. if e != nil {
  552. br.Msg = "获取失败"
  553. br.ErrMsg = "获取英文客户点击量详情列表失败, Err: " + e.Error()
  554. return
  555. }
  556. respList := make([]*models.EnglishCompanyViewResp, 0)
  557. for i := range list {
  558. v := &models.EnglishCompanyViewResp{
  559. EmailId: list[i].Id,
  560. UserName: list[i].Name,
  561. Email: list[i].Email,
  562. ViewTotal: list[i].ViewTotal,
  563. LastViewTime: list[i].LastViewTime.Format(utils.FormatDateTime),
  564. }
  565. if v.LastViewTime == "0001-01-01 00:00:00" {
  566. v.LastViewTime = ""
  567. }
  568. respList = append(respList, v)
  569. }
  570. page := paging.GetPaging(currentIndex, pageSize, total)
  571. resp := &models.GetEnglishCompanyViewPageListResp{
  572. Paging: page,
  573. List: respList,
  574. }
  575. br.Ret = 200
  576. br.Success = true
  577. br.Msg = "获取成功"
  578. br.Data = resp
  579. }
  580. // EditEnabled
  581. // @Title 英文客户-启用/禁用
  582. // @Description 英文客户-启用/禁用
  583. // @Param request body models.EnglishCompanyEditEnabledReq true "type json string"
  584. // @Success 200 string "操作成功"
  585. // @router /company/edit_enabled [post]
  586. func (this *EnglishCompanyController) EditEnabled() {
  587. br := new(models.BaseResponse).Init()
  588. br.IsSendEmail = false
  589. defer func() {
  590. this.Data["json"] = br
  591. this.ServeJSON()
  592. }()
  593. sysUser := this.SysUser
  594. if sysUser == nil {
  595. br.Msg = "请登录"
  596. br.ErrMsg = "请登录,SysUser Is Empty"
  597. br.Ret = 408
  598. return
  599. }
  600. var req models.EnglishCompanyEditEnabledReq
  601. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  602. br.Msg = "参数解析异常!"
  603. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  604. return
  605. }
  606. if req.CompanyId <= 0 {
  607. br.Msg = "参数有误"
  608. return
  609. }
  610. if req.Enabled != 0 && req.Enabled != 1 {
  611. br.Msg = "参数有误"
  612. return
  613. }
  614. item, e := models.GetEnglishCompanyById(req.CompanyId)
  615. if e != nil {
  616. br.Msg = "客户已被删除, 请刷新页面"
  617. return
  618. }
  619. // 更新客户
  620. nowTime := time.Now().Local()
  621. item.Enabled = req.Enabled
  622. item.ModifyTime = nowTime
  623. updateCols := []string{"Enabled", "ModifyTime"}
  624. if e = item.Update(updateCols); e != nil {
  625. br.Msg = "操作失败"
  626. br.ErrMsg = "更新英文客户状态失败, Err:" + e.Error()
  627. return
  628. }
  629. // 更新其下联系人状态
  630. if e = models.UpdateEnglishEmailEnabledByCompanyId(item.CompanyId, req.Enabled); e != nil {
  631. br.Msg = "操作失败"
  632. br.ErrMsg = "更新英文客户下的联系人状态失败, Err: " + e.Error()
  633. return
  634. }
  635. // 记录日志
  636. go func() {
  637. opByte, e := json.Marshal(req)
  638. if e != nil {
  639. return
  640. }
  641. opData := string(opByte)
  642. opLog := &models.EnglishCompanyLog{
  643. CompanyId: item.CompanyId,
  644. AdminId: sysUser.AdminId,
  645. OptionData: opData,
  646. OptionType: models.EnglishCompanyLogOptionTypeEditEnabled,
  647. CreateTime: nowTime,
  648. }
  649. if e = opLog.Create(); e != nil {
  650. return
  651. }
  652. }()
  653. br.Ret = 200
  654. br.Success = true
  655. br.Msg = "操作成功"
  656. }
  657. // SavePermission
  658. // @Title 英文客户-保存权限
  659. // @Description 英文客户-保存权限
  660. // @Param request body models.EnglishCompanySaveReq true "type json string"
  661. // @Success 200 string "操作成功"
  662. // @router /company/save_permission [post]
  663. func (this *EnglishCompanyController) SavePermission() {
  664. br := new(models.BaseResponse).Init()
  665. br.IsSendEmail = false
  666. defer func() {
  667. this.Data["json"] = br
  668. this.ServeJSON()
  669. }()
  670. sysUser := this.SysUser
  671. if sysUser == nil {
  672. br.Msg = "请登录"
  673. br.ErrMsg = "请登录,SysUser Is Empty"
  674. br.Ret = 408
  675. return
  676. }
  677. var req models.EnglishCompanySaveReq
  678. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  679. br.Msg = "参数解析异常!"
  680. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  681. return
  682. }
  683. if req.CompanyId <= 0 {
  684. br.Msg = "参数有误"
  685. br.ErrMsg = "参数有误"
  686. return
  687. }
  688. if len(req.EnPermissions) <= 0 {
  689. br.Msg = "请至少选择一个品种权限"
  690. return
  691. }
  692. nowTime := time.Now().Local()
  693. item, e := models.GetEnglishCompanyById(req.CompanyId)
  694. if e != nil {
  695. if e.Error() == utils.ErrNoRow() {
  696. br.Msg = "客户已被删除, 请刷新页面"
  697. return
  698. }
  699. br.Msg = "操作失败"
  700. br.ErrMsg = "获取客户信息失败, Err: " + e.Error()
  701. return
  702. }
  703. // 品种权限
  704. permissions := make([]*models.EnCompanyPermission, 0)
  705. for _, p := range req.EnPermissions {
  706. v := new(models.EnCompanyPermission)
  707. v.EnPermissionId = p
  708. v.EnCompanyId = item.CompanyId
  709. v.CreateTime = nowTime
  710. permissions = append(permissions, v)
  711. }
  712. if e := models.ClearAndCreateEnCompanyPermissions(item.CompanyId, permissions); e != nil {
  713. br.Msg = "保存失败"
  714. br.ErrMsg = "更新客户品种权限失败, Err: " + e.Error()
  715. return
  716. }
  717. // 记录日志
  718. go func() {
  719. opByte, e := json.Marshal(req)
  720. if e != nil {
  721. return
  722. }
  723. opData := string(opByte)
  724. opLog := &models.EnglishCompanyLog{
  725. CompanyId: item.CompanyId,
  726. AdminId: sysUser.AdminId,
  727. OptionData: opData,
  728. OptionType: models.EnglishCompanyLogOptionTypeSavePermission,
  729. CreateTime: nowTime,
  730. }
  731. if e = opLog.Create(); e != nil {
  732. return
  733. }
  734. }()
  735. br.Ret = 200
  736. br.Success = true
  737. br.Msg = "操作成功"
  738. }