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

use app\api\lists\BaseApiDataLists;
use app\common\lists\ListsExtendInterface;
use app\common\lists\ListsSearchInterface;
use app\common\model\property\PropertyHead;
use app\common\model\works\GroupServiceWork;

/**
 * 代理人完结工单列表
 * Class PropertyServiceWorkLists
 * @package app\api\lists\property
 */
class PropertyServiceWorkLists extends BaseApiDataLists implements ListsSearchInterface, ListsExtendInterface
{
    /**
     * @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[] = ['b.property_head_id', '=', $propertyHeadId];
        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[] = ['a.appointment_time', 'between', [$monthStart, $monthEnd]];
        }
        $where[] = ['a.service_status', '=', 3];
        return $where;
    }
    
    /**
     * @notes 获取列表
     * @return array
     */
    public function lists(): array
    {
        $lists = GroupServiceWork::alias('a')
            ->leftJoin('group_order b','a.group_order_id=b.id')
            ->leftJoin('user c','a.user_id=c.id')
            ->field('a.id,a.work_sn,a.title,a.appointment_time,a.work_total,c.avatar as image')
            ->where($this->queryWhere())
            ->limit($this->limitOffset, $this->limitLength)
            ->order('a.appointment_time', 'desc')
            ->select()
            ->toArray();

        foreach($lists as &$item) {
            $item['appointment_time'] = date('n月j日', strtotime($item['appointment_time']));
        }
        return $lists;
    }

    /**
     * @notes  获取数量
     * @return int
     */
    public function count(): int
    {
        return GroupServiceWork::alias('a')
            ->leftJoin('group_order b','a.group_order_id=b.id')
            ->where($this->queryWhere())
            ->count();
    }

    /**
     * @notes 统计工单总收益
     * @return float
     */
    public function extend(): array
    {
        $totalWorkTotal = GroupServiceWork::alias('a')
                            ->leftJoin('group_order b','a.group_order_id=b.id')
                            ->where($this->searchWhere)
                            ->where($this->queryWhere())
                            ->sum('a.work_total');

        $totalWorkAmount = GroupServiceWork::alias('a')
                            ->leftJoin('group_order b','a.group_order_id=b.id')
                            ->where($this->searchWhere)
                            ->where($this->queryWhere())
                            ->sum('a.work_amount');
        return ["total_amount" => $totalWorkTotal - $totalWorkAmount];
    }

}