xhStatIncomeService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\models\xhStatIncome;
  5. use common\components\configDict;
  6. use common\components\stringUtil;
  7. class xhStatIncomeService
  8. {
  9. /**
  10. * 添加与更新
  11. */
  12. public static function replace($merchantId, $totalFee, $payWay)
  13. {
  14. $time = date("Ymd");
  15. $income = self::getByIds($merchantId, $time);
  16. $weixin = 0;
  17. $alipay = 0;
  18. $balancePay = 0;
  19. $cash = 0;
  20. $addTime = time();
  21. $payList = configDict::getConfig('payWay');
  22. if ($payList['weixinPay'] == $payWay) {
  23. $weixin = $totalFee;
  24. } elseif ($payWay == $payList['alipay']) {
  25. $alipay = $totalFee;
  26. } elseif ($payWay == $payList['balancePay']) {
  27. $balancePay = $totalFee;
  28. } elseif ($payWay == $payList['cashPay']) {
  29. $cash = $totalFee;
  30. } elseif ($payWay == $payList['mini']) {
  31. $weixin = $totalFee;
  32. }
  33. if (empty($income)) {
  34. $data = [
  35. 'time' => $time,
  36. 'amount' => $totalFee,
  37. 'merchantId' => $merchantId,
  38. 'addTime' => $addTime,
  39. 'weixin' => $weixin,
  40. 'alipay' => $alipay,
  41. 'balancePay' => $balancePay,
  42. 'cash' => $cash,
  43. ];
  44. self::add($data);
  45. } else {
  46. $amount = stringUtil::calcAdd($income['amount'], $totalFee);
  47. $weixin = stringUtil::calcAdd($income['weixin'], $weixin);
  48. $cash = stringUtil::calcAdd($income['cash'], $cash);
  49. $balancePay = stringUtil::calcAdd($income['balancePay'], $balancePay);
  50. $alipay = stringUtil::calcAdd($income['alipay'], $alipay);
  51. $data = [
  52. 'amount' => $amount,
  53. 'weixin' => $weixin,
  54. 'alipay' => $alipay,
  55. 'balancePay' => $balancePay,
  56. 'cash' => $cash,
  57. ];
  58. self::updateByIds($merchantId, $time, $data);
  59. }
  60. }
  61. public static function updateByIds($merchantId, $time, $data)
  62. {
  63. xhStatIncome::updateByCondition(['time' => $time, 'merchantId' => $merchantId], $data);
  64. self::getByIds($merchantId, $time);
  65. $cacheKey = configDict::getCacheKey('xhStatIncome') . '_' . $merchantId . '_' . $time;
  66. $params = [$cacheKey];
  67. if (!empty($data)) {
  68. foreach ($data as $key => $val) {
  69. $params[] = $key;
  70. $params[] = $val;
  71. }
  72. }
  73. Yii::$app->redis->executeCommand('HMSET', $params);
  74. return self::getByIds($merchantId, $time);
  75. }
  76. public static function add($data)
  77. {
  78. $return = xhStatIncome::add($data);
  79. return $return;
  80. }
  81. public static function getByIds($merchantId, $time)
  82. {
  83. $income = xhStatIncome::find()->where(['merchantId' => $merchantId, 'time' => $time])->asArray()->one();
  84. return $income;
  85. }
  86. public static function getTodayIncome($merchantId, $time)
  87. {
  88. $income = self::getByIds($merchantId, $time);
  89. $amount = 0;
  90. if (!empty($income)) {
  91. $amount = $income['amount'];
  92. }
  93. return $amount;
  94. }
  95. public static function getLatestIncome($merchantId)
  96. {
  97. $num = 14;//取最近15天
  98. $arr = [];
  99. $dateList = [];
  100. $amountList = [];
  101. for ($i = $num; $i >= 0; $i--) {
  102. $time = strtotime(date("Y-m-d")) - $i * 86400;
  103. $date = date("n/j", $time);
  104. $income = self::getByIds($merchantId, $time);
  105. $amount = 0;
  106. if (!empty($income)) {
  107. $amount = $income['amount'];
  108. }
  109. $arr[$date] = $amount;
  110. $dateList[] = $date;
  111. $amountList[] = $amount;
  112. }
  113. return ['list' => $arr, 'date' => $dateList, 'amount' => $amountList];
  114. }
  115. }