MoneyLog.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\common\model;
  4. use think\Model;
  5. class MoneyLog Extends Model
  6. {
  7. // 自动写入时间戳字段
  8. protected $autoWriteTimestamp = true;
  9. protected $createTime = 'createtime';
  10. protected $updateTime = 'updatetime';
  11. protected $type = [
  12. 'createtime' => 'timestamp:Y-m-d H:i',
  13. 'updatetime' => 'timestamp:Y-m-d H:i',
  14. ];
  15. const TYPE_INCOME = 1;
  16. const TYPE_EXPENDITURE = 2;
  17. const STATUS_HIDDEN= 0; //隐藏
  18. const STATUS_NORMAL = 1; //正常
  19. //
  20. public function users()
  21. {
  22. return $this->hasOne(User::class,'id','user_id')->field('id,nickname');
  23. }
  24. public function stockconfig()
  25. {
  26. return $this->hasOne(StockConfig::class,'id','variety_id')->field('id,title');
  27. }
  28. //按照类型月统计 收入- 支出
  29. public static function getCountMonthBalance(string $time = '')
  30. {
  31. $income = self::where('type', self::TYPE_INCOME)->where('status', self::STATUS_NORMAL);
  32. if(!empty($time)){
  33. $income = $income->whereMonth('create_date', $time);
  34. }
  35. $income = $income->sum('change');
  36. //支出
  37. $expenditure = self::where('type', self::TYPE_EXPENDITURE)->where('status', self::STATUS_NORMAL);
  38. if(!empty($time)){
  39. $expenditure = $expenditure->whereMonth('create_date', $time);
  40. }
  41. $expenditure = $expenditure->sum('change');
  42. $balance = $income - $expenditure;
  43. return [
  44. 'balance' => round($balance ,2),
  45. 'income' => round($income,2),
  46. 'expenditure' => round($expenditure,2)
  47. ];
  48. }
  49. //按照类型年统计 收入- 支出
  50. public static function getCountYearBalance(string $time = '')
  51. {
  52. $income = self::where('type', self::TYPE_INCOME)->where('status', self::STATUS_NORMAL);
  53. if(!empty($time)){
  54. $income = $income->whereYear('create_date', $time);
  55. }
  56. $income = $income->sum('change');
  57. //支出
  58. $expenditure = self::where('type', self::TYPE_EXPENDITURE)->where('status', self::STATUS_NORMAL);
  59. if(!empty($time)){
  60. $expenditure = $expenditure->whereYear('create_date', $time);
  61. }
  62. $expenditure = $expenditure->sum('change');
  63. $balance = $income - $expenditure;
  64. return [
  65. 'balance' => round($balance,2),
  66. 'income' => round($income,2),
  67. 'expenditure' => round($expenditure,2)
  68. ];
  69. }
  70. //按照类型天统计 收入- 支出
  71. public static function getCountDayBalance(string $time = '')
  72. {
  73. $list = self::where('status', self::STATUS_NORMAL);
  74. if(!empty($time)) $list = $list->whereMonth('create_date', $time);
  75. $list = $list->group('create_date')
  76. ->field('create_date, sum(if(type=1,`change`,0)) as income, sum(if(type=2,`change`, 0)) as expenditure')
  77. ->select();
  78. return $list;
  79. }
  80. //支收总览
  81. public static function getCountAllBalance(int $type, string $time = '', string $user_ids='')
  82. {
  83. $list = self::where('status', self::STATUS_NORMAL);
  84. if($type == 1 && !empty($time)) $list = $list->whereMonth('create_date', $time);
  85. if($type == 2 && !empty($time)) $list = $list->whereYear('create_date', $time);
  86. if($type == 3 && !empty($time)){
  87. $expTime = explode(',', $time);
  88. $list = $list->whereTime('create_date', [$expTime[0], $expTime[1]]);
  89. }
  90. if(!empty($user_ids)) $list = $list->whereIn('user_id', $user_ids);
  91. $list = $list
  92. ->field('type, sum(if(type=1,`change`,0)) as income, sum(if(type=2,`change`, 0)) as expenditure')
  93. ->group('type')
  94. ->select();
  95. return $list;
  96. }
  97. //统计用户余额
  98. public static function getCountAllUserBalance(int $type, string $time = '', string $user_ids='')
  99. {
  100. $list = self::alias('a')
  101. ->join('user b', 'a.user_id=b.id')
  102. ->where('a.status', self::STATUS_NORMAL);
  103. if($type == 1 && !empty($time)) $list = $list->whereMonth('create_date', $time);
  104. if($type == 2 && !empty($time)) $list = $list->whereYear('create_date', $time);
  105. if($type == 3 && !empty($time)){
  106. $expTime = explode(',', $time);
  107. $list = $list->whereTime('create_date', [$expTime[0], $expTime[1]]);
  108. }
  109. if(!empty($user_ids)) $list = $list->whereIn('user_id', $user_ids);
  110. $list = $list
  111. ->field('a.user_id, sum(if(a.type=1,`change`,0)) as income, sum(if(a.type=2,`change`, 0)) as expenditure,b.nickname,b.avatar')
  112. ->group('user_id')
  113. ->select();
  114. return $list;
  115. }
  116. //统计所有月统计
  117. public static function getCountAllMonthBalance(int $type, string $time = '', string $user_ids='')
  118. {
  119. $list = self::where('status', self::STATUS_NORMAL);
  120. if($type == 1 && !empty($time)) $list = $list->whereMonth('create_date', $time);
  121. //年
  122. if($type == 2 && !empty($time)) $list = $list->whereYear('create_date', $time);
  123. //自定义
  124. if($type == 3 && !empty($time)){
  125. $expTime = explode(',', $time);
  126. $list = $list->whereTime('create_date', [$expTime[0], $expTime[1]]);
  127. }
  128. if(!empty($user_ids)) $list = $list->whereIn('user_id', $user_ids);
  129. //月
  130. $list = $list
  131. ->field('create_month, sum(if(type=1,`change`,0)) as income, sum(if(type=2,`change`, 0)) as expenditure')
  132. ->group('create_month')
  133. ->order('create_month desc')
  134. ->select();
  135. return $list;
  136. }
  137. }