<?php

namespace app\api\controller\company;

use app\common\controller\Apic;
use think\Db;

/**
 * 会员接口
 */
class Index extends Apic
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';


    //首页
    public function index(){


    }

    //首页轮播
    public function banner(){
        $where = [
            'status'     => 1,
            'position'  => 1
        ];
        $list = Db::name('platform_banner')->where($where)->order('weigh asc,id asc')->select();
        $list = list_domain_image($list,['image']);
        $this->success(1,$list);
    }

    //商家钱包明细
    public function money_log(){

        $map = [
            'user_id' => $this->auth->company_id,
        ];

        $list = Db::name('company_money_log')
            ->field('id,log_type,change_value,remain,remark,createtime')
            ->where($map)->order('id desc')->autopage()->select();
        //$list = $this->list_appen_logtext($list);

        $this->success(1,$list);
    }

    //追加log_text
    private function list_appen_logtext($list){
        if(!empty($list)){
            $conf = config('wallet.logtype');
            foreach($list as $key => $val){
                $list[$key]['log_text'] = isset($conf[$val['log_type']]) ? $conf[$val['log_type']] : '';
            }
        }
        return $list;
    }

    //资金账单，用来显示本门店下所有用户的储值卡的流水日志
    public function usermoney_info(){
        $usernumber = Db::name('user_wallet')->where('company_id',$this->auth->company_id)->count();
        $summoney = Db::name('user_wallet')->where('company_id',$this->auth->company_id)->sum('money');

        $rs = [
            'usernumber' => $usernumber,
            'summoney' => $summoney,
        ];
        $this->success(1,$rs);
    }

    //资金账单，下部列表
    public function usermoney_lists(){
        $keyword = input('keyword','');

        $where = [
            'log.company_id' => $this->auth->company_id,
        ];
        if(!empty($keyword)){
            $where['user.nickname|user.mobile'] = ['LIKE','%'.$keyword.'%'];
        }
        $lists = Db::name('user_money_log')->alias('log')
            ->field('log.id,log.log_type,log.change_value,log.remain,log.remark,log.createtime,user.nickname,user.mobile')
            ->join('user','log.user_id = user.id','LEFT')
            ->where($where)->order('log.id desc')->autopage()->select();

        $this->success(1,$lists);
    }

    //数据中心
    public function datacenter_one(){
        $date = input('date',date('Y-m-01'));
        $starttime = strtotime($date);    //月初第一天
        $endtime = strtotime("+1 month",$starttime) - 1; //月末最后一天

        //柱状图
        $ec_date = [];
        $ec_ordernum = [];

        $map = [
            'company_id'  => $this->auth->company_id,
            'status'      => 3,
        ];
        if($this->auth->type == 2){
            $map['staff_id'] = $this->auth->id;
        }

        $ec_ordernum = [
            0 => [
                'name'=>'笔数',
                'data'=>[],
            ],
        ];
        for($i=$starttime;$i<$endtime;$i+=86400){
            $starttime_i = $i;
            $endtime_i = $i + 86399;
            //日历
            $ec_date[] = date('d',$starttime_i).'日';
            //下单人数
            $map['finish_time'] = ['between',[$starttime_i,$endtime_i]];
            $ec_ordernum[0]['data'][] = Db::name('order')->where($map)->count('id');
        }

        //饼图
        $map['finish_time'] = ['between',[$starttime,$endtime]];
        $servicetype = Db::name('servicetype')->field('id,title as name')->select();
        foreach($servicetype as $key => &$val){
            $map['servicetype_id'] = $val['id'];
            $val['value'] = Db::name('order')->where($map)->count('id');
            unset($val['id']);
        }

        $result = [
            'ec_data' => $ec_date,
            'ec_ordernum' => $ec_ordernum,
            'servicetype' => $servicetype,
        ];
        $this->success(1,$result);
    }

    public function datacenter_two(){
        $usernumber = Db::name('user_wallet')->where('company_id',$this->auth->company_id)->count();//充卡客户数量
        $summoney = Db::name('user_wallet')->where('company_id',$this->auth->company_id)->sum('money');//充卡余额
        //客户类别
        $comefrom_config = ['线下新客','老带新','平台引流','自然进店'];
        $comefrom_array = [];
        foreach($comefrom_config as $key => $val){
            $number = Db::name('user_wallet')->where('company_id',$this->auth->company_id)->where('comefrom',$val)->count();

            $comefrom_array[] = ['name'=>$val,'value'=>$number];
        }

        //
        $result = [
            'usernumber' => $usernumber,
            'summoney' => $summoney,
            'comefrom_array'  => $comefrom_array,
        ];

        $this->success(1,$result);
    }

    public function datacenter_three(){
        $type = input('type',1);

        $servicetype = Db::name('servicetype')->field('id,title as name')->select();
        //七日
        if($type == 1){
            $starttime = strtotime(date('Y-m-d')) - 518400;
            $endtime   = strtotime(date('Y-m-d')) + 86399;

            $ec_date = [];

            $map = [
                'company_id'  => $this->auth->company_id,
                'status'      => 3,
            ];
            if($this->auth->type == 2){
                $map['staff_id'] = $this->auth->id;
            }


            for($i=$starttime;$i<$endtime;$i+=86400){
                $starttime_i = $i;
                $endtime_i = $i + 86399;
                //日历
                $ec_date[] = date('d',$starttime_i).'日';
            }

            foreach($servicetype as $key => &$val){
                $map['servicetype_id'] = $val['id'];
                $val['data'] = [];
                $val['textColor'] = '#FFFFFF';
                for($i=$starttime;$i<$endtime;$i+=86400){
                    $starttime_i = $i;
                    $endtime_i = $i + 86399;
                    //销售金额
                    $map['finish_time'] = ['between',[$starttime_i,$endtime_i]];
                    $val['data'][] = Db::name('order')->where($map)->sum('total_fee');
                }
                unset($val['id']);
            }

            $result = [
                'ec_data'     => $ec_date,
                'ec_totalfee' => $servicetype,
            ];
            $this->success(1,$result);
        }else{
            $thismonth = strtotime(date('Y-m-01'));

            $ec_date = [];

            $map = [
                'company_id'  => $this->auth->company_id,
                'status'      => 3,
            ];
            if($this->auth->type == 2){
                $map['staff_id'] = $this->auth->id;
            }


            for($i=5;$i>=0;$i--){
                $starttime_i = strtotime("-".$i." month",$thismonth);
                $endtime_i = strtotime("-".($i+1) ." month",$thismonth) - 1;
                //日历
                $ec_date[] = date('m',$starttime_i).'月';
                //dump(date('Y-m-d H:i:s',$starttime_i));
                //dump(date('Y-m-d H:i:s',$endtime_i));
            }


            foreach($servicetype as $key => &$val){
                $map['servicetype_id'] = $val['id'];
                $val['data'] = [];
                $val['textColor'] = '#FFFFFF';
                for($i=5;$i>=0;$i-=1){
                    $starttime_i = strtotime("-".$i." month",$thismonth);
                    $endtime_i = strtotime("-".($i+1) ." month",$thismonth) - 1;
                    //销售金额
                    $map['finish_time'] = ['between',[$starttime_i,$endtime_i]];
                    $val['data'][] = Db::name('order')->where($map)->sum('total_fee');
                }
                unset($val['id']);
            }

            $result = [
                'ec_data'     => $ec_date,
                'ec_totalfee' => $servicetype,
            ];
            $this->success(1,$result);
        }

    }
    public function datacenter_four(){
        $date = input('date',date('Y-m-01'));
        $starttime = strtotime($date);    //月初第一天
        $endtime = strtotime("+1 month",$starttime) - 1; //月末最后一天

        $staff = Db::name('company_staff')->field('id,truename')->where('type',2)->where('company_id',$this->auth->company_id)->select();

        //柱状图
        $ec_date = [];
        $ec_ordernum = [
            0 => [
                'name'=>'笔数',
                'data'=>[],
            ],
        ];

        $map = [
            'company_id'  => $this->auth->company_id,
            'status'      => 3,
            'finish_time' => ['between',[$starttime,$endtime]],
        ];

        foreach($staff as $key => $val){
            //日历
            $ec_date[] = $val['truename'];
            //下单人数
            $map['staff_id']    = $val['id'];
            $ec_ordernum[0]['data'][] = Db::name('order')->where($map)->count('id');
        }

        $result = [
            'ec_data'     => $ec_date,
            'ec_ordernum' => $ec_ordernum,
        ];
        $this->success(1,$result);


    }



}
