123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <?php
- namespace app\data\model;
- use think\admin\Model;
- use think\exception\ErrorException;
- use think\facade\Db;
- /**
- * 商城订单主模型
- * Class ShopOrder
- * @package app\data\model
- */
- class ShopOrder extends Model
- {
- /**
- * @param $data
- * @return void
- * 1 创建订单
- * 2 创建订单商品
- * 3 创建订单发货
- */
- public function order_create($data){
- $address_info = $this->address_info($data['uuid'],$data['address_id']);
- $goods_info = $this->goods_info($data['item_id']);
- $order_no =$this->order_no($data['uuid']);
- $order_data = [
- 'uuid'=>$data['uuid'],
- 'order_no'=>$order_no,
- 'order_name'=>$goods_info['name'],
- 'amount_real'=>$goods_info['price_selling']*$data['num'],
- 'amount_total'=>$goods_info['price_selling']*$data['num'],
- 'amount_goods'=>$goods_info['price_market']*$data['num'],
- 'number_goods'=>$data['num'],
- 'payment_trade'=>$data['pay_no'],
- 'payment_amount'=>$goods_info['price_selling']*$data['num'],
- 'payment_status'=>0,
- 'admin_id'=>$goods_info['admin_id'],
- 'order_remark'=>$data['order_remark']
- ];
- $item_data=[
- 'uuid'=>$data['uuid'],
- 'order_no'=>$order_no,
- 'goods_sku'=>$goods_info['goods_sku'],
- 'goods_code'=>$goods_info['goods_code'],
- 'goods_spec'=>$goods_info['goods_spec'],
- 'goods_name'=>$goods_info['name'],
- 'goods_cover'=>$goods_info['cover'],
- 'price_market'=>$goods_info['price_market'],
- 'price_selling'=>$goods_info['price_selling'],
- 'total_market'=>$goods_info['price_market']*$data['num'],
- 'total_selling'=>$goods_info['price_selling']*$data['num'],
- 'stock_sales'=>$data['num'],
- ];
- $send_data = [
- 'uuid'=>$data['uuid'],
- 'order_no'=>$order_no,
- 'address_code'=>$data['address_id'],
- 'address_name'=>$address_info['name'],
- 'address_phone'=>$address_info['phone'],
- 'address_province'=>$address_info['province'],
- 'address_city'=>$address_info['city'],
- 'address_area'=>$address_info['area'],
- 'address_content'=>$address_info['address'],
- ];
- Db::startTrans();
- // 启动事务
- try {
- $order_id = ShopOrder::mk()->insertGetId($order_data);
- ShopOrderItem::mk()->insertGetId($item_data);
- ShopOrderSend::mk()->insertGetId($send_data);
- if($order_id){
- $admin_id = ShopOrder::mk()->where('id',$order_id)->field('order_no,admin_id')->find();
- $mes = ShopOrderItem::mk()->where('order_no',$admin_id['order_no'])->find();
- $message = [
- 'type'=>2,
- 'uuid'=>$admin_id['admin_id'],
- 'title'=>'买家拍下商品',
- 'content'=>$mes['goods_name'],
- 'image'=>$mes['goods_cover'],
- 'remark'=>'买家已拍下,请尽快跟进哦~'
- ];
- $shop_order_model = new SystemUserMessage();
- $shop_order_model->systemMessageadd($message);
- }
- // 提交事务
- Db::commit();
- return $order_id;
- } catch (ErrorException $e) {
- // 回滚事务
- Db::rollback();
- }
- }
- /**
- * @param $user_id
- * @param $address_id
- * @return DataUserAddress|array|mixed|\think\Model|null
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * 购物车下单
- */
- public function cart_create($data)
- {
- $address_info = $this->address_info($data['uuid'], $data['address_id']);
- $goods_info = $this->cart_goods_info($data['cart_ids'], $data['uuid']);
- foreach ($goods_info as $k => $v) {
- //订单主表
- $order_no = $this->order_no($v['admin_id']);
- $order_data = [
- 'uuid' => $data['uuid'],
- 'order_no' => $order_no,
- 'order_name'=>$v['order_name'],
- 'amount_real' => $v['goods_money'],
- 'amount_total' => $v['goods_money'],
- 'amount_goods' => $v['goods_money'],
- 'number_goods'=>$v['goods_num'],
- 'payment_trade' => $data['pay_no'],
- 'payment_amount'=>$v['goods_money'],
- 'payment_status' => 0,
- 'admin_id'=>$v['admin_id'],
- 'order_remark'=>$data['order_remark'],
- ];
- $order_id = ShopOrder::mk()->insertGetId($order_data);
- $order_id_arr[] = $order_id;
- //订单项目表
- foreach ($v['goods_list'] as $k_order_goods => $order_goods) {
- $item_data = array(
- 'uuid'=>$data['uuid'],
- 'order_no'=>$order_no,
- 'goods_sku'=>$order_goods['goods_sku'],
- 'goods_code'=>$order_goods['goods_code'],
- 'goods_spec'=>$order_goods['goods_spec'],
- 'goods_name'=>$order_goods['name'],
- 'goods_cover'=>$order_goods['show_image'],
- 'price_market'=>$order_goods['price_market'],
- 'price_selling'=>$order_goods['price_selling'],
- 'total_market'=>$order_goods['price_market']*$order_goods['num'],
- 'total_selling'=>$order_goods['price_selling']*$order_goods['num'],
- 'stock_sales'=>$order_goods['num'],
- );
- $item_id = ShopOrderItem::mk()->insertGetId($item_data);
- }
- $mes = ShopOrderItem::mk()->where('order_no',$order_no)->find();
- $admin_id = ShopOrder::mk()->where('order_no',$order_no)->value('admin_id');
- $message = [
- 'type'=>2,
- 'uuid'=>$admin_id,
- 'title'=>'买家拍下商品',
- 'content'=>$mes['goods_name'],
- 'image'=>$mes['goods_cover'],
- 'remark'=>'买家已拍下,请尽快跟进哦~'
- ];
- $shop_order_model = new SystemUserMessage();
- $shop_order_model->systemMessageadd($message);
- $send_data = [
- 'uuid' => $data['uuid'],
- 'order_no' => $order_no,
- 'address_code' => $data['address_id'],
- 'address_name' => $address_info['name'],
- 'address_phone' => $address_info['phone'],
- 'address_province' => $address_info['province'],
- 'address_city' => $address_info['city'],
- 'address_area' => $address_info['area'],
- 'address_content' => $address_info['address'],
- ];
- Db::startTrans();
- // 启动事务
- try {
- $send_id = ShopOrderSend::mk()->insertGetId($send_data);
- // 提交事务
- Db::commit();
- } catch (ErrorException $e) {
- // 回滚事务
- Db::rollback();
- }
- }
- }
- public function address_info($user_id,$address_id){
- $address_info = DataUserAddress::mk()->where(array('id'=>$address_id,'uuid'=>$user_id))->find();
- return $address_info;
- }
- public function goods_info($item_id){
- $goods_info = ShopGoodsItem::mk()->alias('item')
- ->join('shop_goods goods','goods.code=item.goods_code')
- ->where('item.id',$item_id)
- ->field('item.*,goods.cover,goods.name,goods.admin_id')
- ->find();
- return $goods_info;
- }
- public function cart_goods_info($cart_ids,$uuid){
- //组装商品列表
- $goods_list = ShopGoodsCart::mk()->alias('car')
- ->join('shop_goods_item item','item.id=car.item_id')
- ->join('shop_goods goods','goods.code=item.goods_code')
- ->whereIn('car.id',$cart_ids)->where('car.user_id',$uuid)
- ->field('car.*,item.*,goods.name,goods.admin_id,goods.stock_total')
- ->select();
- $shop_goods_list = [];
- if(!empty($goods_list))
- {
- foreach ($goods_list as $k => $v)
- {
- $site_id = $v['admin_id'];
- if(isset($shop_goods_list[$site_id]))
- {
- $shop_goods_list[$site_id]['goods_list'][] = $v;
- $shop_goods_list[$site_id]['goods_num'] += $v['num'];
- $shop_goods_list[$site_id]['goods_money'] += $v['price_selling'] * $v['num'];
- }else{
- $shop_goods_list[$site_id]['admin_id'] = $site_id;
- $shop_goods_list[$site_id]['admin_name'] = 111;
- $shop_goods_list[$site_id]['order_name'] = $v['name'];
- $shop_goods_list[$site_id]['goods_money'] = $v['price_selling'] * $v['num'];
- $shop_goods_list[$site_id]['goods_num'] = $v['num'];
- $shop_goods_list[$site_id]['cover'] = $v['cover'];
- $shop_goods_list[$site_id]['goods_list'][] = $v;
- }
- }
- }
- return $shop_goods_list;
- }
- public function order_no($uuid){
- $time_str = date('YmdHi').rand('000','999');
- $order_no = $time_str . sprintf("%04d", $uuid);
- return $order_no;
- }
- }
|