123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package logic
- import (
- "app.yhyue.com/moapp/jyResourcesCenter/entity"
- "app.yhyue.com/moapp/jyResourcesCenter/rpc/internal/config"
- "context"
- "fmt"
- "time"
- "app.yhyue.com/moapp/jyResourcesCenter/rpc/internal/svc"
- "app.yhyue.com/moapp/jyResourcesCenter/rpc/resourcesCenter"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type EntSourceNumAddLogic struct {
- ctx context.Context
- svcCtx *svc.ServiceContext
- logx.Logger
- }
- func NewEntSourceNumAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EntSourceNumAddLogic {
- return &EntSourceNumAddLogic{
- ctx: ctx,
- svcCtx: svcCtx,
- Logger: logx.WithContext(ctx),
- }
- }
- // EntSourceNumAdd 新增资源数量
- func (l *EntSourceNumAddLogic) EntSourceNumAdd(in *resourcesCenter.EntSourceNumAddReq) (*resourcesCenter.Response, error) {
- eCode, msg := func() (int64, string) {
- if config.ConfigJson.ProductMap[in.ResourceType] == nil || in.CompanyId == 0 || in.EmpowerId == 0 {
- return entity.ErrorCode, "参数异常"
- }
- var accountBalanceArr []*entity.Balance
- orm := entity.Engine.NewSession()
- err := orm.Table(AccountResources).
- Where("companyId=? AND resourceType=? AND empowerId=? AND endTime>= ?", in.CompanyId, in.ResourceType, in.EmpowerId, time.Now().Format("2006-01-02")).Find(&accountBalanceArr)
- if err != nil {
- return entity.ErrorCode, "查询数据异常"
- }
- if err = orm.Begin(); err != nil {
- return entity.ErrorCode, "数据库操作异常"
- }
- for _, balance := range accountBalanceArr {
- var (
- detailed = entity.Detailed{
- AccountId: balance.AccountId,
- CompanyId: in.CompanyId,
- EmpowerId: in.EmpowerId,
- DepartmentId: in.DepartmentId,
- Name: in.Name,
- ResourceType: in.ResourceType,
- Number: in.Number,
- CreateTime: time.Now().Local(),
- UserType: 1,
- Remarks: in.Remarks,
- UserId: balance.AccountId,
- }
- )
- insertNumb, err := orm.Table(ConsumeRecord).Insert(&detailed)
- if err != nil || insertNumb <= 0 {
- fmt.Println("新增流水失败:", err)
- _ = orm.Rollback()
- return entity.ErrorCode, "新增流水失败"
- }
- balance.Number = balance.Number + in.Number
- updateNumb, err := orm.Table(AccountResources).Cols("number").
- Where(" id=?", balance.Id).
- Update(balance)
- if err != nil || updateNumb <= 0 {
- _ = orm.Rollback()
- fmt.Println("资源新增异常:", err)
- return entity.ErrorCode, "结存修改失败"
- }
- }
- _ = orm.Commit()
- return entity.SuccessCode, "操作成功"
- }()
- return &resourcesCenter.Response{
- Code: eCode,
- Message: msg,
- }, nil
- }
|