| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace addons\CloudStock\common\models;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\goods\Goods;
- use common\models\user\User;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_cloud_stock_dispense_order".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $type 1上级配货;2:平台充值
- * @property int $user_id 代理商用户id
- * @property float $agent_price 代理拿货价
- * @property int $child_id 配货下级用户id
- * @property float $child_price 配货下级的拿货价
- * @property float $money 配货金额
- * @property string $order_no 补货订单编号
- * @property int $goods_id 商品id
- * @property int $num 补货商品数量
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class CloudStockDispenseOrder extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_cloud_stock_dispense_order}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'child_id', 'order_no', 'goods_id'], 'required'],
- [['mall_id', 'type', 'user_id', 'child_id', 'goods_id', 'num', 'status', 'created_at', 'updated_at'], 'integer'],
- [['agent_price', 'child_price', 'money'], 'number'],
- [['order_no'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'type' => 'Type',
- 'user_id' => 'User ID',
- 'agent_price' => 'Agent Price',
- 'child_id' => 'Child ID',
- 'child_price' => 'Child Price',
- 'money' => 'Money',
- 'order_no' => 'Order No',
- 'goods_id' => 'Goods ID',
- 'num' => 'Num',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select(['id', 'nickname', 'avatar_url', 'mobile']);
- }
- public function getChild()
- {
- return $this->hasOne(User::class, ['id' => 'child_id'])->select(['id', 'nickname', 'avatar_url', 'mobile']);
- }
- public function getGoods()
- {
- return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select(['id', 'goods_name', 'cover_pic']);
- }
- public static function addData($params)
- {
- $cloud_stock_goods = CloudStockGoods::findOne(['goods_id' => $params['goods_id'], 'status' => StatusEnum::ENABLED]);
- if (empty($cloud_stock_goods)) {
- throw new \Exception('商品不存在');
- }
- $agent_price = json_decode($cloud_stock_goods['agent_price'], true);
- $agent_price_list = array_column($agent_price, null, 'level');
- $child_cloud_stock_agent = CloudStockAgent::findOne(['user_id' => $params['target_user_id'], 'status' => StatusEnum::ENABLED]);
- if (empty($child_cloud_stock_agent)) {
- throw new \Exception('代理商不存在');
- }
- $agent_price = 0;
- if (!empty($params['user_id'])) {
- $cloud_stock_agent = CloudStockAgent::findOne(['user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED]);
- $agent_price = $agent_price_list[$cloud_stock_agent['level']]['price'] ?? 0;
- }
- $child_price = $agent_price_list[$child_cloud_stock_agent['level']]['price'] ?? 0;
- $add = [
- 'mall_id' => $params['mall_id'],
- 'user_id' => $params['user_id'],
- 'child_id' => $params['target_user_id'],
- 'agent_price' => $agent_price,
- 'child_price' => $child_price,
- 'money' => $child_price * $params['number'],
- 'order_no' => self::getOrderNo($params['mall_id']),
- 'goods_id' => $params['goods_id'],
- 'num' => $params['number'],
- 'type' => $params['type'],
- 'status' => StatusEnum::ENABLED,
- ];
- return self::setData($params['mall_id'], $add);
- }
- public static function setData(int $mall_id, array $data)
- {
- if (!empty($data['id'])) {
- $model = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']]);
- if (empty($model)) {
- self::$static_error = '数据异常';
- return false;
- }
- } else {
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $mall_id;
- }
- foreach ($data as $key => $val) {
- $model->$key = $val;
- }
- if (!$model->save()) {
- self::$static_error = '保存数据失败:' . $model->getErrorMessage();
- return false;
- }
- return $model;
- }
- public static function getOrderNo($mall_id)
- {
- $date = date('Ymd');
- $start = strtotime($date . ' 00:00:00');
- $end = strtotime($date . ' 23:59:59');
- $res = self::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->andWhere(['between', 'created_at', $start, $end])->orderBy('id desc')->one();
- if ($res) {
- $num = $res['order_no'];
- $num = substr($num, 8);
- $num = intval($num) + 1;
- $num = str_pad($num, 5, '0', STR_PAD_LEFT);
- return $date . $num;
- } else {
- return $date . '00001';
- }
- }
- }
|