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.

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