作者 yangfu

机会编码

package agg
import (
"encoding/json"
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"opp/internal/utils"
)
//模块编号
const (
M_ENTERPRISE string = "ENTERPRISE" //企业基础设置
M_SYSTEM string = "SYSTEM" //系统设置
M_SYSTEM_OPPORTUNITY string = "SYSTEM_OPPORTUNITY" //机会管理
M_ENTERPRISE_PROFILE string = "ENTERPRISE_PROFILE" //企业信息维护
M_ENTERPRISE_ORGANIZATION string = "ENTERPRISE_ORGANIZATION" //组织架构设置
M_ENTERPRISE_EMPLOYEE_POST string = "ENTERPRISE_EMPLOYEE-POST" //员工职位设置
M_ENTERPRISE_EMPLOYEE_ROLE string = "ENTERPRISE_EMPLOYEE-ROLE" //员工角色/权限设置
M_ENTERPRISE_EMPLOYEE string = "ENTERPRISE_EMPLOYEE" //员工设置
M_SYSTEM_OPPORTUNITY_TEMPLATE string = "SYSTEM_OPPORTUNITY-TEMPLATE" //机会模板管理
M_SYSTEM_RATING string = "SYSTEM_RATING" //评分模式设置
M_SYSTEM_ANNOUNCEMENT string = "SYSTEM_ANNOUNCEMENT" //公告管理
)
type CodeToObject func() PermissionOptionObject
var CodePermissionObject = map[string]CodeToObject{
M_ENTERPRISE_ORGANIZATION: NewPermissionOptionBase, //公司组织架构设置
M_ENTERPRISE_EMPLOYEE_POST: NewPermissionOptionBase, //公司职务管理
M_ENTERPRISE_EMPLOYEE_ROLE: NewPermissionOptionBase, //员工角色/权限设置
M_ENTERPRISE_EMPLOYEE: NewPermissionOptionBase, //公司员工管理
M_ENTERPRISE: NewPermissionOptionBase, //企业基础设置(大节点)
M_SYSTEM: NewPermissionOptionBase, //系统设置(大节点)
M_ENTERPRISE_PROFILE: NewPermissionOptionBase, //企业信息维护
M_SYSTEM_OPPORTUNITY_TEMPLATE: NewPermissionOptionBase, //机会模板管理
M_SYSTEM_RATING: NewPermissionOptionBase, //评分模式
M_SYSTEM_OPPORTUNITY: NewOptionOpportunity, //机会管理
M_SYSTEM_ANNOUNCEMENT: NewPermissionOptionBase, //公告管理
}
type PermissionOptionObject interface {
// StringUnmarshal(string) error
// ObjectMarshal() string
GetValidFunc(string) func(UserObject) bool
MergeObject(string) error
}
type UserObject struct {
UserId int64 `json:"user_id"`
CompanyId int64 `json:"company_id"`
UserCompanyId int64 `json:"user_company_id"`
}
//PermissionOptionBase 基本权限
type PermissionOptionBase struct {
Check int8 `json:"check"`
}
var (
_ PermissionOptionObject = &PermissionOptionBase{}
)
func NewPermissionOptionBase() PermissionOptionObject {
return &PermissionOptionBase{
Check: 1,
}
}
func (p *PermissionOptionBase) ValidDefault(obj UserObject) bool {
if p.Check == 1 {
return true
}
return false
}
//GetValidFunc PermissionOptionBase 接口实现
func (p *PermissionOptionBase) GetValidFunc(k string) func(UserObject) bool {
m := map[string]func(UserObject) bool{
"default": p.ValidDefault,
}
if _, ok := m[k]; ok {
return m[k]
}
return nil
}
func (p *PermissionOptionBase) MergeObject(jsonString string) error {
var obj PermissionOptionBase
err := json.Unmarshal([]byte(jsonString), &obj)
if err != nil {
return err
}
if obj.Check > p.Check {
p.Check = obj.Check
}
return nil
}
// //StringUnmarshal PermissionOptionBase 接口实现
// func (p *PermissionOptionBase) StringUnmarshal(s string) error {
// err := json.Unmarshal([]byte(s), p)
// return err
// }
// //ObjectMarshal PermissionOptionBase 接口实现
// func (p *PermissionOptionBase) ObjectMarshal() string {
// bt, err := json.Marshal(p)
// if err != nil {
// return ""
// }
// return string(bt)
// }
/*
机会管理模块
CheckOpp
CheckDeparment
OptionOpportunity
*/
//CheckDeparment 特殊的查看条件设定中关于部门的设定
type CheckDeparment struct {
Id int64 `json:"id"`
Name string `json:"name,omitempty"`
Wait int `json:"wait"`
OpenAll int `json:"open_all"`
OpenDepart int `json:"open_depart"`
}
//CheckOpp 特殊的查看条件设定
type CheckOpp struct {
Departments []CheckDeparment `json:"departments"`
}
//OptionOpportunity 机会管理 高级权限设置
type OptionOpportunity struct {
Check int `json:"check"`
CheckMap map[int]int `json:"-"`
CheckOption CheckOpp `json:"check_option"`
EditSorce int `json:"edit_sorce"`
EditPublicStatus int `json:"edit_public_status"`
CloseChance int `json:"close_chance"`
EditChance int `json:"edit_chance"`
}
/*
机会管理高级设置中的 check
1:禁止查看所有机会:禁止查看所有机会(除自己提交过的机会及可执行审核操作的机会)
2:仅查看自己部门和公开机会:查看对自己部门公开的机会+公司公开的机会
3:特定部门的机会:自由配置选定部门的待审核、公司公开、部门公开的机会+查看对自己部门公开的机会
4:查看所有机会:查看所有部门的待审核机会、公开机会及部门公开机会
*/
const (
OpportunityCheckLv1 int = 1
OpportunityCheckLv2 int = 2
OpportunityCheckLv3 int = 3
OpportunityCheckLv4 int = 4
)
var (
_ PermissionOptionObject = &OptionOpportunity{}
)
func NewOptionOpportunity() PermissionOptionObject {
return &OptionOpportunity{
Check: OpportunityCheckLv4,
CheckMap: make(map[int]int),
CheckOption: CheckOpp{
Departments: []CheckDeparment{},
},
}
}
//GetValidFunc PermissionOptionBase 接口实现
func (p *OptionOpportunity) GetValidFunc(k string) func(UserObject) bool {
m := map[string]func(UserObject) bool{
"check": p.ValidCheck,
}
if _, ok := m[k]; ok {
return m[k]
}
return nil
}
//MergeObject PermissionOptionBase 接口实现
func (p *OptionOpportunity) MergeObject(jsonString string) error {
var obj OptionOpportunity
err := json.Unmarshal([]byte(jsonString), &obj)
if err != nil {
return err
}
if p.CheckMap == nil {
p.CheckMap = make(map[int]int)
}
p.CheckMap[obj.Check] = 1
departMap := make(map[int64]*CheckDeparment)
for k := range p.CheckOption.Departments {
i := p.CheckOption.Departments[k].Id
departMap[i] = &p.CheckOption.Departments[k]
}
//列表合并
for k := range obj.CheckOption.Departments {
i := obj.CheckOption.Departments[k].Id
if _, ok := departMap[i]; ok {
if obj.CheckOption.Departments[k].OpenAll > departMap[i].OpenAll {
departMap[i].OpenAll = obj.CheckOption.Departments[k].OpenAll
}
if obj.CheckOption.Departments[k].OpenDepart > departMap[i].OpenDepart {
departMap[i].OpenDepart = obj.CheckOption.Departments[k].OpenDepart
}
if obj.CheckOption.Departments[k].Wait > departMap[i].Wait {
departMap[i].Wait = obj.CheckOption.Departments[k].Wait
}
} else {
departMap[i] = &obj.CheckOption.Departments[k]
}
}
p.CheckOption.Departments = make([]CheckDeparment, 0)
for k := range departMap {
p.CheckOption.Departments = append(p.CheckOption.Departments, *departMap[k])
}
if obj.CloseChance > p.CloseChance {
p.CloseChance = obj.CloseChance
}
if obj.EditPublicStatus > p.EditPublicStatus {
p.EditPublicStatus = obj.EditPublicStatus
}
if obj.EditSorce > p.EditSorce {
p.EditSorce = obj.EditSorce
}
if obj.EditChance > p.EditChance {
p.EditChance = obj.EditChance
}
return nil
}
func (p *OptionOpportunity) ValidCheck(obj UserObject) bool {
if p.Check > 0 {
return true
}
return false
}
func (p *OptionOpportunity) ValidEditSorce(obj UserObject) bool {
if p.EditSorce > 0 {
return true
}
return false
}
func (p *OptionOpportunity) ValidEditPublicStatus(obj UserObject) bool {
if p.EditPublicStatus > 0 {
return true
}
return false
}
func GetUserPermission(userCompanyid int64) (map[string]PermissionOptionObject, error) {
type CodeOpptionData struct {
Code string `orm:"column(code)"`
Opption string `orm:"column(opption)"`
}
const datasql string = `SELECT a.code,a.opption
FROM role_menu AS a
JOIN user_role AS b ON a.role_id = b.role_id
JOIN role AS c ON a.role_id = c.id
WHERE b.user_company_id=? AND c.delete_at =0`
var (
data []CodeOpptionData
err error
objMap = make(map[string]PermissionOptionObject)
)
err = utils.ExecuteQueryAll(&data, datasql, userCompanyid)
if err != nil {
e := fmt.Errorf("EXCUTE SQL ERR:%s", err)
return nil, e
}
for _, v := range data {
if _, ok := objMap[v.Code]; ok {
err = objMap[v.Code].MergeObject(v.Opption)
if err != nil {
log.Warn(err.Error())
}
continue
}
if fn, ok := CodePermissionObject[v.Code]; ok {
obj := fn()
if err = json.Unmarshal([]byte(v.Opption), obj); err != nil {
log.Debug("解析权限配置option 失败%s", err)
}
objMap[v.Code] = obj
} else {
log.Error("未知code:%s", v.Code)
}
}
return objMap, nil
}
... ...
... ... @@ -255,7 +255,8 @@ func MsgChanceApprove(header *protocol.RequestHeader, request *protocol.MsgChanc
commItem.ChanceStatus = protocol.ChanceStatusDelete
} else if chance.ChanceEnableStatus == 0 { //机会关闭
commItem.ChanceStatus = protocol.ChanceStatusClose
} else {
}
if provider, err = agg.GetUserBaseInfo(chance.ChanceUserId, header.CompanyId); err != nil {
commItem.ChanceStatus = protocol.ChanceStatusDelete
log.Error(err)
... ... @@ -273,7 +274,6 @@ func MsgChanceApprove(header *protocol.RequestHeader, request *protocol.MsgChanc
utils.JsonUnmarshal(chance.Videos, &item.Videos)
commItem.Chance = item
}
}
if chance.ReviewStatus == protocol.ReviewStatusPass {
var approveData *protocol.ApproveData
... ... @@ -316,7 +316,8 @@ func MsgChanceSubmit(header *protocol.RequestHeader, request *protocol.MsgChance
commItem.ChanceStatus = protocol.ChanceStatusDelete
} else if chance.ChanceEnableStatus == 0 { //机会关闭
commItem.ChanceStatus = protocol.ChanceStatusClose
} else {
}
if provider, err = agg.GetUserBaseInfo(chance.ChanceUserId, header.CompanyId); err != nil {
commItem.ChanceStatus = protocol.ChanceStatusDelete
log.Error(err)
... ... @@ -334,7 +335,6 @@ func MsgChanceSubmit(header *protocol.RequestHeader, request *protocol.MsgChance
utils.JsonUnmarshal(chance.Videos, &item.Videos)
commItem.Chance = item
}
}
if chance.ReviewStatus == protocol.ReviewStatusPass {
var approveData *protocol.ApproveData
... ... @@ -451,7 +451,8 @@ func MsgChanceThumbUp(header *protocol.RequestHeader, request *protocol.MsgChanc
commItem.ChanceStatus = protocol.ChanceStatusDelete
} else if chance.ChanceEnableStatus == 0 { //机会关闭
commItem.ChanceStatus = protocol.ChanceStatusClose
} else {
}
if provider, err = agg.GetUserBaseInfo(chance.Uid, header.CompanyId); err != nil {
commItem.ChanceStatus = protocol.ChanceStatusDelete
log.Error(err)
... ... @@ -469,7 +470,6 @@ func MsgChanceThumbUp(header *protocol.RequestHeader, request *protocol.MsgChanc
utils.JsonUnmarshal(chance.Videos, &item.Videos)
commItem.Chance = item
}
}
commItem.ReviewStatus = chance.ReviewStatus
}
if chance.SourceType == protocol.SourceTypeComment {
... ...