| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- namespace common\models\order;
- use common\enums\GoodsTypeEnum;
- use common\enums\OrderSourceEnum;
- use common\enums\OrderStatusEnum;
- use common\enums\RefundStatusEnum;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use Yii;
- /**
- * This is the model class for table "{{%order_user_statistics}}".
- *
- *
- * @property int $id
- * @property int $mall_id 商场ID
- * @property int|null $order_pay_num 待付款数量
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property int|null $order_received_num 待收货数量
- * @property int|null $order_goods_num 待发货数量
- * @property int|null $order_evaluate_num 待评价数量
- * @property int|null $order_return_num 退货数量
- * @property int|null $user_id 会员ID
- * @property int|null $is_statistics 是否已经执行过统计,因为这功能是后面上的,需要执行一次统计数据才会正确
- */
- class OrderUnreadMsg extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%order_unread_msg}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'order_pay_num', 'status', 'created_at', 'updated_at', 'order_received_num', 'order_goods_num', 'order_evaluate_num', 'order_return_num', 'user_id', 'is_statistics'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商场ID',
- 'order_pay_num' => '待付款数量',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- 'order_received_num' => '待收货数量',
- 'order_goods_num' => '待发货数量',
- 'order_evaluate_num' => '待评价数量',
- 'order_return_num' => '退货数量',
- 'user_id' => '会员ID',
- 'is_statistics' => '是否已经执行过统计,因为这功能是后面上的,需要执行一次统计数据才会正确',
- ];
- }
- //完成订单调用方法
- public static function orderFinish($params){
- $orderStatis['user_id'] = $params['user_id'];
- $orderStatis['mall_id'] = $params['mall_id'];
- $is_virtual = 0;;
- if($params['goods_ids']){
- $param['where'][] = ['in', 'id', $params['goods_ids']];
- $param['select'] = ['goods_type'];
- $goodsList = Goods::lists($param); //var_dump($goodsList);die;
- if($goodsList){
- foreach($goodsList as $k=>$v){
- if($v['goods_type']==GoodsTypeEnum::GoodsTypeVirtual){
- $is_virtual = 1;
- }
- }
- }
- }
- if($is_virtual==0){
- $orderStatis['order_evaluate_num'] = -1;
- self::orderStastics($orderStatis);
- }
- }
- //收货调用方法
- public static function takeDelivery($params){
- $order = Order::getOne([['id' => $params['id']]],true);
- $orderStatis['user_id'] = $order['user_id'];
- $orderStatis['mall_id'] = $order['mall_id'];
- $orderStatis['order_received_num'] = -1;
- $orderStatis['order_evaluate_num'] = 1;
- self::orderStastics($orderStatis);
- }
- //发货调用方法
- public static function delivery($params){
- $order_express = OrderExpress::find()->select('order_id')->where(['id' => $params['id']])->asArray()->one();
- $order = Order::getOne([['id' => $order_express['order_id']]],true);
- if ($order['order_status'] == OrderStatusEnum::SHIPMENTS) {// 全部发货的情况下才可以增加发货数量
- $orderStatis['user_id'] = $order['user_id'];
- $orderStatis['mall_id'] = $order['mall_id'];
- $orderStatis['order_received_num'] = 1;
- $orderStatis['order_goods_num'] = -1;
- self::orderStastics($orderStatis);
- }
- }
- public static function orderStastics($params){
- self::updateData($params['mall_id'],$params['user_id']);
- return true;
- try {
- $model = self::findOne(['user_id' => $params['user_id'],'mall_id'=>$params['mall_id']]);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- }
- if($params['order_pay_num']) $model->order_pay_num = $model->order_pay_num + $params['order_pay_num'];
- if($params['order_received_num']) $model->order_received_num = $model->order_received_num + $params['order_received_num'];
- if($params['order_goods_num']) $model->order_goods_num = $model->order_goods_num + $params['order_goods_num'];
- if($params['order_evaluate_num']) $model->order_evaluate_num = $model->order_evaluate_num + $params['order_evaluate_num'];
- if($params['order_return_num']) $model->order_return_num = $model->order_return_num + $params['order_return_num'];
- unset($params['order_pay_num'],$params['order_received_num'],$params['order_goods_num'],$params['order_evaluate_num'],$params['order_return_num']);
- //var_dump($params,$model);die;
- if (empty($model)) throw new \Exception('查无此订单');
- if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2022/3/3 0003 17:24
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $params
- * @return bool|PlatformOrder
- */
- public static function setData($params)
- {
- try {
- if (!empty($params['id'])) {
- $model = self::findOne(['id' => $params['id']]);
- } else {
- $model = new self();
- $model->loadDefaultValues();
- }
- if (empty($model)) throw new \Exception('查无此订单');
- if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- //订单统计初始化
- public static function orderStatistic($params,$return_data = false){
- //查询订单数量
- $orderStatis['user_id'] = $params['user_id'];
- $orderStatis['mall_id'] = $params['mall_id'];
- $orderStatis['is_statistics'] = 1;
- //统计代付款
- $where = [['=','mall_id',$params['mall_id']],['=','user_id',$params['user_id']],['=','is_feedback',StatusEnum::DISABLED]];
- $paywhere = $where;
- $paywhere[] = ['=','order_status',OrderStatusEnum::NOT_PAY];
- $paywhere[] = ['not in','order_source',[OrderSourceEnum::SOURCE_RESERVE, OrderSourceEnum::SOURCE_STORE_RESERVE]];
- $orderStatis['order_pay_num'] = Order::count(['where'=>$paywhere]);
- //统计待发货
- $goodswhere = $where;
- $goodswhere[] = ['=','order_status',OrderStatusEnum::PAY];
- $goodswhere[] = ['not in','order_source',[OrderSourceEnum::SOURCE_RESERVE, OrderSourceEnum::SOURCE_STORE_RESERVE]];
- $orderStatis['order_goods_num'] = Order::count(['where'=>$goodswhere]);
- //统计待收货
- $receivedwhere = $where;
- $receivedwhere[] = ['=','order_status',OrderStatusEnum::SHIPMENTS];
- $receivedwhere[] = ['not in','order_source',[OrderSourceEnum::SOURCE_RESERVE, OrderSourceEnum::SOURCE_STORE_RESERVE]];
- $orderStatis['order_received_num'] = Order::count(['where'=>$receivedwhere]);
- //待评价
- $evaluatewhere = $where;
- $evaluatewhere[] = ['=','order_status',OrderStatusEnum::SING];
- $evaluatewhere[] = ['not in','order_source',[OrderSourceEnum::SOURCE_RESERVE, OrderSourceEnum::SOURCE_STORE_RESERVE]];
- $orderStatis['order_evaluate_num'] = Order::count(['where'=>$evaluatewhere]);
- //待退换货
- $order_refund_1 = OrderRefund::count(['where'=>[['=','refund_status',RefundStatusEnum::APPLY],['=','mall_id',$params['mall_id']],['=','user_id',$params['user_id']]]]);
- //$order_refund_2 = OrderRefund::count(['where'=>[['=','refund_status',RefundStatusEnum::FINISH],['=','mall_id',$params['mall_id']],['=','user_id',$params['user_id']]]]);
- //$order_refund_3 = OrderRefund::count(['where'=>[['=','refund_status',RefundStatusEnum::REVOKE],['=','mall_id',$params['mall_id']],['=','user_id',$params['user_id']]]]);
- $orderStatis['order_return_num'] = $order_refund_1;
- if($return_data) return $orderStatis;
- $model = self::orderStastics($orderStatis);
- return $model;
- }
- /**
- * 修改统计数据
- * @param $mall_id
- * @param $user_id
- * @return OrderUnreadMsg|null
- * @throws
- */
- public static function updateData($mall_id,$user_id)
- {
- $params = self::orderStatistic(['user_id' => $user_id,'mall_id' => $mall_id],true);
- //print_r($params);
- $model = self::findOne(['user_id' => $user_id,'mall_id'=>$mall_id]);
- if($model){
- }else{
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($params, '') || !$model->save()){
- Yii::error($model->getErrorMessage());
- throw new \Exception($model->getErrorMessage());
- }
- return $model;
- }
- }
|