<?php
/**
 * @package merchant
 *
 * @author xaboy
 * @day 2020/5/30
 *
 *
 */

namespace app\common\dao\merchant\order;


use app\common\dao\BaseDao;
use app\common\model\merchant\order\OrderMerchant;
use app\common\repositories\user\UserRepository;

class OrderMerchantDao extends BaseDao
{
    protected function getModel(): string
    {
        return OrderMerchant::class;
    }

    public function dayOrderNum($day, $merId = null)
    {
        return OrderMerchant::getDB()->where('paid', 1)->when($merId, function ($query, $merId) {
            $query->where('mer_id', $merId);
        })->when($day, function ($query, $day) {
            getModelTime($query, $day, 'pay_time');
        })->count();
    }

    public function dayOrderPrice($day, $merId = null)
    {
        return getModelTime(OrderMerchant::getDB()->where('paid', 1)->when($merId, function ($query, $merId) {
            $query->where('mer_id', $merId);
        }), $day, 'pay_time')->sum('money');
    }

    public function dayOrderAvailable($day, $merId = null)
    {
        return getModelTime(OrderMerchant::getDB()->where('paid', 1)->when($merId, function ($query, $merId) {
            $query->where('mer_id', $merId);
        }), $day, 'pay_time')->sum('available');
    }

    public function dayOrderUserNum($day, $merId = null)
    {
        return OrderMerchant::getDB()->where('paid', 1)->when($merId, function ($query, $merId) {
            $query->where('mer_id', $merId);
        })->when($day, function ($query, $day) {
            getModelTime($query, $day, 'pay_time');
        })->group('uid')->count();
    }


    public function test()
    {
        return OrderMerchant::getDB()->select();
    }
    public function merchantSearch($merId, array $where, $sysDel = 0)
    {
        return OrderMerchant::getDB()
            ->when($merId, function ($query, $merId) {
                $query->where('mer_id', $merId);
            })
            ->when(!$merId && isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
                $query->where('mer_id', $where['mer_id']);
            })
            ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
                getModelTime($query, $where['date']);
            })
            ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
                $query->where('merch_no|out_trade_no|refund_no', 'like', "%" . $where['keyword'] . "%");
            })
            ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
                $query->where('status', 1);
            })
            ->order('create_time DESC');
    }
    public function search(array $where, $sysDel = 0)
    {
        return OrderMerchant::getDB()
            ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
                getModelTime($query, $where['date']);
            })
            ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
                $query->where('merch_no|out_trade_no|refund_no', 'like', "%" . $where['keyword'] . "%");
            })
            ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
                $query->where('status', 1);
            })
            ->order('create_time DESC');
    }
    public function updateBindstatus($uinionid, $uid)
    {
        return OrderMerchant::getDB()->where('pay_user_id', $uinionid)->update(['uid' => $uid, 'binds' => 1]);
    }
}
