Эх сурвалжийг харах

feature: 合同添加数据权限

liuyaqi 3 жил өмнө
parent
commit
0582aa3a8b

+ 305 - 4
opms_parent/app/dao/contract/internal/ctr_contract.go

@@ -7,10 +7,13 @@ package internal
 import (
 	"context"
 	"database/sql"
+	"fmt"
+	"time"
+
 	"github.com/gogf/gf/database/gdb"
 	"github.com/gogf/gf/frame/g"
 	"github.com/gogf/gf/frame/gmvc"
-	"time"
+	"github.com/gogf/gf/util/gconv"
 
 	model "dashoo.cn/micro/app/model/contract"
 )
@@ -31,6 +34,10 @@ type ctrContractColumns struct {
 	ContractName      string // 合同名称
 	CustId            string // 关联客户
 	CustName          string // 客户名称
+	CustProvinceId    string // 所在省ID
+	CustProvince      string // 所在省
+	CustCityId        string // 所在市ID
+	CustCity          string // 所在市
 	NboId             string // 关联项目
 	NboName           string // 项目名称
 	ApproStatus       string // 审核状态 10 待提交审核 20 待审核 30 审核已同意 40 审核已拒绝 50 审核已撤销
@@ -71,6 +78,10 @@ var (
 			ContractName:      "contract_name",
 			CustId:            "cust_id",
 			CustName:          "cust_name",
+			CustProvinceId:    "cust_province_id",
+			CustProvince:      "cust_province",
+			CustCityId:        "cust_city_id",
+			CustCity:          "cust_city",
 			NboId:             "nbo_id",
 			NboName:           "nbo_name",
 			ApproStatus:       "appro_status",
@@ -113,6 +124,10 @@ func NewCtrContractDao(tenant string) CtrContractDao {
 			ContractName:      "contract_name",
 			CustId:            "cust_id",
 			CustName:          "cust_name",
+			CustProvinceId:    "cust_province_id",
+			CustProvince:      "cust_province",
+			CustCityId:        "cust_city_id",
+			CustCity:          "cust_city",
 			NboId:             "nbo_id",
 			NboName:           "nbo_name",
 			ApproStatus:       "appro_status",
@@ -152,6 +167,12 @@ func (d *CtrContractDao) Ctx(ctx context.Context) *CtrContractDao {
 	return &CtrContractDao{M: d.M.Ctx(ctx)}
 }
 
+// GetCtx returns the context for current Model.
+// It returns "context.Background() i"s there's no context previously set.
+func (d *CtrContractDao) GetCtx() context.Context {
+	return d.M.GetCtx()
+}
+
 // As sets an alias name for current table.
 func (d *CtrContractDao) As(as string) *CtrContractDao {
 	return &CtrContractDao{M: d.M.As(as)}
@@ -178,6 +199,12 @@ func (d *CtrContractDao) Args(args ...interface{}) *CtrContractDao {
 	return &CtrContractDao{M: d.M.Args(args...)}
 }
 
+// Handler calls each of "handlers" on current Model and returns a new Model.
+// ModelHandler is a function that handles given Model and returns a new Model that is custom modified.
+func (d *CtrContractDao) Handler(handlers ...gdb.ModelHandler) *CtrContractDao {
+	return &CtrContractDao{M: d.M.Handler(handlers...)}
+}
+
 // LeftJoin does "LEFT JOIN ... ON ..." statement on the model.
 // The parameter <table> can be joined table and its joined condition,
 // and also with its alias name, like:
@@ -217,7 +244,33 @@ func (d *CtrContractDao) FieldsEx(fieldNamesOrMapStruct ...interface{}) *CtrCont
 	return &CtrContractDao{M: d.M.FieldsEx(fieldNamesOrMapStruct...)}
 }
 
-// Option sets the extra operation option for the model.
+// FieldCount formats and appends commonly used field "COUNT(column)" to the select fields of model.
+func (d *CtrContractDao) FieldCount(column string, as ...string) *CtrContractDao {
+	return &CtrContractDao{M: d.M.FieldCount(column, as...)}
+}
+
+// FieldSum formats and appends commonly used field "SUM(column)" to the select fields of model.
+func (d *CtrContractDao) FieldSum(column string, as ...string) *CtrContractDao {
+	return &CtrContractDao{M: d.M.FieldSum(column, as...)}
+}
+
+// FieldMin formats and appends commonly used field "MIN(column)" to the select fields of model.
+func (d *CtrContractDao) FieldMin(column string, as ...string) *CtrContractDao {
+	return &CtrContractDao{M: d.M.FieldMin(column, as...)}
+}
+
+// FieldMax formats and appends commonly used field "MAX(column)" to the select fields of model.
+func (d *CtrContractDao) FieldMax(column string, as ...string) *CtrContractDao {
+	return &CtrContractDao{M: d.M.FieldMax(column, as...)}
+}
+
+// FieldAvg formats and appends commonly used field "AVG(column)" to the select fields of model.
+func (d *CtrContractDao) FieldAvg(column string, as ...string) *CtrContractDao {
+	return &CtrContractDao{M: d.M.FieldAvg(column, as...)}
+}
+
+// Option adds extra operation option for the model.
+// Deprecated, use separate operations instead.
 func (d *CtrContractDao) Option(option int) *CtrContractDao {
 	return &CtrContractDao{M: d.M.Option(option)}
 }
@@ -228,7 +281,39 @@ func (d *CtrContractDao) OmitEmpty() *CtrContractDao {
 	return &CtrContractDao{M: d.M.OmitEmpty()}
 }
 
+// OmitEmptyWhere sets optionOmitEmptyWhere option for the model, which automatically filers
+// the Where/Having parameters for "empty" values.
+func (d *CtrContractDao) OmitEmptyWhere() *CtrContractDao {
+	return &CtrContractDao{M: d.M.OmitEmptyWhere()}
+}
+
+// OmitEmptyData sets optionOmitEmptyData option for the model, which automatically filers
+// the Data parameters for "empty" values.
+func (d *CtrContractDao) OmitEmptyData() *CtrContractDao {
+	return &CtrContractDao{M: d.M.OmitEmptyData()}
+}
+
+// OmitNil sets optionOmitNil option for the model, which automatically filers
+// the data and where parameters for "nil" values.
+func (d *CtrContractDao) OmitNil() *CtrContractDao {
+	return &CtrContractDao{M: d.M.OmitNil()}
+}
+
+// OmitNilWhere sets optionOmitNilWhere option for the model, which automatically filers
+// the Where/Having parameters for "nil" values.
+func (d *CtrContractDao) OmitNilWhere() *CtrContractDao {
+	return &CtrContractDao{M: d.M.OmitNilWhere()}
+}
+
+// OmitNilData sets optionOmitNilData option for the model, which automatically filers
+// the Data parameters for "nil" values.
+func (d *CtrContractDao) OmitNilData() *CtrContractDao {
+	return &CtrContractDao{M: d.M.OmitNilData()}
+}
+
 // Filter marks filtering the fields which does not exist in the fields of the operated table.
+// Note that this function supports only single table operations.
+// Deprecated, filter feature is automatically enabled from GoFrame v1.16.0, it is so no longer used.
 func (d *CtrContractDao) Filter() *CtrContractDao {
 	return &CtrContractDao{M: d.M.Filter()}
 }
@@ -257,18 +342,174 @@ func (d *CtrContractDao) WherePri(where interface{}, args ...interface{}) *CtrCo
 	return &CtrContractDao{M: d.M.WherePri(where, args...)}
 }
 
+// Having sets the having statement for the model.
+// The parameters of this function usage are as the same as function Where.
+// See Where.
+func (d *CtrContractDao) Having(having interface{}, args ...interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.Having(having, args...)}
+}
+
+// Wheref builds condition string using fmt.Sprintf and arguments.
+// Note that if the number of "args" is more than the place holder in "format",
+// the extra "args" will be used as the where condition arguments of the Model.
+func (d *CtrContractDao) Wheref(format string, args ...interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.Wheref(format, args...)}
+}
+
+// WhereLT builds "column < value" statement.
+func (d *CtrContractDao) WhereLT(column string, value interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereLT(column, value)}
+}
+
+// WhereLTE builds "column <= value" statement.
+func (d *CtrContractDao) WhereLTE(column string, value interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereLTE(column, value)}
+}
+
+// WhereGT builds "column > value" statement.
+func (d *CtrContractDao) WhereGT(column string, value interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereGT(column, value)}
+}
+
+// WhereGTE builds "column >= value" statement.
+func (d *CtrContractDao) WhereGTE(column string, value interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereGTE(column, value)}
+}
+
+// WhereBetween builds "column BETWEEN min AND max" statement.
+func (d *CtrContractDao) WhereBetween(column string, min, max interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereBetween(column, min, max)}
+}
+
+// WhereLike builds "column LIKE like" statement.
+func (d *CtrContractDao) WhereLike(column string, like interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereLike(column, like)}
+}
+
+// WhereIn builds "column IN (in)" statement.
+func (d *CtrContractDao) WhereIn(column string, in interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereIn(column, in)}
+}
+
+// WhereNull builds "columns[0] IS NULL AND columns[1] IS NULL ..." statement.
+func (d *CtrContractDao) WhereNull(columns ...string) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereNull(columns...)}
+}
+
+// WhereNotBetween builds "column NOT BETWEEN min AND max" statement.
+func (d *CtrContractDao) WhereNotBetween(column string, min, max interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereNotBetween(column, min, max)}
+}
+
+// WhereNotLike builds "column NOT LIKE like" statement.
+func (d *CtrContractDao) WhereNotLike(column string, like interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereNotLike(column, like)}
+}
+
+// WhereNot builds "column != value" statement.
+func (d *CtrContractDao) WhereNot(column string, value interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereNot(column, value)}
+}
+
+// WhereNotIn builds "column NOT IN (in)" statement.
+func (d *CtrContractDao) WhereNotIn(column string, in interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereNotIn(column, in)}
+}
+
+// WhereNotNull builds "columns[0] IS NOT NULL AND columns[1] IS NOT NULL ..." statement.
+func (d *CtrContractDao) WhereNotNull(columns ...string) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereNotNull(columns...)}
+}
+
+// WhereOr adds "OR" condition to the where statement.
+func (d *CtrContractDao) WhereOr(where interface{}, args ...interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereOr(where, args...)}
+}
+
+// WhereOrf builds "OR" condition string using fmt.Sprintf and arguments.
+func (d *CtrContractDao) WhereOrf(format string, args ...interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereOrf(format, args...)}
+}
+
+// WhereOrLT builds "column < value" statement in "OR" conditions..
+func (d *CtrContractDao) WhereOrLT(column string, value interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereOrLT(column, value)}
+}
+
+// WhereOrLTE builds "column <= value" statement in "OR" conditions..
+func (d *CtrContractDao) WhereOrLTE(column string, value interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereOrLTE(column, value)}
+}
+
+// WhereOrGT builds "column > value" statement in "OR" conditions..
+func (d *CtrContractDao) WhereOrGT(column string, value interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereOrGT(column, value)}
+}
+
+// WhereOrGTE builds "column >= value" statement in "OR" conditions..
+func (d *CtrContractDao) WhereOrGTE(column string, value interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereOrGTE(column, value)}
+}
+
+// WhereOrBetween builds "column BETWEEN min AND max" statement in "OR" conditions.
+func (d *CtrContractDao) WhereOrBetween(column string, min, max interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereOrBetween(column, min, max)}
+}
+
+// WhereOrLike builds "column LIKE like" statement in "OR" conditions.
+func (d *CtrContractDao) WhereOrLike(column string, like interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereOrLike(column, like)}
+}
+
+// WhereOrIn builds "column IN (in)" statement in "OR" conditions.
+func (d *CtrContractDao) WhereOrIn(column string, in interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereOrIn(column, in)}
+}
+
+// WhereOrNull builds "columns[0] IS NULL OR columns[1] IS NULL ..." statement in "OR" conditions.
+func (d *CtrContractDao) WhereOrNull(columns ...string) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereOrNull(columns...)}
+}
+
+// WhereOrNotBetween builds "column NOT BETWEEN min AND max" statement in "OR" conditions.
+func (d *CtrContractDao) WhereOrNotBetween(column string, min, max interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereOrNotBetween(column, min, max)}
+}
+
+// WhereOrNotLike builds "column NOT LIKE like" statement in "OR" conditions.
+func (d *CtrContractDao) WhereOrNotLike(column string, like interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereOrNotLike(column, like)}
+}
+
+// WhereOrNotIn builds "column NOT IN (in)" statement.
+func (d *CtrContractDao) WhereOrNotIn(column string, in interface{}) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereOrNotIn(column, in)}
+}
+
+// WhereOrNotNull builds "columns[0] IS NOT NULL OR columns[1] IS NOT NULL ..." statement in "OR" conditions.
+func (d *CtrContractDao) WhereOrNotNull(columns ...string) *CtrContractDao {
+	return &CtrContractDao{M: d.M.WhereOrNotNull(columns...)}
+}
+
+// Group sets the "GROUP BY" statement for the model.
+func (d *CtrContractDao) Group(groupBy ...string) *CtrContractDao {
+	return &CtrContractDao{M: d.M.Group(groupBy...)}
+}
+
 // And adds "AND" condition to the where statement.
+// Deprecated, use Where instead.
 func (d *CtrContractDao) And(where interface{}, args ...interface{}) *CtrContractDao {
 	return &CtrContractDao{M: d.M.And(where, args...)}
 }
 
 // Or adds "OR" condition to the where statement.
+// Deprecated, use WhereOr instead.
 func (d *CtrContractDao) Or(where interface{}, args ...interface{}) *CtrContractDao {
 	return &CtrContractDao{M: d.M.Or(where, args...)}
 }
 
-// Group sets the "GROUP BY" statement for the model.
-func (d *CtrContractDao) Group(groupBy string) *CtrContractDao {
+// GroupBy sets the "GROUP BY" statement for the model.
+func (d *CtrContractDao) GroupBy(groupBy string) *CtrContractDao {
 	return &CtrContractDao{M: d.M.Group(groupBy)}
 }
 
@@ -277,6 +518,28 @@ func (d *CtrContractDao) Order(orderBy ...string) *CtrContractDao {
 	return &CtrContractDao{M: d.M.Order(orderBy...)}
 }
 
+// OrderAsc sets the "ORDER BY xxx ASC" statement for the model.
+func (d *CtrContractDao) OrderAsc(column string) *CtrContractDao {
+	return &CtrContractDao{M: d.M.OrderAsc(column)}
+}
+
+// OrderDesc sets the "ORDER BY xxx DESC" statement for the model.
+func (d *CtrContractDao) OrderDesc(column string) *CtrContractDao {
+	return &CtrContractDao{M: d.M.OrderDesc(column)}
+}
+
+// OrderRandom sets the "ORDER BY RANDOM()" statement for the model.
+func (d *CtrContractDao) OrderRandom() *CtrContractDao {
+	return &CtrContractDao{M: d.M.OrderRandom()}
+}
+
+// OrderBy is alias of Model.Order.
+// See Model.Order.
+// Deprecated, use Order instead.
+func (d *CtrContractDao) OrderBy(orderBy string) *CtrContractDao {
+	return &CtrContractDao{M: d.M.Order(orderBy)}
+}
+
 // Limit sets the "LIMIT" statement for the model.
 // The parameter <limit> can be either one or two number, if passed two number is passed,
 // it then sets "LIMIT limit[0],limit[1]" statement for the model, or else it sets "LIMIT limit[0]"
@@ -291,6 +554,11 @@ func (d *CtrContractDao) Offset(offset int) *CtrContractDao {
 	return &CtrContractDao{M: d.M.Offset(offset)}
 }
 
+// Distinct forces the query to only return distinct results.
+func (d *CtrContractDao) Distinct() *CtrContractDao {
+	return &CtrContractDao{M: d.M.Distinct()}
+}
+
 // Page sets the paging number for the model.
 // The parameter <page> is started from 1 for paging.
 // Note that, it differs that the Limit function start from 0 for "LIMIT" statement.
@@ -484,3 +752,36 @@ func (d *CtrContractDao) LockShared() *CtrContractDao {
 func (d *CtrContractDao) Unscoped() *CtrContractDao {
 	return &CtrContractDao{M: d.M.Unscoped()}
 }
+
+// DataScope enables the DataScope feature.
+func (d *CtrContractDao) DataScope(ctx context.Context, userCol ...string) *CtrContractDao {
+	cs := ctx.Value("contextService")
+	dataScope := gconv.Map(cs)["dataScope"].(g.Map)
+	fmt.Println(dataScope, "-----------------------")
+	fmt.Println(d.GetFieldsStr(), "---------------")
+	if dataScope != nil {
+		m := d.M
+		userIds, ok := dataScope["userIds"]
+		delete(dataScope, "userIds")
+		if ok && userIds != "-1" {
+			column := "created_by"
+			if len(userCol) == 1 {
+				column = userCol[0]
+			}
+			if d.Table == "sys_user" {
+				s := column + " IN (?) OR id IN (?)"
+				m = m.WhereOr(s, g.Slice{userIds, userIds})
+			} else {
+				m = m.WhereOr(column, userIds)
+			}
+		}
+		for k, v := range dataScope {
+			m = m.WhereOr(k, v)
+			//if ok, _ := d.M.HasField(k); ok {
+			//	m = m.WhereOr(k, v)
+			//}
+		}
+		return &CtrContractDao{M: d.M.Where(dataScope)}
+	}
+	return d
+}

+ 4 - 0
opms_parent/app/model/contract/internal/ctr_contract.go

@@ -15,6 +15,10 @@ type CtrContract struct {
 	ContractName      string      `orm:"contract_name"       json:"contractName"`      // 合同名称
 	CustId            int         `orm:"cust_id"             json:"custId"`            // 关联客户
 	CustName          string      `orm:"cust_name"           json:"custName"`          // 客户名称
+	CustProvinceId    int         `orm:"cust_province_id"    json:"custProvinceId"`    // 所在省ID
+	CustProvince      string      `orm:"cust_province"       json:"custProvince"`      // 所在省
+	CustCityId        int         `orm:"cust_city_id"        json:"custCityId"`        // 所在市ID
+	CustCity          string      `orm:"cust_city"           json:"custCity"`          // 所在市
 	NboId             int         `orm:"nbo_id"              json:"nboId"`             // 关联项目
 	NboName           string      `orm:"nbo_name"            json:"nboName"`           // 项目名称
 	ApproStatus       string      `orm:"appro_status"        json:"approStatus"`       // 审核状态 10 待提交审核 20 待审核 30 审核已同意 40 审核已拒绝 50 审核已撤销

+ 10 - 5
opms_parent/app/service/contract/ctr_contract.go

@@ -26,6 +26,7 @@ import (
 	"dashoo.cn/opms_libary/request"
 	"dashoo.cn/opms_libary/utils"
 	"github.com/gogf/gf/database/gdb"
+	"github.com/gogf/gf/frame/g"
 	"github.com/gogf/gf/os/gtime"
 	"github.com/gogf/gf/util/gvalid"
 )
@@ -38,8 +39,9 @@ type CtrContractService struct {
 	ProductDao      *basedao.BaseProductDao
 	DynamicsDao     *dao.CtrContractDynamicsDao
 
-	Tenant   string
-	userInfo request.UserInfo
+	Tenant    string
+	userInfo  request.UserInfo
+	DataScope g.Map `json:"dataScope"`
 }
 
 func NewCtrContractService(ctx context.Context) (*CtrContractService, error) {
@@ -62,6 +64,7 @@ func NewCtrContractService(ctx context.Context) (*CtrContractService, error) {
 		DynamicsDao:     dao.NewCtrContractDynamicsDao(tenant),
 		Tenant:          tenant,
 		userInfo:        userInfo,
+		DataScope:       userInfo.DataScope,
 	}, nil
 }
 
@@ -144,9 +147,11 @@ func (s CtrContractService) DynamicsList(ctx context.Context, req *model.CtrCont
 }
 
 func (s CtrContractService) List(ctx context.Context, req *model.CtrContractListReq) (int, []*model.CtrContractListRsp, error) {
-	dao := s.Dao.DB.Table("ctr_contract a").
-		LeftJoin("cust_customer b", "a.cust_id=b.id").
-		Unscoped().Where("a.deleted_time is null")
+	ctx = context.WithValue(ctx, "contextService", s)
+	dao := s.Dao.DataScope(ctx, "incharge_id").As("a").
+		LeftJoin("cust_customer b", "a.cust_id=b.id").Unscoped().
+		Where("a.deleted_time is null")
+
 	if req.SearchText != "" {
 		likestr := fmt.Sprintf("%%%s%%", req.SearchText)
 		dao = dao.Where("(a.contract_code LIKE ? || a.contract_name LIKE ? || a.cust_name LIKE ? || a.nbo_name LIKE ?)", likestr, likestr, likestr, likestr)