MoneyLog.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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' => $balance,
  45. 'income' => $income,
  46. 'expenditure' => $expenditure
  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' => $balance,
  66. 'income' => $income,
  67. 'expenditure' => $expenditure
  68. ];
  69. }
  70. }