<?php

namespace app\admin\controller\finance;

use app\admin\service\sys_manage\DeptService;
use app\common\Util;
use app\model\Member;
use support\Db;
use support\Request;

class IncomeAndExpendController
{

    public function list(Request $request)
    {
        $days = $request->get('days');
        if (!empty($days)) {
            $days[0] = strtotime($days[0]);
            $days[1] = strtotime($days[1]);
            if (date('m', $days[0]) != date('m', $days[1])) {
                return json_fail('暂不支持跨月查询');
            }

            $month = date('Ym', $days[0]);
            $days[1] = strtotime(date('Y-m-d', $days[1]) . " 23:59:59");
        } else {
            $month = date('Ym');
            $days[0] = strtotime(date('Y-m') . '-01');
            $days[1] = time();
        }
        $premises = $request->get('premises', DeptService::getPremisesList());
        // $premisesKeyValue = [];
        // foreach($premises as $item){
        //     $premisesKeyValue[$item['key']] = $item['label'];
        // }
        $premisesKey = [];
        foreach ($premises as $item) {
            $premisesKey[] = $item['key'];
        }

        $dayNum = ($days[1] - $days[0]) / (60 * 60 * 24);
        $currentDay = date('Y-m-d', $days[0]);
        $data = [];
        $statistics = ['income_amount' => 0, 'income_wxpay' => 0, 'income_alipay' => 0, 'income_money' => 0, 'expend_amount' => 0, 'expend_wxpay' => 0, 'expend_alipay' => 0, 'expend_money' => 0];
        for ($i = 0; $i <= $dayNum; $i++) {
            $dayData = [];
            $dayData['day'] = $currentDay;
            // 收入
            if (Util::schema()->hasTable("data_income_{$month}")) {
                $wxpay = Db::table("data_income_{$month}")
                    ->where('data_income_days', $currentDay)
                    ->where('data_income_pay_type', 'WXPAY')
                    ->whereIn('join_data_income_dept_id', $premisesKey)
                    ->selectRaw("data_income_pay_type,SUM(data_income_amount) as data_income_amount")
                    ->groupBy('data_income_pay_type')
                    ->first();

                $alipay = Db::table("data_income_{$month}")
                    ->where('data_income_days', $currentDay)
                    ->where('data_income_pay_type', 'ALIPAY')
                    ->whereIn('join_data_income_dept_id', $premisesKey)
                    ->selectRaw("data_income_pay_type,SUM(data_income_amount) as data_income_amount")
                    ->groupBy('data_income_pay_type')
                    ->first();

                $money = Db::table("data_income_{$month}")
                    ->where('data_income_days', $currentDay)
                    ->where('data_income_pay_type', 'MONEY')
                    ->whereIn('join_data_income_dept_id', $premisesKey)
                    ->selectRaw("data_income_pay_type,SUM(data_income_amount) as data_income_amount")
                    ->groupBy('data_income_pay_type')
                    ->first();
            }

            $dayData['income']['wxpay'] = $wxpay->data_income_amount ?? 0.00;
            $statistics['income_wxpay'] += $dayData['income']['wxpay'];
            $dayData['income']['alipay'] = $alipay->data_income_amount ?? 0.00;
            $statistics['income_alipay'] += $dayData['income']['alipay'];
            $dayData['income']['money'] = $money->data_income_amount ?? 0.00;
            $statistics['income_money'] += $dayData['income']['money'];

            $dayData['income']['amount'] = $dayData['income']['wxpay'] + $dayData['income']['alipay'] + $dayData['income']['money'];

            $statistics['income_amount'] += $dayData['income']['amount'];

            // 支出
            if (Util::schema()->hasTable("data_income_{$month}")) {
                $extendWxpay = Db::table("data_expend_{$month}")
                    ->where('data_expend_days', $currentDay)
                    ->where('data_expend_pay_type', 'WXPAY')
                    ->whereIn('join_data_expend_dept_id', $premisesKey)
                    ->selectRaw("data_expend_pay_type,SUM(data_expend_amount) as data_expend_amount")
                    ->groupBy('data_expend_pay_type')
                    ->first();

                $extendAlipay = Db::table("data_expend_{$month}")
                    ->where('data_expend_days', $currentDay)
                    ->where('data_expend_pay_type', 'ALIPAY')
                    ->whereIn('join_data_expend_dept_id', $premisesKey)
                    ->selectRaw("data_expend_pay_type,SUM(data_expend_amount) as data_expend_amount")
                    ->groupBy('data_expend_pay_type')
                    ->first();

                $extendMoney = Db::table("data_expend_{$month}")
                    ->where('data_expend_days', $currentDay)
                    ->where('data_expend_pay_type', 'MONEY')
                    ->whereIn('join_data_expend_dept_id', $premisesKey)
                    ->selectRaw("data_expend_pay_type,SUM(data_expend_amount) as data_expend_amount")
                    ->groupBy('data_expend_pay_type')
                    ->first();
            }
            $dayData['expend']['wxpay'] = $extendWxpay->data_expend_amount ?? 0.00;
            $statistics['expend_wxpay'] += $dayData['expend']['wxpay'];
            $dayData['expend']['alipay'] = $extendAlipay->data_expend_amount ?? 0.00;
            $statistics['expend_alipay'] += $dayData['expend']['alipay'];
            $dayData['expend']['money'] = $extendMoney->data_expend_amount ?? 0.00;
            $statistics['expend_money'] += $dayData['expend']['money'];
            $dayData['expend']['amount'] = $dayData['expend']['wxpay'] + $dayData['expend']['alipay'] + $dayData['expend']['money'];

            $statistics['expend_amount'] += $dayData['expend']['amount'];

            // 当前日期+1
            $currentDay = date('Y-m-d', strtotime("+1 days", strtotime($currentDay)));

            // 没有数据，跳过
            if ($dayData['expend']['amount'] == 0 && $dayData['income']['amount'] == 0) {
                continue;
            }

            $data[] = $dayData;
        }

        return json_success('', ['rows' => $data, 'statistics' => $statistics]);
    }
}