| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- <?php
- namespace addons\Happiness\common\models;
- use addons\Store\common\models\StoreGoodsAttr;
- use common\enums\OrderSourceEnum;
- use common\enums\OrderStatusEnum;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\events\order\OrderTakeDeliveryEvent;
- use common\helpers\LockHelper;
- use common\models\order\OrderDetail;
- use common\models\order\OrderExpress;
- use common\models\order\OrderExpressLog;
- use common\models\order\OrderReserve;
- use common\models\user\User;
- use Exception;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_happiness_order".
- *
- * @property int $id id
- * @property int $mall_id 商城id
- * @property int $user_id 用户id
- * @property int $store_id 门店id
- * @property int $order_id 订单id
- * @property int|null $shipping_type 快递方式(1:快递配送,2:门店自提,3:送货上门)
- * @property string|null $order_no 订单编号
- * @property int $order_status 订单状态
- * @property int|null $created_at 创建时间
- * @property int|null $updated_at 修改时间
- * @property int $parent_id 推荐用户id
- */
- class HappinessOrder extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'qimall_addons_happiness_order';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- // [['id', 'mall_id', 'user_id', 'store_id', 'order_id'], 'required'],
- [['mall_id', 'user_id', 'store_id', 'order_id', 'shipping_type', 'created_at', 'updated_at','parent_id'], 'integer'],
- [['order_no'], 'string', 'max' => 255],
- [['price'], 'number']
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'parent_id' => 'Parent ID',
- 'store_id' => 'Store ID',
- 'order_id' => 'Order ID',
- 'shipping_type' => 'Shipping Type',
- 'order_no' => 'Order No',
- 'order_status' => 'Order Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getBase_user()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select(['id', 'username','nickname', 'avatar_url','mobile']);
- }
- public function getDetail()
- {
- return $this->hasMany(OrderDetail::class, ['order_id' => 'id']);
- }
- public function getOrderDetail()
- {
- return $this->hasMany(OrderDetail::class, ['order_id' => 'order_id']);
- }
- public function getReserve()
- {
- return $this->hasOne(OrderReserve::class, ['order_id' => 'id']);
- }
- public function getStore()
- {
- return $this->hasOne(HappinessStore::class, ['id' => 'store_id'])->asArray(true);
- }
- public static function setData(array $params)
- {
- try {
- if (!empty($params['mall_id']) && !empty($params['id'])) {
- $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id']]);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- }
- } else {
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * @name:
- * @msg: 关闭订单
- * @param {*} $params
- * @param {*} $force
- * @return {*}
- */
- public static function close($params, $force = false)
- {
- $trans = Yii::$app->db->beginTransaction();
- $stock_locks = [];
- $close_lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_GOODS, $params['id']);
- try {
- // 加锁
- $close_identification = uniqid();
- $stock_locks[$close_lock_key] = $close_identification;
- // if (!LockHelper::lock($close_lock_key, $close_identification, 30, 60)) throw new \Exception('正在关闭订单');
- $cond = ['mall_id' => $params['mall_id'], 'id' => $params['id']];
- isset($params['user_id']) && $cond['user_id'] = $params['user_id'];
- isset($params['store_id']) && $cond['store_id'] = $params['store_id'];
- $model = self::findOne($cond);
- if (empty($model)) throw new Exception('订单不存在或已删除1');
- // 判断订单是否已经被关闭
- if ($model->order_status == OrderStatusEnum::REPEAL) throw new Exception('订单已经关闭');
- // 判断是否是未支付的订单
- if ($force == false && $model->order_status != OrderStatusEnum::NOT_PAY) throw new Exception('订单已经被处理');
- $order = Order::findOne(['order_no'=>$model['order_no']]);
- // $model->close_time = time();
- $model->order_status = OrderStatusEnum::REPEAL;
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- // if(!empty($order)){
- // $order->close_time = $model->close_time;
- // $order->order_status = $model->order_status;
- // if (!$order->save()) throw new Exception($order->getErrorMessage());
- // }
- // $goods_attr_arr = array_column($model->detail, 'num', 'goods_attr_id');
- // //操作规格库存-回退
- // foreach ($goods_attr_arr as $goods_attr_id => $num) {
- // $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_GOODS_ATTR, $goods_attr_id);
- // $identification = uniqid();
- // if (!LockHelper::lock($lock_key, $identification, 30, 60)) throw new Exception('当前访问人数过多');
- // $stock_locks[$lock_key] = $identification;
- // //操作商品库存-回退
- // HappinessStoreGoodsAttr::handleStock($goods_attr_id, $num, $params['store_id']);
- // }
- $trans->commit();
- // 触发订单取消事件
- // 解锁
- // LockHelper::batchUlock($stock_locks);
- return $model;
- } catch (Exception $e) {
- $trans->rollback();
- // 解锁
- LockHelper::batchUlock($stock_locks);
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function searchParams($mall_id, $search)
- {
- // 查询参数
- $isReserve = $search['is_reserve'];
- $where[] = ['=', 'o.mall_id', $mall_id];
- $where[] = ['>=', 'o.status', StatusEnum::DISABLED];
- if ($isReserve) {
- $where[] = ['=', 'o.order_source', OrderSourceEnum::SOURCE_RESERVE];
- } else {
- $where[] = ['not in', 'o.order_source', [OrderSourceEnum::SOURCE_SELF_MALL, OrderSourceEnum::SOURCE_RESERVE]];
- }
- if (isset($search['order_id'])) $where[] = ['in', 'o.id', explode(',', $search['order_id'])];
- ($search['payment_type'] ?? false) && $where[] = ['=', 'o.payment_type', $search['payment_type']];
- ($search['shipping_type'] ?? false) && $where[] = ['=', 'o.shipping_type', $search['shipping_type']];
- if (isset($search['order_status']) && $search['order_status'] != 999) {
- $where[] = ['=', 'o.order_status', $search['order_status']];
- }
- ($search['order_no'] ?? false) && $where[] = ['=', 'o.order_no', $search['order_no']];
- ($search['start'] ?? false) && $where[] = ['>=', 'o.created_at', strtotime($search['start'])];
- ($search['end'] ?? false) && $where[] = ['<=', 'o.created_at', strtotime($search['end'])];
- if ($search['type_value'] ?? false) {
- switch ($search['type']) {
- case 'mobile':
- $uids = User::find()->select('id')->where(['like', 'mobile', $search['type_value'] . '%', false])->column();
- !empty($uids) && $where[] = ['in', 'yo.user_id', $uids];
- break;
- case 'user_id':
- $where[] = ['in', 'yo.user_id', $search['type_value']];
- break;
- default:
- }
- }
- if (!empty($search['store_name'])) {
- $store_ids = HappinessStore::find()->select('id')->where(['like', 'store_name', '%'.$search['store_name'].'%', false])->column();
- if (!empty($store_ids)) {
- $where[] = ['in', 'yo.store_id', $store_ids];
- } else {
- $where[] = ['yo.store_id' => 0];
- }
- }
- if (!empty($search['store_user_id'])) {
- $store_ids = HappinessStore::find()->select('id')->where(['user_id' => $search['store_user_id']])->column();
- if (!empty($store_ids)) {
- $where[] = ['in', 'yo.store_id', $store_ids];
- } else {
- $where[] = ['yo.store_id' => 0];
- }
- }
- return $where;
- }
- public static function OrderExpressDelivery($params,$trigger_event = true){
- try{
- $store_order = HappinessOrder::getOne([['mall_id' => $params['mall_id'], 'order_id' => $params['id'],'store_id'=>$params['store_id']]]);
- $order = Order::getOne([['mall_id' => $params['mall_id'], 'order_no' => $store_order['order_no'],'status'=>[StatusEnum::ENABLED]]]);
- if(empty($order)) throw new Exception('订单不存在或已删除');
- $trans = Yii::$app->db->beginTransaction();
- $model = new OrderExpress();
- $params['order_id'] = $order['id'];
- $params['buyer_id'] = $order['user_id'];
- $params['buyer_name'] = $order['receiver_name'];
- $params['operator_username'] = $params['operator_name'];
- if(!$model->load($params,'') || !$model->save()) throw new \Exception($model->getErrorMessage());
- //保存物流轨迹记录
- $params['order_express_id'] = $model->id;
- $res = OrderExpressLog::saveLog($params);
- if ($res == false) throw new Exception(OrderExpressLog::getStaticError());
- $condition['where'] = [['in', 'order_id', $order['id']],['is_feedback'=>[OrderStatusEnum::FEEDBACKING,OrderStatusEnum::FEEDBACKED]]];
- $res = OrderDetail::lists($condition);
- if(!empty($res)) throw new \Exception('订单申请售后中,请先处理售后问题');
- // 订单商品的物流状态改变
- OrderDetail::updateAll(['shipping_status' => StatusEnum::ENABLED], ['in', 'order_id', $order['id']]);
- // 自动维护订单状态
- $res = Order::autoUpdateStatus($order);
- if($res === false) throw new \Exception(Order::getStaticError());
- $trans->commit();
- if ($trigger_event) {
- // 发货成功触发事件,事务提交完成后触发
- $event = new \common\events\order\OrderDeliveryEvent($params['mall_id']);
- $data['id'] = $model->id;
- $data['operator_id'] = $params['operator_id'];
- $data['operator_name'] = $params['operator_name'];
- Yii::$app->services->events->dispatch($event,$data, $event->listeners);
- }
- return true;
- }catch(Exception $e){
- if(isset($trans)) $trans->rollback();
- self::$static_error = $e->getMessage().$e->getFile().$e->getLine();
- return false;
- }
- }
- public static function takeDelivery($params)
- {
- try {
- $cond = ['mall_id' => $params['mall_id'],'id' => $params['id'], 'status' => [StatusEnum::ENABLED]];
- // isset($params['user_id']) && $cond['user_id'] = $params['user_id'];
- // isset($params['store_id']) && $cond['store_id'] = $params['store_id'];
- $model = Order::findOne($cond);
- if (empty($model)) throw new Exception('订单不存在或已删除');
- if ($model->order_status == OrderStatusEnum::SING) throw new Exception('订单已经签收');
- if ($model->order_status != OrderStatusEnum::PAY) throw new Exception('订单已经被处理');
- if (OrderDetail::getAfterSaleCountByOrderId($model['id']) > 0) throw new Exception('请先处理或关闭订单售后');
- $model->order_status = OrderStatusEnum::SING;
- $model->sign_time = time();
- $model->consign_time = time();
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- // 触发订单收货事件,事务提交完成后触发
- $event = new OrderTakeDeliveryEvent($params['mall_id']);
- $data['id'] = $model['id'];
- $data['operator_id'] = $params['operator_id'];
- $data['operator_name'] = $params['operator_name'];
- Yii::$app->services->events->dispatch($event, $data, $event->listeners);
- return $model;
- } catch (Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- }
|