Order.php 4.2 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/12
 * Time: 18:19
 */

namespace app\index\controller;


use app\common\controller\HomeBase;
use app\index\model\Address;
use app\index\model\Area;
use app\index\model\Store;

class Order extends HomeBase
{
    public function confirm(){
        $user_id = $this->getUserId();
//        $car_ids = $this->request->param('car_ids');
        $data = $this->request->param('data');
        $province_id = $this->request->param('province_id',0,'intval');
        if(empty($data) || empty($province_id)){
            $this->error('缺少必要参数');
        }
        $data = json_decode($data,true);
        /*if(!empty($car_ids)){
            $car_ids = explode(',',$car_ids);
            $carModel = new \app\index\model\Car();
            $result = $carModel->where(['id'=>$car_ids])->delete();
            if(empty($result)){
                $this->error('sql执行失败');
            }
        }*/
        $goodsModel = new \app\index\model\Goods();
        $goods_price = [];
        $store_ids = [];
        foreach($data as $key1 => $vo){
            //获取店铺id
            $goods = $goodsModel->findData(['g.id'=>$vo['goods_id']]);
            $data[$key1]['store_id'] = $goods['store_id'];
            $store_ids[] = $goods['store_id'];
        }
        $storeModel = new Store();
        $store = $storeModel->selectData(['id'=>['in',$store_ids]]);
        foreach($store as $key2 => $s){
            if($s['is_svip'] != '1'){
                if($s['is_vip'] != '1' && !in_array($province_id,$s['province_ids'])){
                    $this->error("店铺 【$s[name]】 不可以进行购买~");
                }
            }
            $goods_ids = [];
            $number = [];
            foreach($data as $key3 => $vo){
                if($vo['store_id'] == $s['id']){
                    $goods_ids[] = $vo['goods_id'];
                    $number[] = $vo['number'];
                }
            }
            $store[$key2]['goods_ids'] = $goods_ids;
            $goods_arr = [];
            foreach($goods_ids as $key4 => $goods_id){
                $goods = $goodsModel->findData(['g.id'=>$goods_id]);
                if($goods['inventory'] < $number[$key4]){
                    $this->error("商品 【$goods[goodsname]】 库存不足~");
                }
                $goods['number'] = $number[$key4];
                $goods_price[] = $goods['price']*$number[$key4];
                $goods_arr[] = $goods;
            }
            //商品总价格
            $store[$key2]['sum_goods_price'] = array_sum($goods_price);
            //商品实际支付价格
            $goods_total = $store[$key2]['sum_goods_price'];
            $store[$key2]['goods_total'] = $goods_total;
            //配送价格
            $postage_price = 0;
            if($goods_total < $s['money']){
                $postage_price = $store['freight'];
            }
            $store[$key2]['postage_price'] = $postage_price;
            $store[$key2]['goods'] = $goods_arr;
        }
        //总合计
        $goods_total1 = 0;
        $postage_price1 = 0;
        foreach($store as $key => $vo3){
            $goods_total1 = $goods_total1 + $vo3['goods_total'];
            $postage_price1 = $postage_price1 + $vo3['postage_price'];
        }
        $total = $goods_total1 + $postage_price1;
        //收货地址
        $addressModel = new Address();
        $address = $addressModel->selectData(['user_id'=>$user_id]);
        //省市区三级联动
        $areaModel = new Area();
        $province = $areaModel->selectData(['pid'=>0,'level'=>1]);
        $province_id = !empty($province[0]['id']) ? $province[0]['id'] : 0;
        $city = $areaModel->selectData(['pid'=>$province_id,'level'=>2]);
        $city_id = !empty($city[0]['id']) ? $city[0]['id'] : 0;
        $county = $areaModel->selectData(['pid'=>$city_id,'level'=>3]);
        $this->assign('province',$province);
        $this->assign('city',$city);
        $this->assign('county',$county);
        $this->assign('store',$store);
        $this->assign('total',$total);
        $this->assign('address',$address);
        $this->assign('title','确认订单');
        return $this->fetch();
    }
}