LoadController.php 6.6 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/28
 * Time: 15:41
 */

namespace app\index\controller;


use app\index\model\OrderInfoModel;
use cmf\controller\HomeBaseController;
use EasyWeChat\Foundation\Application;
use think\Db;

class LoadController extends HomeBaseController
{
    public function demo($data){
        $middle = $data[0];//取一个中间值
        $left = [];//存放小于中间值的数据
        $right = [];//存放大于中间值的数据
        for($i=0;$i<count($data);$i++){
            if($middle['distance']<$data[$i]['distance']){
                $left[] = $data[$i];
            }else{
                $right[] = $data[$i];
            }
        }
        $left = $this->demo($left);
        $right = $this->demo($right);
        return array_merge($left, array($middle), $right);
    }
    public function test(){
        $array=[
            ['id'=>1,'name'=>'天津西青','distance'=>10,'unit'=>'KM'],
            ['id'=>2,'name'=>'天津河西','distance'=>15,'unit'=>'KM'],
            ['id'=>3,'name'=>'山东滨州','distance'=>220,'unit'=>'KM'],
            ['id'=>4,'name'=>'山东青岛','distance'=>800,'unit'=>'KM'],
            ['id'=>5,'name'=>'天津南开','distance'=>2,'unit'=>'KM'],
            ['id'=>6,'name'=>'天津河东','distance'=>20,'unit'=>'KM'],
            ['id'=>7,'name'=>'天津北辰','distance'=>18,'unit'=>'KM'],
            ['id'=>8,'name'=>'天津红桥','distance'=>13,'unit'=>'KM'],
            ['id'=>9,'name'=>'乌鲁木齐','distance'=>3800,'unit'=>'KM'],
            ['id'=>10,'name'=>'河北邯郸','distance'=>150,'unit'=>'KM'],
            ['id'=>11,'name'=>'河北石家庄','distance'=>250,'unit'=>'KM']
        ];
        $this->demo($array);
    }
    public function index(){
        $type = $this->request->param('type');
        if(!$type){
            $arr['code'] = 40001;
            $arr['msg'] = '缺点参数!';
            return json_encode($arr);
        }
        //查询所有在托管中的保单
        $orderInfoModel = new OrderInfoModel();
        $time = time();
        if($type == 1){
            $where['o.status'] = ['eq',3];
            $where['o.order_about_time'] = array(array('<',$time),array('>',$time-30*24*60*60));//array(array('gt',1),array('lt',10))
            $whereor = "o.status = 1 and o.order_about_time < $time and o.order_about_time > ".($time-30*24*60*60);
            $data = $orderInfoModel->selectData1($where,$whereor);
            dump($data);
            foreach($data as $key1 => $vo){
                //判断是否已经发送
                $send_vice = Db::name('send_vice')->where(array('order_id'=>$vo['o_id'],'collocation_id'=>$vo['id'],'time'=>$vo['order_about_time'],'type'=>1))->find();
                if(empty($send_vice)){
                    //模板消息
                    $templateId = '83NoWJ74UmI-DY_FwrDrTBx1iysu9__6fosB-eMFm78';
                    $data1 = array(
                        'first'=>"尊敬的用户,您有一份保单即将到期需要续费,请在保障截止日期前完成续保,否则保单将失效",
                        'keyword1'=>$vo['insurer'],
                        'keyword2'=>$vo['product_name'],
                        'keyword3'=>$vo['insurance_num'],
                        'keyword4'=>date('Y-m-d',$vo['order_about_time']+30*24*60*60),
                        'remark'=>"请在保障期间内续费",
                    );
                    $url = url('/index/all_guarantee/guarantee_info',array('id'=>$vo['o_id']),'',true);
                    $user1 = Db::name('third_party_user')->where('user_id',$vo['user_id'])->find();
                    $this->template($templateId,$data1,$url,$user1['openid']);
                    //短信
                    $user2 = Db::name('user')->where('id',$vo['user_id'])->find();
                    $content = "【橙象保单】尊敬的用户,您有一份保单即将到期需要续费,请在保障截止日期前完成续保,否则保单将失效,保险单号$vo[insurance_num],请在".date('Y月m日d',$vo['order_about_time']+30*24*60*60)."之前续费。";
                    $this->note($content,$user2['mobile']);
                    Db::name('send_vice')->insert(array('order_id'=>$vo['o_id'],'collocation_id'=>$vo['id'],'time'=>$vo['order_about_time'],'user_id'=>$vo['user_id'],'create_time'=>time(),'type'=>1));
                    //Db::name('order')->where(['id'=>$vo['o_id']])->setInc('order_about_time',30*24*60*60);
                }
            }
        }else if($type == 2){
            //$data['status'] eq 2 || $data['order_expire_time'] lt time() || ($data['order_about_time']+24*30*60*60) lt time()
//            $where['o.status'] = ['eq',2];
            $where = [];
            $whereor = "(o.status = 1 and o.order_about_time < ".($time-24*30*60*60)." and o.delete_time = 0) or (o.status = 3 and o.order_about_time < ".($time-24*30*60*60).")";
            $data = $orderInfoModel->selectData1($where,$whereor);
            foreach($data as $key1 => $vo){
                //判断是否已经发送
                $send_vice = Db::name('send_vice')->where(array('order_id'=>$vo['o_id'],'collocation_id'=>$vo['id'],'time'=>$vo['order_about_time'],'type'=>2))->find();
                if(empty($send_vice)){
                    //短信
                    $user2 = Db::name('user')->where('id',$vo['user_id'])->find();
                    $content = "【橙象保单】尊敬的用户,您有一份保单已经失效,保险单号$vo[insurance_num]";
                    $this->note($content,$user2['mobile']);
                    Db::name('send_vice')->insert(array('order_id'=>$vo['o_id'],'collocation_id'=>$vo['id'],'time'=>$vo['order_about_time'],'user_id'=>$vo['user_id'],'create_time'=>time(),'type'=>2));
                }
            }
        }
    }
    public function template($templateId,$data,$url=null,$openid){
        $options=config('wechat_config');
        $app = new Application($options);
        $notice = $app->notice;
        if(empty($url)){
            $notice->uses($templateId)->andData($data)->andReceiver($openid)->send();
        }else{
            $notice->uses($templateId)->withUrl($url)->andData($data)->andReceiver($openid)->send();
        }
    }
    public function note($content,$mobile){
        $data = array(
            'content' 	=> $content,//短信内容
            'mobile' 	=> $mobile,//手机号码
            'productid' => '887361',//产品id
            'xh'		=> ''//小号
        );
        $result = send_sms($data);
        if(substr($result,0,strpos($result,',')) == "1"){
            echo '发送成功!';
        }else{
            echo '接口出错!';
        }
    }
}