WH01243 há 4 anos atrás
pai
commit
ee6dba0ccc

+ 4 - 5
api/integral.api

@@ -15,11 +15,10 @@ type resourcesReq {
 	Url              string `form:"url,optional"`              //下载地址
 	SearchCriteria   string `form:"searchCriteria,optional"`   //搜索条件
 	Source           string `form:"source,optional"`           //数据来源
-	StartTime        string `form:"startTime,optional"`        //新增时数据包开始时间
 	EndTime          string `form:"endTime,optional"`          //新增时数据包截止时间
 	InfoId           string `form:"infoId,optional"`           //信息标识
 	DuplicateRemoval int64  `form:"duplicateRemoval,optional"` //是否去重0不去1去重
-
+	DeductionType    string `form:"deductionType,optional"`    //扣费类型(1高级字段包)
 }
 
 type resourcesRes {
@@ -28,9 +27,9 @@ type resourcesRes {
 	Message string `form:"message"`
 }
 type previewReq {
-	InfoId        string `form:"infoId,optional"`        //信息标识
-	AccountId     string `form:"accountId,optional"`     //企业标识
-	DeductionType string `form:"deductionType,optional"` //资源代码
+	InfoId       string `form:"infoId,optional"`       //信息标识
+	AccountId    string `form:"accountId,optional"`    //企业标识
+	ResourceType string `form:"resourceType,optional"` //资源类型
 }
 
 type previewRes {

+ 11 - 17
api/internal/logic/findpreviewlogic.go

@@ -1,6 +1,7 @@
 package logic
 
 import (
+	"app.yhyue.com/moapp/jyResourcesCenter/rpc/resourcesCenterclient"
 	"context"
 	"fmt"
 
@@ -28,25 +29,18 @@ func (l *FindPreviewLogic) FindPreview(req types.PreviewReq) (*types.PreviewRes,
 	// todo: add your logic here and delete this line
 	result := &types.PreviewRes{}
 	fmt.Println(req)
-	//lsi := l.svcCtx.ResourcesCenter
-	/*resp, err := lsi(l.ctx, &activityclient.UpdateStateReq{
-		UserId:         req.UserId,
-		AppId:          req.AppId,
-		UserLottertId:  req.UserLotteryId,
-		Model:          req.Model,
-		OrderCode:      req.OrderCode,
-		UserName:       req.UserName,
-		DiscountId:     req.DiscountId,
-		UserPhone:      req.UserPhone,
-		UseProduct:     req.UseProduct,
-		UseProductType: req.UseProductType,
+	lsi := l.svcCtx.ResourcesCenter
+	resp, err := lsi.FindPreview(l.ctx, &resourcesCenterclient.PreviewReq{
+		AccountId:    req.AccountId,
+		ResourceType: req.ResourceType,
+		InfoId:       req.InfoId,
 	})
 	if err != nil {
 		return nil, err
-	}*/
-	result.Code = 0
-	result.Message = "资源重复查询"
-	result.DeductionNumb=0
-	result.RepeatNumb=0
+	}
+	result.Code = resp.Code
+	result.Message = resp.Message
+	result.DeductionNumb = resp.DeductionNumb
+	result.RepeatNumb = resp.RepeatNumb
 	return result, nil
 }

+ 39 - 16
api/internal/logic/updateuserbalancelogic.go

@@ -1,6 +1,7 @@
 package logic
 
 import (
+	"app.yhyue.com/moapp/jyResourcesCenter/rpc/resourcesCenterclient"
 	"context"
 	"fmt"
 
@@ -28,23 +29,45 @@ func (l *UpdateUserBalanceLogic) UpdateUserBalance(req types.ResourcesReq) (*typ
 	// todo: add your logic here and delete this line
 	result := &types.ResourcesRes{}
 	fmt.Println(req)
-	//lsi := l.svcCtx.ResourcesCenter
-	/*resp, err := lsi(l.ctx, &activityclient.UpdateStateReq{
-		UserId:         req.UserId,
-		AppId:          req.AppId,
-		UserLottertId:  req.UserLotteryId,
-		Model:          req.Model,
-		OrderCode:      req.OrderCode,
-		UserName:       req.UserName,
-		DiscountId:     req.DiscountId,
-		UserPhone:      req.UserPhone,
-		UseProduct:     req.UseProduct,
-		UseProductType: req.UseProductType,
-	})
+	lsi := l.svcCtx.ResourcesCenter
+	var err error
+	resp := &resourcesCenterclient.Response{}
+	if req.Model == 0 {
+		resp, err = lsi.UseUserDetailed(l.ctx, &resourcesCenterclient.Resources{
+			AccountId:        req.AccountId,
+			CompanyId:        req.CompanyId,
+			DepartmentId:     req.DepartmentId,
+			Name:             req.Name,
+			ResourceType:     req.ResourceType,
+			Number:           req.Number,
+			Spec:             req.Spec,
+			AppId:            req.AppId,
+			Model:            req.Model,
+			Url:              req.Url,
+			SearchCriteria:   req.SearchCriteria,
+			Source:           req.Source,
+			InfoId:           req.InfoId,
+			DuplicateRemoval: req.DuplicateRemoval,
+			RuleId:           req.RuleId,
+			UserId:           req.UserId,
+		})
+	} else {
+		resp, err = lsi.PurchaseUserBalance(l.ctx, &resourcesCenterclient.Resources{
+			AccountId:    req.AccountId,
+			CompanyId:    req.CompanyId,
+			DepartmentId: req.DepartmentId,
+			Name:         req.Name,
+			ResourceType: req.ResourceType,
+			Number:       req.Number,
+			Spec:         req.Spec,
+			AppId:        req.AppId,
+			EndTime:      req.EndTime,
+		})
+	}
 	if err != nil {
 		return nil, err
-	}*/
-	result.Code = 0
-	result.Message = "资源操作成功"
+	}
+	result.Code = resp.Code
+	result.Message = resp.Message
 	return result, nil
 }

+ 4 - 4
api/internal/types/types.go

@@ -16,10 +16,10 @@ type ResourcesReq struct {
 	Url              string `form:"url,optional"`              //下载地址
 	SearchCriteria   string `form:"searchCriteria,optional"`   //搜索条件
 	Source           string `form:"source,optional"`           //数据来源
-	StartTime        string `form:"startTime,optional"`        //新增时数据包开始时间
 	EndTime          string `form:"endTime,optional"`          //新增时数据包截止时间
 	InfoId           string `form:"infoId,optional"`           //信息标识
 	DuplicateRemoval int64  `form:"duplicateRemoval,optional"` //是否去重0不去1去重
+	DeductionType    string `form:"deductionType,optional"`    //扣费类型(1高级字段包)
 }
 
 type ResourcesRes struct {
@@ -28,9 +28,9 @@ type ResourcesRes struct {
 }
 
 type PreviewReq struct {
-	InfoId        string `form:"infoId,optional"`        //信息标识
-	AccountId     string `form:"accountId,optional"`     //企业标识
-	DeductionType string `form:"deductionType,optional"` //资源代码
+	InfoId       string `form:"infoId,optional"`       //信息标识
+	AccountId    string `form:"accountId,optional"`    //企业标识
+	ResourceType string `form:"resourceType,optional"` //资源类型
 }
 
 type PreviewRes struct {

+ 2 - 1
entity/accountResources.go

@@ -2,7 +2,7 @@ package entity
 
 type Balance struct {
 	Id           int64  `xorm:"pk autoincr id" form:"id" json:"id"`
-	AccountId    string  `xorm:"accountId" form:"accountId" json:"accountId"`          //账户标识
+	AccountId    string `xorm:"accountId" form:"accountId" json:"accountId"`          //账户标识
 	CompanyId    int64  `xorm:"companyId" form:"companyId" json:"companyId"`          //企业标识
 	DepartmentId int64  `xorm:"departmentId" form:"departmentId" json:"departmentId"` //组织标识
 	Name         string `xorm:"name" form:"name" json:"name"`                         //资源名称
@@ -10,4 +10,5 @@ type Balance struct {
 	Number       int64  `xorm:"number" form:"number" json:"number"`                   //数量
 	Spec         string `xorm:"spec" form:"spec" json:"spec"`                         //规格
 	AppId        string `xorm:"appId" form:"appId" json:"appId"`                      //标识
+	EndTime      string `xorm:"endTime" form:"endTime" json:"endTime"`                //截止时间
 }

+ 1 - 1
entity/detailed.go

@@ -9,7 +9,6 @@ type Detailed struct {
 	DepartmentId   int64     `xorm:"departmentId" form:"departmentId" json:"departmentId"`       //组织标识
 	ResourceType   string    `xorm:"resourceType" form:"resourceType" json:"resourceType"`       //资源类型
 	ExportNum      int64     `xorm:"exportNum" form:"exportNum" json:"exportNum"`                //导出数量
-	DeductionType  int64     `xorm:"deductionType" form:"deductionType" json:"deductionType"`    //扣费类型(1高级字段包)
 	RuleId         string    `xorm:"ruleId" form:"ruleId" json:"ruleId"`                         //使用规则标识
 	ExportTime     time.Time `xorm:"exportTime" form:"exportTime" json:"exportTime"`             //导出时间
 	UserType       int64     `xorm:"userType" form:"userType" json:"userType"`                   //流水类型0使用1新增
@@ -18,4 +17,5 @@ type Detailed struct {
 	SearchCriteria string    `xorm:"searchCriteria" form:"searchCriteria" json:"searchCriteria"` //搜索条件
 	Source         string    `xorm:"source" form:"source" json:"source"`                         //数据来源
 	UserId         string    `xorm:"userId" form:"userId" json:"userId"`                         //用户标识
+	Name           string    `xorm:"name" form:"name" json:"name"`                               //资源名称
 }

+ 1 - 1
rpc/etc/resourcescenter.yaml

@@ -13,7 +13,7 @@ Etcd:
   Hosts:
     - 127.0.0.1:2379
   Key: resourcescenter.rpc
-DataSource: root:Topnet123@tcp(127.0.0.1:3366)/jy_user_center?charset=utf8mb4&parseTime=true&loc=Local
+DataSource: root:Topnet123@tcp(127.0.0.1:13306)/jy_user_center?charset=utf8mb4&parseTime=true&loc=Local
 FileSystemConf:
   Etcd:
     Hosts:

+ 8 - 3
rpc/internal/logic/findpreviewlogic.go

@@ -24,8 +24,13 @@ func NewFindPreviewLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FindP
 }
 
 // 预览信息
-func (l *FindPreviewLogic) FindPreview(in *resourcesCenter.PreviewReq) (*resourcesCenter.ConsumeRecordRes, error) {
+func (l *FindPreviewLogic) FindPreview(in *resourcesCenter.PreviewReq) (*resourcesCenter.PreviewRes, error) {
 	// todo: add your logic here and delete this line
-
-	return &resourcesCenter.ConsumeRecordRes{}, nil
+	code, message, repeatNumbm, deductionNumb := balanceService.FindPreview(in)
+	return &resourcesCenter.PreviewRes{
+		Code:          code,
+		Message:       message,
+		RepeatNumb:    repeatNumbm,
+		DeductionNumb: deductionNumb,
+	}, nil
 }

+ 58 - 0
rpc/internal/logic/purchaseuserbalancelogic.go

@@ -0,0 +1,58 @@
+package logic
+
+import (
+	"app.yhyue.com/moapp/jyResourcesCenter/service"
+	"context"
+
+	"app.yhyue.com/moapp/jyResourcesCenter/rpc/internal/svc"
+	"app.yhyue.com/moapp/jyResourcesCenter/rpc/resourcesCenter"
+
+	"github.com/tal-tech/go-zero/core/logx"
+)
+
+type PurchaseUserBalanceLogic struct {
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+	logx.Logger
+}
+
+func NewPurchaseUserBalanceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PurchaseUserBalanceLogic {
+	return &PurchaseUserBalanceLogic{
+		ctx:    ctx,
+		svcCtx: svcCtx,
+		Logger: logx.WithContext(ctx),
+	}
+}
+
+var balanceService = service.BalanceService{}
+
+// 根据账户标识购买资源
+func (l *PurchaseUserBalanceLogic) PurchaseUserBalance(in *resourcesCenter.Resources) (*resourcesCenter.Response, error) {
+	// todo: add your logic here and delete this line
+	balance := &resourcesCenter.Balance{
+		AccountId:    in.AccountId,
+		CompanyId:    in.CompanyId,
+		DepartmentId: in.DepartmentId,
+		Name:         in.Name,
+		ResourceType: in.ResourceType,
+		Number:       in.Number,
+		Spec:         in.Spec,
+		AppId:        in.AppId,
+		Model:        in.Model,
+		EndTime:      in.EndTime,
+	}
+	detailed := &resourcesCenter.Detailed{
+		AccountId:     in.AccountId,
+		CompanyId:     in.CompanyId,
+		ResourceType:  in.AccountId,
+		ExportNum:     in.Number,
+		DepartmentId:  in.DepartmentId,
+		Name:          in.Name,
+		UserType:      in.Model,
+	}
+	code, msg := balanceService.PurchaseUserBalance(balance,detailed)
+	return &resourcesCenter.Response{
+		Code:    code,
+		Message: msg,
+	}, nil
+}

+ 0 - 36
rpc/internal/logic/updateuserbalancelogic.go

@@ -1,36 +0,0 @@
-package logic
-
-import (
-	"app.yhyue.com/moapp/jyResourcesCenter/rpc/internal/svc"
-	"app.yhyue.com/moapp/jyResourcesCenter/rpc/resourcesCenter"
-	"app.yhyue.com/moapp/jyResourcesCenter/service"
-	"context"
-
-	"github.com/tal-tech/go-zero/core/logx"
-)
-
-type UpdateUserBalanceLogic struct {
-	ctx    context.Context
-	svcCtx *svc.ServiceContext
-	logx.Logger
-}
-
-func NewUpdateUserBalanceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserBalanceLogic {
-	return &UpdateUserBalanceLogic{
-		ctx:    ctx,
-		svcCtx: svcCtx,
-		Logger: logx.WithContext(ctx),
-	}
-}
-var balanceService service.BalanceService
-
-// 根据账户标识修改资源结存账
-func (l *UpdateUserBalanceLogic) UpdateUserBalance(in *resourcesCenter.Balance) (*resourcesCenter.Response, error) {
-	// todo: add your logic here and delete this line
-
-	code, msg := balanceService.UpdateUserBalance(in)
-	return &resourcesCenter.Response{
-		Code:    code,
-		Message: msg,
-	}, nil
-}

+ 0 - 34
rpc/internal/logic/updateuserdetailedlogic.go

@@ -1,34 +0,0 @@
-package logic
-
-import (
-	"context"
-
-	"app.yhyue.com/moapp/jyResourcesCenter/rpc/internal/svc"
-	"app.yhyue.com/moapp/jyResourcesCenter/rpc/resourcesCenter"
-
-	"github.com/tal-tech/go-zero/core/logx"
-)
-
-type UpdateUserDetailedLogic struct {
-	ctx    context.Context
-	svcCtx *svc.ServiceContext
-	logx.Logger
-}
-
-func NewUpdateUserDetailedLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserDetailedLogic {
-	return &UpdateUserDetailedLogic{
-		ctx:    ctx,
-		svcCtx: svcCtx,
-		Logger: logx.WithContext(ctx),
-	}
-}
-
-// 根据账户标识记录资源使用流水账
-func (l *UpdateUserDetailedLogic) UpdateUserDetailed(in *resourcesCenter.Detailed) (*resourcesCenter.Response, error) {
-	// todo: add your logic here and delete this line
-	code, msg := balanceService.UpdateUserDetailed(in)
-	return &resourcesCenter.Response{
-		Code:    code,
-		Message: msg,
-	}, nil
-}

+ 60 - 0
rpc/internal/logic/useuserdetailedlogic.go

@@ -0,0 +1,60 @@
+package logic
+
+import (
+	"context"
+
+	"app.yhyue.com/moapp/jyResourcesCenter/rpc/internal/svc"
+	"app.yhyue.com/moapp/jyResourcesCenter/rpc/resourcesCenter"
+
+	"github.com/tal-tech/go-zero/core/logx"
+)
+
+type UseUserDetailedLogic struct {
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+	logx.Logger
+}
+
+func NewUseUserDetailedLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UseUserDetailedLogic {
+	return &UseUserDetailedLogic{
+		ctx:    ctx,
+		svcCtx: svcCtx,
+		Logger: logx.WithContext(ctx),
+	}
+}
+
+// 根据账户标识使用资源
+func (l *UseUserDetailedLogic) UseUserDetailed(in *resourcesCenter.Resources) (*resourcesCenter.Response, error) {
+	// todo: add your logic here and delete this line
+	balance := &resourcesCenter.Balance{
+		AccountId:    in.AccountId,
+		CompanyId:    in.CompanyId,
+		DepartmentId: in.DepartmentId,
+		Name:         in.Name,
+		ResourceType: in.ResourceType,
+		Number:       in.Number,
+		Spec:         in.Spec,
+		AppId:        in.AppId,
+		Model:        in.Model,
+		EndTime:      in.EndTime,
+	}
+	detailed := &resourcesCenter.Detailed{
+		AccountId:      in.AccountId,
+		CompanyId:      in.CompanyId,
+		ResourceType:   in.AccountId,
+		ExportNum:      in.Number,
+		DepartmentId:   in.DepartmentId,
+		Name:           in.Name,
+		UserId:         "",
+		UserType:       in.Model,
+		Url:            in.Url,
+		SearchCriteria: in.SearchCriteria,
+		Source:         in.Source,
+		RuleId:         in.RuleId,
+	}
+	code, msg := balanceService.UseUserDetailed(balance, detailed)
+	return &resourcesCenter.Response{
+		Code:    code,
+		Message: msg,
+	}, nil
+}

+ 9 - 9
rpc/internal/server/resourcescenterserver.go

@@ -39,20 +39,20 @@ func (s *ResourcesCenterServer) FindConsumeRecord(ctx context.Context, in *resou
 	return l.FindConsumeRecord(in)
 }
 
-// 根据账户标识修改资源结存账
-func (s *ResourcesCenterServer) UpdateUserBalance(ctx context.Context, in *resourcesCenter.Balance) (*resourcesCenter.Response, error) {
-	l := logic.NewUpdateUserBalanceLogic(ctx, s.svcCtx)
-	return l.UpdateUserBalance(in)
+// 根据账户标识购买资源
+func (s *ResourcesCenterServer) PurchaseUserBalance(ctx context.Context, in *resourcesCenter.Resources) (*resourcesCenter.Response, error) {
+	l := logic.NewPurchaseUserBalanceLogic(ctx, s.svcCtx)
+	return l.PurchaseUserBalance(in)
 }
 
-// 根据账户标识记录资源使用流水账
-func (s *ResourcesCenterServer) UpdateUserDetailed(ctx context.Context, in *resourcesCenter.Detailed) (*resourcesCenter.Response, error) {
-	l := logic.NewUpdateUserDetailedLogic(ctx, s.svcCtx)
-	return l.UpdateUserDetailed(in)
+// 根据账户标识使用资源
+func (s *ResourcesCenterServer) UseUserDetailed(ctx context.Context, in *resourcesCenter.Resources) (*resourcesCenter.Response, error) {
+	l := logic.NewUseUserDetailedLogic(ctx, s.svcCtx)
+	return l.UseUserDetailed(in)
 }
 
 // 预览信息
-func (s *ResourcesCenterServer) FindPreview(ctx context.Context, in *resourcesCenter.PreviewReq) (*resourcesCenter.ConsumeRecordRes, error) {
+func (s *ResourcesCenterServer) FindPreview(ctx context.Context, in *resourcesCenter.PreviewReq) (*resourcesCenter.PreviewRes, error) {
 	l := logic.NewFindPreviewLogic(ctx, s.svcCtx)
 	return l.FindPreview(in)
 }

+ 30 - 10
rpc/resourcesCenter.proto

@@ -1,7 +1,7 @@
 syntax = "proto3";
 
 package resourcesCenter;
-//option go_package = "resourcesCenter/";
+option go_package = "resourcesCenter/";
 message Response {
     int64 code = 1; //响应代码
     string message = 2; //响应消息
@@ -16,15 +16,34 @@ message Balance {
     string spec = 7; //规格
     string appId = 8; //标识
     int64 model = 9; //操作类型0使用1新增
+    string endTime = 10; //截止时间
+}
+message Resources {
+    string accountId = 1; //账户标识
+    int64 companyId = 2; //企业标识
+    int64 departmentId = 3; //组织标识
+    string name = 4; //资源名称
+    string resourceType = 5; //资源类型
+    int64 number = 6; //数量
+    string spec = 7; //规格
+    string appId = 8; //标识
+    int64 model = 9; //操作类型0使用1新增
+    string endTime = 10; //截止时间
+    string url = 12; //下载地址
+    string searchCriteria = 13; //搜索条件
+    string source = 14; //数据来源
+    string infoId = 15; //信息标识
+    int64 duplicateRemoval = 16; //是否去重0不去1去重
+    string ruleId = 17; //使用规则标识
+    string userId = 11; //用户标识
 }
 message Detailed {
     string accountId = 1; //账户标识
     int64 companyId = 2; //企业标识
     string resourceType = 3; //资源类型
-    int64 exportNum = 4; //导出数量
-    string deductionType = 5; //扣费类型(1高级字段包)
+    int64 exportNum = 4; //导出数量/购买数量
     string ruleId = 6; //使用规则标识
-    int64 exportTime = 7; //导出时间
+    int64 exportTime = 7; //导出时间/购买时间
     int64 userType = 8; //流水类型0使用1新增
     int64 departmentId = 9; //组织标识
     string userId = 10; //用户标识
@@ -32,6 +51,7 @@ message Detailed {
     string url = 12; //下载地址
     string searchCriteria = 13; //搜索条件
     string source = 14; //数据来源
+    string name = 5; //资源名称
 
 }
 
@@ -78,7 +98,7 @@ message ResourcesReq {
 message PreviewReq {
     string infoId  = 1; //信息标识
     string accountId = 2;  //企业标识
-    string deductionType = 3;  //资源代码
+    string resourceType = 3;  //资源代码
 }
 message PreviewRes {
      int64 code = 1; //响应代码
@@ -124,10 +144,10 @@ service ResourcesCenter {
     //查询流水账
     rpc findConsumeRecord (RecordReq) returns (ConsumeRecordRes);
 
-    //根据账户标识修改资源结存账
-    rpc updateUserBalance (Balance) returns (Response);
-    //根据账户标识记录资源使用流水账
-    rpc updateUserDetailed (Detailed) returns (Response);
+    //根据账户标识购买资源
+    rpc purchaseUserBalance (Resources) returns (Response);
+     //根据账户标识使用资源
+    rpc useUserDetailed (Resources) returns (Response);
     //预览信息
-    rpc findPreview (PreviewReq) returns (ConsumeRecordRes);
+    rpc findPreview (PreviewReq) returns (PreviewRes);
 }

Diff do ficheiro suprimidas por serem muito extensas
+ 430 - 221
rpc/resourcesCenter/resourcesCenter.pb.go


+ 22 - 21
rpc/resourcesCenterclient/resourcescenter.go

@@ -1,7 +1,7 @@
 // Code generated by goctl. DO NOT EDIT!
 // Source: resourcesCenter.proto
 
-//go:generate mockgen -destination ./resourcescenter_mock.go -package resourcescenterclient -source $GOFILE
+//go:generate mockgen -destination ./resourcescenter_mock.go -package resourcescenter -source $GOFILE
 
 package resourcesCenterclient
 
@@ -14,19 +14,20 @@ import (
 )
 
 type (
-	Balance           = resourcesCenter.Balance
-	ResourcesAuth     = resourcesCenter.ResourcesAuth
+	ResourcesReq      = resourcesCenter.ResourcesReq
+	PreviewReq        = resourcesCenter.PreviewReq
+	AccountBalanceRes = resourcesCenter.AccountBalanceRes
 	ResourceBalance   = resourcesCenter.ResourceBalance
+	ConsumeRecord     = resourcesCenter.ConsumeRecord
 	ResourcesAuthRes  = resourcesCenter.ResourcesAuthRes
-	AccountBalanceRes = resourcesCenter.AccountBalanceRes
-	RecordReq         = resourcesCenter.RecordReq
 	ConsumeRecordRes  = resourcesCenter.ConsumeRecordRes
-	Response          = resourcesCenter.Response
+	Balance           = resourcesCenter.Balance
 	Detailed          = resourcesCenter.Detailed
-	ConsumeRecord     = resourcesCenter.ConsumeRecord
-	ResourcesReq      = resourcesCenter.ResourcesReq
-	PreviewReq        = resourcesCenter.PreviewReq
 	PreviewRes        = resourcesCenter.PreviewRes
+	Response          = resourcesCenter.Response
+	ResourcesAuth     = resourcesCenter.ResourcesAuth
+	Resources         = resourcesCenter.Resources
+	RecordReq         = resourcesCenter.RecordReq
 
 	ResourcesCenter interface {
 		// 查询账户资源权限
@@ -35,12 +36,12 @@ type (
 		FindAccountBalance(ctx context.Context, in *ResourcesReq) (*AccountBalanceRes, error)
 		// 查询流水账
 		FindConsumeRecord(ctx context.Context, in *RecordReq) (*ConsumeRecordRes, error)
-		// 根据账户标识修改资源结存账
-		UpdateUserBalance(ctx context.Context, in *Balance) (*Response, error)
-		// 根据账户标识记录资源使用流水账
-		UpdateUserDetailed(ctx context.Context, in *Detailed) (*Response, error)
+		// 根据账户标识购买资源
+		PurchaseUserBalance(ctx context.Context, in *Resources) (*Response, error)
+		// 根据账户标识使用资源
+		UseUserDetailed(ctx context.Context, in *Resources) (*Response, error)
 		// 预览信息
-		FindPreview(ctx context.Context, in *PreviewReq) (*ConsumeRecordRes, error)
+		FindPreview(ctx context.Context, in *PreviewReq) (*PreviewRes, error)
 	}
 
 	defaultResourcesCenter struct {
@@ -72,20 +73,20 @@ func (m *defaultResourcesCenter) FindConsumeRecord(ctx context.Context, in *Reco
 	return client.FindConsumeRecord(ctx, in)
 }
 
-// 根据账户标识修改资源结存账
-func (m *defaultResourcesCenter) UpdateUserBalance(ctx context.Context, in *Balance) (*Response, error) {
+// 根据账户标识购买资源
+func (m *defaultResourcesCenter) PurchaseUserBalance(ctx context.Context, in *Resources) (*Response, error) {
 	client := resourcesCenter.NewResourcesCenterClient(m.cli.Conn())
-	return client.UpdateUserBalance(ctx, in)
+	return client.PurchaseUserBalance(ctx, in)
 }
 
-// 根据账户标识记录资源使用流水账
-func (m *defaultResourcesCenter) UpdateUserDetailed(ctx context.Context, in *Detailed) (*Response, error) {
+// 根据账户标识使用资源
+func (m *defaultResourcesCenter) UseUserDetailed(ctx context.Context, in *Resources) (*Response, error) {
 	client := resourcesCenter.NewResourcesCenterClient(m.cli.Conn())
-	return client.UpdateUserDetailed(ctx, in)
+	return client.UseUserDetailed(ctx, in)
 }
 
 // 预览信息
-func (m *defaultResourcesCenter) FindPreview(ctx context.Context, in *PreviewReq) (*ConsumeRecordRes, error) {
+func (m *defaultResourcesCenter) FindPreview(ctx context.Context, in *PreviewReq) (*PreviewRes, error) {
 	client := resourcesCenter.NewResourcesCenterClient(m.cli.Conn())
 	return client.FindPreview(ctx, in)
 }

+ 136 - 42
service/balanceService.go

@@ -14,44 +14,68 @@ const (
 	AccountResources = "account_resources" //结存表
 )
 
-//根据账户标识修改资源结存账
-func (service *BalanceService) UpdateUserBalance(in *resourcesCenter.Balance) (int64, string) {
-	orm := entity.Engine
+//根据账户标识新增资源
+func (service *BalanceService) PurchaseUserBalance(balanceData *resourcesCenter.Balance, detailedData *resourcesCenter.Detailed) (int64, string) {
+	orm := entity.Engine.NewSession()
+	err := orm.Begin()
 	balance := entity.Balance{}
 	fool, err := orm.Table(AccountResources).
 		Select("*").
-		Where("accountId=? and  companyId=? and  departmentId=? and  ResourceType=?", in.AccountId, in.CompanyId, in.DepartmentId, in.ResourceType).
+		Where("accountId=? and  companyId=? and  departmentId=? and  ResourceType=? and  endTime=?", balanceData.AccountId, balanceData.CompanyId, balanceData.DepartmentId, balanceData.ResourceType, balanceData.EndTime).
 		Get(&balance)
 	if err != nil && !fool {
 		log.Panicln("结存查询失败:", err)
 		return entity.ErrorCode, "企业下的组织查询失败"
 	}
+	//新增流水记录
+	detailed := entity.Detailed{
+		AccountId:    detailedData.AccountId,
+		CompanyId:    detailedData.CompanyId,
+		DepartmentId: detailedData.DepartmentId,
+		ResourceType: detailedData.ResourceType,
+		ExportNum:    detailedData.ExportNum,
+		RuleId:       detailedData.RuleId,
+		Name:         detailedData.Name,
+		ExportTime:   time.Now().Local(),
+		UserType:     detailedData.UserType,
+	}
+	insertNumb, err := orm.Table(ConsumeRecord).Insert(&detailed)
+	if err != nil {
+		log.Panicln("新增流水失败:", err)
+		orm.Rollback()
+		return entity.ErrorCode, "新增流水失败"
+	}
+	if insertNumb <= 0 {
+		orm.Rollback()
+		return entity.ErrorCode, "新增流水失败"
+	}
 	if balance.Id == 0 {
-		if in.Model == 0 {
-			return entity.ErrorCode, "该用户没有结存不可使用"
-		}
 		//新增结存记录
 		balance = entity.Balance{
-			AccountId:    in.AccountId,
-			CompanyId:    in.CompanyId,
-			DepartmentId: in.DepartmentId,
-			Name:         in.Name,
-			ResourceType: in.ResourceType,
-			Number:       in.Number,
-			Spec:         in.Spec,
-			AppId:        in.AppId,
+			AccountId:    balanceData.AccountId,
+			CompanyId:    balanceData.CompanyId,
+			DepartmentId: balanceData.DepartmentId,
+			Name:         balanceData.Name,
+			ResourceType: balanceData.ResourceType,
+			Number:       balanceData.Number,
+			Spec:         balanceData.Spec,
+			AppId:        balanceData.AppId,
+			EndTime:      balanceData.EndTime,
 		}
-		insertNumb, err := orm.Table(AccountResources).Insert(&balance)
+		insertNumb, err = orm.Table(AccountResources).Insert(&balance)
 		if err != nil {
 			log.Panicln("结存查询失败:", err)
-			return entity.ErrorCode, "企业下的组织查询失败"
+			orm.Rollback()
+			return entity.ErrorCode, "结存新增失败"
 		}
-		if insertNumb > 0 {
-			return entity.SuccessCode, "结存新增成功"
+		if insertNumb <= 0 {
+			orm.Rollback()
+			return entity.ErrorCode, "结存新增失败"
 		}
-
+		orm.Commit()
+		return entity.SuccessCode, "结存新增成功"
 	}
-	if in.Model == 0 {
+	/*if in.Model == 0 {
 		if (balance.Number < in.Number) {
 			return entity.ErrorCode, "该用户结存不够不可使用"
 		}
@@ -68,47 +92,117 @@ func (service *BalanceService) UpdateUserBalance(in *resourcesCenter.Balance) (i
 			return entity.SuccessCode, "结存修改成功"
 		}
 		return entity.ErrorCode, "结存修改失败"
-	}
-	balance.Number = balance.Number + in.Number
+	}*/
+	balance.Number = balance.Number + balanceData.Number
 	updateNumb, err := orm.Table(AccountResources).
-		Where("accountId=? and  companyId=? and  departmentId=? and  resourceType=?", in.AccountId, in.CompanyId, in.DepartmentId, in.ResourceType).
+		Where("accountId=? and  companyId=? and  departmentId=? and  resourceType=? and endTime=?", balanceData.AccountId, balanceData.CompanyId, balanceData.DepartmentId, balanceData.ResourceType, balanceData.EndTime).
 		Update(&balance)
 	if err != nil {
+		orm.Rollback()
 		log.Panicln("结存修改失败:", err)
 		return entity.ErrorCode, "结存修改失败"
 	}
 	if updateNumb > 0 {
+		orm.Commit()
 		return entity.SuccessCode, "结存修改成功"
 	}
+	orm.Rollback()
 	return entity.ErrorCode, "结存修改失败"
 }
 
-//根据账户标识记录资源使用流水账
-func (service *BalanceService) UpdateUserDetailed(in *resourcesCenter.Detailed) (int64, string) {
-	orm := entity.Engine
+//根据账户标识使用资源
+func (service *BalanceService) UseUserDetailed(balanceData *resourcesCenter.Balance, detailedData *resourcesCenter.Detailed) (int64, string) {
+	orm := entity.Engine.NewSession()
+	err := orm.Begin()
+	//去重
+	deductionNumb := int64(0)
+
+	//查询结存是否够用
+	type SumStruct struct {
+		count int64
+	}
+	sumStruct := new(SumStruct)
+	totals, err := orm.Table(AccountResources).
+		Select("*").
+		Where("accountId=? and  companyId=? and  departmentId=? and  ResourceType=? and  endTime=?", balanceData.AccountId, balanceData.CompanyId, balanceData.DepartmentId, balanceData.ResourceType, balanceData.EndTime).
+		SumsInt(sumStruct, "number")
+	if err != nil {
+		log.Panicln("结存查询失败:", err)
+		orm.Rollback()
+		return entity.ErrorCode, "结存查询失败"
+	}
+	surplusNumb := totals[0]
+	if surplusNumb < deductionNumb {
+		return entity.ErrorCode, "结存不足,请立即充值"
+	}
 	//新增流水记录
 	detailed := entity.Detailed{
-		AccountId:      in.AccountId,
-		CompanyId:      in.CompanyId,
-		DepartmentId:   in.DepartmentId,
-		ResourceType:   in.ResourceType,
-		ExportNum:      in.ExportNum,
-		RuleId:         in.RuleId,
+		AccountId:      detailedData.AccountId,
+		CompanyId:      detailedData.CompanyId,
+		DepartmentId:   detailedData.DepartmentId,
+		ResourceType:   detailedData.ResourceType,
+		ExportNum:      detailedData.ExportNum,
+		RuleId:         detailedData.RuleId,
 		ExportTime:     time.Now().Local(),
-		UserType:       in.UserType,
-		UserId:         in.UserId,
-		Url:            in.Url,
-		Source:         in.Source,
-		SearchCriteria: in.SearchCriteria,
-		DeductionNumb:  in.DeductionNumb,
+		UserType:       detailedData.UserType,
+		UserId:         detailedData.UserId,
+		Url:            detailedData.Url,
+		Source:         detailedData.Source,
+		SearchCriteria: detailedData.SearchCriteria,
+		DeductionNumb:  deductionNumb,
+		Name:           detailedData.Name,
 	}
 	insertNumb, err := orm.Table(ConsumeRecord).Insert(&detailed)
 	if err != nil {
 		log.Panicln("新增流水失败:", err)
+		orm.Rollback()
+		return entity.ErrorCode, "新增流水失败"
+	}
+	if insertNumb <= 0 {
+		orm.Rollback()
 		return entity.ErrorCode, "新增流水失败"
 	}
-	if insertNumb > 0 {
-		return entity.SuccessCode, "新增流水成功"
+	//修改结存
+	balanceList := []entity.Balance{}
+	err = orm.Table(AccountResources).
+		Select("*").
+		Where("accountId=? and  companyId=? and  departmentId=? and  ResourceType=? and  endTime>=?", balanceData.AccountId, balanceData.CompanyId, balanceData.DepartmentId, balanceData.ResourceType, time.Now().Format("2006-01-02")).
+		OrderBy("endTime").
+		Find(&balanceList)
+	if err != nil {
+		log.Panicln("结存查询失败:", err)
+		orm.Rollback()
+		return entity.ErrorCode, "企业下的组织查询失败"
 	}
-	return entity.ErrorCode, "新增流水失败"
+	for _, value := range balanceList {
+		if value.Number > deductionNumb {
+			value.Number = value.Number - deductionNumb
+			deductionNumb = 0
+		} else {
+			deductionNumb = deductionNumb - value.Number
+			value.Number = 0
+		}
+		updateNumb, err := orm.Table(AccountResources).
+			Cols("number").ID(value).
+			Update(&value)
+		if err != nil {
+			log.Panicln("结存修改失败:", err)
+			orm.Rollback()
+			return entity.ErrorCode, "结存修改失败"
+		}
+		if updateNumb <= 0 {
+			orm.Rollback()
+			return entity.ErrorCode, "结存修改失败"
+		}
+		if deductionNumb == 0 {
+			break
+		}
+	}
+	orm.Commit()
+	return entity.SuccessCode, "使用结存成功"
+}
+
+//根据账户标识使用资源
+func (service *BalanceService) FindPreview(balanceData *resourcesCenter.PreviewReq) (int64, string, int64, int64) {
+	return entity.SuccessCode, "去重", 1, 1
 }

Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff