xhStatIncomeMonthService.php 3.6 KB

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