<?php
/**
 * @author 林海涛
 * @date 2024/7/28 上午11:30
 */
namespace app\workerapi\lists;
use app\common\model\master_worker\MasterWorkerRetentionMoneyLog;

class MasterWorkerRetentionMoneyLogLists extends BaseWorkerDataLists
{

    public function queryWhere()
    {
        $where = [];
        if(isset($this->params['action_freeze']) && !empty($this->params['action_freeze'])){
            switch ($this->params['action_freeze']){
                case 1:
                    $where[] = ['action','=',1];
                    $where[] = ['to_freeze','=',0];
                    break;
                case 2:
                    $where[] = ['action','=',2];
                    $where[] = ['to_freeze','=',0];
                    break;
                case 3:
                    $where[] = ['to_freeze','=',1];
                    break;
                case 4:
                    $where[] = ['to_freeze','=',2];
                    break;
                default:
                    break;
            }
        }
        if(isset($this->params['worker_id'])){
           $where[]= ['worker_id','=',$this->params['worker_id']];
        } else{
            $where[]= ['worker_id','=',$this->userId];
        }
        if(isset($this->params['create_time']) && !empty($this->params['create_time'])){
            $time = [strtotime($this->params['create_time'][0]), strtotime($this->params['create_time'][1])];
            $where[] = ['create_time', 'between', $time];
        }
        if(isset($this->params['month']) && !empty($this->params['month'])){
            $firstDay =date("Y-m-d 00:00:00", strtotime("first day of {$this->params['month']}"));
            $lastDay = date("Y-m-d 23:59:59", strtotime("{$this->params['month']} +1 month -1 day"));
            $firstTime = strtotime($firstDay);
            $lastTime = strtotime($lastDay);
            $where[] = ['create_time','between',[$firstTime,$lastTime]];
        }
        return $where;
    }

    public function lists(): array
    {
        $lists = MasterWorkerRetentionMoneyLog::where($this->searchWhere)
            ->where($this->queryWhere())
            ->append(['action_text'])
            ->field(['*'])
            ->order(['id' => 'desc'])
            ->select()
            ->toArray();
        foreach ($lists as &$item) {
            $item['amount'] = $item['action_text'] . $item['amount'];
            $item['action_name'] = empty($item['to_freeze'])?($item['action']==1?'缴纳质保金':'扣除质保金'):($item['to_freeze']==1?'冻结质保金':'解冻质保金');
        }
        return $lists;
    }

    public function count(): int
    {
        return MasterWorkerRetentionMoneyLog::where($this->queryWhere())->count();
    }
}