2023-12-20 15:39:25 +00:00
|
|
|
package mysqlkindbox
|
2024-01-23 07:39:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-23 10:21:56 +00:00
|
|
|
|
2024-01-23 07:39:58 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
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-23 10:21:56 +00:00
|
|
|
func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]entity.KindBox, error) {
|
|
|
|
const op = "mysqlkindbox.AddBatchKindBox"
|
|
|
|
tx, tErr := d.conn.Conn().Begin()
|
|
|
|
if tErr != nil {
|
|
|
|
return nil, richerror.New(op).WithErr(tErr).
|
|
|
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
for _, kindBox := range kindBoxes {
|
|
|
|
res, err := tx.
|
|
|
|
ExecContext(ctx,
|
|
|
|
"insert into kind_boxes (kind_box_req_id , benefactor_id , type ,serial_number , status) values (? , ? , ? , ? ,?);",
|
|
|
|
kindBox.KindBoxReqID, kindBox.BenefactorID, kindBox.Type.String(), kindBox.SerialNumber, kindBox.Status.String(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if rErr := tx.Rollback(); rErr != nil {
|
|
|
|
return nil, richerror.New(op).WithErr(rErr).
|
|
|
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint
|
|
|
|
// err is always nil
|
|
|
|
id, _ := res.LastInsertId()
|
|
|
|
kindBox.ID = uint(id)
|
|
|
|
}
|
|
|
|
if cErr := tx.Commit(); cErr != nil {
|
|
|
|
return nil, richerror.New(op).WithErr(cErr).
|
|
|
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
return kindBoxes, nil
|
2024-01-23 07:39:58 +00:00
|
|
|
}
|