<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\api\lists\recharge;

use app\api\lists\BaseApiDataLists;
use app\common\enum\PayEnum;
use app\common\enum\WorkEnum;
use app\common\model\recharge\RechargeOrder;
use app\common\model\works\ServiceWork;

/**
 * 服务订单列别
 * Class RechargeLists
 * @package app\api\lists\recharge
 */
class ServiceOrderLists extends BaseApiDataLists
{
    protected $count = 0;

    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function lists(): array
    {
        $where = [];
        $service_status = $this->params['service_status'];
        $work_ids = ServiceWork::where(['user_id' => $this->userId])->where(function ($query) use($service_status) {
            if($service_status != 'all'){
                $query->where(['service_status' => $service_status]);
            }
        })->column('id');
        if($service_status != 'all'){
            $where['pay_status'] = $service_status==4?2:1;
        }
        $work_ids = !empty($work_ids)?implode(',',$work_ids):'999999';

        $lists = RechargeOrder::with(['order_goods'=>function ($query) {
            $query->visible(['goods_name','goods_image','goods_number','good_unit']);
        },'service_work'=>function ($query) {
            $query->visible(['work_sn','service_status','work_status','user_confirm_status','appointment_time'])->append(['service_status_text','work_status_text','user_confirm_status_text','user_service_status','user_service_status_text']);
        }])
            ->where($where)
            ->whereIn('work_id',$work_ids)
            ->field('id, sn, payment_type, order_total,work_id, pay_status, create_time, SUM(order_amount) as order_amount')
            ->visible(['id','sn','payment_type','order_total','pay_status','create_time','order_amount'])
            ->where([
                'order_type' => 0,
                'user_id' => $this->userId,
            ])
            ->limit($this->limitOffset, $this->limitLength)
            ->group('work_id')
            ->order('id', 'desc')
            ->select()->each(function ($item) {
                //用户按钮状态显示
            })
            ->toArray();

        $this->count = RechargeOrder::where([
                'order_type' => 0,
                'user_id' => $this->userId,
            ])
            ->where($where)
            ->whereIn('work_id',$work_ids)
            ->group('work_id')
            ->count();

        return $lists;
    }


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

}