2023-12-20 15:39:25 +00:00
|
|
|
package mysqlkindbox
|
2024-01-23 07:39:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-02-03 04:56:27 +00:00
|
|
|
|
2024-01-23 07:39:58 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
2024-01-25 15:15:53 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/logger"
|
2024-01-23 10:21:56 +00:00
|
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
|
|
|
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
2024-01-23 07:39:58 +00:00
|
|
|
)
|
|
|
|
|
2024-01-27 07:05:56 +00:00
|
|
|
func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error {
|
2024-01-23 11:15:36 +00:00
|
|
|
const op = "mysqlkindbox.AddBatchKindBoxConcurrentlyRollback"
|
|
|
|
errCh := make(chan error, len(kindBoxes))
|
|
|
|
resultCh := make(chan entity.KindBox, len(kindBoxes))
|
2024-01-23 10:21:56 +00:00
|
|
|
tx, tErr := d.conn.Conn().Begin()
|
|
|
|
if tErr != nil {
|
2024-01-27 07:05:56 +00:00
|
|
|
return richerror.New(op).WithErr(tErr).
|
2024-01-23 10:21:56 +00:00
|
|
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
2024-01-23 11:15:36 +00:00
|
|
|
|
2024-01-23 10:21:56 +00:00
|
|
|
for _, kindBox := range kindBoxes {
|
2024-01-23 11:15:36 +00:00
|
|
|
go func(kb entity.KindBox) {
|
2024-01-27 07:05:56 +00:00
|
|
|
_, err := tx.ExecContext(ctx,
|
2024-01-23 11:15:36 +00:00
|
|
|
"insert into kind_boxes (kind_box_req_id, benefactor_id, type, serial_number, status) values (?, ?, ?, ?, ?);",
|
|
|
|
kb.KindBoxReqID, kb.BenefactorID, kb.Type.String(), kb.SerialNumber, kb.Status.String(),
|
2024-01-23 10:21:56 +00:00
|
|
|
)
|
2024-01-23 11:15:36 +00:00
|
|
|
if err != nil {
|
|
|
|
errCh <- richerror.New(op).WithErr(err).
|
2024-01-23 10:21:56 +00:00
|
|
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
2024-01-23 11:15:36 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resultCh <- kb
|
|
|
|
errCh <- nil
|
|
|
|
}(kindBox)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(kindBoxes); i++ {
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
2024-01-25 15:15:53 +00:00
|
|
|
logger.L().Error("Rollback error: ", err)
|
2024-01-23 11:15:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-27 07:05:56 +00:00
|
|
|
return err
|
2024-01-23 11:15:36 +00:00
|
|
|
}
|
2024-01-27 07:05:56 +00:00
|
|
|
case <-resultCh:
|
|
|
|
|
2024-01-23 11:15:36 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
if err := tx.Rollback(); err != nil {
|
2024-01-25 15:15:53 +00:00
|
|
|
logger.L().Error("Rollback error: ", err)
|
2024-01-23 10:21:56 +00:00
|
|
|
}
|
|
|
|
|
2024-01-27 07:05:56 +00:00
|
|
|
return richerror.New(op).WithErr(ctx.Err()).
|
2024-01-23 10:21:56 +00:00
|
|
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
}
|
2024-01-23 11:15:36 +00:00
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
2024-01-25 15:15:53 +00:00
|
|
|
logger.L().Error("Commit error: ", err)
|
2024-01-23 11:15:36 +00:00
|
|
|
|
2024-01-27 07:05:56 +00:00
|
|
|
return richerror.New(op).WithErr(err).
|
2024-01-23 10:21:56 +00:00
|
|
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
2024-01-23 11:15:36 +00:00
|
|
|
|
2024-01-27 07:05:56 +00:00
|
|
|
return nil
|
2024-01-23 07:39:58 +00:00
|
|
|
}
|