|
@@ -14,44 +14,68 @@ const (
|
|
AccountResources = "account_resources" //结存表
|
|
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{}
|
|
balance := entity.Balance{}
|
|
fool, err := orm.Table(AccountResources).
|
|
fool, err := orm.Table(AccountResources).
|
|
Select("*").
|
|
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)
|
|
Get(&balance)
|
|
if err != nil && !fool {
|
|
if err != nil && !fool {
|
|
log.Panicln("结存查询失败:", err)
|
|
log.Panicln("结存查询失败:", err)
|
|
return entity.ErrorCode, "企业下的组织查询失败"
|
|
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 balance.Id == 0 {
|
|
- if in.Model == 0 {
|
|
|
|
- return entity.ErrorCode, "该用户没有结存不可使用"
|
|
|
|
- }
|
|
|
|
//新增结存记录
|
|
//新增结存记录
|
|
balance = entity.Balance{
|
|
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 {
|
|
if err != nil {
|
|
log.Panicln("结存查询失败:", err)
|
|
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) {
|
|
if (balance.Number < in.Number) {
|
|
return entity.ErrorCode, "该用户结存不够不可使用"
|
|
return entity.ErrorCode, "该用户结存不够不可使用"
|
|
}
|
|
}
|
|
@@ -68,47 +92,117 @@ func (service *BalanceService) UpdateUserBalance(in *resourcesCenter.Balance) (i
|
|
return entity.SuccessCode, "结存修改成功"
|
|
return entity.SuccessCode, "结存修改成功"
|
|
}
|
|
}
|
|
return entity.ErrorCode, "结存修改失败"
|
|
return entity.ErrorCode, "结存修改失败"
|
|
- }
|
|
|
|
- balance.Number = balance.Number + in.Number
|
|
|
|
|
|
+ }*/
|
|
|
|
+ balance.Number = balance.Number + balanceData.Number
|
|
updateNumb, err := orm.Table(AccountResources).
|
|
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)
|
|
Update(&balance)
|
|
if err != nil {
|
|
if err != nil {
|
|
|
|
+ orm.Rollback()
|
|
log.Panicln("结存修改失败:", err)
|
|
log.Panicln("结存修改失败:", err)
|
|
return entity.ErrorCode, "结存修改失败"
|
|
return entity.ErrorCode, "结存修改失败"
|
|
}
|
|
}
|
|
if updateNumb > 0 {
|
|
if updateNumb > 0 {
|
|
|
|
+ orm.Commit()
|
|
return entity.SuccessCode, "结存修改成功"
|
|
return entity.SuccessCode, "结存修改成功"
|
|
}
|
|
}
|
|
|
|
+ orm.Rollback()
|
|
return entity.ErrorCode, "结存修改失败"
|
|
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{
|
|
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(),
|
|
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)
|
|
insertNumb, err := orm.Table(ConsumeRecord).Insert(&detailed)
|
|
if err != nil {
|
|
if err != nil {
|
|
log.Panicln("新增流水失败:", err)
|
|
log.Panicln("新增流水失败:", err)
|
|
|
|
+ orm.Rollback()
|
|
|
|
+ return entity.ErrorCode, "新增流水失败"
|
|
|
|
+ }
|
|
|
|
+ if insertNumb <= 0 {
|
|
|
|
+ orm.Rollback()
|
|
return entity.ErrorCode, "新增流水失败"
|
|
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
|
|
}
|
|
}
|