<?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\PropertyCommission;
use app\common\model\property\PropertyHead;
use app\common\model\property\PropertyOrder;
use app\common\model\property\PropertySurplusLog;
use app\common\model\works\ServiceWork;
use DateTime;

/**
 * 订单列表
 */
class PropertyOrderLists extends BaseApiDataLists implements ListsSearchInterface,ListsExtendInterface
{

    public function setSearch(): array
    {
        return [
            '=' => ['id','order_status'],
        ];
    }
    public function queryWhere()
    {
        // 指定用户
        $propertyHeadId = PropertyHead::where('user_id',$this->userId)->value('id');
        $where[] = ['property_head_id', '=', $propertyHeadId];
        return $where;
    }
    public function lists(): array
    {
        $lists = PropertyOrder::with(['propertyHead','propertyUser','propertyWork'])->where($this->searchWhere)
            ->where($this->queryWhere())
            ->limit($this->limitOffset, $this->limitLength)
            ->field(['id','property_head_id','property_user_id','remark','order_status','work_id','create_time','update_time'])
            ->order('create_time desc')
            ->select()
            ->toArray();
        foreach ($lists as &$item){
            $item['householder_name'] = $item['propertyUser']?$item['propertyUser']['householder_name']:'';
            $item['householder_mobile'] = $item['propertyUser']?$item['propertyUser']['householder_mobile']:'';
            $item['work_address'] = $item['propertyWork']?$item['propertyWork']['address']:'';
            $item['work_create_time'] = $item['propertyWork']?$item['propertyWork']['create_time']:'';
        }
        return $lists;
    }

    /**
     * @notes  获取数量
     * @return int
     */
    public function count(): int
    {
        return PropertyOrder::where($this->searchWhere)->where($this->queryWhere())->count();
    }
    /**
     * @notes  返回扩展数据
     * @return array|int
     */
    public function extend(): array
    {
        $workIds = PropertyOrder::where('order_status',3)->where($this->queryWhere())->column('work_id');
        if(empty($workIds)){
            return ['month_amount' => 0,'month_num' => 0];
        }
        $startDateTime = strtotime(date('Y-m-01'));
        if(isset($this->params['start_month']) && $this->params['start_month']){
            $startDateTime = strtotime($this->params['start_month']);
        }
        $endDateTime = strtotime('+1 month', strtotime($startDateTime))-1;
        $serviceWork = ServiceWork::where([['id','in',$workIds],['work_pay_status','=',2],['approval','=',1]])
            ->where('user_id',$this->userId)
            ->where('create_time','BETWEEN',[$startDateTime,$endDateTime]);
        return [
            'month_amount' =>$serviceWork->sum('work_amount'),
            'month_num' => $serviceWork->count('id')
        ];
    }
}