Files
liche/api/controllers/wxapp/licheb/Cusorder.php
T
2021-07-22 18:14:12 +08:00

318 lines
12 KiB
PHP

<?php
defined('WXAPP_APP') OR exit('No direct script access allowed');
/**
* Created by Vim
* User: lcc
* Date: 2021/07/09
* Time: 14:08
*/
require_once APPPATH.'controllers/wxapp/Wxapp.php';
class Cusorder extends Wxapp{
function __construct($inputs, $app_key){
parent::__construct($inputs, $app_key);
$this->login_white = array();//登录白名单
$this->check_status = array();//用户状态校验
$this->check_mobile = array();//需要手机号
$this->check_headimg =array();//授权微信信息
$this->load->model('receiver/receiver_customers_model','customers_model');
$this->load->model('receiver/order/receiver_orders_model','orders_model');
$this->load->model('receiver/order/receiver_order_signs_model','order_signs_model');
$this->load->model('receiver/order/receiver_order_bills_model','order_bills_model');
$this->load->model('receiver/order/receiver_order_deliverys_model','order_deliverys_model');
$this->load->model('auto/auto_series_model');
$this->load->model('auto/auto_brand_model');
$this->load->model('auto/auto_attr_model');
}
protected function get(){
$id = $this->input_param('id');
if($id){
return $this->detail($id);
}else{
return $this->lists();
}
}
//创建订单
protected function post(){
$cus_id = $this->input_param('cus_id');
$car_id = $this->input_param('car_id');
$color_id = $this->input_param('color_id');
$incolor_id = $this->input_param('incolor_id');
$v_id = $this->input_param('v_id');
$price = $this->input_param('price');
$deposit = $this->input_param('deposit');
$payway = $this->input_param('payway');
$row = $this->customers_model->get(['id'=>$cus_id]);
$series_row = $this->auto_series_model->get(['id'=>$car_id]);
if(!$row || !$series_row){
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
//判断是否存在未完成流程
if($this->orders_model->get_step($row['mobile'])){
throw new Exception('该手机号用户存在未完成订单', API_CODE_FAIL);
}
$where = [
"id in ($v_id,$color_id,$incolor_id)" => null
];
$attr_row = $this->auto_attr_model->map('id','',$where);
if($attr_row[$color_id]){
$color_row = $attr_row[$color_id][0];
$color_row['jsondata'] = json_decode($color_row['jsondata'],true);
}
if($attr_row[$incolor_id]){
$incolor_row = $attr_row[$incolor_id][0];
$incolor_row['jsondata'] = json_decode($incolor_row['jsondata'],true);
}
if($attr_row[$v_id]){
$version_row = $attr_row[$v_id][0];
$version_row['jsondata'] = json_decode($version_row['jsondata'],true);
}
$car_json = [
'c_id' => $color_id,
'inc_id' => $incolor_id,
'v_id' => $v_id,
'color' => isset($color_row) ? $color_row : '',
'incolor' => isset($incolor_row) ? $incolor_row : '',
'version' => isset($version_row) ? $version_row : ''
];
$data = [
'rid' => $cus_id,
'sid' => create_order_no(350200,$this->app_key),
'name' => $row['name'],
'mobile' => $row['mobile'],
'biz_id' => $this->session['biz_id'],
'brand_id' => $series_row['brand_id'],
's_id' => $series_row['id'],
'v_id' => $v_id,
'cor_id' => $color_id,
'incor_id' => $incolor_id,
'admin_id' => $this->session['uid'],
'car_json' => json_encode($car_json,JSON_UNESCAPED_UNICODE),
'price' => $price,
'deposit' => $deposit,
'c_time' => time()
];
$payway && $data['payway'] = 1;
$o_id = $this->orders_model->add($data);
if($o_id){
$sign_data = [
'o_id' => $o_id,
'c_time' => time()
];
$this->order_signs_model->add($sign_data);
return ['id'=>$o_id];
}else{
throw new Exception('创建失败', ERR_PARAMS_ERROR);
}
}
//修改订单信息
protected function put(){
$id = $this->input_param('id');
$payway = $this->input_param('payway');
$row = $this->orders_model->get(['id'=>$id]);
if(!$row){
throw new Exception('订单不存在', ERR_PARAMS_ERROR);
}
$result = false;
$up_data = [];
if(strlen($payway)){
if($row['status']>0){
throw new Exception('修改失败,已签订合同', ERR_PARAMS_ERROR);
}
$up_data['payway'] = $payway;
}
if($up_data){
$result = $this->orders_model->update($up_data,['id'=>$id]);
}
if($result){
throw new Exception('修改成功', API_CODE_SUCCESS);
}else{
throw new Exception('修改失败', ERR_PARAMS_ERROR);
}
}
//订单列表头部
protected function get_tabs(){
$rows = $this->orders_model->get_status();
$lists = [];
if($rows){
foreach($rows as $key=>$val){
$lists[] = [
'key' => $key,
'name' => $val
];
}
}
return $lists;
}
//订单列表
private function lists(){
$group_id = $this->session['group_id'];
$uid = $this->session['uid'];
$biz_id = $this->session['biz_id'];
$keyword = $this->input_param('keyword');
$status = $this->input_param('status');
$where = [
'biz_id' => $biz_id
];
if($group_id==1){ //销售
$where ["admin_id"] = $uid;
}
if($keyword){
$where["(name='{$keyword}' or mobile='{$keyword}')"] = null;
}
strlen($status) && $where['status'] = $status;
$fileds = 'id,name,mobile,car_json,brand_id,s_id,deposit,payway,status,c_time';
$count = $this->orders_model->count($where);
$lists = [];
if($count){
$rows = $this->orders_model->select($where,'id desc',$page,$size,$fileds);
//品牌车型
$brand_arr = array_unique(array_column($rows,'brand_id'));
$brands = $this->auto_brand_model->get_map_by_ids($brand_arr,'id,name');
//车系车型
$series_arr = array_unique(array_column($rows,'s_id'));
$series = $this->auto_series_model->get_map_by_ids($series_arr,'id,name');
$status_arr = $this->orders_model->get_status();
foreach($rows as $key=>$val){
$car_json = json_decode($val['car_json'],true);
$color = isset($car_json['color']) ? $car_json['color']['title'] : '';
$version = isset($car_json['version']) ? $car_json['version']['title'] : '';
$brand_name = isset($brands[$val['brand_id']]) ? $brands[$val['brand_id']][0]['name'] : '';
$serie_name = isset($series[$val['s_id']]) ? $series[$val['s_id']][0]['name'] : '';
$other_data = [
'品牌车型' => "{$brand_name}{$serie_name}-{$color}-{$version}",
'付款方式' => $val['payway']?'全款':'分期',
'代办车牌' => '需要',
'定金金额' => $val['deposit'],
'订单日期' => date('Y-m-d',$val['c_time']),
];
$lists[] = [
'id' => $val['id'],
'name' => $val['name'],
'mobile' => mobile_asterisk($val['mobile']),
'status_name' => $status_arr[$val['status']],
'other_data' => $other_data,
];
}
}
$data = [
'list' => $lists,
'total' => $count
];
return $data;
}
//订单详情
private function detail($id){
$row = $this->orders_model->get(['id'=>$id]);
if(!$row){
throw new Exception('订单不存在', ERR_PARAMS_ERROR);
}
$brand = $this->auto_brand_model->get(['id'=>$row['brand_id']],'name');
$series = $this->auto_series_model->get(['id'=>$row['s_id']],'name');
$car_json = json_decode($row['car_json'],true);
$color = isset($car_json['color']) ? $car_json['color']['title'] : '';
$version = isset($car_json['version']) ? $car_json['version']['title'] : '';
//贷款信息
$loan_data = [];
$loan_status = 0;
if(!$row['payway'] && $row['status']>=1){
$this->load->model('receiver/order/receiver_order_loans_model','order_loans_model');
$loan = $this->order_loans_model->get(['o_id'=>$row['id']]);
if($loan){
$loan_data = [
'id' => $loan['id'],
'bank' => $loan['title'],
'year' => $loan['year'],
'money' => $loan['first_price']
];
}
$loan_status = $loan['status']>0 ? 2 : 1;
}
$car_data = [
'车辆名称' => $brand['name'].$series['name'],
'车辆级别' => $version,
'颜色' => $color,
'车辆平台价格' => $row['price'],
'定金' => $row['deposit']
];
//开票信息
$bill_status = 0;
$bill_data = [];
if($row['status']>=3){
$bill = $this->order_bills_model->get(['o_id'=>$row['id']]);
if($bill){
$bill_data = [
'id' => $bill['id'],
'carid_a' => $bill['cardidA'] ? build_qiniu_image_url($bill['cardidA']) : '',
'carid_a_path' => $bill['cardidA'] ? $bill['cardidA'] : '',
'carid_b' => $bill['cardidB'] ? build_qiniu_image_url($bill['cardidB']) : '',
'carid_b_path' => $bill['cardidB'] ? $bill['cardidB'] : '',
];
}
$bill_status = $bill['status'] ? 2 : 1;
}
//确认车辆状态
$delivery = $this->order_deliverys_model->get(['o_id'=>$row['id']],'status');
$ckcar_status = 0;
if($row['status']==5&&$delivery['status']==1){
$ckcar_status = 1;
}
$data = [
'id' => $id,
'name' => $row['name'],
'mobile' => mobile_asterisk($row['mobile']),
'admin_img' => 'https://qimg.haodian.cn/liche/2021/07/a6665d32b5dca522/7bc424442bcb05f7.png',
'payway' => $row['payway'],
'car_data' => $car_data,
'loan_status' => $loan_status,
'loan_data' => $loan_data,
'bill_status' => $bill_status,
'bill_data' => $bill_data,
'ckcar_status' => $ckcar_status
];
return $data;
}
//确认交付
protected function put_ckcar(){
$id = $this->input_param('id');
$row = $this->order_deliverys_model->get(['o_id'=>$id]);
if($row['status']!=1){
throw new Exception('当前不可修改', ERR_PARAMS_ERROR);
}
$result = $this->order_deliverys_model->update(['status'=>2],['id'=>$row['id']]);
if($result){
$this->orders_model->update(['status'=>6],['id'=>$row['o_id']]);
throw new Exception('修改成功', API_CODE_SUCCESS);
}else{
throw new Exception('修改失败', ERR_PARAMS_ERROR);
}
}
}