StatIncomeUsageService.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace biz\stat\services;
  3. use biz\base\services\BaseService;
  4. use biz\goods\services\UsageRelationService;
  5. use common\components\business;
  6. use common\components\stringUtil;
  7. use common\components\validate;
  8. use common\components\util;
  9. use Yii;
  10. class StatIncomeUsageService extends BaseService
  11. {
  12. public static $baseFile = '\biz\stat\classes\StatIncomeUsageClass';
  13. //添加更新记录 shish 2010.8.20
  14. public static function replaceData($data)
  15. {
  16. $valid = ['usageId', 'amount', 'merchantId', 'payWay'];
  17. validate::easyCheck($valid, $data);
  18. $time = date("Ymd");
  19. if (isset($data['time'])) {
  20. //如果传time参数必须是时间戳
  21. if (strtotime(date('Y-m-d H:i:s', $data['time'])) != $data['time']) {
  22. util::fail('time不是时间戳');
  23. }
  24. $time = date("Ymd", $data['time']);
  25. $data['time'] = $time;
  26. }
  27. $merchantId = $data['merchantId'];
  28. $usageId = $data['usageId'];
  29. $amount = $data['amount'];
  30. $payWay = $data['payWay'];
  31. $income = self::getByCondition(['merchantId' => $merchantId, 'time' => $time, 'usageId' => $usageId], true);
  32. if (empty($income)) {
  33. $income = self::add($data, true);
  34. } else {
  35. $income->amount += $data['amount'];
  36. }
  37. business::savePayWayAmount($income, $amount, $payWay);
  38. /**每个月增加**/
  39. StatIncomeUsageMonthService::replaceData($data);
  40. $income->save();
  41. }
  42. public static function getLatestIncome($start, $end)
  43. {
  44. $merchantId = Yii::$app->params['merchantId'];
  45. $list = self::getAllList('usageId,amount', ['merchantId' => $merchantId, 'time' => ['between', [$start, $end]]], 'time asc');
  46. $totalAmount = 0;
  47. $usageList = UsageRelationService::getMyUsage();
  48. $data = [];
  49. foreach ($usageList as $usageId => $category) {
  50. $data[$usageId]['name'] = $category['showName'];
  51. $data[$usageId]['value'] = 0;
  52. }
  53. if (!empty($list)) {
  54. foreach ($list as $val) {
  55. $usageId = $val['usageId'];
  56. $amount = $val['amount'];
  57. $data[$usageId]['value'] = stringUtil::calcAdd($data[$usageId]['value'], $amount);
  58. $totalAmount = stringUtil::calcAdd($totalAmount, $amount);
  59. }
  60. }
  61. $usageName = array_column($usageList, 'showName');
  62. return ['data' => array_values($data), 'usageName' => $usageName, 'totalAmount' => $totalAmount];
  63. }
  64. }