english_company.go 20 KB

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