ShopOrder.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace app\data\model;
  3. use think\admin\Model;
  4. use think\exception\ErrorException;
  5. use think\facade\Db;
  6. /**
  7. * 商城订单主模型
  8. * Class ShopOrder
  9. * @package app\data\model
  10. */
  11. class ShopOrder extends Model
  12. {
  13. /**
  14. * @param $data
  15. * @return void
  16. * 1 创建订单
  17. * 2 创建订单商品
  18. * 3 创建订单发货
  19. */
  20. public function order_create($data){
  21. $address_info = $this->address_info($data['uuid'],$data['address_id']);
  22. $goods_info = $this->goods_info($data['item_id']);
  23. $order_no =$this->order_no($data['uuid']);
  24. $order_data = [
  25. 'uuid'=>$data['uuid'],
  26. 'order_no'=>$order_no,
  27. 'order_name'=>$goods_info['name'],
  28. 'amount_real'=>$goods_info['price_selling']*$data['num'],
  29. 'amount_total'=>$goods_info['price_selling']*$data['num'],
  30. 'amount_goods'=>$goods_info['price_market']*$data['num'],
  31. 'number_goods'=>$data['num'],
  32. 'payment_trade'=>$data['pay_no'],
  33. 'payment_amount'=>$goods_info['price_selling']*$data['num'],
  34. 'payment_status'=>0,
  35. 'admin_id'=>$goods_info['admin_id'],
  36. 'order_remark'=>$data['order_remark']
  37. ];
  38. $item_data=[
  39. 'uuid'=>$data['uuid'],
  40. 'order_no'=>$order_no,
  41. 'goods_sku'=>$goods_info['goods_sku'],
  42. 'goods_code'=>$goods_info['goods_code'],
  43. 'goods_spec'=>$goods_info['goods_spec'],
  44. 'goods_name'=>$goods_info['name'],
  45. 'goods_cover'=>$goods_info['cover'],
  46. 'price_market'=>$goods_info['price_market'],
  47. 'price_selling'=>$goods_info['price_selling'],
  48. 'total_market'=>$goods_info['price_market']*$data['num'],
  49. 'total_selling'=>$goods_info['price_selling']*$data['num'],
  50. 'stock_sales'=>$data['num'],
  51. ];
  52. $send_data = [
  53. 'uuid'=>$data['uuid'],
  54. 'order_no'=>$order_no,
  55. 'address_code'=>$data['address_id'],
  56. 'address_name'=>$address_info['name'],
  57. 'address_phone'=>$address_info['phone'],
  58. 'address_province'=>$address_info['province'],
  59. 'address_city'=>$address_info['city'],
  60. 'address_area'=>$address_info['area'],
  61. 'address_content'=>$address_info['address'],
  62. ];
  63. Db::startTrans();
  64. // 启动事务
  65. try {
  66. $order_id = ShopOrder::mk()->insertGetId($order_data);
  67. ShopOrderItem::mk()->insertGetId($item_data);
  68. ShopOrderSend::mk()->insertGetId($send_data);
  69. if($order_id){
  70. $admin_id = ShopOrder::mk()->where('id',$order_id)->field('order_no,admin_id')->find();
  71. $mes = ShopOrderItem::mk()->where('order_no',$admin_id['order_no'])->find();
  72. $message = [
  73. 'type'=>2,
  74. 'uuid'=>$admin_id['admin_id'],
  75. 'title'=>'买家拍下商品',
  76. 'content'=>$mes['goods_name'],
  77. 'image'=>$mes['goods_cover'],
  78. 'remark'=>'买家已拍下,请尽快跟进哦~'
  79. ];
  80. $shop_order_model = new SystemUserMessage();
  81. $shop_order_model->systemMessageadd($message);
  82. }
  83. // 提交事务
  84. Db::commit();
  85. return $order_id;
  86. } catch (ErrorException $e) {
  87. // 回滚事务
  88. Db::rollback();
  89. }
  90. }
  91. /**
  92. * @param $user_id
  93. * @param $address_id
  94. * @return DataUserAddress|array|mixed|\think\Model|null
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. * 购物车下单
  99. */
  100. public function cart_create($data)
  101. {
  102. $address_info = $this->address_info($data['uuid'], $data['address_id']);
  103. $goods_info = $this->cart_goods_info($data['cart_ids'], $data['uuid']);
  104. foreach ($goods_info as $k => $v) {
  105. //订单主表
  106. $order_no = $this->order_no($v['admin_id']);
  107. $order_data = [
  108. 'uuid' => $data['uuid'],
  109. 'order_no' => $order_no,
  110. 'order_name'=>$v['order_name'],
  111. 'amount_real' => $v['goods_money'],
  112. 'amount_total' => $v['goods_money'],
  113. 'amount_goods' => $v['goods_money'],
  114. 'number_goods'=>$v['goods_num'],
  115. 'payment_trade' => $data['pay_no'],
  116. 'payment_amount'=>$v['goods_money'],
  117. 'payment_status' => 0,
  118. 'admin_id'=>$v['admin_id'],
  119. 'order_remark'=>$data['order_remark'],
  120. ];
  121. $order_id = ShopOrder::mk()->insertGetId($order_data);
  122. $order_id_arr[] = $order_id;
  123. //订单项目表
  124. foreach ($v['goods_list'] as $k_order_goods => $order_goods) {
  125. $item_data = array(
  126. 'uuid'=>$data['uuid'],
  127. 'order_no'=>$order_no,
  128. 'goods_sku'=>$order_goods['goods_sku'],
  129. 'goods_code'=>$order_goods['goods_code'],
  130. 'goods_spec'=>$order_goods['goods_spec'],
  131. 'goods_name'=>$order_goods['name'],
  132. 'goods_cover'=>$order_goods['show_image'],
  133. 'price_market'=>$order_goods['price_market'],
  134. 'price_selling'=>$order_goods['price_selling'],
  135. 'total_market'=>$order_goods['price_market']*$order_goods['num'],
  136. 'total_selling'=>$order_goods['price_selling']*$order_goods['num'],
  137. 'stock_sales'=>$order_goods['num'],
  138. );
  139. $item_id = ShopOrderItem::mk()->insertGetId($item_data);
  140. }
  141. $mes = ShopOrderItem::mk()->where('order_no',$order_no)->find();
  142. $admin_id = ShopOrder::mk()->where('order_no',$order_no)->value('admin_id');
  143. $message = [
  144. 'type'=>2,
  145. 'uuid'=>$admin_id,
  146. 'title'=>'买家拍下商品',
  147. 'content'=>$mes['goods_name'],
  148. 'image'=>$mes['goods_cover'],
  149. 'remark'=>'买家已拍下,请尽快跟进哦~'
  150. ];
  151. $shop_order_model = new SystemUserMessage();
  152. $shop_order_model->systemMessageadd($message);
  153. $send_data = [
  154. 'uuid' => $data['uuid'],
  155. 'order_no' => $order_no,
  156. 'address_code' => $data['address_id'],
  157. 'address_name' => $address_info['name'],
  158. 'address_phone' => $address_info['phone'],
  159. 'address_province' => $address_info['province'],
  160. 'address_city' => $address_info['city'],
  161. 'address_area' => $address_info['area'],
  162. 'address_content' => $address_info['address'],
  163. ];
  164. Db::startTrans();
  165. // 启动事务
  166. try {
  167. $send_id = ShopOrderSend::mk()->insertGetId($send_data);
  168. // 提交事务
  169. Db::commit();
  170. } catch (ErrorException $e) {
  171. // 回滚事务
  172. Db::rollback();
  173. }
  174. }
  175. }
  176. public function address_info($user_id,$address_id){
  177. $address_info = DataUserAddress::mk()->where(array('id'=>$address_id,'uuid'=>$user_id))->find();
  178. return $address_info;
  179. }
  180. public function goods_info($item_id){
  181. $goods_info = ShopGoodsItem::mk()->alias('item')
  182. ->join('shop_goods goods','goods.code=item.goods_code')
  183. ->where('item.id',$item_id)
  184. ->field('item.*,goods.cover,goods.name,goods.admin_id')
  185. ->find();
  186. return $goods_info;
  187. }
  188. public function cart_goods_info($cart_ids,$uuid){
  189. //组装商品列表
  190. $goods_list = ShopGoodsCart::mk()->alias('car')
  191. ->join('shop_goods_item item','item.id=car.item_id')
  192. ->join('shop_goods goods','goods.code=item.goods_code')
  193. ->whereIn('car.id',$cart_ids)->where('car.user_id',$uuid)
  194. ->field('car.*,item.*,goods.name,goods.admin_id,goods.stock_total')
  195. ->select();
  196. $shop_goods_list = [];
  197. if(!empty($goods_list))
  198. {
  199. foreach ($goods_list as $k => $v)
  200. {
  201. $site_id = $v['admin_id'];
  202. if(isset($shop_goods_list[$site_id]))
  203. {
  204. $shop_goods_list[$site_id]['goods_list'][] = $v;
  205. $shop_goods_list[$site_id]['goods_num'] += $v['num'];
  206. $shop_goods_list[$site_id]['goods_money'] += $v['price_selling'] * $v['num'];
  207. }else{
  208. $shop_goods_list[$site_id]['admin_id'] = $site_id;
  209. $shop_goods_list[$site_id]['admin_name'] = 111;
  210. $shop_goods_list[$site_id]['order_name'] = $v['name'];
  211. $shop_goods_list[$site_id]['goods_money'] = $v['price_selling'] * $v['num'];
  212. $shop_goods_list[$site_id]['goods_num'] = $v['num'];
  213. $shop_goods_list[$site_id]['cover'] = $v['cover'];
  214. $shop_goods_list[$site_id]['goods_list'][] = $v;
  215. }
  216. }
  217. }
  218. return $shop_goods_list;
  219. }
  220. public function order_no($uuid){
  221. $time_str = date('YmdHi').rand('000','999');
  222. $order_no = $time_str . sprintf("%04d", $uuid);
  223. return $order_no;
  224. }
  225. }