AssetsStatisticsLogin.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace common\logic\statistics;
  3. use common\models\mall\Mall;
  4. use common\models\common\CapitalLog;
  5. use common\models\user\UserWithdrawLog;
  6. use common\models\statistics\CorntabAssetsStatistics;
  7. use common\helpers\FormatHelper;
  8. use common\traits\ErrorTrait;
  9. use common\models\common\CapitalStatistics;
  10. use Yii;
  11. class AssetsStatisticsLogin
  12. {
  13. use ErrorTrait;
  14. const LIMIT = 500;
  15. const ASSETS_BALANCE = 1; //余额
  16. const ASSETS_SCORE = 2; //积分
  17. const ASSETS_COMMISSION = 3; //佣金
  18. const ASSETS_BALANCE_FROZEN = 4; //余额冻结
  19. const ASSETS_SCORE_FROZEN = 5; //积分冻结
  20. const ASSETS_COMMISSION_FROZEN = 6; //佣金冻结
  21. const ASSETS_WITHDRAW = 7; //提现
  22. const ASSETS_ENUM = [
  23. self::ASSETS_BALANCE => 'balance',
  24. self::ASSETS_SCORE => 'score',
  25. self::ASSETS_COMMISSION => 'commission',
  26. self::ASSETS_BALANCE_FROZEN => 'balance_frozen',
  27. self::ASSETS_SCORE_FROZEN => 'score_frozen',
  28. self::ASSETS_COMMISSION_FROZEN => 'commission_frozen',
  29. self::ASSETS_WITHDRAW => 'withdraw',
  30. ];
  31. //每个指标需要查询的表
  32. const ASSETS_ARR = [
  33. self::ASSETS_BALANCE => [self::ASSETS_BALANCE, self::ASSETS_BALANCE_FROZEN, self::ASSETS_WITHDRAW],
  34. self::ASSETS_COMMISSION => [self::ASSETS_COMMISSION, self::ASSETS_COMMISSION_FROZEN, self::ASSETS_WITHDRAW],
  35. self::ASSETS_SCORE => [self::ASSETS_SCORE, self::ASSETS_SCORE_FROZEN]
  36. ];
  37. /**
  38. * 执行统计操作
  39. *
  40. * @author hpei
  41. * @return bool
  42. */
  43. public function execute() {
  44. $mall_list = Mall::getEnableMallList(true);
  45. if (!empty($mall_list)) {
  46. $date = date('Ymd');
  47. foreach ($mall_list as $mall) {
  48. $mallId = $mall['id'];
  49. try {
  50. $lastStatistics = CorntabAssetsStatistics::find()->select(['type', 'last_id', 'id'])->where(['mall_id' => $mallId])->indexBy('type')->asArray()->all();
  51. $trans = Yii::$app->db->beginTransaction();
  52. foreach (self::ASSETS_ARR as $type => $actions) {
  53. $statisticsData = [];
  54. //循环每一张表
  55. foreach ($actions as $assetsType) {
  56. $lastId = isset($lastStatistics[$assetsType]) ? $lastStatistics[$assetsType]['last_id'] : 0;
  57. $name = self::ASSETS_ENUM[$assetsType];
  58. //积分没有提现,不需要type
  59. if ($type == self::ASSETS_ENUM[self::ASSETS_SCORE]) {
  60. $data = $this->getData($mallId, $name, $lastId);
  61. } else {
  62. $data = $this->getData($mallId, $name, $lastId, $type);
  63. }
  64. $statisticsData = array_merge($statisticsData, $data);
  65. unset($statisticsData['last_id']);
  66. if ($name == self::ASSETS_ENUM[self::ASSETS_WITHDRAW] && $type == self::ASSETS_COMMISSION) continue;
  67. if (isset($lastStatistics[$assetsType])) {
  68. $id = $lastStatistics[$assetsType]['id'];
  69. $updateData = [];
  70. $updateData['updated_at'] = time();
  71. if ($data['last_id'] > 0) $updateData['last_id'] = $data['last_id'];
  72. $res = CorntabAssetsStatistics::updateAll($updateData, ['id' => $id]);
  73. } else {
  74. $object = new CorntabAssetsStatistics();
  75. $object->updated_at = time();
  76. $object->created_at = time();
  77. $object->mall_id = $mallId;
  78. $object->type = $assetsType;
  79. $object->last_id = $data['last_id'];
  80. $res = $object->save();
  81. }
  82. if (!$res) {
  83. $trans->rollBack();
  84. throw new \Exception($object->getErrorMessage());
  85. }
  86. }
  87. $capitalStatistics = CapitalStatistics::find()->where(['mall_id' => $mallId, 'date' => $date, 'type' => $type])->one();
  88. if (empty($capitalStatistics)) {
  89. $capitalStatistics = new CapitalStatistics();
  90. $capitalStatistics->date = $date;
  91. $capitalStatistics->type = $type;
  92. $capitalStatistics->mall_id = $mallId;
  93. }
  94. if (!empty($statisticsData)) {
  95. foreach ($statisticsData as $key => $value) {
  96. if ($value) $capitalStatistics->$key = $value;
  97. }
  98. if (!$capitalStatistics->save()) {
  99. $trans->rollBack();
  100. throw new \Exception($capitalStatistics->getErrorMessage());
  101. }
  102. }
  103. }
  104. $trans->commit();
  105. } catch (\Exception $e) {
  106. $trans->rollBack();
  107. $exception = FormatHelper::exception($e, "统计资金定时任务失败,行数". $e->getLine() . $e->getFile());
  108. echo $exception;
  109. Yii::error($exception);
  110. $this->setError($exception);
  111. return false;
  112. }
  113. }
  114. }
  115. return true;
  116. }
  117. /**
  118. * 查询每个统计项数据
  119. *
  120. * @param int $mall_id
  121. * @param string $name
  122. * @param int $last_id 上次执行最后一条记录ID
  123. * @param int $from 提现来源1佣金2余额
  124. */
  125. public function getData($mall_id, $name, $last_id, $from = 0) {
  126. switch ($name) {
  127. //积分 余额 佣金
  128. case ($name == self::ASSETS_ENUM[self::ASSETS_SCORE] || $name == self::ASSETS_ENUM[self::ASSETS_COMMISSION] || $name == self::ASSETS_ENUM[self::ASSETS_BALANCE]):
  129. CapitalLog::$capitalType = $name;
  130. $scoreList = CapitalLog::find()->select(['id', 'change_type', 'change_num'])->where(['mall_id' => $mall_id])->andWhere(['>', 'id', $last_id])->limit(self::LIMIT)->all();
  131. $last_id = 0;
  132. $capitalTotal = 0; //发放的资产
  133. $expendCapitalTotal = 0; //支出的资产
  134. if (!empty($scoreList)) {
  135. foreach ($scoreList as $score) {
  136. $last_id = $score['id'];
  137. if ($score['change_type'] == CapitalLog::CHANGE_TYPE_ADD) $capitalTotal += $score['change_num'];
  138. if ($score['change_type'] == CapitalLog::CHANGE_TYPE_SUB) $expendCapitalTotal += $score['change_num'];
  139. }
  140. }
  141. return ['capital_total' => $capitalTotal, 'expend_capital_total' => $expendCapitalTotal, 'last_id' => $last_id];
  142. //冻结余额 冻结积分 冻结佣金
  143. case ($name == self::ASSETS_ENUM[self::ASSETS_SCORE_FROZEN] || $name == self::ASSETS_ENUM[self::ASSETS_COMMISSION_FROZEN] || $name == self::ASSETS_ENUM[self::ASSETS_BALANCE_FROZEN]):
  144. CapitalLog::$capitalType = $name;
  145. $scoreList = CapitalLog::find()->select(['id', 'change_type', 'change_num', 'status'])->where(['mall_id' => $mall_id])->andWhere(['>', 'id', $last_id])->asArray()->limit(self::LIMIT)->all();
  146. $last_id = 0;
  147. $incomeCapitalTotal = 0;//总资产,包括已经冻结的
  148. $invalidCapitalTotal = 0;//无效资产
  149. if (!empty($scoreList)) {
  150. foreach ($scoreList as $score) {
  151. $last_id = $score['id'];
  152. if ($score['status'] == -1 && $score['change_type'] == CapitalLog::CHANGE_TYPE_ADD) $invalidCapitalTotal += $score['change_num'];
  153. if ($score['status'] != -1 && $score['change_type'] == CapitalLog::CHANGE_TYPE_ADD) $incomeCapitalTotal += $score['change_num'];
  154. }
  155. }
  156. return ['income_capital_total' => $incomeCapitalTotal, 'invalid_capital_total' => $invalidCapitalTotal, 'last_id' => $last_id];
  157. //提现
  158. case self::ASSETS_ENUM[self::ASSETS_WITHDRAW]:
  159. //资金表1:余额,3:佣金;提现表1:佣金,2:余额
  160. if ($from == 1) $from = 2;
  161. if ($from == 3) $from = 1;
  162. $where = ['mall_id' => $mall_id, 'status' => [UserWithdrawLog::STATUS_AGREE, UserWithdrawLog::STATUS_ALREADY_TRANS]];
  163. $withdrawLog = UserWithdrawLog::find()->select(['id', 'from', 'actual_num', 'poundage_num'])->where($where)->andWhere(['>', 'id', $last_id])->asArray()->limit(self::LIMIT)->all();
  164. $last_id = 0;
  165. $cashOutTotal = 0;//提现
  166. $serviceChargesTatoal = 0;//手续费
  167. if (!empty($withdrawLog)) {
  168. foreach ($withdrawLog as $log) {
  169. $last_id = $log['id'];
  170. if ($log['from'] == $from) {
  171. $cashOutTotal += $log['actual_num'] ?? 0;
  172. $serviceChargesTatoal += $log['poundage_num'] ?? 0;
  173. }
  174. }
  175. }
  176. return ['cash_out_total' => $cashOutTotal, 'service_charges_tatoal' => $serviceChargesTatoal, 'last_id' => $last_id];
  177. }
  178. }
  179. }