You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
7.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use App\Common\ReturnData;
  4. use DB;
  5. class Order extends BaseModel
  6. {
  7. //购物车模型
  8. /**
  9. * 关联到模型的数据表
  10. *
  11. * @var string
  12. */
  13. protected $table = 'order';
  14. public $timestamps = false;
  15. //获取订单列表
  16. public static function getList(array $param)
  17. {
  18. extract($param);
  19. $limit = isset($limit) ? $limit : 10;
  20. $offset = isset($offset) ? $offset : 0;
  21. $where['user_id'] = $user_id;
  22. $where['is_delete'] = 0;
  23. //0或者不传表示全部,1待付款,2待发货,3待收货,4待评价(确认收货,交易完成),5退款/售后
  24. if($status == 1)
  25. {
  26. $where['order_status'] = 0;
  27. $where['pay_status'] = 0;
  28. }
  29. elseif($status == 2)
  30. {
  31. $where['order_status'] = 0;
  32. $where['shipping_status'] = 0;
  33. $where['pay_status'] = 1;
  34. }
  35. elseif($status == 3)
  36. {
  37. $where['order_status'] = 0;
  38. $where['refund_status'] = 0;
  39. $where['shipping_status'] = 1;
  40. $where['pay_status'] = 1;
  41. }
  42. elseif($status == 4)
  43. {
  44. $where['order_status'] = 3;
  45. $where['refund_status'] = 0;
  46. $where['shipping_status'] = 2;
  47. $where['is_comment'] = 0;
  48. }
  49. elseif($status == 5)
  50. {
  51. $where['order_status'] = 3;
  52. $where['refund_status'] = 1;
  53. }
  54. $model = self::where($where);
  55. $res['count'] = $model->count();
  56. $res['list'] = array();
  57. if($res['count']>0)
  58. {
  59. $res['list'] = $model->skip($offset)->take($limit)->get();
  60. if($res['list'])
  61. {
  62. foreach($res['list'] as $k=>$v)
  63. {
  64. $order_goods = OrderGoods::where(array('order_id'=>$v['id']))->get();
  65. $res[$k]['goods_list'] = $order_goods;
  66. }
  67. }
  68. }
  69. return ReturnData::create(ReturnData::SUCCESS,$res);
  70. }
  71. public static function getOne($where)
  72. {
  73. $goods = self::where($where)->first();
  74. return $goods;
  75. }
  76. //生成订单
  77. public static function add(array $param)
  78. {
  79. extract($param);
  80. //获取订单商品列表
  81. $cartCheckoutGoods = Cart::cartCheckoutGoodsList(array('ids'=>$cartids,'user_id'=>$user_id));
  82. $order_goods = $cartCheckoutGoods['data'];
  83. if(!$order_goods['list']){return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'订单商品不存在');}
  84. //获取收货地址
  85. $user_address = UserAddress::getOne($user_id,$default_address_id);
  86. if(!$user_address){return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'收货地址不存在');}
  87. //获取优惠券信息
  88. $user_bonus = UserBonus::getUserBonusByid(array('user_bonus_id'=>$user_bonus_id,'user_id'=>$user_id));
  89. $discount = !empty($user_bonus)?$user_bonus['money']:0.00; //优惠金额=优惠券
  90. $order_amount = $order_goods['total_price'] - $discount;
  91. $pay_status = 0; //未付款
  92. //如果各种优惠金额大于订单实际金额跟运费之和,则默认订单状态为已付款
  93. if($order_amount < 0)
  94. {
  95. $order_amount = 0;
  96. $pay_status = 1; //已付款
  97. }
  98. //构造订单字段
  99. $order_info = array(
  100. 'order_sn' => date('YmdHis'.rand(1000,9999)),
  101. 'add_time' => time(),
  102. 'pay_status' => $pay_status,
  103. 'user_id' => $user_id,
  104. 'goods_amount' => $order_goods['total_price'], //商品的总金额
  105. 'order_amount' => $order_amount, //应付金额=商品总价+运费-优惠(积分、红包)
  106. 'discount' => $discount, //优惠金额
  107. 'name' => $user_address['name'],
  108. //'country' => $user_address['country'],
  109. 'province' => $user_address['province'],
  110. 'city' => $user_address['city'],
  111. 'district' => $user_address['district'],
  112. 'address' => $user_address['address'],
  113. 'zipcode' => $user_address['zipcode'],
  114. 'mobile' => $user_address['mobile'],
  115. 'place_type' => $place_type, //订单来源
  116. 'bonus_id' => !empty($user_bonus)?$user_bonus['id']:0,
  117. 'bonus_money' => !empty($user_bonus)?$user_bonus['money']:0.00,
  118. 'message' => $message
  119. );
  120. //插入订单
  121. $order_id = self::insertGetId($order_info);
  122. if ($order_id)
  123. {
  124. //订单生成成功之后,扣除用户的积分和改变优惠券的使用状态
  125. //改变优惠券使用状态
  126. UserBonus::where(array('user_id'=>$user_id,'id'=>$user_bonus_id))->update(array('status'=>1,'used_time'=>time()));
  127. //扣除用户积分,预留
  128. //$updateMember['validscore'] = $addressInfo['validscore']-$PointPay;
  129. //M("Member")->where(array('id'=>$CustomerSysNo))->save($updateMember);
  130. //增加一条积分支出记录,一条购物获取积分记录
  131. //插入订单商品
  132. $order_goods_list = array();
  133. foreach($order_goods['list'] as $k=>$v)
  134. {
  135. $temp_order_goods = array(
  136. 'order_id' => $order_id,
  137. 'goods_id' => $v['goods_id'],
  138. 'goods_name' => $v['title'],
  139. 'goods_number' => $v['goods_number'],
  140. 'market_price' => $v['market_price'],
  141. 'goods_price' => $v['final_price'],
  142. //'goods_attr' => '', //商品属性,预留
  143. 'goods_img' => $v['litpic']
  144. );
  145. array_push($order_goods_list,$temp_order_goods);
  146. //订单商品直行减库存操作
  147. Goods::changeGoodsStock(array('goods_id'=>$v['goods_id'],'goods_number'=>$v['goods_number']));
  148. }
  149. $result = DB::table('order_goods')->insert($order_goods_list);
  150. if($result)
  151. {
  152. //删除购物对应的记录
  153. Cart::where(array('user_id'=>$user_id))->whereIn('id', explode("_",$cartids))->delete();
  154. return ReturnData::create(ReturnData::SUCCESS,$order_id);
  155. }
  156. else
  157. {
  158. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'订单商品添加失败');
  159. }
  160. }
  161. else
  162. {
  163. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'生成订单失败');
  164. }
  165. }
  166. public static function modify($where, array $data)
  167. {
  168. if (self::where($where)->update($data))
  169. {
  170. return true;
  171. }
  172. return false;
  173. }
  174. //删除一条记录
  175. public static function remove($id,$user_id)
  176. {
  177. if(!is_array($id)){$id = explode(',', $id);}
  178. if (self::whereIn('id', $id)->where('user_id',$user_id)->delete() === false)
  179. {
  180. return false;
  181. }
  182. return true;
  183. }
  184. //获取未支付的订单详情
  185. public static function getUnpaidOrder(array $param)
  186. {
  187. extract($param);
  188. $res = self::where(array('id'=>$order_id,'order_status'=>0,'pay_status'=>0,'user_id'=>$user_id))->select('id', 'order_sn', 'user_id', 'add_time', 'order_amount')->first();
  189. if(!$res)
  190. {
  191. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'');
  192. }
  193. return ReturnData::create(ReturnData::SUCCESS,$res);
  194. }
  195. }