| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- namespace common\models\user;
- use addons\ServeBonus\common\models\ServeBonus;
- use addons\ServeBonus\common\models\ServeBonusLevel;
- use common\enums\AddonsEnum;
- use common\enums\OrderStatusEnum;
- use common\enums\StatusEnum;
- use common\models\mall\MallAddons;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\order\OrderRefund;
- use Exception;
- use Yii;
- /**
- * This is the model class for table "qimall_user_team_month_statistics".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $user_id 用户id
- * @property string $month 年月
- * @property int $status 状态,-1删除,0禁用,1正常
- * @property int $created_at
- * @property int $updated_at
- * @property float $price 支付完成业绩
- * @property float $finish_price 订单完成业绩
- */
- class UserTeamMonthStatistics extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'qimall_user_team_month_statistics';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'status', 'created_at', 'updated_at'], 'integer'],
- [['price', 'finish_price'], 'number'],
- [['month'], 'string', 'max' => 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;
- }
- }
|