10], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => '商城id', 'user_id' => '用户id', 'month' => '年月', 'status' => '状态,-1删除,0禁用,1正常', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'price' => '支付完成业绩', 'finish_price' => '订单完成业绩', ]; } public static function setData(array $data) { try { $model = self::findOne(['user_id' => $data['user_id'], 'status' => StatusEnum::ENABLED, 'month' => $data['month']]); if (empty($model)) { $model = new self(); $model->loadDefaultValues(); } if (!$model->load($data, '') || !$model->save()) { throw new \Exception($model->getErrorMessage()); } return $model; } catch (\Exception $e) { self::setStaticError($e->getMessage()); return false; } } public static function calculatePerformance($params) { $mall_id = $params['mall_id']; $uid = $params['user_id']; $order_id = $params['order_id']; $is_virtual = $params['is_virtual']; try { $configs = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SERVEBONUS, 'basic'); //获取订单数据 $order = Order::getOne([ ['mall_id' => $mall_id], ['user_id' => $uid], ['id' => $order_id], ['status' => StatusEnum::ENABLED] ], true, ['detail']); if (empty($order)) throw new Exception('查无此订单'); $finish_price = 0; $price = 0; /* 如果订单是虚拟订单时。订单在次方法之前已经将订单状态改为完成。此处数据统计永远无法统计订单支付数据。 所以需要修改订单为虚拟订单时。需要统计支付数据 */ if ($is_virtual) { $price += $order['pay_money']; } else { if ($order['order_status'] == OrderStatusEnum::ACCOMPLISH) { $finish_price += $order['pay_money']; } else { $price += $order['pay_money']; } } $parent_id_arr = UserPartitionRelationship::getParentIdArr($uid); $parent_id_arr[] = $uid; //获取当前用户的月订单数据 $list = self::lists([ 'where' => [ ['mall_id' => $mall_id], ['user_id' => $parent_id_arr], ['month' => date('Y-m')], ['status' => StatusEnum::ENABLED] ], 'index' => 'user_id', ], false); foreach ($parent_id_arr as $user_id) { $item = $list[$user_id]; if (empty($item)) { $item = new UserTeamMonthStatistics(); $item->loadDefaultValues(); $item->mall_id = $mall_id; $item->user_id = $user_id; $item->month = date('Y-m'); $item->status = StatusEnum::ENABLED; } $item->price += $price; $item->finish_price += $finish_price; if (!$item->save()) var_dump('统计用户月订单数据失败:user_id:'. $user_id.',order_id:'.$order_id); // 是否服务商 // $bonus = ServeBonus::find()->where(['user_id' => $user_id, 'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->one(); // if (empty($bonus)) { // continue; // } } return true; } catch (\Exception $e) { \Yii::error(__METHOD__ . ' ' . json_encode(['msg' => $e->getMessage(), 'line' => $e->getLine()])); self::setStaticError($e->getMessage()); echo $e->getMessage() . '异常'; return false; } } //售后扣减 public static function calculatePerformanceRefund($params) { try { $mall_id = $params['mall_id']; $user_id = $params['user_id']; $refund_id = $params['refund_id']; //获取售后订单数据 $refund_data = OrderRefund::getOne([ ['mall_id' => $mall_id], ['user_id' => $user_id], ['id' => $refund_id], ], true, ['order']); if (empty($refund_data)) throw new Exception('查无此售后订单'); $parent_id_arr = UserPartitionRelationship::getParentIdArr($user_id); $parent_id_arr[] = $user_id; $list = self::lists([ 'where' => [ ['mall_id' => $mall_id], ['user_id' => $parent_id_arr], ['month' => date('Y-m', $refund_data['order']['pay_time'])], ['status' => StatusEnum::ENABLED] ], ], false); foreach ($list as $key => $value) { $value->price -= $refund_data['reality_refund_price']; if (!$value->save()) var_dump('售后扣减业绩失败:user_id:'. $user_id.',refund_id:'.$refund_id); } return true; } catch (\Throwable $th) { //throw $th; } } public static function computeGoodsPerformance($user_id, $andwhere, $is_last_month = false) { if ($is_last_month) { $start = strtotime(date('Y-m-01 00:00:00', strtotime('-1 month'))); $end = strtotime(date('Y-m-t 23:59:59', strtotime('-1 month'))); } else { $start = strtotime(date('Y-m-01 00:00:00')); $end = strtotime(date('Y-m-t 23:59:59')); } $data = Order::find() ->where(['user_id' => $user_id, 'status' => StatusEnum::ENABLED]) ->andWhere($andwhere) ->andWhere(['between', 'created_at', $start, $end]) ->sum('pay_money'); return $data ?? 0; } }