<?php

namespace app\common\model;

use think\facade\Db;


class Coupon extends BaseModel
{

    public $page_info;

    /**
     * 数量
     */
    public function getCardCount($condition)
    {
        return Db::name('sub_card')->where($condition)->count();
    }


    /**
     * 删除
     */
    public function delCard($condition)
    {
        return Db::name('sub_card')->where($condition)->delete();
    }


    /**
     * 获得卡券列表
     */
    public function getCardList($condition = '', $pagesize = '', $order = 'id desc')
    {
        $condition[] = ['card_no', 'like', "%" . input('param.input_card_no') . "%"];

        $cardStatus = input('param.card_status');
        if (!empty($cardStatus)) {
            $condition[] = ['card_status', '=', input('param.card_status')];
        }


        $userName = input('param.input_user_name');

        if (!empty($userName)) {
            $userList = Db::name('member')->where([
                ['member_nickname', 'like', "%{$userName}%"],
            ])->limit(10)->order('member_addtime desc')->select()->toArray();
            $uids     = array_column($userList, 'member_id');

            $condition[] = ['member_id', 'in', $uids];

        }


        if (input('param.input_datetime_end')) {
            $stime       = strtotime(input('param.input_datetime_start'));
            $condition[] = ['create_time', '>=', $stime];
        }
        if (input('param.input_datetime_end')) {
            $etime       = strtotime(input('param.input_datetime_end')) + 86399;
            $condition[] = ['create_time', '<=', $etime];
        }


        if ($pagesize) {
            $res             = Db::name('sub_card')->where($condition)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
            $this->page_info = $res;
            return $res->items();
        } else {
            return Db::name('sub_card')->where($condition)->order($order)->select()->toArray();
        }
    }


    /**
     * 获取卡券消费列表
     */
    public function getExpenseList($condition = '', $pagesize = '', $order = 'id desc')
    {
        $condition[] = ['expense_status', '<>', 2];

        //订单编号
        $orderSn = input('param.input_order_sn');
        if (!empty($orderSn)) {
            $orderList   = Db::name('order')->where([
                ['order_sn', 'like', "%{$orderSn}%"],
            ])->select()->toArray();
            $orderIds    = array_column($orderList, 'order_id');
            $condition[] = ['order_id', 'in', $orderIds];
        }


        //卡号
        $cardNo = input('param.input_card_no');
        if (!empty($cardNo)) {
            $cardList    = Db::name('sub_card')->where([
                ['card_no', 'like', "%{$cardNo}%"],
            ])->limit(10)->order('create_time desc')->select()->toArray();
            $cids        = array_column($cardList, 'id');
            $condition[] = ['card_id', 'in', $cids];
        }


        //用户名
        $userName = input('param.input_user_name');
        if (!empty($userName)) {
            $userList    = Db::name('member')->where([
                ['member_nickname', 'like', "%{$userName}%"],
            ])->limit(10)->order('member_addtime desc')->select()->toArray();
            $uids        = array_column($userList, 'member_id');
            $condition[] = ['member_id', 'in', $uids];

        }


        //开始和结束时间
        if (input('param.input_datetime_end')) {
            $stime       = strtotime(input('param.input_datetime_start'));
            $condition[] = ['expense_time', '>=', $stime];
        }
        if (input('param.input_datetime_end')) {
            $etime       = strtotime(input('param.input_datetime_end')) + 86399;
            $condition[] = ['expense_time', '<=', $etime];
        }


        if ($pagesize) {
            $res             = Db::name('sub_card_expense')->where($condition)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
            $this->page_info = $res;
            return $res->items();
        } else {
            return Db::name('sub_card_expense')->where($condition)->order($order)->select()->toArray();
        }


    }



//    /**
//     * 获得投诉商品列表
//     */
//    public function getComplainGoodsList($complain_list) {
//        $goods_ids = array();
//        if (!empty($complain_list) && is_array($complain_list)) {
//            foreach ($complain_list as $key => $value) {
//                $goods_ids[] = $value['order_goods_id']; //订单商品表编号
//            }
//        }
//        $res = Db::name('ordergoods')->where('rec_id','in',$goods_ids)->select()->toArray();
//        return ds_change_arraykey($res, 'rec_id');
//    }


    /**
     * 根据id获取卡券详情
     */
    public function getOneCard($cardId)
    {
        return Db::name('sub_card')->where('id', intval($cardId))->find();
    }


}
