<?php

namespace app\middleapi\controller;

use app\adminapi\logic\finance\RefundLogic;
use app\common\model\refund\RefundRecord;
use app\common\controller\BaseLikeAdminController;
use app\common\service\FileService;

/**
 * 退款控制器
 * Class RefundController
 * @package app\adminapi\controller\finance
 */
class RefundController extends BaseLikeAdminController
{

    /**
     * @notes 退还统计
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 段誉
     * @date 2023/3/3 12:10
     */
    public function stat()
    {
        $result = RefundLogic::stat();
        return $this->success('', $result);
    }


    /**
     * @notes 退款记录
     * @return \think\response\Json
     * @author 段誉
     * @date 2023/3/1 9:47
     */
    public function record()
    {
        if(!$this->request->isPost()){
            return $this->fail('请求方式错误');
        }
        $params = $this->request->post(['page_no','page_size', 'user_info', 'start_time', 'end_time', 'sn', 'order_sn', 'refund_type', 'refund_status']);
        $where = [];
        if (!empty($params['sn'])) {
            $where[] = ['r.sn', '=', $params['sn']];
        }
        if (!empty($params['order_sn'])) {
            $where[] = ['r.order_sn', '=', $params['order_sn']];
        }
        if (!empty($params['refund_type'])) {
            $where[] = ['r.refund_type', '=', $params['refund_type']];
        }
        if (!empty($params['user_info'])) {
            $where[] = ['u.sn|u.nickname|u.mobile', 'like', '%' . $params['user_info'] . '%'];
        }
        if (!empty($params['start_time']) && !empty($params['end_time'])) {
            $where[] = ['r.create_time', 'between', [strtotime($params['start_time']), strtotime($params['end_time'])]];
        }
        if (isset($params['refund_status']) && $params['refund_status'] != '') {
            $where[] = ['r.refund_status', '=', $params['refund_status']];
        }
        $pageNo = !empty($params['page_no']) ? $params['page_no'] : 1;
        $pageSize = !empty($params['page_size']) ? $params['page_size'] : 20;
        $lists = (new RefundRecord())->alias('r')
            ->field('r.*,u.nickname,u.avatar')
            ->join('user u', 'u.id = r.user_id')
            ->order(['r.id' => 'desc'])
            ->where($where)
            ->page($pageNo, $pageSize)
            ->append(['refund_type_text', 'refund_status_text', 'refund_way_text'])
            ->select()
            ->toArray();

        foreach ($lists as &$item) {
            $item['avatar'] = FileService::getFileUrl($item['avatar']);
        }
        $count = (new RefundRecord())->alias('r')
            ->field('r.*,u.nickname,u.avatar')
            ->join('user u', 'u.id = r.user_id')
            ->order(['r.id' => 'desc'])
            ->where($where)->count();
        $result = [
            'lists' => $lists,
            'count' => $count,
            'page_no' => $pageNo,
            'page_size' => $pageSize
        ];
        return $this->success('请求成功',$result);
    }


    /**
     * @notes 退款日志
     * @return \think\response\Json
     * @author 段誉
     * @date 2023/3/1 9:47
     */
    public function log()
    {
        $params = $this->request->post(['record_id']);
        $recordId = $params['record_id'] ?? 0;
        $result = RefundLogic::refundLog($recordId);
        return $this->success('', $result);
    }

}