'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' => round($balance ,2), 'income' => round($income,2), 'expenditure' => round($expenditure,2) ]; } //按照类型年统计 收入- 支出 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' => round($balance,2), 'income' => round($income,2), 'expenditure' => round($expenditure,2) ]; } //按照类型天统计 收入- 支出 public static function getCountDayBalance(string $time = '') { $list = self::where('status', self::STATUS_NORMAL)->whereMonth('create_date', $time) ->group('create_date') ->field('create_date, sum(if(type=1,`change`,0)) as income, sum(if(type=2,`change`, 0)) as expenditure') ->select(); return $list; } }