<?php
/*
 * @Descripttion:
 * @version:
 * @Author: kangkang
 * @Date: 2020-10-16 14:44:02
 * @LastEditors: “wanyongkang” “937888580@qq.com”
 * @LastEditTime: 2023-08-10 19:04:59
 */

namespace app\manager\model;

use enum\order\ProductOrder as ProductOrderEnum;
use fastphp\base\Model;

class ProductOrder extends Model
{
    protected $table = 'product_order';

    /**
     * @description: 获取所有支付成功用户的支付总金额
     * @param {*}
     * @return {*}
     */
    public function getCost()
    {
        return $this->field('UserId,sum(PaymentAmount) as money')
            ->where(['OrderState' => ['in', ProductOrderEnum::$PayComplete]])
            ->group(['UserId'])
            ->order('money')
            ->fetchAll();
    }

    /**
     * @description: 获取支付总金额
     * @param {*}
     * @return {*}
     */
    public function getAllMoney($where = '', $where2 = [])
    {
        $data = $this->field('sum(PaymentAmount) as money,UserId')
            ->where(['OrderState' => ['in', ProductOrderEnum::$PayComplete]])
            ->where($where)
            ->where($where2)
            ->group(['UserId'])
            ->fetchAll();
        $result = [
            'money' => 0,
            'num' => 0,
        ];
        foreach ($data as $info) {
            $result['money'] += $info['money'];
            $result['num']++;
        }
        return $result;
    }

    /**
     * @description: 获取退款总金额
     * @param {*}
     * @return {*}
     */
    public function getRefund($where = '', $where2 = [])
    {
        return $this->field('sum(RefundAmount) as money')
            ->where(['OrderState' => ['in', ProductOrderEnum::$Refunds]])
            ->where($where)
            ->where($where2)
            ->fetch();
    }

    /**
     * @description: 获取所有支付成功用户近一个月的支付总金额
     * @param {*}
     * @return {*}
     */
    public function getMonthCost($user_id)
    {
        $last_month = date("Y-m-d H:i:s", strtotime("-1 month"));
        $new_month = date("Y-m-d H:i:s", time());
        return $this->field('UserId,sum(PaymentAmount) as money')
            ->where(['OrderState' => ['in', ProductOrderEnum::$PayComplete], 'UpdateTime' => ['<', $new_month]])
            ->where(['UpdateTime' => ['>', $last_month]])
            ->where(['UserId' => ['in', $user_id]])
            ->group(['UserId'])
            ->order('money')
            ->fetchAll();
    }

    /**
     * @description: 获取所有支付成功用户的支付总金额
     * @param {*}
     * @return {*}
     */
    public function getAllCost($user_id)
    {
        return $this->field('UserId,sum(PaymentAmount) as money')
            ->where(['OrderState' => ['in', ProductOrderEnum::$PayComplete]])
            ->where(['UserId' => ['in', $user_id]])
            ->group(['UserId'])
            ->fetchAll();
    }

    /**
     * @description: 获取所有支付成功用户上一个月在筛选范围的支付总金额
     * @param {*}
     * @return {*}
     */
    public function getScreenMonthCost($screen)
    {
        $last_month = date("Y-m-01", strtotime("-1 month"));
        $new_month = date("Y-m-t", strtotime("-1 month"));
        return $this->field('UserId,sum(PaymentAmount) as money')
            ->where(['OrderState' => ['in', ProductOrderEnum::$PayComplete], 'UpdateTime' => ['<', $new_month]])
            ->where(['UpdateTime' => ['>', $last_month]])
            ->group(['UserId'], 'having money>' . $screen['money1'] . ' and money<' . $screen['money2'])
            ->fetchAll();
    }

    /**
     * @description: 获取所有支付成功用户上一个月或者本月支付金额
     * @param {*} flag = true 上一个月
     * @return {*} false 本月
     */
    public function getPreMonthCost($user_id, $where)
    {
        return $this->field('UserId,sum(PaymentAmount) as money')
            ->where(['OrderState' => ['in', ProductOrderEnum::$PayComplete]])
            ->where($where)
            ->where(['UserId' => ['in', $user_id]])
            ->group(['UserId'])
            ->order('money')
            ->fetchAll();
    }

    /**
     * @description: 获取用户所有的帐号记录
     * @param {*}
     * @return {*}
     */
    public function getUserAccount($user_id)
    {
        return $this->field('UserName,ProductName,PackageName,PaymentAmount,Accounts,UpdateTime')
            ->where(['OrderState' => ['in', ProductOrderEnum::$PayComplete], 'UserId' => $user_id])
            ->order('Id desc')
            ->fetchAll();
    }

    /**
     * @description: 按照排序条件进行查询
     * @param {*}
     * @return {*}
     */
    public function getSort($where = [], $order = 1, $limit = '50', $time_where, $having = '')
    {
        $order_type = 'money';
        if ($order == '0') {
            $order_type = 'money desc';
        }
        return $this->field('product_order.UserId,sum(product_order.PaymentAmount) as money')
            ->join('user ON product_order.UserId=user.Id')
            ->where(['OrderState' => ['in', ProductOrderEnum::$PayComplete]])
            ->where($time_where)
            ->where($where)
            ->group(['product_order.UserId'], $having)
            ->order($order_type)
            ->limit($limit)
            ->fetchAll();
    }

    /**
     * @description: 按照排序条件进行查询上一个月或者本月支付金额
     * @param {*}  flag = true 上一个月
     * @return {*}  flag = false 本月
     */
    public function getMonthSort($where = [], $order = 1, $limit = '50', $where2 = '', $having = '')
    {
        $order_type = 'money';
        if ($order == '0') {
            $order_type = 'money desc';
        }
        return $this->field('product_order.UserId,sum(product_order.PaymentAmount) as money')
            ->join('user ON product_order.UserId=user.Id')
            ->where(['OrderState' => ['in', ProductOrderEnum::$PayComplete]])
            ->where($where)
            ->where($where2)
            ->group(['product_order.UserId'], $having)
            ->order($order_type)
            ->limit($limit)
            ->fetchAll();
    }

    /**
     * @description: 获取所有支付成功用户筛选金额
     * @param {*}
     * @return {*}
     */
    public function getScreenCost($where, $having, $limit = '50', $where2 = [])
    {
        return $this->field('UserId,sum(PaymentAmount) as money')
            ->join('user ON product_order.UserId=user.Id')
            ->where(['OrderState' => ['in', ProductOrderEnum::$PayComplete]])
            ->where($where)
            ->where($where2)
            ->group(['UserId'], $having)
            ->fetchAll();
    }

    /**
     * @description: 获取用户所有的消费记录
     * @param {*}
     * @return {*}
     */
    public function getUserCost($user_id, $index, $where = '')
    {
        return $this->field('UpdateTime,OrderNo,OrderType,ProductName,PackageName,PaymentAmount,Accounts,ConnectCount,AccountCount,PayType,DayPrice as OrderAmount')
            ->where(['UserId' => $user_id])
            ->where($where)
            ->where(['OrderState' => ['in', ProductOrderEnum::$PayComplete]])
            ->order('Id desc')
            ->fetchAll();
    }

    /**
     * @description: 获取用户最后消费时间
     * @param {*} //SELECT UserId,max(UpdateTime) from product_order group by UserId
     * @return {*}
     */
    public function getUserCostTime()
    {
        return $this->field('UserId,max(UpdateTime) as last_time')
            ->where(['OrderState' => ['in', ProductOrderEnum::$PayComplete]])
            ->group(['UserId'])
            ->fetchAll();
    }
}
