| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace common\services;
- use common\components\stringUtil;
- use Yii;
- use common\models\xhStatIncomeMonth;
- use common\components\dict;
- class xhStatIncomeMonthService
- {
- /**
- * 添加与更新
- */
- public static function replace($sjId, $totalFee, $payWay)
- {
- if ($totalFee <= 0) {
- return true;
- }
- $year = date("Y");
- $month = date("n");
- $time = date("Ym");
- $income = self::getByTime($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 = ['year' => $year,
- 'month' => $month,
- '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, $year, $month, $data);
- }
- }
- public static function updateByIds($sjId, $year, $month, $data)
- {
- xhStatIncomeMonth::updateByCondition(['year' => $year, 'month' => $month, 'sjId' => $sjId], $data);
- self::getByIds($sjId, $year, $month);
- $cacheKey = dict::getCacheKey('xhStatIncomeMonth') . '_' . $sjId . '_' . $year . '_' . $month;
- $params = [$cacheKey];
- if (!empty($data)) {
- foreach ($data as $key => $val) {
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET', $params);
- return self::getByIds($sjId, $year, $month);
- }
- public static function add($data)
- {
- $return = xhStatIncomeMonth::add($data);
- return $return;
- }
- public static function getByIds($sjId, $year, $month)
- {
- $income = xhStatIncomeMonth::find()->where(['sjId' => $sjId, 'year' => $year, 'month' => $month])->asArray()->one();
- return $income;
- }
- public static function getByTime($sjId, $time)
- {
- $income = xhStatIncomeMonth::find()->where(['sjId' => $sjId, 'time' => $time])->asArray()->one();
- return $income;
- }
- /**
- * 获取本月收入
- */
- public static function getMonthIncome($sjId, $year, $month)
- {
- $income = self::getByIds($sjId, $year, $month);
- $amount = 0;
- if (!empty($income)) {
- $amount = $income['amount'];
- }
- return $amount;
- }
- }
|