审查视图

app/admin/controller/OrderController.php 12.5 KB
王晓刚 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?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=[];
王晓刚 authored
20
        $whereor = '';
王晓刚 authored
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
        $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)) {
王晓刚 authored
36
            $where['o.num|c.product_name|c.insurance_num|c.application|c.insurer|c.agent_name|c.agent_phone'] = ['like', "%$keyword%"];
王晓刚 authored
37
        }
王晓刚 authored
38
        $time = time();
王晓刚 authored
39
        $status = empty($param['status']) ? '' : $param['status'];
王晓刚 authored
40 41 42 43 44 45 46 47 48 49 50 51 52 53
        if(!empty($status)){//0为全部1为托管中2为即将到期3为未托管
            if($param['status'] == 1){
                $where['o.status'] = ['eq',1];
                $where['o.order_about_time'] = ['>',$time];
                $whereor = "o.status = 3 and o.order_about_time > $time";
            }else if($param['status'] == 2){
                $where['o.status'] = ['eq',3];
                $where['o.order_about_time'] = ['<',$time];
                $where['o.order_expire_time'] = ['>',$time];
//                $whereor = "o.status = 3 and o.order_about_time < $time";
            }else if($param['status'] == 3){
                $where['o.status'] = ['eq',2];
                $whereor = "o.status = 3 and o.order_expire_time < $time";
            }
王晓刚 authored
54
        }
王晓刚 authored
55
        $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";
王晓刚 authored
56 57 58 59 60 61
        $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')
王晓刚 authored
62
            ->where('c.delete_time',0)
王晓刚 authored
63
            ->where($where)
王晓刚 authored
64
            ->whereor($whereor)
王晓刚 authored
65
            ->order('o.create_time desc')
王晓刚 authored
66 67 68 69 70
//            ->fetchSql()
//            ->select();
//        echo $data;
//        exit();
            ->paginate(10);
王晓刚 authored
71 72 73 74 75 76 77 78
        $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'] : '',
王晓刚 authored
79
            'status'=>isset($param['status']) ? $param['status'] : '',
王晓刚 authored
80 81 82 83 84 85 86 87
        ]);
        return $this->fetch();
    }
    //编辑
    public function edit(){
        $id=$this->request->param('id', 0, 'intval');
        if($this->request->isPost()){
            $param=$this->request->param();
王晓刚 authored
88
            $param['insurer_time'] = strtotime($param['insurer_time']);
王晓刚 authored
89
            $validate = new Validate([
王晓刚 authored
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
                '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',
王晓刚 authored
107 108
            ]);
            $validate->message([
王晓刚 authored
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                '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'=>'请选择是否豁免!',
王晓刚 authored
126 127 128 129
            ]);
            if (!$validate->check($param)) {
                $this->error($validate->getError());
            }
王晓刚 authored
130 131 132 133 134 135 136
            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])){
王晓刚 authored
137
                        $this->error("附加险".($key+1)."的产品名不能为空!");
王晓刚 authored
138 139
                    }
                    if(empty($param['subjoin_insurance_type_id'][$key])){
王晓刚 authored
140
                        $this->error("附加险".($key+1)."的保单类型不能为空!");
王晓刚 authored
141 142
                    }
                    if(empty($param['subjoin_price'][$key])){
王晓刚 authored
143
                        $this->error("附加险".($key+1)."的保险金额不能为空!");
王晓刚 authored
144 145
                    }
                    if(empty($param['subjoin_time'][$key])){
王晓刚 authored
146
                        $this->error("附加险".($key+1)."的保险时间不能为空!");
王晓刚 authored
147 148
                    }
                    if(empty($param['subjoin_payment_time'][$key])){
王晓刚 authored
149
                        $this->error("附加险".($key+1)."的缴费期限不能为空!");
王晓刚 authored
150 151
                    }
                    if(empty($param['subjoin_pay_method_price'][$key])){
王晓刚 authored
152
                        $this->error("附加险".($key+1)."的期缴保费不能为空!");
王晓刚 authored
153 154
                    }
                    if(empty($param['subjoin_guarantee_deadline_time'][$key])){
王晓刚 authored
155
                        $this->error("附加险".($key+1)."的保障期限不能为空!");
王晓刚 authored
156 157 158 159 160 161 162 163 164 165
                    }
                    $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,
王晓刚 authored
166 167
                        'create_time' => time(),
                        'user_id' => $param['user_id']
王晓刚 authored
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
                    ];
                    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'],
王晓刚 authored
189
                'insurer_time' => $param['insurer_time'],
王晓刚 authored
190 191 192 193 194 195 196 197
                '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['guarantee_deadline_time']*365*24*60*60,
            ];
            //更新主险
            Db::name('collocation')->where('id',$id)->update($data);
王晓刚 authored
198 199
            $this->success('更新成功!');
        }else{
王晓刚 authored
200
            $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";
王晓刚 authored
201 202 203 204 205 206 207
            $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)
王晓刚 authored
208
                ->find();
王晓刚 authored
209
            $subjoin = Db::name('subjoin_insurance')->where('collocation_id',$id)->select()->toArray();
王晓刚 authored
210
            $data['subjoin'] = $subjoin;
王晓刚 authored
211 212
            $insurance_company = Db::name('insurance_company')
                ->where('delete_time',0)
王晓刚 authored
213
                ->select()->toArray();
王晓刚 authored
214 215
            $this->assign([
                'data'=>$data,
王晓刚 authored
216
                'insurance_company'=>$insurance_company,
王晓刚 authored
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
            ]);
            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('操作成功!','');
        }
    }
}