<?php
/*
 * @Descripttion:
 * @version:
 * @Author: kangkang
 * @Date: 2020-10-03 18:23:43
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-11-27 09:44:13
 */
$is_script = 1;
include_once __DIR__ . '/../index.php';

use app\api\model\IncomeCheck as IncomeCheckModel;
use app\api\model\ProductOrder as ProductOrderModel;
use app\api\model\User;
use app\api\model\UserChargeOrder as ChargeOrderModel;
use app\api\model\UserScore as ScoreModel;
use enum\order\ProductOrder as ProductOrderEnum;
use enum\order\UserChargeOrder as ChargeOrderEnum;
use enum\order\UserScore as ScoreEnum;

$user = new User;

$charge_order_model = new ChargeOrderModel;
$charge_order_enum = new ChargeOrderEnum;

$score_model = new ScoreModel;
$score_enum = new ScoreEnum;

$product_order_model = new ProductOrderModel;
$product_order_enum = new ProductOrderEnum;

$income_check_model = new IncomeCheckModel;

$data = [];
$today = date('Y-m-d 00:00:00', time());
$yesterday = date('Y-m-d 00:00:00', (time() - 86400));
$data['user_balance'] = $user->field('SUM(`RestAmount`) AS balance')->fetch()['balance'];

$where1 = ['UpdateTime' => ['<', $today]];
$where2 = ['UpdateTime' => ['>', $yesterday]];

$data['ali_invest'] = $charge_order_model
    ->field('SUM(`OrderAmount`) AS ali_invest')
    ->where(['TradeNo' => ['like', '202%'], 'OrderState' => ['in', $charge_order_enum::$PayComplete], 'UpdateTime' => ['<', $today]])
    ->where(['UpdateTime' => ['>', $yesterday]])
    ->fetch()["ali_invest"] ?? 0;

$data['wechat_invest'] = $charge_order_model
    ->field('SUM(`OrderAmount`) AS wechat_invest')
    ->where(['TradeNo' => ['like', '4200%'], 'OrderState' => ['in', $charge_order_enum::$PayComplete], 'UpdateTime' => ['<', $today]])
    ->where(['UpdateTime' => ['>', $yesterday]])
    ->fetch()["wechat_invest"] ?? 0;

$invest_info = $score_model
    ->field('ScoreType,SUM(`ScoreValue`) AS money')
    ->where($where1)
    ->where($where2)
    ->group(['ScoreType'])
    ->fetchAll();

$data['manager_invest'] = $data['cash_out_refund'] = $data['manager_deduct'] = $data['cash_out'] = $data['taobao_invest'] = $data['taobao_refund'] = $data['balance_pay'] = $data['wechat_pay'] = $data['ali_pay'] = $data['new_amount'] = $data['renew_amount'] = $data['refund_amount'] = 0;
foreach ($invest_info as $v) {
    switch ($v['ScoreType']) {
        case $score_enum::$ManageAdd:
            $data['manager_invest'] = $v['money'];
            break;
        case $score_enum::$ManagerDeduct:
            $data['manager_deduct'] = $v['money'];
            break;
        case $score_enum::$UserCashOut:
            $data['cash_out'] = $v['money'];
            break;
        case $score_enum::$TaoBaoAdd:
            $data['taobao_invest'] = $v['money'];
            break;
        case $score_enum::$TaoBaoRefund:
            $data['taobao_refund'] = $v['money'];
            break;
        case $score_enum::$ReturnMoney:
            $data['cash_out_refund'] = $v['money'];
            break;
    }
}

$pay_info_where = array_merge($where2, ['OrderState' => ['in', $product_order_enum::$PayComplete]]);
$pay_info = $product_order_model
    ->field('PayType,SUM(`PaymentAmount`) AS money')
    ->where($where1)
    ->where($pay_info_where)
    ->group(['PayType'])
    ->fetchAll();
foreach ($pay_info as $v) {
    switch ($v['PayType']) {
        case $product_order_enum::$Balance:
            $data['balance_pay'] = $v['money'];
            break;
        case $product_order_enum::$Wechat:
            $data['wechat_pay'] = $v['money'];
            break;
        case $product_order_enum::$Ali:
            $data['ali_pay'] = $v['money'];
            break;
    }
}
$amount_info_where = array_merge($where2, ['OrderState' => ['in', $product_order_enum::$PayComplete]]);
$amount_info = $product_order_model
    ->field('OrderType,SUM(`PaymentAmount`) AS money')
    ->where($where1)
    ->where($amount_info_where)
    ->group(['OrderType'])
    ->fetchAll();

$data['new_amount'] = $data['renew_amount'] = 0;
foreach ($amount_info as $v) {
    switch ($v['OrderType']) {
        case $product_order_enum::$New:
        case $product_order_enum::$News:
            $data['new_amount'] += $v['money'];
            break;
        case $product_order_enum::$AgainBuy:
        case $product_order_enum::$AgainBuys:
            $data['renew_amount'] += $v['money'];
            break;
    }
}
$data['refund_amount'] = $product_order_model
    ->field('SUM(`RefundAmount`) AS refund_amount')
    ->where($where1)
    ->where(array_merge($where2, ['OrderState' => ['in', $product_order_enum::$Refunds]]))
    ->fetch()["refund_amount"] ?? 0;
//获取昨天的数据
$yestoday_user_balance = $income_check_model->getNewOne()['user_balance'] ?? 0;
//base＝次日会员余额一当日会员余额＋支付宝充值＋微信充值＋淘宝充值＋管理员充值＋退款金额一管理员扣除一提现一淘宝退款扣除
$base_money = $yestoday_user_balance - $data['user_balance'] + $data['ali_invest'] + $data['wechat_invest'] + $data['taobao_invest'] + $data['manager_invest'] + $data['refund_amount'] - $data['manager_deduct'] - $data['cash_out'] - $data['taobao_refund'] + $data['cash_out_refund'];
//         余额差值＝次日会员余额一当日会员余额＋支付宝充值＋微信充值＋淘宝充值＋管理员充值＋退款金额一管理员扣除一提现一淘宝退款扣除  一余额支付
// update  余额差值=当日0点会员余额-当日24点会员余额+支付宝充+微信充+淘宝充+管理员充+退款金额+提现拒绝返还-管理员扣除-提现扣除-淘宝退款扣除-余额支付
//                收支差值＝次日会员余额一当日会员余额＋支付宝充值＋微信充值＋淘宝充值＋管理员充值＋退款金额一管理员扣除一提现一淘宝退款扣除   ＋余额支付＋支付宝支付＋微信支付一新开金额一续费金额
//10月26日需修改为：支付差值=余额支付+支付宝付+微信付一新开金额一续费金额
//当日销售额=新开金额＋续费金额一退货金额
//当日在总进账＝支付宝充值＋微信充值＋淘宝充值＋支付宝支付＋微信支付
//当日总支出=提现扣除金额＋淘宝扣除金额
$data['balance_diff'] = $base_money - $data['balance_pay'];
$data['in_out_diff'] = $data['balance_pay'] + $data['ali_pay'] + $data['wechat_pay'] - $data['new_amount'] - $data['renew_amount'];
$data['sell_monet'] = $data['new_amount'] + $data['renew_amount'] - $data['refund_amount'];
$data['income'] = $data['ali_invest'] + $data['wechat_invest'] + $data['taobao_invest'] + $data['ali_pay'] + $data['wechat_pay'];
$data['expend'] = $data['cash_out'] + $data['taobao_refund'];
$data['day_time'] = date('Y-m-d H:i:s');

$income_check_model->add($data);
