123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770 |
- package english_report
- import (
- "encoding/json"
- "eta_gn/eta_api/controllers"
- "eta_gn/eta_api/models"
- "eta_gn/eta_api/models/system"
- "eta_gn/eta_api/services"
- "eta_gn/eta_api/services/alarm_msg"
- "eta_gn/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "strconv"
- "strings"
- "time"
- )
- // EnglishCompanyController 英文客户
- type EnglishCompanyController struct {
- controllers.BaseAuthController
- }
- // List
- // @Title 英文客户列表
- // @Description 英文客户列表
- // @Param Keywords query string false "关键词:客户名称/联系人邮箱/联系人手机号"
- // @Param SortType query int false "点击量排序:1-降序; 2-升序"
- // @Param EnPermissionIds query string false "品种权限IDs(字符串)"
- // @Success 200 {object} models.EnglishCompanyPageListResp
- // @router /company/list [get]
- func (this *EnglishCompanyController) List() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var startSize int
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = paging.StartIndex(currentIndex, pageSize)
- keywords := this.GetString("Keywords", "")
- sortType, _ := this.GetInt("SortType", 0)
- sortParam := this.GetString("SortParam", "")
- strPermissionIds := this.GetString("EnPermissionIds", "")
- var cond, order string
- var pars []interface{}
- if keywords != "" {
- companyIds, e := models.GetEnCompanyIdsByKeyword(keywords)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "关键词获取英文客户IDs失败, Err: " + e.Error()
- return
- }
- if len(companyIds) == 0 {
- page := paging.GetPaging(currentIndex, pageSize, 0)
- resp := &models.EnglishCompanyPageListResp{
- Paging: page,
- List: make([]*models.EnglishCompanyResp, 0),
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- return
- }
- cond += fmt.Sprintf(` AND c.company_id IN (%s) `, utils.GetOrmInReplace(len(companyIds)))
- pars = append(pars, companyIds)
- }
- // 品种权限
- if strPermissionIds != "" {
- permissionIdArr := strings.Split(strPermissionIds, ",")
- permissionIds := make([]int, 0)
- for _, s := range permissionIdArr {
- p, e := strconv.Atoi(s)
- if e != nil {
- br.Msg = "品种权限有误"
- br.ErrMsg = "品种权限筛选有误"
- return
- }
- permissionIds = append(permissionIds, p)
- }
- if len(permissionIds) == 0 {
- br.Msg = "品种权限有误"
- br.ErrMsg = "品种权限筛选ID为空"
- return
- }
- queryCond := fmt.Sprintf(` AND %s IN (%s)`, models.EnCompanyPermissionColumns.EnPermissionId, utils.GetOrmInReplace(len(permissionIds)))
- queryPars := make([]interface{}, 0)
- queryPars = append(queryPars, permissionIds)
- queryOB := new(models.EnCompanyPermission)
- queryList, e := queryOB.GetItemsByCondition(queryCond, queryPars, []string{"DISTINCT en_company_id"}, "")
- if e != nil {
- br.Msg = "品种筛选有误"
- br.ErrMsg = "品种筛选失败, Err: " + e.Error()
- return
- }
- companyIds := make([]int, 0)
- for _, q := range queryList {
- companyIds = append(companyIds, q.EnCompanyId)
- }
- if len(companyIds) == 0 {
- page := paging.GetPaging(currentIndex, pageSize, 0)
- resp := &models.EnglishCompanyPageListResp{
- Paging: page,
- List: make([]*models.EnglishCompanyResp, 0),
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- return
- }
- cond += fmt.Sprintf(` AND c.company_id IN (%s) `, utils.GetOrmInReplace(len(companyIds)))
- pars = append(pars, companyIds)
- }
- if sortParam == "" {
- if sortType == 1 {
- order = ` ORDER BY c.view_total DESC`
- }
- if sortType == 2 {
- order = ` ORDER BY c.view_total ASC`
- }
- } else if sortParam == "deadLine" {
- order = ` ORDER BY todo_end_time asc, c.view_total desc, c.company_id desc`
- } else if sortParam == "todoStatusStr" {
- order = ` ORDER BY FIELD (todo_status_str,'进行中','已完成','无任务'), c.view_total desc, c.company_id desc`
- }
- total, list, e := models.GetEnglishCompanyPageList(cond, pars, order, startSize, pageSize)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取英文客户列表失败, Err: " + e.Error()
- return
- }
- // 客户列表TODO信息
- todoMap := services.GetEnglishCompanyListTodoMap(list, sysUser)
- // 品种权限
- permissionMap := make(map[int][]int)
- {
- companyIds := make([]int, 0)
- for _, v := range list {
- companyIds = append(companyIds, v.CompanyId)
- }
- if len(companyIds) > 0 {
- cond := fmt.Sprintf(` AND %s IN (%s)`, models.EnCompanyPermissionColumns.EnCompanyId, utils.GetOrmInReplace(len(companyIds)))
- pars := make([]interface{}, 0)
- pars = append(pars, companyIds)
- ob := new(models.EnCompanyPermission)
- items, e := ob.GetItemsByCondition(cond, pars, []string{}, "")
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取客户权限列表失败, Err: " + e.Error()
- return
- }
- for _, v := range items {
- if permissionMap[v.EnCompanyId] == nil {
- permissionMap[v.EnCompanyId] = make([]int, 0)
- }
- permissionMap[v.EnCompanyId] = append(permissionMap[v.EnCompanyId], v.EnPermissionId)
- }
- }
- }
- respList := make([]*models.EnglishCompanyResp, 0)
- for i := range list {
- respList = append(respList, &models.EnglishCompanyResp{
- CompanyId: list[i].CompanyId,
- CompanyName: list[i].CompanyName,
- CountryCode: list[i].CountryCode,
- Country: list[i].Country,
- SellerId: list[i].SellerId,
- SellerName: list[i].SellerName,
- ViewTotal: list[i].ViewTotal,
- CreateTime: list[i].CreateTime.Format(utils.FormatDateTime),
- Enabled: list[i].Enabled,
- TodoInfo: todoMap[list[i].CompanyId],
- EnPermissions: permissionMap[list[i].CompanyId],
- })
- }
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp := &models.EnglishCompanyPageListResp{
- Paging: page,
- List: respList,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // Save
- // @Title 保存英文客户
- // @Description 保存英文客户
- // @Param request body models.EnglishCompanySaveReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /company/save [post]
- func (this *EnglishCompanyController) Save() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EnglishCompanySaveReq
- if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- req.CompanyName = strings.TrimSpace(req.CompanyName)
- if req.CompanyName == "" {
- br.Msg = "请输入客户名称"
- return
- }
- if req.CountryCode == "" || req.Country == "" {
- br.Msg = "请选择国家"
- return
- }
- if req.SellerId <= 0 {
- br.Msg = "请选择销售"
- return
- }
- if len(req.EnPermissions) <= 0 {
- br.Msg = "请至少选择一个品种权限"
- return
- }
- seller, e := system.GetSysAdminById(req.SellerId)
- if e != nil {
- br.Msg = "销售信息有误"
- br.ErrMsg = "获取销售信息失败, Err:" + e.Error()
- return
- }
- if seller.Enabled != 1 {
- br.Msg = "该销售已被禁用"
- return
- }
- // 重名校验
- exist, e := models.GetEnglishCompanyByName(req.CompanyName)
- if e != nil && e.Error() != utils.ErrNoRow() {
- br.Msg = "保存失败"
- br.ErrMsg = "获取客户信息失败, Err:" + e.Error()
- return
- }
- item := new(models.EnglishCompany)
- nowTime := time.Now().Local()
- opType := 0
- if req.CompanyId <= 0 {
- if exist != nil {
- br.Msg = "客户名称已存在"
- return
- }
- opType = models.EnglishCompanyLogOptionTypeAdd
- // 新增
- item.CompanyName = req.CompanyName
- item.CountryCode = req.CountryCode
- item.Country = req.Country
- item.SellerId = req.SellerId
- item.SellerName = seller.RealName
- item.CreateTime = nowTime
- item.ModifyTime = nowTime
- item.Status = 1
- item.Enabled = 1
- if e = item.Create(); e != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "新增英文客户失败, Err:" + e.Error()
- return
- }
- } else {
- if exist != nil && exist.CompanyId != req.CompanyId {
- br.Msg = "客户名称已存在"
- return
- }
- opType = models.EnglishCompanyLogOptionTypeEdit
- // 编辑
- ec, e := models.GetEnglishCompanyById(req.CompanyId)
- if e != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "获取英文客户信息失败, Err:" + e.Error()
- return
- }
- item = ec
- item.CompanyName = req.CompanyName
- item.CountryCode = req.CountryCode
- item.Country = req.Country
- item.SellerId = req.SellerId
- item.SellerName = seller.RealName
- item.ModifyTime = nowTime
- updateCols := []string{"CompanyName", "CountryCode", "Country", "SellerId", "SellerName", "ModifyTime"}
- if e = item.Update(updateCols); e != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "更新英文客户信息失败, Err:" + e.Error()
- return
- }
- }
- // 品种权限
- permissions := make([]*models.EnCompanyPermission, 0)
- for _, p := range req.EnPermissions {
- v := new(models.EnCompanyPermission)
- v.EnPermissionId = p
- v.EnCompanyId = item.CompanyId
- v.CreateTime = nowTime
- permissions = append(permissions, v)
- }
- if e = models.ClearAndCreateEnCompanyPermissions(item.CompanyId, permissions); e != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "更新客户品种权限失败, Err: " + e.Error()
- return
- }
- // 记录日志
- go func() {
- opByte, e := json.Marshal(req)
- if e != nil {
- return
- }
- opData := string(opByte)
- opLog := &models.EnglishCompanyLog{
- CompanyId: item.CompanyId,
- AdminId: sysUser.AdminId,
- OptionData: opData,
- OptionType: opType,
- CreateTime: nowTime,
- }
- if e = opLog.Create(); e != nil {
- return
- }
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // Del
- // @Title 删除英文客户及联系人
- // @Description 删除英文客户及联系人
- // @Param request body models.EnglishCompanyDelReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /company/del [post]
- func (this *EnglishCompanyController) Del() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EnglishCompanyDelReq
- if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.CompanyId <= 0 {
- br.Msg = "参数有误"
- return
- }
- item, e := models.GetEnglishCompanyById(req.CompanyId)
- if e != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "获取英文客户信息失败, Err:" + e.Error()
- return
- }
- if e := models.DeleteEnglishCompanyAndEmails(req.CompanyId); e != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "删除英文客户及联系人失败, Err: " + e.Error()
- return
- }
- // 删除权限
- if e = models.ClearAndCreateEnCompanyPermissions(item.CompanyId, make([]*models.EnCompanyPermission, 0)); e != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "删除英文客户权限失败, Err: " + e.Error()
- return
- }
- // 记录日志
- go func() {
- nowTime := time.Now().Local()
- opByte, e := json.Marshal(req)
- if e != nil {
- return
- }
- opData := string(opByte)
- opLog := &models.EnglishCompanyLog{
- CompanyId: item.CompanyId,
- AdminId: sysUser.AdminId,
- OptionData: opData,
- OptionType: models.EnglishCompanyLogOptionTypeDel,
- CreateTime: nowTime,
- }
- if e = opLog.Create(); e != nil {
- return
- }
- }()
- // 同步删除客户TODO
- go func() {
- if e = models.DeleteEnglishCompanyTodoByCompanyId(req.CompanyId); e != nil {
- alarm_msg.SendAlarmMsg("删除英文客户同步删除TODO失败, ErrMsg: "+e.Error(), 3)
- }
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // Detail
- // @Title 英文客户详情
- // @Description 英文客户详情
- // @Param CompanyId query int true "客户ID"
- // @Success 200 {object} models.EnglishCompanyResp
- // @router /company/detail [get]
- func (this *EnglishCompanyController) Detail() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- companyId, _ := this.GetInt("CompanyId", 0)
- if companyId <= 0 {
- br.Msg = "参数有误"
- return
- }
- item, e := models.GetEnglishCompanyById(companyId)
- if e != nil {
- br.Msg = "获取失败, 客户或已被删除"
- return
- }
- // 品种权限
- permissions := make([]int, 0)
- {
- cond := fmt.Sprintf(` AND %s = ?`, models.EnCompanyPermissionColumns.EnCompanyId)
- pars := make([]interface{}, 0)
- pars = append(pars, item.CompanyId)
- ob := new(models.EnCompanyPermission)
- list, e := ob.GetItemsByCondition(cond, pars, []string{}, "")
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取英文客户权限失败, Err: " + e.Error()
- return
- }
- for _, v := range list {
- permissions = append(permissions, v.EnPermissionId)
- }
- }
- resp := &models.EnglishCompanyResp{
- CompanyId: item.CompanyId,
- CompanyName: item.CompanyName,
- CountryCode: item.CountryCode,
- Country: item.Country,
- SellerId: item.SellerId,
- SellerName: item.SellerName,
- ViewTotal: item.ViewTotal,
- CreateTime: item.CreateTime.Format(utils.FormatDateTime),
- EnPermissions: permissions,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // ViewList
- // @Title 英文客户-点击量详情列表
- // @Description 英文客户-点击量详情列表
- // @Param CompanyId query int false "客户ID"
- // @Param SortParam query int false "排序字段:1-点击量;2-点击时间"
- // @Param SortType query int false "排序方式:1-倒序;2-正序"
- // @Success 200 {object} models.GetEnglishCompanyViewPageListResp
- // @router /company/view_list [get]
- func (this *EnglishCompanyController) ViewList() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- // 默认点击时间降序
- companyId, _ := this.GetInt("CompanyId", 0)
- sortParam, _ := this.GetInt("SortParam", 2)
- sortType, _ := this.GetInt("SortType", 1)
- var cond, order string
- var pars []interface{}
- if companyId > 0 {
- cond += ` AND company_id = ?`
- pars = append(pars, companyId)
- }
- sortArr := []int{1, 2}
- if utils.InArrayByInt(sortArr, sortParam) && utils.InArrayByInt(sortArr, sortType) {
- pMap := map[int]string{1: "view_total", 2: "last_view_time"}
- tMap := map[int]string{1: "DESC", 2: "ASC"}
- order = fmt.Sprintf(` ORDER BY %s %s`, pMap[sortParam], tMap[sortType])
- }
- var startSize int
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = paging.StartIndex(currentIndex, pageSize)
- // 注:点击量列表是包含被删除的联系人的历史点击量
- total, list, e := models.GetEnglishCompanyViewPageList(cond, pars, order, startSize, pageSize)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取英文客户点击量详情列表失败, Err: " + e.Error()
- return
- }
- respList := make([]*models.EnglishCompanyViewResp, 0)
- for i := range list {
- v := &models.EnglishCompanyViewResp{
- EmailId: list[i].Id,
- UserName: list[i].Name,
- Email: list[i].Email,
- ViewTotal: list[i].ViewTotal,
- LastViewTime: list[i].LastViewTime.Format(utils.FormatDateTime),
- }
- if v.LastViewTime == "0001-01-01 00:00:00" {
- v.LastViewTime = ""
- }
- respList = append(respList, v)
- }
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp := &models.GetEnglishCompanyViewPageListResp{
- Paging: page,
- List: respList,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // EditEnabled
- // @Title 英文客户-启用/禁用
- // @Description 英文客户-启用/禁用
- // @Param request body models.EnglishCompanyEditEnabledReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /company/edit_enabled [post]
- func (this *EnglishCompanyController) EditEnabled() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EnglishCompanyEditEnabledReq
- if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.CompanyId <= 0 {
- br.Msg = "参数有误"
- return
- }
- if req.Enabled != 0 && req.Enabled != 1 {
- br.Msg = "参数有误"
- return
- }
- item, e := models.GetEnglishCompanyById(req.CompanyId)
- if e != nil {
- br.Msg = "客户已被删除, 请刷新页面"
- return
- }
- // 更新客户
- nowTime := time.Now().Local()
- item.Enabled = req.Enabled
- item.ModifyTime = nowTime
- updateCols := []string{"Enabled", "ModifyTime"}
- if e = item.Update(updateCols); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "更新英文客户状态失败, Err:" + e.Error()
- return
- }
- // 更新其下联系人状态
- if e = models.UpdateEnglishEmailEnabledByCompanyId(item.CompanyId, req.Enabled); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "更新英文客户下的联系人状态失败, Err: " + e.Error()
- return
- }
- // 记录日志
- go func() {
- opByte, e := json.Marshal(req)
- if e != nil {
- return
- }
- opData := string(opByte)
- opLog := &models.EnglishCompanyLog{
- CompanyId: item.CompanyId,
- AdminId: sysUser.AdminId,
- OptionData: opData,
- OptionType: models.EnglishCompanyLogOptionTypeEditEnabled,
- CreateTime: nowTime,
- }
- if e = opLog.Create(); e != nil {
- return
- }
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // SavePermission
- // @Title 英文客户-保存权限
- // @Description 英文客户-保存权限
- // @Param request body models.EnglishCompanySaveReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /company/save_permission [post]
- func (this *EnglishCompanyController) SavePermission() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EnglishCompanySaveReq
- if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.CompanyId <= 0 {
- br.Msg = "参数有误"
- br.ErrMsg = "参数有误"
- return
- }
- if len(req.EnPermissions) <= 0 {
- br.Msg = "请至少选择一个品种权限"
- return
- }
- nowTime := time.Now().Local()
- item, e := models.GetEnglishCompanyById(req.CompanyId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "客户已被删除, 请刷新页面"
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = "获取客户信息失败, Err: " + e.Error()
- return
- }
- // 品种权限
- permissions := make([]*models.EnCompanyPermission, 0)
- for _, p := range req.EnPermissions {
- v := new(models.EnCompanyPermission)
- v.EnPermissionId = p
- v.EnCompanyId = item.CompanyId
- v.CreateTime = nowTime
- permissions = append(permissions, v)
- }
- if e := models.ClearAndCreateEnCompanyPermissions(item.CompanyId, permissions); e != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "更新客户品种权限失败, Err: " + e.Error()
- return
- }
- // 记录日志
- go func() {
- opByte, e := json.Marshal(req)
- if e != nil {
- return
- }
- opData := string(opByte)
- opLog := &models.EnglishCompanyLog{
- CompanyId: item.CompanyId,
- AdminId: sysUser.AdminId,
- OptionData: opData,
- OptionType: models.EnglishCompanyLogOptionTypeSavePermission,
- CreateTime: nowTime,
- }
- if e = opLog.Create(); e != nil {
- return
- }
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
|