审查视图

app/admin/controller/OrderController.php 13.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
        if(!empty($status)){//0为全部1为托管中2为即将到期3为未托管
            if($param['status'] == 1){
                $where['o.status'] = ['eq',1];
王晓刚 authored
43
                $whereor = "o.status = 3 and o.order_expire_time2 > $time";
王晓刚 authored
44 45
            }else if($param['status'] == 2){
                $where['o.status'] = ['eq',3];
王晓刚 authored
46 47 48
                $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";
王晓刚 authored
49
            }else if($param['status'] == 3){
王晓刚 authored
50
                $where['o.status'] = ['eq',3];
王晓刚 authored
51
                $where['o.order_expire_time2'] = ['lt',$time];//"(o.status = 3 and o.order_expire_time2 < $time)";
王晓刚 authored
52
            }
王晓刚 authored
53
        }
王晓刚 authored
54
        $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";
王晓刚 authored
55 56 57 58 59 60
        $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
61
            ->where('c.delete_time',0)
王晓刚 authored
62
            ->where($where)
王晓刚 authored
63
            ->whereor($whereor)
王晓刚 authored
64
            ->order('o.create_time desc')
王晓刚 authored
65 66 67 68 69
//            ->fetchSql()
//            ->select();
//        echo $data;
//        exit();
            ->paginate(10);
王晓刚 authored
70 71 72 73 74 75 76 77
        $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
78
            'status'=>isset($param['status']) ? $param['status'] : '',
王晓刚 authored
79 80 81 82 83 84 85 86
        ]);
        return $this->fetch();
    }
    //编辑
    public function edit(){
        $id=$this->request->param('id', 0, 'intval');
        if($this->request->isPost()){
            $param=$this->request->param();
王晓刚 authored
87
            $param['insurer_time'] = strtotime($param['insurer_time']);
王晓刚 authored
88
            $validate = new Validate([
王晓刚 authored
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
                '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',
王晓刚 authored
104
//                'insure_time' => 'require',
王晓刚 authored
105
                'remit'=>'require',
王晓刚 authored
106 107
            ]);
            $validate->message([
王晓刚 authored
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
                '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' => '合同生效日起不能为空!',
王晓刚 authored
123
//                'insure_time' => '保险时间不能为空!',
王晓刚 authored
124
                'remit'=>'请选择是否豁免!',
王晓刚 authored
125 126 127 128
            ]);
            if (!$validate->check($param)) {
                $this->error($validate->getError());
            }
王晓刚 authored
129 130 131 132 133 134 135
            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
136
                        $this->error("附加险".($key+1)."的产品名不能为空!");
王晓刚 authored
137 138
                    }
                    if(empty($param['subjoin_insurance_type_id'][$key])){
王晓刚 authored
139
                        $this->error("附加险".($key+1)."的保单类型不能为空!");
王晓刚 authored
140 141
                    }
                    if(empty($param['subjoin_price'][$key])){
王晓刚 authored
142
                        $this->error("附加险".($key+1)."的保险金额不能为空!");
王晓刚 authored
143
                    }
王晓刚 authored
144 145 146
//                    if(empty($param['subjoin_time'][$key])){
//                        $this->error("附加险".($key+1)."的保险时间不能为空!");
//                    }
王晓刚 authored
147
                    if(empty($param['subjoin_payment_time'][$key])){
王晓刚 authored
148
                        $this->error("附加险".($key+1)."的缴费期限不能为空!");
王晓刚 authored
149 150
                    }
                    if(empty($param['subjoin_pay_method_price'][$key])){
王晓刚 authored
151
                        $this->error("附加险".($key+1)."的期缴保费不能为空!");
王晓刚 authored
152 153
                    }
                    if(empty($param['subjoin_guarantee_deadline_time'][$key])){
王晓刚 authored
154
                        $this->error("附加险".($key+1)."的保障期限不能为空!");
王晓刚 authored
155 156 157 158 159
                    }
                    $array = [
                        'subjoin_product_name' => $value,
                        'insurance_type_id' => $param['subjoin_insurance_type_id'][$key],
                        'subjoin_price' => $param['subjoin_price'][$key],
王晓刚 authored
160
//                        'subjoin_time' => $param['subjoin_time'][$key],
王晓刚 authored
161 162 163 164
                        '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
165 166
                        'create_time' => time(),
                        'user_id' => $param['user_id']
王晓刚 authored
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
                    ];
                    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
188
                'insurer_time' => $param['insurer_time'],
王晓刚 authored
189
                'take_time' => strtotime($param['take_time']),
王晓刚 authored
190
//                'insure_time' => $param['insure_time'],
王晓刚 authored
191 192
                'remit' => $param['remit'],
                'image' => !empty($param['image']) ? json_encode($param['image']) : null,
王晓刚 authored
193
                'expire_time' => strtotime($param['take_time'])+$param['payment_time']*365*24*60*60,
王晓刚 authored
194 195 196
            ];
            //更新主险
            Db::name('collocation')->where('id',$id)->update($data);
王晓刚 authored
197 198 199 200 201 202 203 204 205 206 207 208 209
            $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;
                }
            }
王晓刚 authored
210
            dump($data4[count($data4)-1]['time']);
王晓刚 authored
211
            $array3 = array('order_expire_time'=>$data['expire_time'],'order_about_time'=>$data4[count($data4)-1]['time']);
王晓刚 authored
212 213
            $a = Db::name('order')->where(array('collocation_id'=>$id,'user_id'=>$param['user_id']))->update($array3);
            dump($a);
王晓刚 authored
214 215
            $this->success('更新成功!');
        }else{
王晓刚 authored
216
            $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
217 218 219 220 221 222 223
            $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
224
                ->find();
王晓刚 authored
225
            $subjoin = Db::name('subjoin_insurance')->where('collocation_id',$id)->select()->toArray();
王晓刚 authored
226
            $data['subjoin'] = $subjoin;
王晓刚 authored
227 228
            $insurance_company = Db::name('insurance_company')
                ->where('delete_time',0)
王晓刚 authored
229
                ->select()->toArray();
王晓刚 authored
230 231
            $this->assign([
                'data'=>$data,
王晓刚 authored
232
                'insurance_company'=>$insurance_company,
王晓刚 authored
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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
            ]);
            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('操作成功!','');
        }
    }
}