<?php
namespace app\api\lists\property;

use app\api\lists\BaseApiDataLists;
use app\common\lists\ListsSearchInterface;
use app\common\model\group_activity\GroupActivityCategory;
use app\common\model\property\PropertyHead;

/**
 * 代理人拼团活动列表
 * Class PropertyGroupActivityLists
 * @package app\api\lists\property
 */
class PropertyGroupActivityLists extends BaseApiDataLists implements ListsSearchInterface
{
    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/07/07 18:37
     */
    public function setSearch(): array
    {
        return [
            '=' => [],
        ];
    }
    public function queryWhere()
    {
        // 指定用户
        $propertyHeadId = (int)PropertyHead::where('user_id',$this->userId)->value('id');
        $where[] = ['property_head_id', '=', $propertyHeadId];
        $where[] = ['status','=',1];
        if (isset($this->params['start_time']) && !empty($this->params['start_time'])) {
            $monthStart = strtotime(date("Y-m-01",strtotime($this->params['start_time'])));
            $monthEnd = strtotime(date("Y-m-t 23:59:59",strtotime($this->params['start_time'])));
            $where[] = ['start_time', 'between', [$monthStart, $monthEnd]];
        }
        return $where;
    }
    
    /**
     * @notes 获取列表
     * @return array
     */
    public function lists(): array
    {
        $lists = GroupActivityCategory::where($this->queryWhere())
            ->field('id,title,start_time,end_time,participant_num')
            ->limit($this->limitOffset, $this->limitLength)
            ->order('start_time', 'desc')
            ->select()
            ->toArray();
        foreach($lists as &$item) {
            $item['start_time'] = date('Y-m-d', strtotime($item['start_time']));
            $item['end_time'] = date('m-d', $item['end_time']);
        }
        return $lists;
    }

    /**
     * @notes  获取数量
     * @return int
     */
    public function count(): int
    {
        return GroupActivityCategory::where($this->queryWhere())->count();
    }

}