| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace common\services;
- use Yii;
- use common\models\xhStatIncome;
- use common\components\dict;
- use common\components\stringUtil;
- class xhStatIncomeService
- {
-
- /**
- * 添加与更新
- */
- public static function replace($sjId, $totalFee, $payWay)
- {
- if ($totalFee <= 0) {
- return true;
- }
- $time = date("Ymd");
- $income = self::getByIds($sjId, $time);
- $weixin = 0;
- $alipay = 0;
- $balancePay = 0;
- $cash = 0;
- $addTime = time();
- $payList = dict::getDict('payWay');
- if ($payList['wxPay'] == $payWay) {
- $weixin = $totalFee;
- } elseif ($payWay == $payList['alipay']) {
- $alipay = $totalFee;
- } elseif ($payWay == $payList['balancePay']) {
- $balancePay = $totalFee;
- } elseif ($payWay == $payList['cashPay']) {
- $cash = $totalFee;
- } elseif ($payWay == $payList['mini']) {
- $weixin = $totalFee;
- }
- if (empty($income)) {
- $data = [
- 'time' => $time,
- 'amount' => $totalFee,
- 'sjId' => $sjId,
- 'addTime' => $addTime,
- 'weixin' => $weixin,
- 'alipay' => $alipay,
- 'balancePay' => $balancePay,
- 'cash' => $cash,
- ];
- self::add($data);
- } else {
- $amount = stringUtil::calcAdd($income['amount'], $totalFee);
- $weixin = stringUtil::calcAdd($income['weixin'], $weixin);
- $cash = stringUtil::calcAdd($income['cash'], $cash);
- $balancePay = stringUtil::calcAdd($income['balancePay'], $balancePay);
- $alipay = stringUtil::calcAdd($income['alipay'], $alipay);
- $data = [
- 'amount' => $amount,
- 'weixin' => $weixin,
- 'alipay' => $alipay,
- 'balancePay' => $balancePay,
- 'cash' => $cash,
- ];
- self::updateByIds($sjId, $time, $data);
- }
- }
-
- public static function updateByIds($sjId, $time, $data)
- {
- xhStatIncome::updateByCondition(['time' => $time, 'sjId' => $sjId], $data);
- self::getByIds($sjId, $time);
- $cacheKey = dict::getCacheKey('xhStatIncome') . '_' . $sjId . '_' . $time;
- $params = [$cacheKey];
- if (!empty($data)) {
- foreach ($data as $key => $val) {
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET', $params);
- return self::getByIds($sjId, $time);
- }
-
- public static function add($data)
- {
- $return = xhStatIncome::add($data);
- return $return;
- }
-
- public static function getByIds($sjId, $time)
- {
- $income = xhStatIncome::find()->where(['sjId' => $sjId, 'time' => $time])->asArray()->one();
- return $income;
- }
-
- public static function getTodayIncome($sjId, $time)
- {
- $income = self::getByIds($sjId, $time);
- $amount = 0;
- if (!empty($income)) {
- $amount = $income['amount'];
- }
- return $amount;
- }
-
- public static function getLatestIncome($sjId)
- {
- $num = 14;//取最近15天
- $arr = [];
- $dateList = [];
- $amountList = [];
- for ($i = $num; $i >= 0; $i--) {
- $time = strtotime(date("Y-m-d")) - $i * 86400;
- $date = date("n/j", $time);
- $income = self::getByIds($sjId, $time);
- $amount = 0;
- if (!empty($income)) {
- $amount = $income['amount'];
- }
- $arr[$date] = $amount;
- $dateList[] = $date;
- $amountList[] = $amount;
- }
- return ['list' => $arr, 'date' => $dateList, 'amount' => $amountList];
- }
-
- }
|