| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- declare(strict_types=1);
- namespace app\common\model;
- use think\Model;
- class MoneyLog Extends Model
- {
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = true;
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $type = [
- 'createtime' => 'timestamp:Y-m-d H:i',
- 'updatetime' => 'timestamp:Y-m-d H:i',
- ];
- const TYPE_INCOME = 1;
- const TYPE_EXPENDITURE = 2;
- //
- 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(int $user_id = 0, string $time = '')
- {
- $income = self::where('type', self::TYPE_INCOME);
- if(!empty($user_id) && !empty($time)){
- $income = $income->where('user_id', $user_id)->whereMonth('create_date', $time);
- }
- $income = $income->sum('change');
- //支出
- $expenditure = self::where('type', self::TYPE_EXPENDITURE);
- if(!empty($user_id) && !empty($time)){
- $expenditure = $expenditure->where('user_id', $user_id)->whereMonth('create_date', $time);
- }
- $expenditure = $expenditure->sum('change');
- $balance = $income - $expenditure;
- return [
- 'balance' => $balance,
- 'income' => $income,
- 'expenditure' => $expenditure
- ];
- }
- //按照类型年统计 收入- 支出
- public static function getCountYearBalance(int $user_id = 0, string $time = '')
- {
- $income = self::where('type', self::TYPE_INCOME);
- if(!empty($user_id) && !empty($time)){
- $income = $income->where('user_id', $user_id)->whereYear('create_date', $time);
- }
- $income = $income->sum('change');
- //支出
- $expenditure = self::where('type', self::TYPE_EXPENDITURE);
- if(!empty($user_id) && !empty($time)){
- $expenditure = $expenditure->where('user_id', $user_id)->whereYear('create_date', $time);
- }
- $expenditure = $expenditure->sum('change');
- $balance = $income - $expenditure;
- return [
- 'balance' => $balance,
- 'income' => $income,
- 'expenditure' => $expenditure
- ];
- }
- }
|