UserTeamMonthStatistics.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace common\models\user;
  3. use addons\ServeBonus\common\models\ServeBonus;
  4. use addons\ServeBonus\common\models\ServeBonusLevel;
  5. use common\enums\AddonsEnum;
  6. use common\enums\OrderStatusEnum;
  7. use common\enums\StatusEnum;
  8. use common\models\mall\MallAddons;
  9. use common\models\order\Order;
  10. use common\models\order\OrderDetail;
  11. use common\models\order\OrderRefund;
  12. use Exception;
  13. use Yii;
  14. /**
  15. * This is the model class for table "qimall_user_team_month_statistics".
  16. *
  17. * @property int $id
  18. * @property int $mall_id 商城id
  19. * @property int $user_id 用户id
  20. * @property string $month 年月
  21. * @property int $status 状态,-1删除,0禁用,1正常
  22. * @property int $created_at
  23. * @property int $updated_at
  24. * @property float $price 支付完成业绩
  25. * @property float $finish_price 订单完成业绩
  26. */
  27. class UserTeamMonthStatistics extends \common\models\base\BaseActiveRecord
  28. {
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public static function tableName()
  33. {
  34. return 'qimall_user_team_month_statistics';
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['mall_id', 'user_id', 'status', 'created_at', 'updated_at'], 'integer'],
  43. [['price', 'finish_price'], 'number'],
  44. [['month'], 'string', 'max' => 10],
  45. ];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'mall_id' => '商城id',
  55. 'user_id' => '用户id',
  56. 'month' => '年月',
  57. 'status' => '状态,-1删除,0禁用,1正常',
  58. 'created_at' => 'Created At',
  59. 'updated_at' => 'Updated At',
  60. 'price' => '支付完成业绩',
  61. 'finish_price' => '订单完成业绩',
  62. ];
  63. }
  64. public static function setData(array $data)
  65. {
  66. try {
  67. $model = self::findOne(['user_id' => $data['user_id'], 'status' => StatusEnum::ENABLED, 'month' => $data['month']]);
  68. if (empty($model)) {
  69. $model = new self();
  70. $model->loadDefaultValues();
  71. }
  72. if (!$model->load($data, '') || !$model->save()) {
  73. throw new \Exception($model->getErrorMessage());
  74. }
  75. return $model;
  76. } catch (\Exception $e) {
  77. self::setStaticError($e->getMessage());
  78. return false;
  79. }
  80. }
  81. public static function calculatePerformance($params)
  82. {
  83. $mall_id = $params['mall_id'];
  84. $uid = $params['user_id'];
  85. $order_id = $params['order_id'];
  86. $is_virtual = $params['is_virtual'];
  87. try {
  88. $configs = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SERVEBONUS, 'basic');
  89. //获取订单数据
  90. $order = Order::getOne([
  91. ['mall_id' => $mall_id],
  92. ['user_id' => $uid],
  93. ['id' => $order_id],
  94. ['status' => StatusEnum::ENABLED]
  95. ], true, ['detail']);
  96. if (empty($order)) throw new Exception('查无此订单');
  97. $finish_price = 0;
  98. $price = 0;
  99. /* 如果订单是虚拟订单时。订单在次方法之前已经将订单状态改为完成。此处数据统计永远无法统计订单支付数据。
  100. 所以需要修改订单为虚拟订单时。需要统计支付数据 */
  101. if ($is_virtual) {
  102. $price += $order['pay_money'];
  103. } else {
  104. if ($order['order_status'] == OrderStatusEnum::ACCOMPLISH) {
  105. $finish_price += $order['pay_money'];
  106. } else {
  107. $price += $order['pay_money'];
  108. }
  109. }
  110. $parent_id_arr = UserPartitionRelationship::getParentIdArr($uid);
  111. $parent_id_arr[] = $uid;
  112. //获取当前用户的月订单数据
  113. $list = self::lists([
  114. 'where' => [
  115. ['mall_id' => $mall_id],
  116. ['user_id' => $parent_id_arr],
  117. ['month' => date('Y-m')],
  118. ['status' => StatusEnum::ENABLED]
  119. ],
  120. 'index' => 'user_id',
  121. ], false);
  122. foreach ($parent_id_arr as $user_id) {
  123. $item = $list[$user_id];
  124. if (empty($item)) {
  125. $item = new UserTeamMonthStatistics();
  126. $item->loadDefaultValues();
  127. $item->mall_id = $mall_id;
  128. $item->user_id = $user_id;
  129. $item->month = date('Y-m');
  130. $item->status = StatusEnum::ENABLED;
  131. }
  132. $item->price += $price;
  133. $item->finish_price += $finish_price;
  134. if (!$item->save()) var_dump('统计用户月订单数据失败:user_id:'. $user_id.',order_id:'.$order_id);
  135. // 是否服务商
  136. // $bonus = ServeBonus::find()->where(['user_id' => $user_id, 'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->one();
  137. // if (empty($bonus)) {
  138. // continue;
  139. // }
  140. }
  141. return true;
  142. } catch (\Exception $e) {
  143. \Yii::error(__METHOD__ . ' ' . json_encode(['msg' => $e->getMessage(), 'line' => $e->getLine()]));
  144. self::setStaticError($e->getMessage());
  145. echo $e->getMessage() . '异常';
  146. return false;
  147. }
  148. }
  149. //售后扣减
  150. public static function calculatePerformanceRefund($params)
  151. {
  152. try {
  153. $mall_id = $params['mall_id'];
  154. $user_id = $params['user_id'];
  155. $refund_id = $params['refund_id'];
  156. //获取售后订单数据
  157. $refund_data = OrderRefund::getOne([
  158. ['mall_id' => $mall_id],
  159. ['user_id' => $user_id],
  160. ['id' => $refund_id],
  161. ], true, ['order']);
  162. if (empty($refund_data)) throw new Exception('查无此售后订单');
  163. $parent_id_arr = UserPartitionRelationship::getParentIdArr($user_id);
  164. $parent_id_arr[] = $user_id;
  165. $list = self::lists([
  166. 'where' => [
  167. ['mall_id' => $mall_id],
  168. ['user_id' => $parent_id_arr],
  169. ['month' => date('Y-m', $refund_data['order']['pay_time'])],
  170. ['status' => StatusEnum::ENABLED]
  171. ],
  172. ], false);
  173. foreach ($list as $key => $value) {
  174. $value->price -= $refund_data['reality_refund_price'];
  175. if (!$value->save()) var_dump('售后扣减业绩失败:user_id:'. $user_id.',refund_id:'.$refund_id);
  176. }
  177. return true;
  178. } catch (\Throwable $th) {
  179. //throw $th;
  180. }
  181. }
  182. public static function computeGoodsPerformance($user_id, $andwhere, $is_last_month = false)
  183. {
  184. if ($is_last_month) {
  185. $start = strtotime(date('Y-m-01 00:00:00', strtotime('-1 month')));
  186. $end = strtotime(date('Y-m-t 23:59:59', strtotime('-1 month')));
  187. } else {
  188. $start = strtotime(date('Y-m-01 00:00:00'));
  189. $end = strtotime(date('Y-m-t 23:59:59'));
  190. }
  191. $data = Order::find()
  192. ->where(['user_id' => $user_id, 'status' => StatusEnum::ENABLED])
  193. ->andWhere($andwhere)
  194. ->andWhere(['between', 'created_at', $start, $end])
  195. ->sum('pay_money');
  196. return $data ?? 0;
  197. }
  198. }