<?php
/**
 * @package merchant
 *
 * @author xaboy
 * @day 2020/6/23
 *
 * 
 */

namespace app\controller\admin\user;



use app\common\dao\BaseDao;
use app\common\model\system\admin\Log;
use crmeb\basic\BaseController;

use app\common\repositories\user\UserBillRepository;
use think\console\command\make\Controller;
use think\Request;

class TeamValue extends  Controller
{
    protected $repository;
    protected $request;

    public function __construct(Request $request)
    {
        parent::__construct();
        $this->request = $request;
    }

    /**
     * 团队设置信息
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     *
     */
    public function getInfo()
    {
        $where = ['create_time'];
        $info=\app\common\model\user\TeamValue::where('create_time >0')->find();
      //  \think\facade\Log::info('ddddd');
        $msg='成功';

        return app('json')->success('',$info);
       // echo 'eeeee';
    }

    /**
     * 编辑团队设置信息
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function editInfo(){
        $params=$this->request->params(['id','sum']);;
        $info=\app\common\model\user\TeamValue::where('create_time >0')->find();
        $res=$info->save(['sum'=>$params['sum']]);
        if($res){
            $msg='成功';
        }else{
            $msg="失败";
        }
        return app('json')->success($msg,$res);
    }

    public function type()
    {
        return app('json')->success($this->repository->type());
    }


    /*平台记录*/
    public  function adminList(){
        [$page, $limit] = $this->getPage();
        $where = $this->request->params(['keyword', 'date', 'type']);
        return app('json')->success($this->repository->getAdminList($where, $page, $limit));
//        return app('json')->success($this->repository->getAdminList($where, $page, $limit));
    }

    public function excel()
    {
        $where = request()->params(['date']);
        $params = request()->params(['commission_type', 'uid', 'status', 'phone', 'user_group', 'status']);
        $commission_type_desc = '佣金统计';
        if ($params['commission_type'] ?? 0 == 5) {
            $commission_type_desc = '养老金统计';
        }
        $data = [];
        //商户ID
        $mer_id = 0;
        $query = Db::name('user_bill')
            ->alias('ub')
            ->leftJoin('user u', 'u.uid=ub.uid')
            ->when($mer_id, function ($query) use ($mer_id) {
                $query->where('ub.mer_id', $mer_id);
            })
            ->when(isset($params['commission_type']) && $params['commission_type'] == 5, function ($query) use ($params) {
                // 养老金
                $query->where('ub.commission_type', 5);
            }, function ($query) use ($params) {
                // 非养老金
                $query->whereIn('ub.commission_type', [3, 16, 14, 10, 12, 20, 15, 7, 19]);
            })
            ->when(isset($params['uid']) && $params['uid'] != '', function ($query) use ($params) {
                $query->where('ub.uid', $params['uid']);
            })
            ->when(isset($params['commission_type']) && $params['commission_type'] != '', function ($query) use ($params) {
                $query->where('ub.commission_type', $params['commission_type']);
            })
            ->when(isset($params['status']) && $params['status'] != '', function ($query) use ($params) {
                $query->where('ub.status', $params['status']);
            })
            ->when(isset($params['phone']) && $params['phone'] != '', function ($query) use ($params) {
                $query->whereLike('u.phone', '%' . $params['phone'] . '%');
            })
            ->when(isset($params['user_group']) && $params['user_group'] != '', function ($query) use ($params) {
                $query->where('u.user_group', $params['user_group']);
            });
        $financeRepository = app()->make(FinanceRepository::class);
        $list = $financeRepository->date_where($query, $where['date'], 'ub.create_time')
            ->field('ub.bill_id,ub.title,ub.number,ub.take_time,ub.commission_type,u.uid,u.phone,u.nickname,u.user_group,ub.order_sn,ub.take_time,ub.status,ub.create_time')
            ->order('ub.bill_id', 'desc')
            ->select()
            ->each(function ($item) {
                if($item['take_time'])
                    $item['take_time'] = date('Y-m-d H:i:s', $item['take_time']);
                return $item;
            });
        foreach ($list as $k => $v) {
            $temp = array(
                $v['bill_id'],
                $v['create_time'],
                $v['nickname'],
                $v['phone'],
                $v['title'],
                $v['number'],
                $v['status'] == 1 ? '已结算': '未结算',
                $v['order_sn'] ?? '',
            );
            $arr[] = $temp;
        }
        $header = [
            'ID', '创建时间', '用户昵称', '用户手机号', '名称', '金额', '状态', '订单号'
        ];
        $filename = $commission_type_desc . date('YmdHis') . '_' . rand(10000, 99999);
        $title = $commission_type_desc;
        $name = $commission_type_desc;
        $info = '生成时间:' . date('Y-m-d H:i:s', time());
        $suffix = 'xlsx';
        $path = 'extract';
        unset($_instance);

        $_instance = SpreadsheetExcelService::instance_xin();

        $_path = $_instance
            ->createOrActive()
            ->setExcelHeader($header)
            ->setExcelTile($title, $name, $info)
            ->setExcelContent($arr)
            ->excelSave($filename, $suffix, $path);

        $data = [
            'name' => $filename . '.' . $suffix,
            'status' => 1,
            'path' => '/' . $_path
        ];

        return app('json')->success('ok', $data);
    }
}