package english_report import ( "encoding/json" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "hongze/hz_crm_api/controllers" "hongze/hz_crm_api/models" "hongze/hz_crm_api/models/system" "hongze/hz_crm_api/services" "hongze/hz_crm_api/services/alarm_msg" "hongze/hz_crm_api/utils" "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", "") sellers := this.GetString("Sellers", "") var cond, order string var pars []interface{} if keywords != "" { k := "%" + keywords + "%" companyIds, e := models.GetEnCompanyIdsByKeyword(k) 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 sellers != "" { sellerIds := strings.Split(sellers, ",") cond += fmt.Sprintf(` AND c.seller_id IN (%s) `, utils.GetOrmInReplace(len(sellerIds))) pars = append(pars, sellerIds) } // 品种权限 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 req.Country != "" && req.Nation == "" { item.Nation = req.Country } else { item.Nation = req.Nation } item.OverseasLabel = 1 item.OverseasStatus = "试用" item.ResetBtn = 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 item.Nation = req.Nation updateCols := []string{"CompanyName", "CountryCode", "Country", "SellerId", "SellerName", "ModifyTime", "Nation"} 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 = "操作成功" }