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

use think\facade\Db;
use app\api\lists\BaseApiDataLists;
use app\common\lists\ListsSearchInterface;
use app\common\model\property\PropertyHead;
use app\common\model\group_activity\GroupUserOrder;

/**
 * 代理人拼团订单列表
 * Class PropertyGroupOrderLists
 * @package app\api\lists\property
 */
class PropertyGroupOrderLists 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[] = ['b.property_head_id', '=', $propertyHeadId];

        if (isset($this->params['status'])) {
            if ($this->params['status'] == 0) {
                //待完成
                $where[] = ['a.status', '=', 1];
                $where[] = ['c.service_status', 'exp', Db::Raw('IS NULL OR c.service_status <> 3')];
            } elseif ($this->params['status'] == 1) {
                //工单已完成
                $where[] = ['c.service_status', '=', 3];
            } else {
                //已退款
                $where[] = ['a.status', 'in', [2,3]];
            }
        }
        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.create_time', 'between', [$monthStart, $monthEnd]];
        }
        return $where;
    }
    
    /**
     * @notes 获取列表
     * @return array
     */
    public function lists(): array
    {
        $lists = GroupUserOrder::alias('a')
            ->leftJoin('group_order b','a.group_order_id=b.id')
            ->leftJoin('group_service_work c','a.id=c.group_user_order_id')
            ->leftJoin('group_activity d','a.group_activity_id=d.id')
            ->field('a.id,a.status,a.order_amount,a.mobile,a.area,a.address,a.create_time,a.group_activity_id,a.num,c.service_status,d.title')
            ->where($this->queryWhere())
            ->limit($this->limitOffset, $this->limitLength)
            ->order('a.create_time', 'desc')
            ->select()
            ->toArray();
        foreach($lists as &$item) {
            if ($item['status'] == 0) {
                $item['status_txt'] = '未支付';
            } else if ($item['status'] == 2 || $item['status'] == 3) {
                $item['status_txt'] = '已退款';
            } else if ($item['status'] == 4) {
                $item['status_txt'] = '已取消';
            } else if ($item['service_status'] == 3) {
                $item['status_txt'] = '已完成';
            } else {
                $item['status_txt'] = '待完成';
            }
        }
        return $lists;
    }

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

}