数据库
<p>因为我们区块链本身就是属于K-V值类型的分布式数据库;需要的数据库不多。</p>
<p>需要一个订单数据库 order</p>
<ul>
<li>order 订单表</li>
</ul>
<table>
<thead>
<tr>
<th>字段</th>
<th>类型</th>
<th>允许空</th>
<th>默认</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>id</td>
<td>int</td>
<td>否</td>
<td></td>
<td>订单ID</td>
</tr>
<tr>
<td>order_no</td>
<td>varchar (100)</td>
<td>否</td>
<td></td>
<td>订单编号</td>
</tr>
<tr>
<td>user_id</td>
<td>varchar (50)</td>
<td>否</td>
<td></td>
<td>用户ID</td>
</tr>
<tr>
<td>merchant_id</td>
<td>int</td>
<td>否</td>
<td></td>
<td>商家ID</td>
</tr>
<tr>
<td>goods_id</td>
<td>varchar (50)</td>
<td>否</td>
<td></td>
<td>商品ID</td>
</tr>
<tr>
<td>goods_num</td>
<td>varchar (50)</td>
<td>否</td>
<td></td>
<td>购买的商品数量</td>
</tr>
<tr>
<td>total_amount</td>
<td>decimal (10)</td>
<td>否</td>
<td></td>
<td>总金额</td>
</tr>
<tr>
<td>status</td>
<td>tinyint</td>
<td>否</td>
<td></td>
<td>0:待付款;1已付款;2已发货;3:已签收;4:已取消;5:已超时</td>
</tr>
<tr>
<td>pay_type</td>
<td>tinyint</td>
<td>否</td>
<td></td>
<td>1:支付宝;2:微信;3:银联;</td>
</tr>
<tr>
<td>ship_address</td>
<td>varchar (50)</td>
<td>否</td>
<td></td>
<td>配送地址</td>
</tr>
<tr>
<td>created_at</td>
<td>datetime</td>
<td>否</td>
<td></td>
<td>-</td>
</tr>
<tr>
<td>updated_at</td>
<td>datetime</td>
<td>否</td>
<td></td>
<td>-</td>
</tr>
<tr>
<td>deleted_at</td>
<td>datetime</td>
<td>是</td>
<td></td>
<td>-</td>
</tr>
</tbody>
</table>
<h2>创建订单&查询订单</h2>
<pre><code>
//mysql我们使用的是gorm连接方式就不展示了
//结构体映射表中字段
type Order struct {
OrderNo string `json:&quot;order_no&quot;`
UserId string `json:&quot;user_id&quot;`
MerchantId string `json:&quot;merchant_id&quot;`
GoodsID string `json:&quot;goods_id&quot;`
GoodsNum string `json:&quot;goods_num&quot;`
TotalAmount float64 `json:&quot;total_amount&quot;`
Status int64 `json:&quot;status&quot;`
PayType int64 `json:&quot;pay_type&quot;`
ShipAddress string `json:&quot;ship_address&quot;`
gorm.Model
}
func (o *Order) TableName() string {
return &quot;order&quot;
}
//创建订单
func (o *Order) Insert(order *Order) error {
if err := DB.Create(order).Error; err != nil {
return err
}
return nil
}
//根据订单号删除订单
func (o *Order) DeleteById(id string) error {
var order Order
if err := DB.Where(&quot;order_no = ?&quot;, id).Delete(&amp;order).Error; err != nil {
return err
}
return nil
}
//根据多个订单号删除多条订单
func (o *Order) DeleteByIds(ids []int64) error {
var order Order
if err := DB.Where(ids).Delete(&amp;order).Error; err != nil {
return err
}
return nil
}
//修改订单
func (o *Order) Update(order *Order) error {
if err := DB.Updates(&amp;order).Error; err != nil {
return err
}
return nil
}
//批量修改
func (o *Order) Updates(ids []int64, order *Order) error {
if err := DB.Where(&quot;id = ?&quot;, ids).Updates(&amp;order).Error; err != nil {
return err
}
return nil
}
//分页展示
func (o *Order) List(page, limit int64) ([]Order, error) {
var order []Order
if page == 0 {
page = 1
}
offset := math.Ceil(float64(page-1) * float64(limit))
if err := DB.Limit(int(limit)).Offset(int(offset)).Find(&amp;order).Error; err != nil {
return nil, err
}
return order, nil
}
//查询单挑数据
func (o *Order) FindById(id string) (Order, error) {
var order Order
if err := DB.Where(&quot;order_no = ?&quot;, id).First(&amp;order).Error; err != nil {
return Order{}, err
}
return order, nil
}
//查询多条数据
func (o *Order) FindByIds(ids []int64) ([]Order, error) {
var order []Order
if err := DB.Where(ids).Find(&amp;order).Error; err != nil {
return []Order{}, err
}
return order, nil
}
</code></pre>