2024-07-09 14:45:50 +00:00
|
|
|
package mysqlkindboxreq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-07-10 01:21:03 +00:00
|
|
|
"database/sql"
|
|
|
|
"time"
|
2024-07-09 14:45:50 +00:00
|
|
|
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
|
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
|
|
|
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-07-09 14:45:50 +00:00
|
|
|
)
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
func (d *DB) UpdateKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) error {
|
2024-07-09 14:45:50 +00:00
|
|
|
const op = "mysqlkindboxreq.UpdateKindBoxReq"
|
|
|
|
|
2024-07-10 01:21:03 +00:00
|
|
|
var (
|
|
|
|
senderAgentID sql.NullInt64
|
|
|
|
deliveredAt sql.NullTime
|
|
|
|
)
|
|
|
|
if kindBoxReq.SenderAgentID != 0 {
|
|
|
|
senderAgentID = sql.NullInt64{Int64: int64(kindBoxReq.SenderAgentID), Valid: true}
|
|
|
|
} else {
|
|
|
|
senderAgentID = sql.NullInt64{Int64: 0, Valid: false}
|
|
|
|
}
|
|
|
|
if !kindBoxReq.DeliveredAt.IsZero() {
|
|
|
|
deliveredAt = sql.NullTime{Time: kindBoxReq.DeliveredAt, Valid: true}
|
|
|
|
} else {
|
|
|
|
deliveredAt = sql.NullTime{Time: time.Time{}, Valid: false}
|
|
|
|
}
|
|
|
|
|
2024-07-09 14:45:50 +00:00
|
|
|
query := `UPDATE kind_box_reqs
|
2024-07-10 01:21:03 +00:00
|
|
|
SET benefactor_id = ?, kind_box_type = ?, count_requested = ?, count_accepted = ?,
|
|
|
|
description = ?, status = ?, deliver_refer_time_id = ?, deliver_refer_date = ?,
|
|
|
|
deliver_address_id = ?, sender_agent_id = ?, delivered_at = ?
|
2024-07-09 14:45:50 +00:00
|
|
|
WHERE id = ? AND deleted_at IS NULL`
|
2024-07-30 11:05:41 +00:00
|
|
|
//nolint
|
|
|
|
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxReqUpdate, query)
|
|
|
|
if err != nil {
|
|
|
|
return richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, uErr := stmt.ExecContext(ctx, kindBoxReq.BenefactorID, kindBoxReq.KindBoxType,
|
2024-07-10 01:21:03 +00:00
|
|
|
kindBoxReq.CountRequested, kindBoxReq.CountAccepted, kindBoxReq.Description, kindBoxReq.Status,
|
|
|
|
kindBoxReq.DeliverReferTimeID, kindBoxReq.DeliverReferDate, kindBoxReq.DeliverAddressID,
|
|
|
|
senderAgentID, deliveredAt, kindBoxReq.ID)
|
2024-07-09 14:45:50 +00:00
|
|
|
if uErr != nil {
|
|
|
|
return richerror.New(op).WithErr(uErr).WithMessage(errmsg.ErrorMsgCantUpdateRecord).
|
|
|
|
WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|