2023-12-20 15:39:25 +00:00
|
|
|
package mysqlkindbox
|
2024-01-23 07:39:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-07-12 21:25:20 +00:00
|
|
|
"database/sql"
|
2024-07-24 23:45:04 +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"
|
2024-07-12 21:25:20 +00:00
|
|
|
querytransaction "git.gocasts.ir/ebhomengo/niki/pkg/query_transaction/sql"
|
2024-01-23 10:21:56 +00:00
|
|
|
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
2024-07-30 11:05:41 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
|
2024-01-23 07:39:58 +00:00
|
|
|
)
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
func (d *DB) AddKindBox(ctx context.Context, kindBox entity.KindBox) error {
|
2024-03-14 13:38:42 +00:00
|
|
|
const op = "mysqlkindbox.AddKindBox"
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
query := `insert into kind_boxes(kind_box_req_id,benefactor_id,type,status,sender_agent_id) values (?,?,?,?,?)`
|
|
|
|
//nolint
|
|
|
|
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxAdd, query)
|
|
|
|
if err != nil {
|
|
|
|
return richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stmt.ExecContext(ctx, kindBox.KindBoxReqID, kindBox.BenefactorID, kindBox.KindBoxType, entity.KindBoxDeliveredStatus.String(), kindBox.SenderAgentID)
|
2024-03-14 13:38:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-30 11:05:41 +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"
|
2024-01-27 07:05:56 +00:00
|
|
|
|
2024-07-12 21:25:20 +00:00
|
|
|
queryStr := "INSERT INTO kind_boxes (kind_box_req_id, benefactor_id, type, serial_number, status ,deliver_refer_time_id,deliver_refer_date,deliver_address_id,sender_agent_id,delivered_at) VALUES "
|
2024-02-03 05:03:58 +00:00
|
|
|
values := []any{}
|
2024-01-23 10:21:56 +00:00
|
|
|
|
2024-02-03 05:03:58 +00:00
|
|
|
for _, kb := range kindBoxes {
|
2024-07-12 21:25:20 +00:00
|
|
|
queryStr += "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?),"
|
|
|
|
if kb.SerialNumber == "" {
|
|
|
|
values = append(values, kb.KindBoxReqID, kb.BenefactorID, kb.KindBoxType, sql.NullString{}, kb.Status.String(), kb.DeliverReferTimeID, kb.DeliverReferDate, kb.DeliverAddressID, kb.SenderAgentID, kb.DeliveredAt)
|
|
|
|
} else {
|
|
|
|
values = append(values, kb.KindBoxReqID, kb.BenefactorID, kb.KindBoxType, kb.SerialNumber, kb.Status.String(), kb.DeliverReferTimeID, kb.DeliverReferDate, kb.DeliverAddressID, kb.SenderAgentID, kb.DeliveredAt)
|
|
|
|
}
|
2024-01-23 10:21:56 +00:00
|
|
|
}
|
2024-07-12 21:25:20 +00:00
|
|
|
// trim the last ','
|
2024-02-03 05:03:58 +00:00
|
|
|
queryStr = queryStr[0 : len(queryStr)-1]
|
2024-01-23 11:15:36 +00:00
|
|
|
|
2024-07-12 21:25:20 +00:00
|
|
|
q, cErr := querytransaction.GetQuerierFromContextOrNew(ctx).Continue(ctx, d.conn.Conn())
|
|
|
|
if cErr != nil {
|
|
|
|
return richerror.New(op).WithErr(cErr).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
if _, aErr := q.Conn().ExecContext(ctx, queryStr, values...); aErr != nil {
|
|
|
|
return richerror.New(op).WithErr(aErr).
|
2024-01-23 10:21:56 +00:00
|
|
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-01-27 07:05:56 +00:00
|
|
|
return nil
|
2024-01-23 07:39:58 +00:00
|
|
|
}
|