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.

249 lines
8.4 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
6 years ago
7 years ago
6 years ago
7 years ago
6 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\Logic;
  3. use App\Common\ReturnData;
  4. use App\Http\Model\Cart;
  5. use App\Http\Model\Goods;
  6. use App\Http\Requests\CartRequest;
  7. use Validator;
  8. class CartLogic extends BaseLogic
  9. {
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. }
  14. public function getModel()
  15. {
  16. return model('Cart');
  17. }
  18. public function getValidate($data, $scene_name)
  19. {
  20. //数据验证
  21. $validate = new CartRequest();
  22. return Validator::make($data, $validate->getSceneRules($scene_name), $validate->getSceneRulesMessages());
  23. }
  24. //列表
  25. public function getList($where = array(), $order = '', $field = '*', $offset = '', $limit = '')
  26. {
  27. $model = $this->getModel()->getDb();
  28. $model = $model->join('goods', 'goods.id', '=', 'cart.goods_id')
  29. ->where('cart.user_id', $where['user_id'])
  30. ->where('goods.status', Goods::GOODS_NORMAL_STATUS)
  31. ->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');
  32. $res['count'] = $model->count();
  33. $res['list'] = array();
  34. if($res['count']>0)
  35. {
  36. $res['list'] = $model->get();
  37. foreach ($res['list'] as $k => $v)
  38. {
  39. $res['list'][$k]->is_promote = 0;
  40. if(model('Goods')->bargain_price($v->goods_price,$v->promote_start_date,$v->promote_end_date) > 0){$res['list'][$k]->is_promote = 1;}
  41. //订货数量大于0
  42. if ($v->goods_number > 0)
  43. {
  44. $goods_tmp = ['price'=>$v->goods_price,'promote_price'=>$v->promote_price,'promote_start_date'=>$v->promote_start_date,'promote_end_date'=>$v->promote_end_date];
  45. $res['list'][$k]->final_price = model('Goods')->get_goods_final_price((object)$goods_tmp); //商品最终价格
  46. $res['list'][$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v->goods_id));
  47. //更新购物车中的商品数量
  48. //self::where('id', $v->id)->update(array('price' => $goods_price));
  49. }
  50. }
  51. }
  52. return $res;
  53. }
  54. //分页html
  55. public function getPaginate($where = array(), $order = '', $field = '*', $limit = '')
  56. {
  57. $res = $this->getModel()->getPaginate($where, $order, $field, $limit);
  58. if($res->count() > 0)
  59. {
  60. foreach($res as $k=>$v)
  61. {
  62. $res[$k] = $this->getDataView($v);
  63. }
  64. }
  65. return $res;
  66. }
  67. //全部列表
  68. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  69. {
  70. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  71. if($res)
  72. {
  73. foreach($res as $k=>$v)
  74. {
  75. $res[$k] = $this->getDataView($v);
  76. }
  77. }
  78. return $res;
  79. }
  80. //详情
  81. public function getOne($where = array(), $field = '*')
  82. {
  83. $res = $this->getModel()->getOne($where, $field);
  84. if(!$res){return false;}
  85. $res = $this->getDataView($res);
  86. return $res;
  87. }
  88. //添加
  89. public function add($data = array(), $type=0)
  90. {
  91. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  92. $validator = $this->getValidate($data, 'add');
  93. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  94. //获取商品信息
  95. $goods = model('Goods')->getDb()->where(['id' => $data['goods_id'], 'status' => Goods::GOODS_NORMAL_STATUS])->first();
  96. if (!$goods)
  97. {
  98. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'商品不存在');
  99. }
  100. //判断库存 是否足够
  101. if($goods->goods_number<$data['goods_number'])
  102. {
  103. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'库存不足');
  104. }
  105. //判断购物车商品数
  106. if(Cart::where(['user_id'=>$data['user_id']])->count() >= 20)
  107. {
  108. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'购物车商品最多20件');
  109. }
  110. //查看是否已经有购物车插入记录
  111. $where = array(
  112. 'user_id' => $data['user_id'],
  113. 'goods_id' => $data['goods_id']
  114. );
  115. $cart = Cart::where($where)->first();
  116. if($cart)
  117. {
  118. //更新购物车
  119. $updateArr = array(
  120. 'goods_number' => $data['goods_number'],
  121. 'add_time' => time(),
  122. );
  123. Cart::where(array('id'=>$cart->id))->update($updateArr);
  124. $cart_id = $cart->id;
  125. }
  126. else
  127. {
  128. //添加购物车
  129. $cartInsert = array(
  130. 'user_id' => $data['user_id'],
  131. 'goods_id' => $data['goods_id'],
  132. 'goods_number' => $data['goods_number'],
  133. 'add_time' => time(),
  134. );
  135. $cart_id = Cart::insertGetId($cartInsert);
  136. }
  137. if(isset($cart_id) && $cart_id){return ReturnData::create(ReturnData::SUCCESS,$cart_id,'购物车添加成功');}
  138. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  139. }
  140. //修改
  141. public function edit($data, $where = array())
  142. {
  143. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  144. $validator = $this->getValidate($data, 'edit');
  145. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  146. $res = $this->getModel()->edit($data,$where);
  147. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  148. return ReturnData::create(ReturnData::FAIL);
  149. }
  150. //删除
  151. public function del($where)
  152. {
  153. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  154. $res = $this->getModel()->del($where);
  155. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  156. return ReturnData::create(ReturnData::FAIL);
  157. }
  158. /**
  159. * 数据获取器
  160. * @param array $data 要转化的数据
  161. * @return array
  162. */
  163. private function getDataView($data = array())
  164. {
  165. return getDataAttr($this->getModel(),$data);
  166. }
  167. //购物车结算商品列表
  168. public function cartCheckoutGoodsList($where)
  169. {
  170. $cartIds = explode("_",$where['ids']);
  171. // 获取购物车列表
  172. $cartList = Cart::where(array('user_id'=>$where['user_id']))->whereIn('id', $cartIds)->get();
  173. $total_price = 0; //商品总金额
  174. $total_goods = 0; //商品总数量
  175. if($cartList)
  176. {
  177. $resultList = array();
  178. $checkArr = array();
  179. foreach($cartList as $k=>$v)
  180. {
  181. $goods = Goods::where(array('id'=>$v['goods_id']))->first();
  182. $cartList[$k]->is_promote = 0;
  183. if(model('Goods')->bargain_price($goods->price,$goods->promote_start_date,$goods->promote_end_date) > 0){$cartList[$k]->is_promote = 1;}
  184. $cartList[$k]->final_price = model('Goods')->get_goods_final_price($goods); //商品最终价格
  185. $cartList[$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v['goods_id']));
  186. $cartList[$k]->title = $goods->title;
  187. $cartList[$k]->litpic = $goods->litpic;
  188. $cartList[$k]->market_price = $goods->market_price;
  189. $cartList[$k]->goods_sn = $goods->sn;
  190. $total_price = $total_price + $cartList[$k]->final_price*$cartList[$k]->goods_number;
  191. $total_goods = $total_goods + $cartList[$k]->goods_number;
  192. }
  193. }
  194. $res['list'] = $cartList;
  195. $res['total_price'] = $total_price;
  196. $res['total_goods'] = $total_goods;
  197. return ReturnData::create(ReturnData::SUCCESS,$res);
  198. }
  199. }