110 lines
2.8 KiB
PHP
110 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace app\modules\api\models;
|
|
|
|
use app\models\Goods;
|
|
use app\models\GoodsHub;
|
|
use app\models\Model;
|
|
use app\models\Signing;
|
|
use yii\data\Pagination;
|
|
|
|
class SigningForm extends ApiModel
|
|
{
|
|
public $goods_hub_id;
|
|
public $company_name;
|
|
public $brand_name;
|
|
|
|
public $product;
|
|
|
|
public $type;
|
|
|
|
public $number;
|
|
|
|
public $remark;
|
|
public $user_id;
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['goods_hub_id', 'company_name', 'brand_name', 'product', 'type', 'number'], 'required'],
|
|
[['goods_hub_id', 'number'], 'integer'],
|
|
[['remark'], 'string']
|
|
];
|
|
}
|
|
|
|
|
|
public function add()
|
|
{
|
|
|
|
if (!$this->validate()) {
|
|
return $this->getModelError();
|
|
}
|
|
|
|
$goods = Goods::findOne($this->goods_hub_id);
|
|
|
|
if (!$goods) {
|
|
return $this->apiReturnError('商品id异常');
|
|
}
|
|
|
|
$signing = new Signing();
|
|
|
|
$signing->order_no = $this->orderNo();
|
|
$signing->user_id = $this->user_id;
|
|
$signing->goods_id = $goods->id;
|
|
$signing->moy = $goods->price * $this->number;
|
|
$signing->company_name = $this->company_name;
|
|
$signing->brand_name = $this->brand_name;
|
|
$signing->product = $this->product;
|
|
$signing->type = $this->type;
|
|
$signing->number = $this->number;
|
|
$signing->remark = $this->remark;
|
|
$signing->create_time = time();
|
|
|
|
return $signing->save() ? $this->apiReturnSuccess('success', ['order_no' => $signing->order_no]) : $this->apiReturnError('error');
|
|
}
|
|
|
|
|
|
private function orderNo()
|
|
{
|
|
$orderNo = mt_rand(100000000000, 9999999999999);
|
|
|
|
if (Signing::find()->where(['order_no' => $orderNo])->count('id')) {
|
|
return $this->orderNo();
|
|
}
|
|
return $orderNo;
|
|
}
|
|
|
|
|
|
public function orderDetail($orderNo)
|
|
{
|
|
|
|
$order = Signing::find()->andWhere(['order_no' => $orderNo, 'user_id' => $this->user_id])->one()->toArray();
|
|
|
|
if (!$order) {
|
|
return $this->apiReturnError('订单号错误');
|
|
}
|
|
|
|
$order['goods'] = Goods::findOne($order['goods_id']);
|
|
|
|
$order['goods_hub'] = GoodsHub::findOne($order['goods']['goods_hub_id']);
|
|
|
|
return $this->apiReturnSuccess('success', $order);
|
|
}
|
|
|
|
public function orderList($status, $limit, $page)
|
|
{
|
|
|
|
$query = Signing::find()->with(['goods.goodsHub'])->where(['user_id' => $this->user_id]);
|
|
|
|
if ($status != null) {
|
|
$query->where(['status' => $status]);
|
|
}
|
|
|
|
$pagination = new Pagination(['totalCount' => $query->count(), 'defaultPageSize' => $limit]);
|
|
|
|
$models = $query->offset($pagination->offset)->limit($pagination->limit)->asArray()->all();
|
|
|
|
|
|
return $this->apiReturnSuccess('success', ['pagination' => $pagination, 'data' => $models]);
|
|
}
|
|
} |