'timestamp:Y-m-d H:i', 'updatetime' => 'timestamp:Y-m-d H:i', ]; const TYPE_INCOME = 1; const TYPE_EXPENDITURE = 2; const STATUS_HIDDEN= 0; //隐藏 const STATUS_NORMAL = 1; //正常 // public function users() { return $this->hasOne(User::class,'id','user_id')->field('id,nickname'); } public function stockconfig() { return $this->hasOne(StockConfig::class,'id','variety_id')->field('id,title'); } //按照类型月统计 收入- 支出 public static function getCountMonthBalance(string $time = '') { $income = self::where('type', self::TYPE_INCOME)->where('status', self::STATUS_NORMAL); if(!empty($time)){ $income = $income->whereMonth('create_date', $time); } $income = $income->sum('change'); //支出 $expenditure = self::where('type', self::TYPE_EXPENDITURE)->where('status', self::STATUS_NORMAL); if(!empty($time)){ $expenditure = $expenditure->whereMonth('create_date', $time); } $expenditure = $expenditure->sum('change'); $balance = $income - $expenditure; return [ 'balance' => $balance, 'income' => $income, 'expenditure' => $expenditure ]; } //按照类型年统计 收入- 支出 public static function getCountYearBalance(string $time = '') { $income = self::where('type', self::TYPE_INCOME)->where('status', self::STATUS_NORMAL); if(!empty($time)){ $income = $income->whereYear('create_date', $time); } $income = $income->sum('change'); //支出 $expenditure = self::where('type', self::TYPE_EXPENDITURE)->where('status', self::STATUS_NORMAL); if(!empty($time)){ $expenditure = $expenditure->whereYear('create_date', $time); } $expenditure = $expenditure->sum('change'); $balance = $income - $expenditure; return [ 'balance' => $balance, 'income' => $income, 'expenditure' => $expenditure ]; } }