123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128 |
- package company
- import (
- "errors"
- "fmt"
- companyCache "hongze/hongze_yb/cache/company"
- "hongze/hongze_yb/global"
- "hongze/hongze_yb/models/response"
- "hongze/hongze_yb/models/tables/admin"
- "hongze/hongze_yb/models/tables/company"
- "hongze/hongze_yb/models/tables/company_product"
- "hongze/hongze_yb/models/tables/company_report_permission"
- "hongze/hongze_yb/models/tables/company_user_chart_classify_permission"
- chart_permission2 "hongze/hongze_yb/models/tables/rddp/chart_permission"
- "hongze/hongze_yb/models/tables/wx_user"
- "hongze/hongze_yb/models/tables/yb_apply_record"
- "hongze/hongze_yb/services/user"
- "hongze/hongze_yb/utils"
- "sort"
- "strings"
- "time"
- )
- var (
- CheckTypeApply = "apply"
- CheckTypeContact = "contact"
- CheckTypeExpire = "expired"
- )
- // GetClassNameListByProductId 根据权限id获取权限分类
- func GetClassNameListByProductId(productId int64) (list []*chart_permission2.ChartPermission, err error) {
- list, err = chart_permission2.GetClassNameListByProductId(productId)
- return
- }
- // GetPermissionListByProductId 根据product_id获取所有的权限列表
- func GetPermissionListByProductId(productId int64) (list []*chart_permission2.ChartPermission, err error) {
- list, err = chart_permission2.GetListByProductId(productId)
- return
- }
- // GetValidPermissionByCompany2ProductId 根据客户id和产品id获取有效的权限列表
- func GetValidPermissionByCompany2ProductId(companyId, productId int64) (list []*company_report_permission.CompanyReportPermission, err error) {
- where := make(map[string]interface{})
- where["company_id ="] = companyId
- where["product_id ="] = productId
- where["status in"] = []string{"正式", "试用", "永续"}
- list, err = company_report_permission.GetByWhereMap(where)
- return
- }
- // GetPurchasePermissionIdsByCompany2ProductId 根据客户id和产品id获取有效的权限id集合
- func GetPurchasePermissionIdsByCompany2ProductId(companyId, productId int64) (permissionIds []int, err error) {
- where := make(map[string]interface{})
- where["company_id ="] = companyId
- where["product_id ="] = productId
- where["status in"] = []string{"正式", "永续"}
- list, err := company_report_permission.GetByWhereMap(where)
- if err != nil {
- return
- }
- for _, v := range list {
- permissionIds = append(permissionIds, v.ChartPermissionID)
- }
- return
- }
- // GetValidPermissionIdListByCompany2ProductId 根据客户id和产品id获取有效的权限id列表
- func GetValidPermissionIdListByCompany2ProductId(companyId, productId int64) (list []int, err error) {
- companyReportPermissionList, err := GetValidPermissionByCompany2ProductId(companyId, productId)
- if err != nil {
- return
- }
- for _, v := range companyReportPermissionList {
- list = append(list, v.ChartPermissionID)
- }
- return
- }
- // PermissionCheckInfo 权限校验完成后的结果
- type PermissionCheckInfo struct {
- Name string `json:"name" description:"销售名称"`
- Mobile string `json:"mobile" description:"手机号"`
- Type string `json:"type" description:"校验失败,没有权限,需要让前端处理的类型,枚举值:apply,contact"`
- CustomerInfo CustomerInfo `json:"customer_info" description:"客户信息"`
- }
- // CustomerInfo 客户信息
- type CustomerInfo struct {
- CompanyName string `json:"company_name" description:"客户(公司)名称"`
- Name string `json:"name" description:"联系人名称"`
- Mobile string `json:"mobile" description:"手机号"`
- Status string `json:"status" description:"状态"`
- IsSuspend int8 `json:"is_suspend" description:"启用与否字段:1:暂停,0:启用"`
- HasApply bool `json:"has_apply" description:"是否有申请过"`
- }
- // CheckBaseFiccPermission 校验用户基本的FICC权限
- func CheckBaseFiccPermission(companyId int64, userId int) (ok bool, checkInfo response.PermissionCheckInfo, permissionIdArr []int, err error) {
- defer func() {
- // 无权限查询是否有申请记录
- if !ok {
- if checkInfo.Type == CheckTypeApply {
- if _, e := yb_apply_record.GetLastNotOpRecordByUserId(userId); e == nil {
- checkInfo.CustomerInfo.HasApply = true
- }
- }
- }
- }()
- // 潜在用户
- if companyId == 1 {
- checkInfo.Type = CheckTypeApply
- return
- }
- // 是否开通FICC
- productId := int64(1)
- productInfo, e := company_product.GetByCompany2ProductId(companyId, productId)
- if e != nil {
- if e == utils.ErrNoRow {
- checkInfo.Type = CheckTypeApply
- return
- }
- err = errors.New("获取用户FICC权限失败 Err:" + e.Error())
- return
- }
- wxUser, e := wx_user.GetByUserId(userId)
- if e != nil {
- err = errors.New("不存在当前用户, Err:" + e.Error())
- return
- }
- // 是否为弘则研究已禁用的联系人
- if companyId == 16 {
- userInfo, e := wx_user.GetByUserId(userId)
- if e != nil {
- err = errors.New("用户信息异常, Err:" + e.Error())
- return
- }
- if userInfo.Enabled != 1 {
- checkInfo.Type = CheckTypeApply
- return
- }
- }
- customerInfo := response.CustomerInfo{
- CompanyName: productInfo.CompanyName,
- Status: productInfo.Status,
- Name: wxUser.RealName,
- IsSuspend: productInfo.IsSuspend,
- Mobile: wxUser.Mobile,
- }
- checkInfo.CustomerInfo = customerInfo
- // 客户状态是否为流失
- if productInfo.Status == "流失" || productInfo.Status == "关闭" {
- checkInfo.Type = CheckTypeApply
- return
- }
- // 客户对应的销售信息
- sellerInfo, e := admin.GetByAdminId(productInfo.SellerID)
- if e != nil {
- err = errors.New("获取用户销售信息失败 Err:" + e.Error())
- return
- }
- checkInfo.Name = sellerInfo.RealName
- checkInfo.Mobile = sellerInfo.Mobile
- if productInfo.Status == "冻结" || (productInfo.Status == "试用" && productInfo.IsSuspend == 1) {
- checkInfo.Type = CheckTypeContact
- return
- }
- // 正常用户, 获取有效权限
- permissionIdArr, e = GetValidPermissionIdListByCompany2ProductId(companyId, productId)
- if e != nil {
- err = errors.New("获取用户有效权限列表失败 Err:" + e.Error())
- return
- }
- ok = true
- return
- }
- // CheckPermissionByPermissionIdList2Ficc 根据权限id集合权限校验(ficc)
- func CheckPermissionByPermissionIdList2Ficc(companyId int64, userId int, permissionIdList []int) (ok bool, permissionCheckInfo PermissionCheckInfo, err error) {
- defer func() {
- // 如果无权限,那么就去查询是否申请过
- if ok == false {
- ok, _ = checkPermissionByPermissionIdList2Rai(companyId, userId, permissionIdList)
- }
- if ok == false && permissionCheckInfo.Type == CheckTypeApply {
- _, err = yb_apply_record.GetLastNotOpRecordByUserId(userId) // 从来源我的/活动申请的记录
- if err != nil && err != utils.ErrNoRow {
- return
- }
- //查询是否有申请过,如果有申请过的话,那么err是nil
- if err == nil {
- permissionCheckInfo.CustomerInfo.HasApply = true
- }
- }
- }()
- //非潜在客户
- if len(permissionIdList) <= 0 {
- err = fmt.Errorf("权限异常,请传入需要校验的权限")
- return
- }
- permissionMap := make(map[int]bool)
- for _, permissionId := range permissionIdList {
- permissionMap[permissionId] = true
- }
- var productId int64
- productId = 1
- if companyId > 1 {
- //查询是否 开通ficc的客户
- companyProductInfo, tmpErr := company_product.GetByCompany2ProductId(companyId, productId)
- if tmpErr != nil {
- // 没有开通ficc的客户
- if tmpErr == utils.ErrNoRow {
- permissionCheckInfo.Type = CheckTypeApply
- return
- }
- err = tmpErr
- return
- }
- wxUser, tmpErr := wx_user.GetByUserId(userId)
- if tmpErr != nil {
- permissionCheckInfo.Type = CheckTypeApply
- err = tmpErr
- return
- }
- // 查询用户是否为弘则研究已禁用的联系人
- if companyId == 16 {
- if wxUser.Enabled != 1 {
- permissionCheckInfo.Type = CheckTypeApply
- return
- }
- }
- //客户信息
- companyInfo, tmpErr := company.GetByCompanyId(companyId)
- if tmpErr != nil {
- // 没有开通ficc的客户
- if tmpErr == utils.ErrNoRow {
- permissionCheckInfo.Type = CheckTypeApply
- return
- }
- err = tmpErr
- return
- }
- customerInfo := CustomerInfo{
- CompanyName: companyInfo.CompanyName,
- Status: companyProductInfo.Status,
- Name: wxUser.RealName,
- IsSuspend: companyProductInfo.IsSuspend,
- Mobile: wxUser.Mobile,
- }
- permissionCheckInfo.CustomerInfo = customerInfo
- // 如果客户ficc产品的状态是流失,那么也是让去申请
- if companyProductInfo.Status == "流失" || companyProductInfo.Status == "关闭" {
- permissionCheckInfo.Type = CheckTypeApply
- return
- }
- //查找对应客户的销售信息
- adminInfo, tmpErr := admin.GetByAdminId(companyProductInfo.SellerID)
- if tmpErr != nil {
- //if tmpErr ==
- err = tmpErr
- return
- }
- permissionCheckInfo.Name = adminInfo.RealName
- permissionCheckInfo.Mobile = adminInfo.Mobile
- if companyProductInfo.Status == "冻结" {
- permissionCheckInfo.Type = CheckTypeContact
- return
- }
- //客户状态是:试用暂停状态(联系销售)
- if companyProductInfo.Status == "试用" && companyProductInfo.IsSuspend == 1 {
- permissionCheckInfo.Type = CheckTypeContact
- return
- }
- // 获取有效的权限id列表
- validPermissionIdList, tmpErr := GetValidPermissionIdListByCompany2ProductId(companyId, productId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- // 校验在有效的权限id列表中是否存在该权限
- for _, validPermissionId := range validPermissionIdList {
- //如果有该权限id,那么直接返回校验通过
- if _, validOk := permissionMap[validPermissionId]; validOk {
- ok = true
- return
- }
- }
- permissionCheckInfo.Type = CheckTypeContact
- } else {
- permissionCheckInfo.Type = CheckTypeApply
- }
- return
- }
- func GetCheckPermission(companyId int64, userId int, permissionIdList []int) (ok bool, permissionCheckInfo response.PermissionCheckInfo, finalValidPermissionIds []int, err error) {
- defer func() {
- // 如果无权限,那么就去查询是否申请过
- if ok == false {
- ok, _ = checkPermissionByPermissionIdList2Rai(companyId, userId, permissionIdList)
- }
- if ok == false && permissionCheckInfo.Type == CheckTypeApply {
- _, err = yb_apply_record.GetLastNotOpRecordByUserId(userId) // 从来源我的/活动申请的记录
- if err != nil {
- if err == utils.ErrNoRow {
- err = nil
- }
- return
- }
- //查询是否有申请过,如果有申请过的话,那么err是nil
- if err == nil {
- permissionCheckInfo.CustomerInfo.HasApply = true
- }
- }
- }()
- permissionCheckInfo.HzPhone = utils.HZPHONE
- //非潜在客户
- permissionMap := make(map[int]bool)
- if len(permissionIdList) > 0 {
- for _, permissionId := range permissionIdList {
- permissionMap[permissionId] = true
- }
- }
- // 潜在客户名下的
- if companyId <= 1 {
- permissionCheckInfo.Type = CheckTypeApply
- return
- }
- var productId int64
- productId = 1
- //查询是否 开通ficc的客户
- companyProductInfo, tmpErr := company_product.GetByCompany2ProductId(companyId, productId)
- if tmpErr != nil {
- // 没有开通ficc的客户
- if tmpErr == utils.ErrNoRow {
- permissionCheckInfo.Type = CheckTypeApply
- return
- }
- err = tmpErr
- return
- }
- wxUser, tmpErr := wx_user.GetByUserId(userId)
- if tmpErr != nil {
- permissionCheckInfo.Type = CheckTypeApply
- err = tmpErr
- return
- }
- //客户信息
- companyInfo, tmpErr := companyCache.GetByCompanyId(companyId)
- if tmpErr != nil {
- // 没有开通ficc的客户
- if tmpErr == utils.ErrNoRow {
- permissionCheckInfo.Type = CheckTypeApply
- return
- }
- err = tmpErr
- return
- }
- customerInfo := response.CustomerInfo{
- CompanyName: companyInfo.CompanyName,
- Status: companyProductInfo.Status,
- Name: wxUser.RealName,
- IsSuspend: companyProductInfo.IsSuspend,
- Mobile: wxUser.Mobile,
- }
- permissionCheckInfo.CustomerInfo = customerInfo
- // 如果客户ficc产品的状态是流失,那么也是让去申请
- if companyProductInfo.Status == "流失" || companyProductInfo.Status == "关闭" {
- permissionCheckInfo.Type = CheckTypeApply
- return
- }
- //查找对应客户的销售信息
- adminInfo, tmpErr := admin.GetByAdminId(companyProductInfo.SellerID)
- if tmpErr != nil {
- //if tmpErr ==
- err = tmpErr
- return
- }
- permissionCheckInfo.Name = adminInfo.RealName
- permissionCheckInfo.Mobile = adminInfo.Mobile
- if companyProductInfo.Status == "冻结" {
- permissionCheckInfo.Type = CheckTypeContact
- return
- }
- //客户状态是:试用暂停状态(联系销售)
- if companyProductInfo.Status == "试用" && companyProductInfo.IsSuspend == 1 {
- permissionCheckInfo.Type = CheckTypeContact
- return
- }
- // 获取有效的权限id列表
- validPermissionIdList, tmpErr := GetValidPermissionIdListByCompany2ProductId(companyId, productId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- // 校验在有效的权限id列表中是否存在该权限
- if len(permissionIdList) > 0 {
- for _, validPermissionId := range validPermissionIdList {
- //如果有该权限id,那么直接返回校验通过
- if _, validOk := permissionMap[validPermissionId]; validOk {
- finalValidPermissionIds = append(finalValidPermissionIds, validPermissionId)
- ok = true
- }
- }
- return
- }
- permissionCheckInfo.Type = CheckTypeContact
- return
- }
- // checkPermissionByPermissionIdList2Rai 根据权限id集合权限校验(权益)
- func checkPermissionByPermissionIdList2Rai(companyId int64, userId int, permissionIdList []int) (ok bool, err error) {
- //非潜在客户
- if len(permissionIdList) <= 0 {
- err = fmt.Errorf("权限异常,请传入需要校验的权限")
- return
- }
- // 查询用户是否为弘则研究已禁用的联系人
- wxUser, tmpErr := wx_user.GetByUserId(userId)
- if tmpErr != nil {
- err = fmt.Errorf("用户信息异常")
- return
- }
- if companyId == 16 {
- if wxUser.Enabled != 1 {
- return
- }
- }
- //市场策略(ficc权限id):33
- ficcPermissionId := 33
- //市场策略(权益权限id):23
- raiPermissionId := 23
- //是否往下校验
- var permissionOk bool
- for _, permissionId := range permissionIdList {
- if permissionId == ficcPermissionId {
- permissionOk = true
- }
- }
- var productId int64
- productId = 2
- if companyId > 1 && permissionOk {
- // 获取有效的权限id列表
- validPermissionIdList, tmpErr := GetValidPermissionIdListByCompany2ProductId(companyId, productId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- // 校验在有效的权限id列表中是否存在该权限
- for _, validPermissionId := range validPermissionIdList {
- if validPermissionId == raiPermissionId {
- ok = true
- return
- }
- }
- }
- return
- }
- // ChartPermissionCheckInfo 图表权限验证信息
- type ChartPermissionCheckInfo struct {
- Name string `json:"name" description:"销售名称"`
- Mobile string `json:"mobile" description:"手机号"`
- Type string `json:"type" description:"无权限,需要前端处理的类型,枚举值:expired, apply, contact"`
- CustomerInfo CustomerInfo `json:"customer_info" description:"客户信息"`
- Jump string `json:"jump" description:"需要跳转的页面,sandbox_list"`
- }
- // CheckUserChartPermission 验证用户/联系人的图库权限
- func CheckUserChartPermission(companyId int64, userId int) (ok bool, permissionCheckInfo ChartPermissionCheckInfo, err error) {
- defer func() {
- // 如果无权限,那么就去查询是否申请过
- if ok == false && permissionCheckInfo.Type == CheckTypeApply {
- _, err = yb_apply_record.GetLastNotOpRecordByUserId(userId) // 图库申请
- // 查询是否有申请过,如果有申请过的话,那么err是nil
- if err != nil {
- if err == utils.ErrNoRow {
- err = nil
- return
- }
- return
- }
- permissionCheckInfo.CustomerInfo.HasApply = true
- }
- }()
- var productId int64
- productId = 1
- if companyId > 1 {
- // 查询是否开通FICC
- companyProductInfo, tmpErr := company_product.GetByCompany2ProductId(companyId, productId)
- if tmpErr != nil {
- // 没有开通FICC
- if tmpErr == utils.ErrNoRow {
- permissionCheckInfo.Type = CheckTypeApply
- return
- }
- err = tmpErr
- return
- }
- wxUser, tmpErr := wx_user.GetByUserId(userId)
- if tmpErr != nil {
- permissionCheckInfo.Type = CheckTypeApply
- err = tmpErr
- return
- }
- // 查询用户是否为弘则研究已禁用的联系人
- if companyId == 16 {
- if wxUser.Enabled != 1 {
- permissionCheckInfo.Type = CheckTypeApply
- return
- }
- }
- // 客户信息
- companyInfo, tmpErr := company.GetByCompanyId(companyId)
- if tmpErr != nil {
- // 没有开通FICC
- if tmpErr == utils.ErrNoRow {
- permissionCheckInfo.Type = CheckTypeApply
- return
- }
- err = tmpErr
- return
- }
- customerInfo := CustomerInfo{
- CompanyName: companyInfo.CompanyName,
- Status: companyProductInfo.Status,
- Name: wxUser.RealName,
- IsSuspend: companyProductInfo.IsSuspend,
- Mobile: wxUser.Mobile,
- }
- permissionCheckInfo.CustomerInfo = customerInfo
- // 如果客户FICC产品的状态是流失-申请
- if companyProductInfo.Status == "流失" || companyProductInfo.Status == "关闭" {
- permissionCheckInfo.Type = CheckTypeApply
- return
- }
- // 查找对应客户的销售信息
- adminInfo, tmpErr := admin.GetByAdminId(companyProductInfo.SellerID)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- // 客户状态为冻结/试用暂停-联系销售
- permissionCheckInfo.Name = adminInfo.RealName
- permissionCheckInfo.Mobile = adminInfo.Mobile
- if companyProductInfo.Status == "冻结" || (companyProductInfo.Status == "试用" && companyProductInfo.IsSuspend == 1) {
- permissionCheckInfo.Type = CheckTypeContact
- return
- }
- // 验证用户的图表权限
- permissionInfo, tmpErr := company_user_chart_classify_permission.GetPermissionByUserId(userId)
- if tmpErr != nil {
- if tmpErr == utils.ErrNoRow {
- // 无权限-联系销售
- permissionCheckInfo.Type = CheckTypeContact
- return
- }
- err = tmpErr
- return
- }
- nowTime := time.Now()
- if permissionInfo.Enabled != 1 {
- // 权限被禁用-联系销售
- permissionCheckInfo.Type = CheckTypeContact
- return
- }
- endTime := permissionInfo.EndTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
- if nowTime.After(permissionInfo.StartTime) && nowTime.Before(endTime) {
- ok = true
- } else {
- // 权限已到期
- permissionCheckInfo.Type = CheckTypeExpire
- return
- }
- // 都不是默认联系销售
- permissionCheckInfo.Type = CheckTypeContact
- } else {
- permissionCheckInfo.Type = CheckTypeApply
- }
- return
- }
- type FiccPermissionList struct {
- ClassifyName string `description:"分类"`
- //HasPermission bool `description:"是否有权限"`
- Items []*PermissionItem `description:"子类"`
- }
- type PermissionItem struct {
- PermissionId int `description:"权限id"`
- PermissionName string `description:"权限名称"`
- //HasPermission bool `description:"是否有权限"`
- }
- // GetFiccPermissionList 获取FICC品种权限列表-不校验用户权限
- func GetFiccPermissionList() (list []*FiccPermissionList, err error) {
- productId := 1
- list = make([]*FiccPermissionList, 0)
- allFiccPermissions, e := chart_permission2.GetListByProductIdAndPermissionType(productId)
- if e != nil {
- err = errors.New("获取品种权限失败 Err:" + e.Error())
- return
- }
- permissionMap := make(map[int][]*chart_permission2.ChartPermission, 0)
- for _, v := range allFiccPermissions {
- if v.ParentId > 0 {
- permissionMap[v.ParentId] = append(permissionMap[v.ParentId], v)
- }
- }
- for _, v := range allFiccPermissions {
- if v.ParentId == 0 {
- classify := new(FiccPermissionList)
- classify.ClassifyName = v.PermissionName
- classify.Items = make([]*PermissionItem, 0)
- items, ok := permissionMap[int(v.ChartPermissionID)]
- if ok {
- for _, iv := range items {
- item := new(PermissionItem)
- item.PermissionId = int(iv.ChartPermissionID)
- item.PermissionName = iv.PermissionName
- classify.Items = append(classify.Items, item)
- }
- }
- list = append(list, classify)
- }
- }
- return
- }
- func GetHomeFiccPermissions(user user.UserInfo) (ret response.PermissionFiccResp, err error) {
- var errMsg string
- defer func() {
- if err != nil {
- global.LOG.Critical(fmt.Sprintf("GetHomeFiccPermissions: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
- }
- }()
- validPermissionList, err := GetValidPermissionByCompany2ProductId(user.CompanyID, 1)
- if err != nil {
- return
- }
- var validPermissionIds []int
- for _, item := range validPermissionList {
- validPermissionIds = append(validPermissionIds, item.ChartPermissionID)
- }
- permissionList, err := chart_permission2.GetFiccListExceptTacticByProductId()
- if err != nil {
- return
- }
- permissionMap := make(map[uint64]bool)
- permissionFirstMap := make(map[int]*response.PermissionFirstItem)
- //查询首页展示的图标
- permissionFirstList, err := chart_permission2.GetFiccFirstListExceptTacticByProductId()
- if err != nil {
- return
- }
- for _, v := range permissionFirstList {
- permissionFirstMap[int(v.ChartPermissionID)] = &response.PermissionFirstItem{
- Id: int(v.ChartPermissionID),
- Sort: v.Sort + 1000,
- YbIndexName: v.PermissionName,
- YbIndexIcon: "",
- }
- }
- for k, v := range permissionList {
- if v.ParentId > 0 {
- permissionMap[v.ChartPermissionID] = false
- for _, myPerId := range validPermissionIds {
- if int(v.ChartPermissionID) == myPerId {
- permissionMap[v.ChartPermissionID] = true
- permissionList[k].Sort = v.Sort - 1000
- if _, ok := permissionFirstMap[v.ParentId]; ok && !permissionFirstMap[v.ParentId].IsCheck {
- permissionFirstMap[v.ParentId].AuthOk = true
- permissionFirstMap[v.ParentId].Sort -= 1000
- permissionFirstMap[v.ParentId].IsCheck = true
- }
- break
- }
- }
- }
- }
- for _, v := range permissionList {
- if v.ParentId > 0 {
- temp := new(response.PermissionFiccSecondItem)
- temp.Sort = v.Sort
- temp.ChartPermissionID = int(v.ChartPermissionID)
- temp.ChartPermissionName = v.PermissionName
- temp.AuthOk = permissionMap[v.ChartPermissionID]
- temp.PirceDrivenState = v.PriceDrivenState
- if _, ok := permissionFirstMap[v.ParentId]; ok {
- permissionFirstMap[v.ParentId].List = append(permissionFirstMap[v.ParentId].List, temp)
- } else {
- permissionFirstMap[v.ParentId] = &response.PermissionFirstItem{
- List: []*response.PermissionFiccSecondItem{temp},
- }
- }
- }
- }
- var list response.PermissionFiccList
- for _, v := range permissionFirstMap {
- temp := new(response.PermissionFiccItem)
- temp.Id = v.Id
- temp.Sort = v.Sort
- temp.ClassifyName = v.YbIndexName
- temp.IconUrl = v.YbIndexIcon
- if len(temp.IconUrl) > 4 {
- tmpIcon := temp.IconUrl[:len(temp.IconUrl)-4]
- temp.SelectIconUrl = tmpIcon + "_select.png"
- }
- if len(v.List) > 0 {
- temp.List = v.List
- sort.Sort(temp.List)
- list = append(list, temp)
- }
- }
- if len(list) > 0 {
- sort.Sort(list)
- }
- //判断是否是已购用户,如果是已购用户,需要返回对应的联系人方式
- companyProduct, err := company_product.GetByCompany2ProductId(user.CompanyID, 1)
- if err != nil {
- if err == utils.ErrNoRow {
- err = nil
- }
- }
- if err != nil {
- errMsg = err.Error()
- err = errors.New("查询用户状态失败")
- return
- }
- // 已购仅付费用户可见
- var contactInfo response.PermissionCheckContactInfo
- checkFlag := false
- if companyProduct.Status != "" && strings.Contains("永续,正式", companyProduct.Status) {
- //查询对应的销售
- //查找对应客户的销售信息
- adminInfo, tmpErr := admin.GetByAdminId(companyProduct.SellerID)
- if tmpErr != nil {
- errMsg = tmpErr.Error()
- err = errors.New("查询销售信息出错")
- return
- }
- contactInfo.Name = adminInfo.RealName
- contactInfo.Mobile = adminInfo.Mobile
- checkFlag = true
- }
- temp := new(response.PermissionFiccItem)
- temp.SelectIconUrl = utils.ALIYUN_YBIMG_HOST + "ficc_icon_idx_more_select.png"
- temp.IconUrl = utils.ALIYUN_YBIMG_HOST + "ficc_icon_idx_more.png"
- temp.ClassifyName = "更多报告"
- temp.Sort = 100000
- list = append(list, temp)
- ret.PermissionList = list
- ret.CheckFlag = checkFlag
- ret.ContactInfo = contactInfo
- return
- }
- // CheckUserSandboxPermission 验证用户/联系人的沙盘权限
- func CheckUserSandboxPermission(companyId int64, userId, permissionId int) (ok bool, companyPermissionIdList []int, permissionCheckInfo ChartPermissionCheckInfo, err error) {
- defer func() {
- // 如果无权限,那么就去查询是否申请过
- if ok == false && permissionCheckInfo.Type == CheckTypeApply {
- _, err = yb_apply_record.GetLastNotOpRecordByUserId(userId) // 图库申请
- // 查询是否有申请过,如果有申请过的话,那么err是nil
- if err != nil {
- if err == utils.ErrNoRow {
- err = nil
- return
- }
- return
- }
- permissionCheckInfo.CustomerInfo.HasApply = true
- }
- }()
- var productId int64
- productId = 1
- if companyId > 1 {
- // 查询是否开通FICC
- companyProductInfo, tmpErr := company_product.GetByCompany2ProductId(companyId, productId)
- if tmpErr != nil {
- // 没有开通FICC
- if tmpErr == utils.ErrNoRow {
- permissionCheckInfo.Type = CheckTypeApply
- return
- }
- err = tmpErr
- return
- }
- wxUser, tmpErr := wx_user.GetByUserId(userId)
- if tmpErr != nil {
- permissionCheckInfo.Type = CheckTypeApply
- err = tmpErr
- return
- }
- // 查询用户是否为弘则研究已禁用的联系人
- if companyId == 16 {
- if wxUser.Enabled != 1 {
- permissionCheckInfo.Type = CheckTypeApply
- return
- }
- }
- // 客户信息
- companyInfo, tmpErr := company.GetByCompanyId(companyId)
- if tmpErr != nil {
- // 没有开通FICC
- if tmpErr == utils.ErrNoRow {
- permissionCheckInfo.Type = CheckTypeApply
- return
- }
- err = tmpErr
- return
- }
- customerInfo := CustomerInfo{
- CompanyName: companyInfo.CompanyName,
- Status: companyProductInfo.Status,
- Name: wxUser.RealName,
- IsSuspend: companyProductInfo.IsSuspend,
- Mobile: wxUser.Mobile,
- }
- permissionCheckInfo.CustomerInfo = customerInfo
- // 如果客户FICC产品的状态是流失-申请
- if companyProductInfo.Status == "流失" || companyProductInfo.Status == "关闭" {
- permissionCheckInfo.Type = CheckTypeApply
- return
- }
- // 查找对应客户的销售信息
- adminInfo, tmpErr := admin.GetByAdminId(companyProductInfo.SellerID)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- // 客户状态为冻结/试用暂停-联系销售
- permissionCheckInfo.Name = adminInfo.RealName
- permissionCheckInfo.Mobile = adminInfo.Mobile
- if companyProductInfo.Status == "冻结" || (companyProductInfo.Status == "试用" && companyProductInfo.IsSuspend == 1) {
- permissionCheckInfo.Type = CheckTypeContact
- return
- }
- // 验证用户的沙盘权限
- //获取所有的权限分类列表
- companyPermissionList, tmpErr := GetValidPermissionByCompany2ProductId(companyId, productId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //客户品种权限赋值
- for _, chartPermission := range companyPermissionList {
- companyPermissionIdList = append(companyPermissionIdList, chartPermission.ChartPermissionID)
- }
- if permissionId > 0 {
- for _, chartPermission := range companyPermissionList {
- if chartPermission.ChartPermissionID == permissionId {
- ok = true
- }
- }
- if ok == false {
- permissionCheckInfo.Jump = `sandbox_list` //如果是没有单个品种的权限,那么提示后并跳转到沙盘列表页吧
- }
- } else {
- // 不校验品种权限的话,那么就是认为有权限的
- if len(companyPermissionList) > 0 {
- ok = true
- }
- }
- // 都不是默认联系销售
- permissionCheckInfo.Type = CheckTypeContact
- } else {
- permissionCheckInfo.Type = CheckTypeApply
- }
- return
- }
- func GetBindFiccPermissions(user user.UserInfo) (ret response.BindPermissionFiccResp, err error) {
- var errMsg string
- defer func() {
- if err != nil {
- global.LOG.Critical(fmt.Sprintf("GetHomeFiccPermissions: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
- }
- }()
- ret.PermissionList = make([]*response.BindPermissionFiccItem, 0)
- // 判断用户状态是否是正常和永续
- var productAuthOk bool
- companyProduct, err := company_product.GetByCompany2ProductId(user.CompanyID, 1)
- if err == utils.ErrNoRow {
- err = nil
- return
- }
- if err != nil {
- errMsg = err.Error()
- err = errors.New("查询用户购买产品出错")
- return
- }
- if companyProduct != nil {
- // 无FICC权限的客户不可见
- if companyProduct.CompanyProductID > 0 {
- // 已购或者试用用户可见
- if strings.Contains("永续,正式", companyProduct.Status) || (companyProduct.Status == "试用" && companyProduct.IsSuspend != 1) {
- productAuthOk = true
- }
- }
- }
- if !productAuthOk {
- return
- }
- validPermissionList, err := GetValidPermissionByCompany2ProductId(user.CompanyID, 1)
- if err != nil {
- return
- }
- var validPermissionIds []int
- for _, item := range validPermissionList {
- validPermissionIds = append(validPermissionIds, item.ChartPermissionID)
- }
- permissionList, err := chart_permission2.GetFiccListExceptTacticByProductId()
- if err != nil {
- return
- }
- permissionMap := make(map[uint64]bool)
- permissionFirstMap := make(map[int]*response.BindPermissionFiccItem)
- //查询首页展示的图标
- permissionFirstList, err := chart_permission2.GetFiccFirstListExceptTacticByProductId()
- if err != nil {
- return
- }
- for _, v := range permissionFirstList {
- permissionFirstMap[int(v.ChartPermissionID)] = &response.BindPermissionFiccItem{
- Id: int(v.ChartPermissionID),
- ClassifyName: v.ChartPermissionName,
- }
- }
- for _, v := range permissionList {
- permissionMap[v.ChartPermissionID] = false
- for _, myPerId := range validPermissionIds {
- if int(v.ChartPermissionID) == myPerId {
- permissionMap[v.ChartPermissionID] = true
- break
- }
- }
- }
- for _, v := range permissionList {
- temp := new(response.BindPermissionFiccSecondItem)
- temp.ChartPermissionID = int(v.ChartPermissionID)
- temp.ChartPermissionName = v.PermissionName
- if _, ok := permissionFirstMap[v.ParentId]; ok && permissionMap[v.ChartPermissionID] {
- permissionFirstMap[v.ParentId].List = append(permissionFirstMap[v.ParentId].List, temp)
- }
- }
- var list []*response.BindPermissionFiccItem
- for _, v := range permissionFirstList {
- item := permissionFirstMap[int(v.ChartPermissionID)]
- if len(item.List) > 0 {
- temp := new(response.BindPermissionFiccItem)
- temp.Id = item.Id
- temp.ClassifyName = item.ClassifyName
- temp.List = item.List
- list = append(list, temp)
- }
- }
- ret.PermissionList = list
- return
- }
- func GetCommoditiesBindFiccPermissions(user user.UserInfo) (ret response.BindPermissionFiccResp, err error) {
- var errMsg string
- defer func() {
- if err != nil {
- global.LOG.Critical(fmt.Sprintf("GetHomeFiccPermissions: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
- }
- }()
- ret.PermissionList = make([]*response.BindPermissionFiccItem, 0)
- // 判断用户状态是否是正常和永续
- var productAuthOk bool
- companyProduct, err := company_product.GetByCompany2ProductId(user.CompanyID, 1)
- if err == utils.ErrNoRow {
- err = nil
- return
- }
- if err != nil {
- errMsg = err.Error()
- err = errors.New("查询用户购买产品出错")
- return
- }
- if companyProduct != nil {
- // 无FICC权限的客户不可见
- if companyProduct.CompanyProductID > 0 {
- // 已购或者试用用户可见
- if strings.Contains("永续,正式", companyProduct.Status) || (companyProduct.Status == "试用" && companyProduct.IsSuspend != 1) {
- productAuthOk = true
- }
- }
- }
- if !productAuthOk {
- return
- }
- validPermissionList, err := GetValidPermissionByCompany2ProductId(user.CompanyID, 1)
- if err != nil {
- return
- }
- var validPermissionIds []int
- for _, item := range validPermissionList {
- validPermissionIds = append(validPermissionIds, item.ChartPermissionID)
- }
- permissionList, err := chart_permission2.GetFiccListExceptTacticByProductId()
- if err != nil {
- return
- }
- permissionMap := make(map[uint64]bool)
- permissionFirstMap := make(map[int]*response.BindPermissionFiccItem)
- //查询首页展示的图标
- permissionFirstList, err := chart_permission2.GetFiccFirstListExceptTacticByProductId()
- if err != nil {
- return
- }
- for _, v := range permissionFirstList {
- permissionFirstMap[int(v.ChartPermissionID)] = &response.BindPermissionFiccItem{
- Id: int(v.ChartPermissionID),
- ClassifyName: v.ChartPermissionName,
- }
- }
- for _, v := range permissionList {
- permissionMap[v.ChartPermissionID] = false
- for _, myPerId := range validPermissionIds {
- if int(v.ChartPermissionID) == myPerId {
- permissionMap[v.ChartPermissionID] = true
- break
- }
- }
- }
- for _, v := range permissionList {
- temp := new(response.BindPermissionFiccSecondItem)
- temp.ChartPermissionID = int(v.ChartPermissionID)
- temp.ChartPermissionName = v.PermissionName
- if _, ok := permissionFirstMap[v.ParentId]; ok && permissionMap[v.ChartPermissionID] {
- permissionFirstMap[v.ParentId].List = append(permissionFirstMap[v.ParentId].List, temp)
- }
- }
- var list []*response.BindPermissionFiccItem
- LOOP:
- for _, v := range permissionFirstList {
- item := permissionFirstMap[int(v.ChartPermissionID)]
- if len(item.List) > 0 {
- temp := new(response.BindPermissionFiccItem)
- temp.Id = item.Id
- temp.ClassifyName = item.ClassifyName
- temp.List = item.List
- //大宗商品分类内合并PTA&MEG
- if temp.ClassifyName == "化工产业" {
- count := 0
- for i, secondItem := range temp.List {
- if secondItem.ChartPermissionName == "PTA" || secondItem.ChartPermissionName == "MEG" {
- count++
- secondItem.ChartPermissionName = "PTA/MEG"
- if count > 1 {
- temp.List = append(temp.List[:i], temp.List[i+1:]...)
- }
- }
- }
- }
- if temp.ClassifyName == "宏观经济" {
- continue LOOP
- }
- list = append(list, temp)
- }
- }
- ret.PermissionList = list
- return
- }
|