<?php
namespace app\workerapi\logic;
use app\common\enum\worker\WorkerAccountLogEnum;
use app\common\logic\BaseLogic;
use app\common\model\finance\MasterWorkerCaseOutLog;
use app\common\model\master_worker\MasterWorker;
use app\common\model\master_worker\MasterWorkerAccountLog;

/**
 * @author 林海涛
 * @date 2024/7/22 下午4:31
 */
class AccountLogic extends BaseLogic
{

    /**
     * 我的账户总资产
     * @param $params
     * @return false|int[]
     * @author 林海涛
     * @date 2024/7/22 下午5:39
     */
    public static function totalAssets($params)
    {
        try{
            $yDay = date("Y-m-d",strtotime("-1 day"));
            $ytime = strtotime("$yDay 00:00:00");//昨天开始时间戳
            $yetime = strtotime("$yDay 23:59:59");//昨天开始时间戳
            $data= ['user_money' =>0,'account_yesterday' => 0,'case_out_money' => 0];
            $data['user_money'] = MasterWorker::where('id',$params['worker_id'])->value('user_money') ?? 0;
            $where = [];
            $where[] = ['worker_id','=',$params['worker_id']] ;
            $where[] = ['change_type', '=', WorkerAccountLogEnum::UM_INC_ADMIN];
            $where[] = ['action', '=',WorkerAccountLogEnum::INC];
            $where[] = ['create_time','between',[$ytime,$yetime]];
            $data['account_yesterday']= MasterWorkerAccountLog::where($where)
                ->sum('change_amount');
            $data['case_out_money']= MasterWorkerCaseOutLog::where([
                'worker_id'=>$params['worker_id'],
                'review_status' => 1,
            ])->sum('change_amount');
            return $data;
        } catch(\Exception $e){
            self::setError($e->getMessage());
            return false;
        }
    }

    public static function monthAccountWithCaseOutTotal($params)
    {
        try{
            $month = $params['month'];
            $firstDay = date("Y-m-d 00:00:00", strtotime("first day of {$month}"));
            $lastDay = date("Y-m-d 23:59:59", strtotime("{$month} +1 month -1 day"));
            $firstTime = strtotime($firstDay);
            $lastTime = strtotime($lastDay);
            $where = [];
            $where[] = ['worker_id','=',$params['worker_id']] ;
            $where[] = ['create_time','between',[$firstTime,$lastTime]];
            $data= [];
            $incAccountWhere[] = $where+[
                ['change_type', '=', WorkerAccountLogEnum::UM_INC_ADMIN],
                ['action', '=',WorkerAccountLogEnum::INC],
                ];
            $data['account_amount_inc_total'] = MasterWorkerAccountLog::where($incAccountWhere)
                ->sum('change_amount');
            $decAccountWhere[] = $where+[
                    ['change_type', '=', WorkerAccountLogEnum::UM_DEC_ADMIN],
                    ['action', '=',WorkerAccountLogEnum::DEC],
                ];
            $data['account_amount_dec_total'] = MasterWorkerAccountLog::where($decAccountWhere)
                ->sum('change_amount');
            $data['account_amount_total'] =  $data['account_amount_inc_total'] - $data['account_amount_dec_total'];
            $caseOutWhere = $where+[
                    ['review_status','=',1]
                ];
            $data['case_out_total'] = MasterWorkerCaseOutLog::where($caseOutWhere)->sum('change_amount');
            return $data;
        }catch(\Exception $e){
            self::setError($e->getMessage());
            return false;
        }
    }
}