xhStatIncomeService.php 3.0 KB

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