|
@@ -0,0 +1,153 @@
|
|
|
+<?php
|
|
|
+/**
|
|
|
+ * Created by PhpStorm.
|
|
|
+ * User: Gold
|
|
|
+ * Date: 2024/1/16
|
|
|
+ * Time: 9:25
|
|
|
+ */
|
|
|
+
|
|
|
+namespace app\api\controller;
|
|
|
+use app\admin\model\order\Order as OrderModel; //订单
|
|
|
+use app\admin\model\order\OrderGoods; //订单商品
|
|
|
+use app\admin\model\books\Books; //教材
|
|
|
+use app\admin\model\video\Video; //教材
|
|
|
+
|
|
|
+use app\common\controller\Api;
|
|
|
+use think\Db;
|
|
|
+
|
|
|
+class Order extends Api
|
|
|
+{
|
|
|
+ protected $noNeedRight = ['*'];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建订单
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public function createOrder(){
|
|
|
+ $data = input();
|
|
|
+ $uid = $this->auth->id;
|
|
|
+ $OrderModel = new OrderModel();
|
|
|
+ $OrderGoods = new OrderGoods();
|
|
|
+ $Books = new Books();
|
|
|
+ $Video = new Video();
|
|
|
+
|
|
|
+ if(!isset($data['type']) || !$data['goods_id']) $this->error('参数错误');
|
|
|
+
|
|
|
+ if($data['type'] == '0'){
|
|
|
+ $total_price = $Books->where('id','in',$data['goods_id'])->sum('price');
|
|
|
+ }else if($data['type'] == '1'){
|
|
|
+ $total_price = $Video->where('id','in',$data['goods_id'])->sum('price');
|
|
|
+ }
|
|
|
+
|
|
|
+// return $total_price;
|
|
|
+
|
|
|
+ $order_arr = [
|
|
|
+ 'user_id' => $uid,
|
|
|
+ 'order_on' => $this->get_order_sn(),
|
|
|
+ 'type' => $data['type'],
|
|
|
+ 'total_price' => $total_price
|
|
|
+ ];
|
|
|
+
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+
|
|
|
+ $save = $OrderModel->save($order_arr);
|
|
|
+ $goods_id = explode(',',$data['goods_id']);
|
|
|
+ if(count($goods_id) > 1){
|
|
|
+ foreach ($goods_id as $k => $v){
|
|
|
+ if($data['type'] == 0){
|
|
|
+ $price = $Books->where('id',$v)->column('price');
|
|
|
+ $arr = [
|
|
|
+ 'order_id' => $OrderModel->id,
|
|
|
+ 'goods_type' => $data['type'],
|
|
|
+ 'goods_id' => $v,
|
|
|
+ 'price' => (int)$price[0],
|
|
|
+ ];
|
|
|
+ $OrderGoods = new OrderGoods();
|
|
|
+ $OrderGoods->save($arr);
|
|
|
+ }else if($data['type'] == 1){
|
|
|
+ $price = $Video->where('id',$v)->column('price');
|
|
|
+ $arr = [
|
|
|
+ 'order_id' => $OrderModel->id,
|
|
|
+ 'goods_type' => $data['type'],
|
|
|
+ 'goods_id' => $v,
|
|
|
+ 'price' => (int)$price[0],
|
|
|
+ ];
|
|
|
+ $OrderGoods = new OrderGoods();
|
|
|
+ $OrderGoods->save($arr);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Db::commit();
|
|
|
+ } catch (Exception $e) {
|
|
|
+ $this->setError($e->getMessage());
|
|
|
+ Db::rollback();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $this->success('操作成功',$OrderModel);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建订单
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public function getOrderList(){
|
|
|
+ $data = input();
|
|
|
+ $where = [];
|
|
|
+ $uid = $this->auth->id;
|
|
|
+ $OrderModel = new OrderModel();
|
|
|
+ if(!isset($data['page']) || $data['page'] == '' || $data['page'] == null){
|
|
|
+ $page = 1;
|
|
|
+ }else{
|
|
|
+ $page = $data['page'];
|
|
|
+ }
|
|
|
+ if(!isset($data['limit']) || $data['limit'] == '' || $data['limit'] == null){
|
|
|
+ $limit = 20;
|
|
|
+ }else{
|
|
|
+ $limit = $data['limit'];
|
|
|
+ }
|
|
|
+ if(isset($data['status']) && $data['status'] != '' && $data['status'] != null){
|
|
|
+ $where['status'] = $data['status'];
|
|
|
+ }
|
|
|
+ $where['user_id'] = $uid;
|
|
|
+ $list = $OrderModel->where($where)->with(['goods'])->paginate([
|
|
|
+ 'list_rows'=> $limit,
|
|
|
+ 'page' => $page,
|
|
|
+ ]);
|
|
|
+ $lists = $list->toArray();
|
|
|
+
|
|
|
+ foreach ($lists['data'] as $key => $item){
|
|
|
+ foreach ($item['goods'] as $k => $v){
|
|
|
+ if($v['goods_type'] == 0){
|
|
|
+ $Books = new Books();
|
|
|
+ $lists['data'][$key]['goods'][$k]['good_item'] = $Books->where('id',$v['goods_id'])->field('id,title,image,book_type,is_new_recommend,author,is_awards,price,entity_price,createtime,updatetime,level_id,arrangement_id,speciality_id,series_id,is_series')->find();
|
|
|
+ }else if($v['goods_type'] == 1){
|
|
|
+ $Video = new Video();
|
|
|
+ $lists['data'][$key]['good_item'] = $Video->where('id',$v['goods_id'])->find();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ $this->success('操作成功',$lists);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成订单号
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public function get_order_sn(){
|
|
|
+ $order_id_main = date('YmdHis') . rand(10000000,99999999);
|
|
|
+ $order_id_len = strlen($order_id_main);
|
|
|
+ $order_id_sum = 0;
|
|
|
+ for($i=0; $i<$order_id_len; $i++){
|
|
|
+ $order_id_sum += (int)(substr($order_id_main,$i,1));
|
|
|
+ }
|
|
|
+ $osn = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT);
|
|
|
+ return $osn;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|