StatOutMonthClass.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * User: admin
  4. * Date Time: 2021/4/13 21:37
  5. */
  6. namespace biz\stat\classes;
  7. use biz\base\classes\BaseClass;
  8. use common\components\dateUtil;
  9. class StatOutMonthClass extends BaseClass
  10. {
  11. public static $baseFile = '\biz\stat\models\StatOutMonth';
  12. public static function updateOrInsert($sjId,$shopId,$amount)
  13. {
  14. $time = date('Ym');
  15. $year = date('Y');
  16. $month = date('m');
  17. $where = [
  18. 'merchantId'=>$sjId,
  19. 'shopId'=>$shopId,
  20. 'year'=>$year,
  21. 'month'=>$month,
  22. 'time'=>$time,
  23. ];
  24. $model = self::getByCondition($where);
  25. if($model){
  26. $id = $model['id'];
  27. //累加金额 amount
  28. $newAmount = bcadd($amount,$model['amount'],2);
  29. $totalAmount = bcadd($amount,$model['totalAmount'],2);
  30. self::updateById($id,['amount'=>$newAmount,'totalAmount'=>$totalAmount]);
  31. }else{
  32. //insert
  33. $data = $where;
  34. $data['amount'] = $amount;
  35. //totalAmount = 前一天的 totalAmount
  36. $totalAmount = 0;
  37. $lastTime = date('Ym',strtotime("-1 month"));
  38. $lastWhere = [
  39. 'merchantId'=>$sjId,
  40. 'shopId'=>$shopId,
  41. 'time'=>$lastTime,
  42. ];
  43. $lastModel = self::getByCondition($lastWhere);
  44. if($lastModel){
  45. $totalAmount = $lastModel['totalAmount'];
  46. }
  47. $data['totalAmount'] = bcadd($amount,$totalAmount,2);
  48. $data['createTime'] = date('Y-m-d H:i:s');
  49. self::add($data);
  50. }
  51. }
  52. }