<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-31 20:43
// +----------------------------------------------------------------------

namespace app\warehouse\controller\v1;


use app\BaseController;
use app\model\api\Tx;
use app\model\warehouse\OrderInfo;
use app\model\warehouse\Warehouse;
use app\model\warehouse\WarehouseDetail;
use app\Request;
use app\warehouse\controller\AuthController;
use library\services\UtilService;
use think\Cache;

class Index extends  AuthController
{

    /**
     * 获取配置信息
     */
    public function getIndex(Request $request){
        //今日订单量
        $post['today_count'] = (new OrderInfo)
                                            ->where('status','>',0)
                                            ->where('warehouse_id',$this->adminInfo['id'])
                                            ->whereTime('time','today')
                                            ->count();
        $post['all_t_order_count'] = (new OrderInfo)
            ->where('warehouse_id',$this->adminInfo['id'])
            ->where('status','>',0)
            ->count();
        //销售额
        $post['today_money'] =  (new OrderInfo)
            ->where('status','>',0)
            ->where('warehouse_id',$this->adminInfo['id'])
            ->whereTime('time','today')
            ->sum('exp_floor_price');

        $post['all_money'] = (new OrderInfo)
            ->where('status','>',0)
            ->where('warehouse_id',$this->adminInfo['id'])
            ->sum('exp_floor_price');

        $post['no_order_count'] = (new \app\model\admin\OrderInfo)
            ->where('warehouse_id',$this->adminInfo['id'])
            ->where('status','=',1)->count();
        $post['order_count'] = (new \app\model\admin\OrderInfo)
            ->where('warehouse_id',$this->adminInfo['id'])
            ->where('status','>',1)->count();
        $sysData = (new \app\model\system\Sys)->where('id',1)->find();


        $dateAr = [];
        $order = new  OrderInfo;
        //8天订单量
        for($i = 0;$i<8;$i++) {
            $time = strtotime('-'.$i.' day');
            $date = date('Y-m-d',$time);
            $count = $order
                ->where('warehouse_id',$this->adminInfo['id'])
                ->where('status','>',0)
                ->whereDay('time',$date)
                ->count();
            $dateAr[] = ['count'=>$count,'name'=>$date];
        }
        $post['date'] = array_reverse($dateAr);



        $post['sysInfo'] = [
            'lx_name'     => $sysData['lx_name'],
            'lx_tel'      => $sysData['lx_tel'],
            'custom_tel'  => $sysData['custom_tel'],
            'custom_qq'   => $sysData['custom_qq'],
            'weixin_logo' => $sysData['weixin_logo'],
            'tag'         => $sysData['tag']
        ];

        //获取自己用户
        $post['commission_money'] = $this->adminInfo['money'];
        //提现的钱
        $money = (new Tx)->where('warehouse_id',$this->adminInfo['id'])->where('type',3)->where('is_type',0)->sum('money');
        $post['tx_commission_money'] = $money;
        //已提现的钱
        $money = (new Tx)->where('warehouse_id',$this->adminInfo['id'])->where('type',3)->where('is_type',1)->sum('money');
        $post['txok_commission_money'] = $money;

        return app('json')->success($post);
    }



    public function getOrderTotal(Request $request){
        [$visitDate] = UtilService::getMore([
            ['visitDate','thirtyday']
        ],$request,true);
        $order = (new \app\model\admin\Order());
        $orderInfo = (new OrderInfo());
        //30天
        $tAr = ['name'=>[],'order_count'=>[],'order_info_count'=>[]];
        if($visitDate == 'thirtyday') {
            for ($i = 30;$i>=0;$i--) {
                $time = date('Y-m-d',strtotime('-'.$i.' day'));
                $tAr['name'][] = $time;
                $tAr['order_count'][] = $order
                                        ->whereDay('time',$time)
                                        ->where('sassid',$request->site['sassid'])
                                        ->where('status','>',0)
                                        ->count();
                $tAr['order_info_count'][] = $orderInfo->where('sassid',$request->site['sassid'])->where('status','>',0)->whereDay('time',$time)->count();
            }
        }
        return app('json')->success($tAr);
    }




    /**
     * 我要提现
     * @param Request $request
     */
    public function subTx(Request $request) {
        $redis = \think\facade\Cache::store('redis');
        $post =  UtilService::getMore(
            [
                ['bank',"","empty","请输入到账账号"],
                ['money','0',"empty","请输入提现金额"],
                ['name','','empty','请输入真实姓名']
            ],$request
        );
        $txKey = 'tx_'.md5($post['bank']);
        $ckBank = $redis->get($txKey);
        if(!empty($ckBank)) {
            return app('json')->fail("请务重复提现！");
        }
        //提现金额
        if($this->adminInfo['money'] < $post['money']) {
            return app('json')->fail("余额不足，无法提现！");
        }
        $redis->set($txKey,1,5);
        (new Tx)
            ->insert([
                'uid'     => 0,
                'warehouse_id' => $this->adminInfo['id'],
                'money'  => $post['money'],
                'sassid' => 0,
                'type'   => 3,
                'bank'   => '支付宝',
                'name'   => $post['name'],
                'code'   => $post['bank'],
                'time'   => time(),
                'is_type' => 0
            ]);
        (new WarehouseDetail)->tx($post['money'],$this->adminInfo['id'],['time'=>date('Y-m-d H:i:s'),'money'=>$post['money']]);
        $redis->delete($txKey);
        return app('json')->success(
            '提现成功，请等待打款！'
        );
    }


    /**
     * 提现列表
     * @param Request $request
     */
    public function txList(Request $request) {
        $pageSize = 10;
        $post =  UtilService::getMore(
            [
                ['page',1]
            ],$request
        );
        $where['warehouse_id'] =$this->adminInfo['id'];
        $tx = new Tx();
        list($pageCount,$data) = $tx->getList($post['page'],$where,$pageSize,"*","time desc");
        $result = UtilService::getParam([
            "id",
            "money",
            "bank",
            'name',
            'code',
            "is_type",
            ["time","time","date('Y-m-d H:i:s',$1)"]
        ],$data);
        return app('json')->success([
            'list'      => $result,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page']
        ]);
    }


    /**
     * 子订单数据
     * @param Request $request
     */
    public function getOrder(Request $request) {
        $pageSize = 20;
        $post =  UtilService::getMore(
            [
                ['page',1],
                ['order_id',''],
                ['mobile',''],
                ['name',''],
                ['data',[]],
                ['status','']
            ],$request
        );
        $where = $post;
        $where['warehouse_id']     = $this->adminInfo['id'];
        $where['status'] = $post['status'];
        list($pageCount,$data) = (new OrderInfo)->getList($post['page'],$where,$pageSize,"*","id desc");
        $result = UtilService::getParam([
            "id",
            "exp_name",
            "in_order_id",
            "name",
            "mobile",
            "address",
            "is_push",
            "mono",
            "time",
            "exp_floor_price",
            "send_time",
            "exp_number",
            "msg_err",
            "money",
            "status",
        ],$data);
        return app('json')->success([
            'list'      => $result,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page']
        ]);
    }


    /**
     * 操作明细
     * @param Request $request
     */
    public function detailList(Request $request) {
        $pageSize = 10;
        $post =  UtilService::getMore(
            [
                ['page',1],
                ['tabType','all']
            ],$request
        );
        $where['warehouse_id'] =$this->adminInfo['id'];
        if(!empty($post['tabType']) && $post['tabType'] != 'all') {
            $where['code'] = $post['tabType'];
        }
        $detail = new WarehouseDetail();
        list($pageCount,$data) = $detail->getList($post['page'],$where,$pageSize,"*","time desc");
        $result = UtilService::getParam([
            "id",
            "code",
            "v",
            'title',
            'content',
            "type",
            ["time","time","date('Y-m-d H:i:s',$1)"]
        ],$data);
        return app('json')->success([
            'list'      => $result,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page']
        ]);
    }



    /**
     * 资金情况
     * @param Request $request
     */
    public function moneyInit(Request $request) {

        $info = [
            'money'       => $this->adminInfo['money'],
            'tx_money'    => (new Tx)->where('warehouse_id',$this->adminInfo['id'])->where('type',3)->where('is_type',0)->sum('money'),//提现金额
            'in_money'    => (new Tx)->where('warehouse_id',$this->adminInfo['id'])->where('type',3)->where('is_type',1)->sum('money')
        ];
        return app('json')->success($info);
    }


    /**
     * 修改密码
     * @param Request $request
     */
    public function password(Request $request) {
        $post =  UtilService::getMore(
            [
                ['oldpass','','empty','请输入旧密码'],
                ['password','','empty','请输入新密码'],
            ],$request
        );

        if($this->adminInfo['password'] != md5($post['password'])) {
            return  app('json')->fail('旧密码错误，请检查之后在修改');
        }
        (new Warehouse)->where('id',$this->adminInfo['id'])->save(['password'=>md5($post['password'])]);
        return app('json')->success('修改成功');
    }


}