OrderController.php 13.4 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/5
 * Time: 15:21
 */

namespace app\admin\controller;


use cmf\controller\AdminBaseController;
use think\Db;
use think\Validate;

class OrderController extends AdminBaseController
{
    public function index(){
        $where=[];
        $whereor = '';
        $param = $this->request->param();
        $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a_a.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a_a.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a_a.create_time'] = ['<= time', $endTime];
            }
        }
        $keyword = empty($param['keyword']) ? '' : $param['keyword'];
        if (!empty($keyword)) {
            $where['o.num|c.product_name|c.insurance_num|c.application|c.insurer|c.agent_name|c.agent_phone'] = ['like', "%$keyword%"];
        }
        $time = time();
        $status = empty($param['status']) ? '' : $param['status'];
        if(!empty($status)){//0为全部1为托管中2为即将到期3为未托管
            if($param['status'] == 1){
                $where['o.status'] = ['eq',1];
                $whereor = "o.status = 3 and o.order_expire_time2 > $time";
            }else if($param['status'] == 2){
                $where['o.status'] = ['eq',3];
                $where['o.order_expire_time2'] = ['>',$time];
                $where['o.order_about_time'] = array(array('<',$time),array('>',$time-30*24*60*60));
                $whereor = "o.order_about_time < $time and o.order_about_time > ".($time-30*24*60*60)." and o.order_expire_time2 > $time";
            }else if($param['status'] == 3){
                $where['o.status'] = ['eq',3];
                $where['o.order_expire_time2'] = ['lt',$time];//"(o.status = 3 and o.order_expire_time2 < $time)";
            }
        }
        $field = "c.*,i_c.thumbnail,i_c.insurance_company_name,o.order_expire_time,o.order_about_time,o.status,o.id o_id,o.pay_time,o.num,o.order_expire_time2";
        $data= Db::name('order_info')
            ->alias('o_i')
            ->field($field)
            ->join('cmf_order o','o.id = o_i.order_id')
            ->join('cmf_collocation c','c.id = o_i.collocation_id')
            ->join('cmf_insurance_company i_c','c.insurance_company_id = i_c.id')
            ->where('c.delete_time',0)
            ->where($where)
            ->whereor($whereor)
            ->order('o.create_time desc')
//            ->fetchSql()
//            ->select();
//        echo $data;
//        exit();
            ->paginate(10);
        $data->appends($param);
        $list=$data->items();
        $this->assign([
            'data'=>$list,
            'page'=>$data->render(),
            'start_time'=>isset($param['start_time']) ? $param['start_time'] : '',
            'end_time'=>isset($param['end_time']) ? $param['end_time'] : '',
            'keyword'=>isset($param['keyword']) ? $param['keyword'] : '',
            'status'=>isset($param['status']) ? $param['status'] : '',
        ]);
        return $this->fetch();
    }
    //编辑
    public function edit(){
        $id=$this->request->param('id', 0, 'intval');
        if($this->request->isPost()){
            $param=$this->request->param();
            $param['insurer_time'] = strtotime($param['insurer_time']);
            $validate = new Validate([
                'insurance_company_id' => 'require',
                'product_name' => 'require',
                'insurance_type_id' => 'require',
                'insurance_price' => 'require',
                'pay_method_id' => 'require',
                'payment_time' => 'require',
                'pay_method_price' => 'require',
                'guarantee_deadline_time' => 'require',
                'insurance_num' => 'require',
                'application' => 'require',
                'insurer' => 'require',
                'relation' => 'require',
                'insurer_birthday' => 'require',
                'insurer_time' => 'require',
                'take_time' => 'require',
//                'insure_time' => 'require',
                'remit'=>'require',
            ]);
            $validate->message([
                'insurance_company_id' => '请选择主险的保险公司!',
                'product_name' => '主险产品名不能为空!',
                'insurance_type_id' => '请选择主险的保单类型!',
                'insurance_price' => '主险保险金额不能为空!',
                'pay_method_id' => '请选择主险的缴费方式!',
                'payment_time' => '主险缴费方式不能为空!',
                'pay_method_price' => '主险期缴保费不能为空!',
                'guarantee_deadline_time' => '主险保障期限不能为空!',
                'insurance_num' => '保险单号不能为空!',
                'application' => '投保人不能为空!',
                'insurer' => '被保人不能为空!',
                'relation' => '与被保人关系不能为空!',
                'insurer_birthday' => '保险人生日不能为空!',
                'insurer_time' => '投保日期不能为空!',
                'take_time' => '合同生效日起不能为空!',
//                'insure_time' => '保险时间不能为空!',
                'remit'=>'请选择是否豁免!',
            ]);
            if (!$validate->check($param)) {
                $this->error($validate->getError());
            }
            if(empty($param['image'])){
                $this->error('至少上传一张图片!');
            }
            $subjoin_data = [];
            if(!empty($param['subjoin_product_name'])){
                foreach($param['subjoin_product_name'] as $key => $value){
                    if(empty($param['subjoin_product_name'][$key])){
                        $this->error("附加险".($key+1)."的产品名不能为空!");
                    }
                    if(empty($param['subjoin_insurance_type_id'][$key])){
                        $this->error("附加险".($key+1)."的保单类型不能为空!");
                    }
                    if(empty($param['subjoin_price'][$key])){
                        $this->error("附加险".($key+1)."的保险金额不能为空!");
                    }
//                    if(empty($param['subjoin_time'][$key])){
//                        $this->error("附加险".($key+1)."的保险时间不能为空!");
//                    }
                    if(empty($param['subjoin_payment_time'][$key])){
                        $this->error("附加险".($key+1)."的缴费期限不能为空!");
                    }
                    if(empty($param['subjoin_pay_method_price'][$key])){
                        $this->error("附加险".($key+1)."的期缴保费不能为空!");
                    }
                    if(empty($param['subjoin_guarantee_deadline_time'][$key])){
                        $this->error("附加险".($key+1)."的保障期限不能为空!");
                    }
                    $array = [
                        'subjoin_product_name' => $value,
                        'insurance_type_id' => $param['subjoin_insurance_type_id'][$key],
                        'subjoin_price' => $param['subjoin_price'][$key],
//                        'subjoin_time' => $param['subjoin_time'][$key],
                        'payment_time' => $param['subjoin_payment_time'][$key],
                        'subjoin_pay_method_price' => $param['subjoin_pay_method_price'][$key],
                        'subjoin_guarantee_deadline_time' => $param['subjoin_guarantee_deadline_time'][$key],
                        'collocation_id' => $id,
                        'create_time' => time(),
                        'user_id' => $param['user_id']
                    ];
                    array_push($subjoin_data, $array);
                }
            }
            //清除当前主险下的所有附加险
            Db::name('subjoin_insurance')->where('collocation_id',$id)->delete();
            Db::name('subjoin_insurance')->insertAll($subjoin_data);
            $data = [
                'insurance_company_id' => $param['insurance_company_id'],
                'product_name' => $param['product_name'],
                'insurance_type_id' => $param['insurance_type_id'],
                'insurance_price' => $param['insurance_price'],
                'pay_method_id' => $param['pay_method_id'],
                'payment_time' => $param['payment_time'],
                'pay_method_price' => $param['pay_method_price'],
                'guarantee_deadline_time' => $param['guarantee_deadline_time'],
                'insurance_num' => $param['insurance_num'],
                'application' => $param['application'],
                'insurer' => $param['insurer'],
                'relation' => $param['relation'],
                'insurer_birthday' => $param['insurer_birthday'],
                'insurer_time' => $param['insurer_time'],
                'take_time' => strtotime($param['take_time']),
//                'insure_time' => $param['insure_time'],
                'remit' => $param['remit'],
                'image' => !empty($param['image']) ? json_encode($param['image']) : null,
                'expire_time' => strtotime($param['take_time'])+$param['payment_time']*365*24*60*60,
            ];
            //更新主险
            Db::name('collocation')->where('id',$id)->update($data);
            $data4 = [];
            $payment_time = $param['payment_time'];
            for($i=0;$i<$payment_time;$i++){
                if($data['expire_time']-($i*365*24*60*60)-(30*24*60*60)>time()){
                    $data3['user_id'] = $param['user_id'];
                    $data3['collocation_id'] = $id;
                    $data3['time'] = $data['expire_time']-($i*365*24*60*60)-(30*24*60*60);
                    $data4[] = $data3;
                }else{
                    $data3['time'] = $param['payment_time'];
                    $data4[] = $data3;
                }
            }
            $array3 = array('order_expire_time'=>$data['expire_time'],'order_about_time'=>$data4[count($data4)-1]['time']);
            $a = Db::name('order')->where(array('collocation_id'=>$id,'user_id'=>$param['user_id']))->update($array3);
            $this->success('更新成功!');
        }else{
            $field = "c.*,i_c.thumbnail,c.insurance_company_id,i_c.insurance_company_name,o.order_expire_time,o.order_about_time,o.status,o.id o_id,o.pay_time,o.num,o.order_expire_time2";
            $data=Db::name('order_info')
                ->alias('o_i')
                ->field($field)
                ->join('cmf_order o','o.id = o_i.order_id')
                ->join('cmf_collocation c','c.id = o_i.collocation_id')
                ->join('cmf_insurance_company i_c','c.insurance_company_id = i_c.id')
                ->where('c.id',$id)
                ->find();
            $subjoin = Db::name('subjoin_insurance')->where('collocation_id',$id)->select()->toArray();
            $data['subjoin'] = $subjoin;
            $insurance_company = Db::name('insurance_company')
                ->where('delete_time',0)
                ->select()->toArray();
            $this->assign([
                'data'=>$data,
                'insurance_company'=>$insurance_company,
            ]);
            return $this->fetch();
        }
    }
    //删除
    public function delete(){
        $param = $this->request->param();

        if (isset($param['id'])) {
            $id = $this->request->param('id', 0, 'intval');
            $resultPortal = Db::name('application_agent')
                ->where(['id' => $id])
                ->update(['delete_time' => time()]);
            if($resultPortal){
                $this->success("删除成功!", '');
            }else{
                $this->error("删除失败!", '');
            }

        }

        if (isset($param['ids'])) {
            $ids = $this->request->param('ids/a');
            $result = Db::name('application_agent')
                ->where(['id' => ['in', $ids]])
                ->update(['delete_time' => time()]);
            if ($result) {
                $this->success("删除成功!", '');
            }else{
                $this->error("删除失败!", '');
            }
        }
    }
    //审核
    public function publish(){
        $param=$this->request->param();
        $ids = $this->request->param('ids/a');
        if(!empty($ids)&&!empty($param['yes'])){
            Db::name('application_agent')->where(['id'=>['in',$ids]])->update(array('status'=>2));
            foreach($ids as $key => $i1){
                $data = Db::name('application_agent')->where('id',$i1)->find();
                Db::name('user')->where('id',$data['user_id'])->update(array('mobile2'=>$data['phone'],'type'=>2,'agent_name'=>$data['name']));
            }
            $this->success('操作成功!','');
        }else{
            Db::name('application_agent')->where(['id'=>['in',$ids]])->update(array('status'=>3));
            foreach($ids as $key => $i2){
                $data = Db::name('application_agent')->where('id',$i2)->find();
                Db::name('user')->where('id',$data['user_id'])->update(array('mobile2'=>null,'type'=>1,'agent_name'=>null));
            }
            $this->success('操作成功!','');
        }
    }
}