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.

316 lines
11 KiB

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); //参数:limit,offset
  19. $model = self::join('goods', 'goods.id', '=', 'cart.goods_id')
  20. ->where('cart.user_id', $user_id)
  21. ->where('goods.status', Goods::STATUS)
  22. ->select('cart.*','goods.id as goods_id','goods.title','goods.sn','goods.price as goods_price','goods.market_price','goods.litpic','goods.goods_number as stock','goods.promote_start_date','goods.promote_price','goods.promote_end_date');
  23. $res['count'] = $model->count();
  24. $res['list'] = array();
  25. if($res['count']>0)
  26. {
  27. $res['list'] = $model->get();
  28. foreach ($res['list'] as $k => $v)
  29. {
  30. $res['list'][$k]->is_promote = 0;
  31. if(Goods::bargain_price($v->goods_price,$v->promote_start_date,$v->promote_end_date) > 0){$res['list'][$k]->is_promote = 1;}
  32. //订货数量大于0
  33. if ($v->goods_number > 0)
  34. {
  35. $res['list'][$k]->final_price = Goods::get_final_price($v->goods_id); //商品最终价格
  36. $res['list'][$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v->goods_id));
  37. //更新购物车中的商品数量
  38. //self::where('id', $v->id)->update(array('price' => $goods_price));
  39. }
  40. }
  41. }
  42. else
  43. {
  44. return false;
  45. }
  46. return $res;
  47. }
  48. public static function getOne($where)
  49. {
  50. $goods = self::where($where)->first();
  51. return $goods;
  52. }
  53. public static function add(array $param)
  54. {
  55. extract($param);
  56. //获取订单商品列表
  57. $order_goods = Cart::cartCheckoutGoodsList(array('ids'=>$cartids,'user_id'=>$user_id));
  58. if(!$order_goods){return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'订单商品不存在');}
  59. return $order_goods;
  60. //获取收货地址
  61. $user_address = UserAddress::getOne($user_id,$default_address_id);
  62. if(!$user_address){return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'收货地址不存在');}
  63. //获取优惠券信息
  64. $user_bonus = UserBonus::getUserBonusByid(array('user_bonus_id'=>$user_bonus_id,'user_id'=>$user_id));
  65. $discount = !empty($user_bonus)?$user_bonus['money']:0.00; //优惠金额=优惠券
  66. $order_amount = $order_goods['total_price'] - $discount;
  67. $pay_status = 0; //未付款
  68. //如果各种优惠金额大于订单实际金额跟运费之和,则默认订单状态为已付款
  69. if($order_amount < 0)
  70. {
  71. $order_amount = 0;
  72. $pay_status = 1; //已付款
  73. }
  74. //构造订单字段
  75. $order_info = array(
  76. 'order_sn' => date('YmdHis'.rand(1000,9999)),
  77. 'add_time' => time(),
  78. 'pay_status' => $pay_status,
  79. 'user_id' => $user_id,
  80. 'goods_amount' => $order_goods['total_price'], //商品的总金额
  81. 'order_amount' => $order_amount, //应付金额=商品总价+运费-优惠(积分、红包)
  82. 'discount' => $discount, //优惠金额
  83. 'name' => $user_address['name'],
  84. 'country' => $user_address['country'],
  85. 'province' => $user_address['province'],
  86. 'city' => $user_address['city'],
  87. 'district' => $user_address['district'],
  88. 'address' => $user_address['address'],
  89. 'zipcode' => $user_address['zipcode'],
  90. 'mobile' => $user_address['mobile'],
  91. 'place_type' => $place_type, //订单来源
  92. 'bonus_id' => !empty($user_bonus)?$user_bonus['id']:0,
  93. 'bonus_money' => !empty($user_bonus)?$user_bonus['money']:0.00,
  94. 'message' => $message
  95. );
  96. //插入订单
  97. $order_id = self::insertGetId($order_info);
  98. if ($order_id)
  99. {
  100. //订单生成成功之后,扣除用户的积分和改变优惠券的使用状态
  101. //改变优惠券使用状态
  102. UserBonus::where(array('user_id'=>$user_id,'id'=>$user_bonus_id))->update(array('status'=>1,'used_time'=>time()));
  103. //扣除用户积分,预留
  104. //$updateMember['validscore'] = $addressInfo['validscore']-$PointPay;
  105. //M("Member")->where(array('id'=>$CustomerSysNo))->save($updateMember);
  106. //增加一条积分支出记录,一条购物获取积分记录
  107. //插入订单商品
  108. $order_goods_list = array();
  109. foreach($order_goods['list'] as $k=>$v)
  110. {
  111. $temp_order_goods = array(
  112. 'order_id' => $order_id,
  113. 'goods_id' => $v['goods_id'],
  114. 'goods_name' => $v['title'],
  115. 'goods_number' => $v['goods_number'],
  116. 'market_price' => $v['market_price'],
  117. 'goods_price' => $v['final_price'],
  118. //'goods_attr' => '', //商品属性,预留
  119. 'goods_img' => $v['litpic']
  120. );
  121. array_push($order_goods_list,$temp_order_goods);
  122. //订单商品直行减库存操作
  123. Goods::changeGoodsStock(array('goods_id'=>$v['goods_id'],'goods_number'=>$v['goods_number']));
  124. }
  125. $result = DB::table('order_goods')->insert($order_goods_list);
  126. if($result)
  127. {
  128. //删除购物对应的记录
  129. Cart::where(array('user_id'=>$user_id))->whereIn('id', explode("_",$cartids))->delete();
  130. return ReturnData::create(ReturnData::SUCCESS,$order_id);
  131. }
  132. else
  133. {
  134. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'订单商品添加失败');
  135. }
  136. }
  137. else
  138. {
  139. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'生成订单失败');
  140. }
  141. }
  142. public static function modify($where, array $data)
  143. {
  144. if (self::where($where)->update($data))
  145. {
  146. return true;
  147. }
  148. return false;
  149. }
  150. //删除一条记录
  151. public static function remove($id,$user_id)
  152. {
  153. if(!is_array($id)){$id = explode(',', $id);}
  154. if (self::whereIn('id', $id)->where('user_id',$user_id)->delete() === false)
  155. {
  156. return false;
  157. }
  158. return true;
  159. }
  160. /**
  161. * 添加商品到购物车
  162. *
  163. * @access public
  164. * @param integer $goods_id 商品编号
  165. * @param integer $num 商品数量
  166. * @param json $property 规格值对应的id json数组
  167. * @return boolean
  168. */
  169. public static function cartAdd(array $attributes)
  170. {
  171. extract($attributes);
  172. //获取商品信息
  173. $goods = Goods::where(['id' => $goods_id, 'status' => Goods::STATUS])->first();
  174. if (!$goods)
  175. {
  176. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'商品不存在');
  177. }
  178. //判断库存 是否足够
  179. if($goods['goods_number']<$goods_number)
  180. {
  181. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'库存不足');
  182. }
  183. //判断购物车商品数
  184. if(Cart::where(['user_id'=>$user_id])->count() >= 20)
  185. {
  186. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'购物车商品最多20件');
  187. }
  188. //查看是否已经有购物车插入记录
  189. $where = array(
  190. 'user_id' => $user_id,
  191. 'goods_id' => $goods_id
  192. );
  193. $cart = Cart::where($where)->first();
  194. if($cart)
  195. {
  196. //更新购物车
  197. $updateArr = array(
  198. 'goods_number' => $goods_number,
  199. 'add_time' => time(),
  200. );
  201. self::where(array('id'=>$cart->id))->update($updateArr);
  202. $cart_id = $cart->id;
  203. }
  204. else
  205. {
  206. //添加购物车
  207. $cartInsert = array(
  208. 'user_id' => $user_id,
  209. 'goods_id' => $goods_id,
  210. 'goods_number' => $goods_number,
  211. 'add_time' => time(),
  212. );
  213. $cart_id = self::insertGetId($cartInsert);
  214. }
  215. return ReturnData::create(ReturnData::SUCCESS,$cart_id,'购物车添加成功');
  216. }
  217. /**
  218. * 清空购物车
  219. *
  220. * @param int $type 类型:默认普通商品
  221. */
  222. public static function clearCart($user_id)
  223. {
  224. self::where('user_id',$user_id)->delete();
  225. return true;
  226. }
  227. //购物车商品总数量
  228. public static function TotalGoodsCount($user_id)
  229. {
  230. return self::where('user_id',$user_id)->sum('goods_number');
  231. }
  232. //购物车结算商品列表
  233. public static function cartCheckoutGoodsList(array $param)
  234. {
  235. extract($param);
  236. $cartIds = explode("_",$ids);
  237. // 获取购物车列表
  238. $cartList = self::where(array('user_id'=>$user_id))->whereIn('id', $cartIds)->get();
  239. $total_price = 0; //商品总金额
  240. $total_goods = 0; //商品总数量
  241. if(!empty($cartList))
  242. {
  243. $resultList = array();
  244. $checkArr = array();
  245. foreach($cartList as $k=>$v)
  246. {
  247. $goods = Goods::where(array('id'=>$v['goods_id']))->first();
  248. $cartList[$k]->is_promote = 0;
  249. if(Goods::bargain_price($goods->price,$goods->promote_start_date,$goods->promote_end_date) > 0){$cartList[$k]->is_promote = 1;}
  250. $cartList[$k]->final_price = Goods::get_final_price($v['goods_id']); //商品最终价格
  251. $cartList[$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v['goods_id']));
  252. $cartList[$k]->title = $goods->title;
  253. $cartList[$k]->litpic = $goods->litpic;
  254. $total_price = $total_price + $cartList[$k]->final_price*$cartList[$k]->goods_number;
  255. $total_goods = $total_goods + $cartList[$k]->goods_number;
  256. }
  257. }
  258. $res['list'] = $cartList;
  259. $res['total_price'] = $total_price;
  260. $res['total_goods'] = $total_goods;
  261. return ReturnData::create(ReturnData::SUCCESS,$res);
  262. }
  263. }