<?php

namespace app\middleapi\controller;

use app\adminapi\lists\finance\WithdrawLists;
use app\common\logic\finance\WithdrawLogic;
use app\common\model\user\Withdraw;
use think\Exception;
use app\common\controller\BaseLikeAdminController;

class WithdrawController extends BaseLikeAdminController
{

    public function index()
    {
        if(!$this->request->isPost()){
            return $this->fail('请求方式错误');
        }
        $params = $this->request->post(['page_no','page_size', 'order_sn', 'status']);
        $where = [];
        if (!empty($params['order_sn'])) {
            $where[] = ['order_sn', '=', $params['order_sn']];
        }
        if (isset($params['status']) && $params['status'] != '') {
            $where[] = ['status', '=', $params['status']];
        }
        $pageNo = !empty($params['page_no']) ? $params['page_no'] : 1;
        $pageSize = !empty($params['page_size']) ? $params['page_size'] : 20;
        $lists = Withdraw::where($where)
            ->append(['s_date', 'e_date', 'company_name'], true)
            ->with('user')
            ->withAttr('company_name', function ($value, $data) {
                $company = Company::where(['admin_id'=>$data['admin_id']])->find();
                return $company['company_name']??'';
            })
            ->withAttr('s_date', function ($value, $data) {
                $withdrawedCount = Withdraw::where(['user_id'=>$data['user_id'], 'status'=>3])->count();
                $company = Company::where(['admin_id'=>$data['admin_id']])->find();
                if ($withdrawedCount == 0) {
                    $firstUserLog = UserAccountLog::where(['company_id'=>$company['id'] ?? 0])->order('id', 'asc')->find();
                    return $firstUserLog['create_time'] ?? '';
                } else {
                    $withdrawedCount = Withdraw::where(['user_id'=>$data['user_id'], 'status'=>3])->order('id', 'desc')->find();
                    return $withdrawedCount['transfer_end_cycel'] ?? '';
                }
            })
            ->withAttr('e_date', function ($value, $data) {
                return date('Y-m-d H:i:s', $data['transfer_end_cycel']);
            })
            ->order('id', 'desc')
            ->page($pageNo, $pageSize)
            ->select()
            ->toArray();
        $count = Withdraw::where($where)->count();
        $result = [
            'lists' => $lists,
            'count' => $count,
            'page_no' => $pageNo,
            'page_size' => $pageSize
        ];
        return $this->success('请求成功',$result);
    }

    public function update()
    {
        $id = $this->request->param('id');
        $status = $this->request->param('status');
        if (empty($id)) {
            return $this->fail('参数id不能为空');
        }
        if (!in_array($status, [1, 2, 3])) {
            return $this->fail('参数status错误');
        }
        $data = Withdraw::find($id);
        if (empty($data)) {
            return $this->fail('数据不存在');
        }
        $data->status = $status;
        $data->udpate_time = time();
        $data->save();
        return $this->success('操作成功', [], 1, 1);
    }

    /**
     * 提现申请审核
     */
    public function audit()
    {
        try {
            $params = $this->request->param();
            if (empty($params['id'])) {
                return $this->fail('参数id不能为空');
            }
            if (empty($params['status'])) {
                return $this->fail('参数status不能为空');
            }
            $re = WithdrawLogic::audit($params);
            if (!$re) {
                return $this->fail(WithdrawLogic::getError());
            }
            return $this->success('操作成功', [], 1, 1);
        } catch (Exception $exception) {
            return $this->fail($exception->getMessage());
        }
    }
}