TestController.php 13.5 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/21
 * Time: 19:08
 */

namespace app\portal\controller;


use cmf\controller\HomeBaseController;
use EasyWeChat\Foundation\Application;
use think\Db;

class TestController extends HomeBaseController
{

    public function prob()
    {
        $uid = 451;
        $user = Db::name('user')->where('id',$uid)->find();
        $indent = Db::name('indent')->where('salesman_uid',$uid)->order('create_time','desc')->select();
        $arr = collection($indent)->toArray();
        $data = [];
        $state_arr = [
            4 => '待付款',
            2 => '待发货',
            3 => '已完成',
            5 => '已发货',
            6 => '待取货'
        ];
        foreach ($arr as &$v) {
            $money = Db::name('money_income')->where('uid',$uid)->where('indent_id',$v['id'])->value('money');
            $fact_money = Db::name('money_income')->where('uid',$uid)->where('indent_id',$v['id'])->sum('money');
//            $income = Db::name('money_income')->where('uid',$uid)->where('indent_id',$v['id'])->select();
//            $v['income'] = collection($income)->toArray();

//            $data[] = [
//                'id' => $v['id'],
//                'money' => $money,
//                'number' => $v['book_num'],
//                'name' => $v['name'],
//                'phone' => $v['phone'],
//                'school' => $v['school'].'-'.$v['grade'].$v['class'],
//                'state' => $state_arr[$v['state']],
//                'create_time' => date('Y-m-d H:i:s',$v['create_time']),
//                'pay_time' => date('Y-m-d H:i:s',$v['pay_time']),
//                'region' => $v['region'],
//                'beiyong_address' => $v['beiyong_address'],
//                'award' => $money,
//                'fact_award' => $fact_money,
//                'is_error' => $fact_money != $money ? '是' : '否'
//            ];
            $data[] = [
                $v['id'],
                $money,
                $v['book_num'],
                $v['name'],
                $v['phone'],
                $v['school'].'-'.$v['grade'].$v['class'],
                $state_arr[$v['state']],
                date('Y-m-d H:i:s',$v['create_time']),
                date('Y-m-d H:i:s',$v['pay_time']),
                $v['region'],
                $v['beiyong_address'],
                $money,
                $fact_money,
                $fact_money != $money ? '是' : '否'
            ];
        }
        $this->excel($user,$data);
        print_r($arr);exit;
    }


    /**
     *
     * 导出Excel
     */
    public function excel($user,$data2)
    {
        //引入PHPExcel库文件
//        Vendor('phpexcel2.PHPExcel');
        //创建对象
        $excel = new \PHPExcel();
        //Excel表格式,这里简略写了8列
        $letter = array('A', 'B', 'C', 'D', 'E', 'F', 'F', 'G','H','I','J','k','L','M','N');
        $excel->getActiveSheet()->setCellValue("A1", "业务员名称");
        $excel->getActiveSheet()->setCellValue("B1", $user['user_nickname']);
        //表头数组
        $tableheader = array('订单号', '价钱', '数量', '姓名', '电话', '学校-班级', '订单状态', '创建时间', '支付时间', '配送时间', '地区', '备用地址',
            '应得收益', '实际收益', '是否异常');
        //填充表头信息
        for ($i = 0; $i < count($tableheader); $i++) {
            $excel->getActiveSheet()->setCellValue("$letter[$i]2", "$tableheader[$i]");
        }
        //表格数组
        $data = array(
            array('1', 'B', 'C', 'D', 'E', 'F', 'G'),
            array('2', 'B', 'C', 'D', 'E', 'F', 'G'),
            array('3', 'B', 'C', 'D', 'E', 'F', 'G'),
            array('4', 'B', 'C', 'D', 'E', 'F', 'G'),
            array('5', 'B', 'C', 'D', 'E', 'F', 'G'),
            array('6', 'B', 'C', 'D', 'E', 'F', 'G'),
            array('7', 'B', 'C', 'D', 'E', 'F', 'G'),
        );
//        $tz = Db::name('taizhang');
//        $data2 = $tz->field('type',true)->select();
        //print_r($data2);exit();
        //填充表格信息
        for ($i = 3; $i <= count($data2) + 1; $i++) {
            $j = 0;
            foreach ($data2[$i - 2] as $key => $value) {
                $excel->getActiveSheet()->setCellValue("$letter[$j]$i", "$value");
                $j++;
            }
        }
        //创建Excel输入对象
        $write = new \PHPExcel_Writer_Excel2007($excel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename="信息系统表.xlsx"');
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');
    }


    /**
     * 恢复父级id
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function test(){
        ini_set('max_execution_time', 3600);//秒为单位,自己根据需要定义
        ini_set('memory_limit','-1');
//        $b = Db::name('indent_pro')->where('salesman_uid',309)->update(['is_out'=>0]);
        $page = $this->request->param('page',1);
        $uid = Db::name('indent_pro')->where('is_out',0)->group('salesman_uid')->order('salesman_uid','desc')->value('salesman_uid');
        if(!$uid) {
            echo 123;exit;
        }
//        print_r($uid);exit;
        $list = Db::name('indent_pro')->where('salesman_uid',$uid)->paginate(50,true,['page'=>$page]);
        $list = $list->items();
        $state_arr = ['2'=>'待发货','3'=>'已完成','5'=>'已发货','6'=>'待收货'];
        $i = [];
        foreach ($list as $v) {
            $order = Db::name('indent')->where('salesman_uid',$v['salesman_uid'])->where('order_number',$v['order_number'])
                ->whereIn('state',['2','3','5','6'])->find();
            if(!$order) continue;
            $school_class = $v['school'].'-'.$v['grade'].$v['class'];
            $salesman_name = Db::name('user')->where('id',$v['salesman_uid'])->value('user_nickname');
            $award = Db::name('money_income')->where('indent_id',$order['id'])->where('uid',$order['salesman_uid'])->value('money');
            if($award) {
                $award_fac = Db::name('money_income')->where('indent_id',$order['id'])->where('uid',$order['salesman_uid'])->sum('money');
                $i[] = [
                    'state_text' => $state_arr[$order['state']],
                    'order_number' => $order['order_number'],
                    'money' => $order['money'],
                    'create_time' => date('Y-m-d H:i:s',$order['create_time']),
                    'book_num' => $order['book_num'],
                    'leave_word' => $order['leave_word'],
                    'name' => $order['name'],
                    'phone' => $order['phone'],
                    'school_class' => $school_class,
                    'salesman_name' => $salesman_name,
                    'pay_time' => date('Y-m-d H:i:s',$order['pay_time']),
                    'start_time' => $order['start_time'],
                    'end_time' => $order['end_time'],
                    'region' => $order['region'],
                    'beiyong_address' => $order['beiyong_address'],
                    'award' => $award,
                    'award_fac' => $award_fac,
                    'is_pro' => $award_fac > $award ? '是' : '否'
                ];
            }
        }
        $b = true;
        Db::startTrans();
        if(count($i) == 0) {
            Db::name('indent_pro')->where('salesman_uid',$uid)->update(['is_out'=>1]);
            $page = 0;
        } else {
            $a = Db::name('indent_orders')->insertAll($i);
            if(count($i) < 50) {
                $b = Db::name('indent_pro')->where('salesman_uid',$uid)->update(['is_out'=>1]);
                $page = 0;
            }
            if(!$a || !$b) Db::rollback();
        }
        Db::commit();
        $page = $page + 1;
        sleep(1);
        $this->redirect(url('portal/test/test',['page'=>$page],true,true));
//        $sql = Db::name('indent_pro')->getLastSql();
//        print_r($sql);
//        print_r($uid);exit;
//        $page = $this->request->param('page',1,'intval');
//        $num = 2000;
////        $arr = [267,295,309];
//        $uid = Db::name('indent_over')->where('status',0)
////            ->whereNotIn('uid',$arr)
//            ->group('uid')->value('uid');
////        print_r($uid);exit;
//        $list = Db::name('indent')->whereIn('salesman_uid',$uid)->paginate($num,true,['page'=>$page]);
//        $indent = $list->items();
//        $insert = [];
//        foreach ($indent as $v) {
//            unset($v['id']);
//            $insert[] = $v;
//        }
//        $res = $res2 = true;
//        Db::startTrans();
//        if(count($indent) == 0) {
//            $res2 = Db::name('indent_over')->where('uid',$uid)->update(['status'=>1]);
//        } else {
//            $res = Db::name('indent_pro')->insertAll($insert);
//        }
//        if(!$res || !$res2) Db::rollback();
//        Db::commit();
//        $all = Db::name('indent_over')->select();
//        foreach ($all as $v) {
//            $indent = Db::name('indent')->where('id',$v['indent_id'])->where('book_num',2)->count();
//            if($indent) Db::name('indent_over')->where('id',$v['id'])->delete();
//            $income = Db::name('money_income')->where('uid',$v['uid'])->where('indent_id',$v['indent_id'])->select();
//            if(count($income) == 2) {
//                $a = [];
//                foreach ($income as $vv) {
//                    $a[] = $v['money'];
//                }
//                if($a[0] != $a[1]) {
//                    Db::name('indent_over')->where('id',$v['id'])->delete();
//                }
//            } else {
//                Db::name('indent_over')->where('id',$v['id'])->delete();
//            }
//        }
//        $indent_data = Db::query("select id,uid,money,indent_id,count(indent_id) count from `cmf_money_income` group by indent_id having count(indent_id) >= 4 order by indent_id desc");
//        $a = [];
//        foreach ($indent_data as $v) {
//            if($v['count'] == 4) {
//                $data = Db::name('money_income')->where('money','>',0)->where('indent_id',$v['indent_id'])->find();
//                if($data) {
//                    $data['nickname'] = Db::name('user')->where('id',$data['uid'])->value('user_nickname');
//                    unset($data['id']);
//                    $a[] = $data;
//                }
//            }
//        }
//        Db::name('indent_over')->insertAll($a);
//        print_r($a);exit;
//        $data = Db::name('my_user')->where(['status'=>3,'inviter_phone'=>['neq','']])->select()->toArray();
//        foreach($data as $key => $vo){
//            $result = Db::name('my_user')->where(['phone'=>$vo['inviter_phone']])->value('id');
//            if(!empty($result)){
//                Db::name('my_user')->where(['id'=>$vo['id']])->update(['pid'=>$result]);
//            }
//        }
    }

    public function index(){
        //生成带参二维码
        $savePath=ROOT_PATH.'public/upload/code_img/';
        if (!file_exists($savePath)){
            mkdir($savePath, 0777,true);
        }
        $code_img = $this->code_img(28,3);
        $code = file_get_contents($code_img);
        file_put_contents(ROOT_PATH."public/upload/code_img/code_img28.png",$code);
        echo cmf_get_image_url('code_img/code_img28.png');
    }
    /**
     * 生成带参二维码
     */
    public function code_img($admin_id,$status)
    {
        $options=config('wechat_config');
        $app = new Application($options);
        $accessToken = $app->access_token; // EasyWeChat\Core\AccessToken 实例
        $token = $accessToken->getToken(false);
        $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=' . $token;
        $param = [
            'expire_seconds' => '',
            'action_name' => 'QR_LIMIT_STR_SCENE',
            'action_info' => ['scene' => ['scene_str' => "$admin_id"."-"."$status"]]
        ];
        $result = $this->api_notice_increment($url, json_encode($param));
        $data = json_decode($result, true);
        $ticket = urlencode($data['ticket']);
        $qr_url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . $ticket;
        return $qr_url;
    }

    /**
     * 获取二维码提交
     */
    public function api_notice_increment($url, $data)
    {
//        $data=json_encode($data);
        $ch = curl_init();
        $header = array("Accept-Charset: utf-8");
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        if (curl_errno($ch)) {
            curl_close($ch);
            return $ch;
        } else {
            curl_close($ch);
            return $tmpInfo;
        }
    }
}