<?php
/*
 * @Descripttion:
 * @version:
 * @Author: kangkang
 * @Date: 2020-10-13 19:52:37
 * @LastEditors: kangkang
 * @LastEditTime: 2020-11-14 11:49:43
 */

namespace app\order\controller;

use app\order\model\Finance as FinanceModel;
use app\order\model\FinanceOperation as FinanceOperationModel;
use fastphp\base\Controller;

class FinanceCheck extends Controller
{
    /**
     * @description: 数据列表
     * @param {*}
     * @return {*}
     */
    public function getList()
    {
        $page = 0;
        if (isset($_GET['page'])) {
            $page = ($_GET['page'] - 1) * 50;
        }

        $finance_model = new FinanceModel;
        $list = $finance_model->getListPage([], '*', 'id desc', "$page,50");
        foreach ($list as $k => &$v) {
            $v['riqi'] = date('Y-m-d', strtotime($v['riqi']) - 86400);
        }
        $data = [
            'Code' => 10000,
            'data' => $list,
            'msg' => '',
            'count' => (int) $finance_model->getCount()['count'],
        ];
        echo json_encode($data);
    }

    public function search()
    {
        $data = json_decode(file_get_contents("php://input"), true)['time'];
        $data[0] = date('Y-m-d', strtotime($data[0]) + 86400) . ' 00:00:00';
        $data[1] = date('Y-m-d', strtotime($data[1]) + 86400) . ' 23:59:59';
        $finance_model = new FinanceModel;
        $list = $finance_model->getListSearch(['riqi' => ['>', $data[0]]], ['riqi' => ['<', $data[1]]], '*', 'id desc', "10000");
        foreach ($list as $k => &$v) {
            $v['riqi'] = date('Y-m-d', strtotime($v['riqi']) - 86400);
        }
        $data = [
            'Code' => 30000,
            'data' => $list,
            'msg' => '',
            'count' => 0,
        ];
        echo json_encode($data);
    }

    public function buyi()
    {
        $data = json_decode(file_get_contents("php://input"), true);
        $userinfo = $this->userinfo;
        $data['op_person'] = $userinfo['LoginName'];
        $data['riqi'] = date('Y-m-d', strtotime($data['riqi']) + 86400);
        $finance_model = new FinanceModel;
        $finance_operation_model = new FinanceOperationModel;
        $finance_data = $finance_model->getOne("date(riqi)='" . $data['riqi'] . "'");
        
        if (!$finance_data) {
            $finance_operation_model->add($data);
        } else {
            $data['finance_id'] = $finance_data['id'];
            $finance_buyi = $finance_data[$data['product']] + $data['money'];
            $types = ['充值', '退款', '补差价'];
            $finance_update = [
                $data['product'] => $finance_buyi,
                'remark' => $finance_data['remark'] .'<'. $data['product'] . '补遗 ' . $types[$data['type'] - 1] . $data['money'] . '>',
            ];
            if ($finance_model->updateOne(['id' => $finance_data['id']], $finance_update)) {
                $finance_operation_model->add($data);

                $finance_data_list = $finance_model->getListPage(['id' => ['>', $finance_data['id']]]);
                if($finance_data_list) {
                    foreach($finance_data_list as $info){
                        $finance_model->updateOne(['id' => $info['id']], [$data['product'] => $info[$data['product']] + $data['money']]);
                    } 
                }
            } else {
                die;
            }
        }
        \result();
    }

    public function buyiList()
    {
        $where = [];
        $where2 = '';
        $page = 0;
        if (isset($_GET['page'])) {
            $page = ($_GET['page'] - 1) * 50;
        }

        $product_ids = [
            '8' => 'xingxing',
            '6' => 'xianfeng',
            '13' => 'jike',
            '1' => 'qiangzi',
            '2' => 'xunlian',
            '14' => 'tiantian',
            '5' => 'laoying',
            '4' => 'jinrui',
            '7' => 'wujin',
            '9' => 'wanmei',
            '3' => 'jinqiao',
            '10' => 'wuxian',
            '16' => 'shihui',
            '15' => 'gongxiang',
            '17' => 'xigua',
            '18' => 'mogu',
            '19' => 'huohu',
        ];

        if (!empty($_GET['ProductId'])){
            $where['product'] = $product_ids[$_GET['ProductId']];
        }
        if (!empty($_GET['type'])){
            $where['type'] = $_GET['type'];
        }
        if (!empty($_GET['Btime'])){
            $date1 = date('Y-m-d', strtotime($_GET['Btime']));
            $date2 = date('Y-m-d', strtotime($_GET['Etime']));
            $where2 .= " riqi>='" . $date1 . "' and riqi<='" . $date2 . "' ";
        }

        // dump($_GET);die;

        $finance_model = new FinanceOperationModel;
        $list = $finance_model->getListSearch($where, $where2,'*', 'id desc', "$page,50");
        $types = ['充值', '退款', '补差价'];
        $products = [
            'xingxing' => '星星',
            'xianfeng' => '先锋',
            'jike' => '极客',
            'qiangzi' => '强子',
            'xunlian' => '讯连',
            'tiantian' => '天天',
            'laoying' => '老鹰',
            'jinrui' => '金瑞',
            'wujin' => '无尽',
            'wanmei' => '完美',
            'jinqiao' => '金桥',
            'wuxian' => '无限',
            'shihui' => '实惠',
            'gongxiang' => '共享',
            'xigua' => '西瓜',
            'mogu' => '蘑菇',
            'huohu' => '火狐',
        ];
        foreach ($list as $k => &$v) {
            $v['type'] = $types[$v['type'] - 1];
            $v['product'] = $products[$v['product']];
            $v['riqi'] = date('Y-m-d', strtotime($v['riqi']) - 86400);
        }
        $data = [
            'Code' => 10000,
            'data' => $list,
            'msg' => '',
            'count' => (int) $finance_model->getCount()['count'],
        ];
        echo json_encode($data);
    }
}
