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.

268 lines
9.2 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
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. $order_list = $model->skip($offset)->take($limit)->get();
  60. if($order_list)
  61. {
  62. foreach($order_list as $k=>$v)
  63. {
  64. $order_list[$k]['order_status_text'] = self::getOrderStatusText($v);
  65. $order_goods = OrderGoods::where(array('order_id'=>$v['id']))->get();
  66. $order_list[$k]['goods_list'] = $order_goods;
  67. }
  68. }
  69. $res['list'] = $order_list;
  70. }
  71. return ReturnData::create(ReturnData::SUCCESS,$res);
  72. }
  73. public static function getOne(array $param)
  74. {
  75. extract($param);
  76. $where['id'] = $order_id;
  77. $where['user_id'] = $user_id;
  78. if(isset($order_status)){$where['order_status'] = $order_status;}
  79. if(isset($pay_status)){$where['pay_status'] = $pay_status;}
  80. $res = self::where($where)->first();
  81. if(!$res)
  82. {
  83. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  84. }
  85. $res['order_status_text'] = self::getOrderStatusText($res);
  86. $res['province_name'] = Region::getRegionName($res['province']);
  87. $res['city_name'] = Region::getRegionName($res['city']);
  88. $res['district_name'] = Region::getRegionName($res['district']);
  89. $order_goods = OrderGoods::where(array('order_id'=>$res['id']))->get();
  90. $res['goods_list'] = $order_goods;
  91. return ReturnData::create(ReturnData::SUCCESS,$res);
  92. }
  93. //生成订单
  94. public static function add(array $param)
  95. {
  96. extract($param);
  97. //获取订单商品列表
  98. $cartCheckoutGoods = Cart::cartCheckoutGoodsList(array('ids'=>$cartids,'user_id'=>$user_id));
  99. $order_goods = $cartCheckoutGoods['data'];
  100. if(empty($order_goods['list'])){return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'订单商品不存在');}
  101. //获取收货地址
  102. $user_address = UserAddress::getOne($user_id,$default_address_id);
  103. if(!$user_address){return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'收货地址不存在');}
  104. //获取优惠券信息
  105. $user_bonus = UserBonus::getUserBonusByid(array('user_bonus_id'=>$user_bonus_id,'user_id'=>$user_id));
  106. $discount = !empty($user_bonus)?$user_bonus['money']:0.00; //优惠金额=优惠券
  107. $order_amount = $order_goods['total_price'] - $discount;
  108. $pay_status = 0; //未付款
  109. //如果各种优惠金额大于订单实际金额跟运费之和,则默认订单状态为已付款
  110. if($order_amount < 0)
  111. {
  112. $order_amount = 0;
  113. $pay_status = 1; //已付款
  114. }
  115. //构造订单字段
  116. $order_info = array(
  117. 'order_sn' => date('YmdHis'.rand(1000,9999)),
  118. 'add_time' => time(),
  119. 'pay_status' => $pay_status,
  120. 'user_id' => $user_id,
  121. 'goods_amount' => $order_goods['total_price'], //商品的总金额
  122. 'order_amount' => $order_amount, //应付金额=商品总价+运费-优惠(积分、红包)
  123. 'discount' => $discount, //优惠金额
  124. 'name' => $user_address['name'],
  125. //'country' => $user_address['country'],
  126. 'province' => $user_address['province'],
  127. 'city' => $user_address['city'],
  128. 'district' => $user_address['district'],
  129. 'address' => $user_address['address'],
  130. 'zipcode' => $user_address['zipcode'],
  131. 'mobile' => $user_address['mobile'],
  132. 'place_type' => $place_type, //订单来源
  133. 'bonus_id' => !empty($user_bonus)?$user_bonus['id']:0,
  134. 'bonus_money' => !empty($user_bonus)?$user_bonus['money']:0.00,
  135. 'message' => $message
  136. );
  137. //插入订单
  138. $order_id = self::insertGetId($order_info);
  139. if ($order_id)
  140. {
  141. //订单生成成功之后,扣除用户的积分和改变优惠券的使用状态
  142. //改变优惠券使用状态
  143. UserBonus::where(array('user_id'=>$user_id,'id'=>$user_bonus_id))->update(array('status'=>1,'used_time'=>time()));
  144. //扣除用户积分,预留
  145. //$updateMember['validscore'] = $addressInfo['validscore']-$PointPay;
  146. //M("Member")->where(array('id'=>$CustomerSysNo))->save($updateMember);
  147. //增加一条积分支出记录,一条购物获取积分记录
  148. //插入订单商品
  149. $order_goods_list = array();
  150. foreach($order_goods['list'] as $k=>$v)
  151. {
  152. $temp_order_goods = array(
  153. 'order_id' => $order_id,
  154. 'goods_id' => $v['goods_id'],
  155. 'goods_name' => $v['title'],
  156. 'goods_number' => $v['goods_number'],
  157. 'market_price' => $v['market_price'],
  158. 'goods_price' => $v['final_price'],
  159. //'goods_attr' => '', //商品属性,预留
  160. 'goods_img' => $v['litpic']
  161. );
  162. array_push($order_goods_list,$temp_order_goods);
  163. //订单商品直行减库存操作
  164. Goods::changeGoodsStock(array('goods_id'=>$v['goods_id'],'goods_number'=>$v['goods_number']));
  165. }
  166. $result = DB::table('order_goods')->insert($order_goods_list);
  167. if($result)
  168. {
  169. //删除购物对应的记录
  170. Cart::where(array('user_id'=>$user_id))->whereIn('id', explode("_",$cartids))->delete();
  171. return ReturnData::create(ReturnData::SUCCESS,$order_id);
  172. }
  173. else
  174. {
  175. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'订单商品添加失败');
  176. }
  177. }
  178. else
  179. {
  180. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'生成订单失败');
  181. }
  182. }
  183. public static function modify($where, array $data)
  184. {
  185. if (self::where($where)->update($data))
  186. {
  187. return true;
  188. }
  189. return false;
  190. }
  191. //删除一条记录
  192. public static function remove($id,$user_id)
  193. {
  194. if(!is_array($id)){$id = explode(',', $id);}
  195. if (self::whereIn('id', $id)->where('user_id',$user_id)->delete() === false)
  196. {
  197. return false;
  198. }
  199. return true;
  200. }
  201. //获取订单状态文字,1待付款,2待发货,3待收货,4待评价(确认收货,交易成功),5退款/售后
  202. public static function getOrderStatusText($where)
  203. {
  204. $res = '';
  205. if($where['order_status'] == 0 && $where['pay_status'] ==0)
  206. {
  207. $res = '待付款';
  208. }
  209. elseif($where['order_status'] == 0 && $where['shipping_status'] == 0 && $where['pay_status'] == 1)
  210. {
  211. $res = '待发货';
  212. }
  213. elseif($where['order_status'] == 0 && $where['refund_status'] == 0 && $where['shipping_status'] == 1 && $where['pay_status'] == 1)
  214. {
  215. $res = '待收货';
  216. }
  217. elseif($where['order_status'] == 3 && $where['refund_status'] == 0 && $where['shipping_status'] == 2 && $where['is_comment'] == 0)
  218. {
  219. $res = '交易成功';
  220. }
  221. elseif($where['order_status'] == 3 && $where['refund_status'] == 1)
  222. {
  223. $res = '售后';
  224. }
  225. return $res;
  226. }
  227. }