<?php
namespace app\api\lists;

use app\common\model\equity\UserEquity;

class UserEquityLists  extends BaseApiDataLists
{
   public function queryWhere()
   {
       $where = [];
       $where[] = ['user_id','=',$this->userId];
       $where[] = ['number','>',0];
       $where[] = ['end_time','>=',time()];

       if(isset($this->params['goods_id']) && !empty($this->params['goods_id'])){
           $where[] = ['goods_id','=',$this->params['goods_id']];
       }
       return $where;
   }

   public function  lists():array
   {
       $list = UserEquity::with(['equityConfig'=>function ($query) {
                   $query->field(['*']);
               },'goods'=>function ($query) {
                   $query->field(['id','goods_image','goods_name','goods_banners','good_unit','sell_num','base_service_fee',
                       'service_total','service_fee','service_image','fee_schedule','warranty_period','goods_payment_type','goods_category_id']);
               }])->where($this->queryWhere())
           ->field(['id','user_id','equity_id','goods_id','number','end_time'])
           ->limit($this->limitOffset, $this->limitLength)
           ->order('end_time')
           ->select()
           ->each(function ($item) {
               $item->end_time = date('Y-m-d H:i:s',$item['end_time']);
           })
           ->toArray();
       /*foreach ($list as &$item) {
           $item['all_num'] = $item['equityConfig']['number'];
           $item['price'] = $item['equityConfig']['price'];
           $item['equity_name'] = $item['equityConfig']['equity_name'];
           $item['goods_name'] = $item['goods']['goods_name'];
       }*/
       return $list;
   }

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