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.

241 lines
8.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 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. return $res;
  59. }
  60. //全部列表
  61. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  62. {
  63. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  64. /* if($res)
  65. {
  66. foreach($res as $k=>$v)
  67. {
  68. $res[$k] = $this->getDataView($v);
  69. }
  70. } */
  71. return $res;
  72. }
  73. //详情
  74. public function getOne($where = array(), $field = '*')
  75. {
  76. $res = $this->getModel()->getOne($where, $field);
  77. if(!$res){return false;}
  78. $res = $this->getDataView($res);
  79. return $res;
  80. }
  81. //添加
  82. public function add($data = array(), $type=0)
  83. {
  84. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  85. $validator = $this->getValidate($data, 'add');
  86. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  87. //获取商品信息
  88. $goods = model('Goods')->getDb()->where(['id' => $data['goods_id'], 'status' => Goods::GOODS_NORMAL_STATUS])->first();
  89. if (!$goods)
  90. {
  91. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'商品不存在');
  92. }
  93. //判断库存 是否足够
  94. if($goods->goods_number<$data['goods_number'])
  95. {
  96. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'库存不足');
  97. }
  98. //判断购物车商品数
  99. if(Cart::where(['user_id'=>$data['user_id']])->count() >= 20)
  100. {
  101. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'购物车商品最多20件');
  102. }
  103. //查看是否已经有购物车插入记录
  104. $where = array(
  105. 'user_id' => $data['user_id'],
  106. 'goods_id' => $data['goods_id']
  107. );
  108. $cart = Cart::where($where)->first();
  109. if($cart)
  110. {
  111. //更新购物车
  112. $updateArr = array(
  113. 'goods_number' => $data['goods_number'],
  114. 'add_time' => time(),
  115. );
  116. Cart::where(array('id'=>$cart->id))->update($updateArr);
  117. $cart_id = $cart->id;
  118. }
  119. else
  120. {
  121. //添加购物车
  122. $cartInsert = array(
  123. 'user_id' => $data['user_id'],
  124. 'goods_id' => $data['goods_id'],
  125. 'goods_number' => $data['goods_number'],
  126. 'add_time' => time(),
  127. );
  128. $cart_id = Cart::insertGetId($cartInsert);
  129. }
  130. if(isset($cart_id) && $cart_id){return ReturnData::create(ReturnData::SUCCESS,$cart_id,'购物车添加成功');}
  131. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  132. }
  133. //修改
  134. public function edit($data, $where = array())
  135. {
  136. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  137. $validator = $this->getValidate($data, 'edit');
  138. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  139. $res = $this->getModel()->edit($data,$where);
  140. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  141. return ReturnData::create(ReturnData::FAIL);
  142. }
  143. //删除
  144. public function del($where)
  145. {
  146. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  147. $res = $this->getModel()->del($where);
  148. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  149. return ReturnData::create(ReturnData::FAIL);
  150. }
  151. /**
  152. * 数据获取器
  153. * @param array $data 要转化的数据
  154. * @return array
  155. */
  156. private function getDataView($data = array())
  157. {
  158. return getDataAttr($this->getModel(),$data);
  159. }
  160. //购物车结算商品列表
  161. public function cartCheckoutGoodsList($where)
  162. {
  163. $cartIds = explode("_",$where['ids']);
  164. // 获取购物车列表
  165. $cartList = Cart::where(array('user_id'=>$where['user_id']))->whereIn('id', $cartIds)->get();
  166. $total_price = 0; //商品总金额
  167. $total_goods = 0; //商品总数量
  168. if($cartList)
  169. {
  170. $resultList = array();
  171. $checkArr = array();
  172. foreach($cartList as $k=>$v)
  173. {
  174. $goods = Goods::where(array('id'=>$v['goods_id']))->first();
  175. $cartList[$k]->is_promote = 0;
  176. if(model('Goods')->bargain_price($goods->price,$goods->promote_start_date,$goods->promote_end_date) > 0){$cartList[$k]->is_promote = 1;}
  177. $cartList[$k]->final_price = model('Goods')->get_goods_final_price($goods); //商品最终价格
  178. $cartList[$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v['goods_id']));
  179. $cartList[$k]->title = $goods->title;
  180. $cartList[$k]->litpic = $goods->litpic;
  181. $cartList[$k]->market_price = $goods->market_price;
  182. $cartList[$k]->goods_sn = $goods->sn;
  183. $total_price = $total_price + $cartList[$k]->final_price*$cartList[$k]->goods_number;
  184. $total_goods = $total_goods + $cartList[$k]->goods_number;
  185. }
  186. }
  187. $res['list'] = $cartList;
  188. $res['total_price'] = $total_price;
  189. $res['total_goods'] = $total_goods;
  190. return ReturnData::create(ReturnData::SUCCESS,$res);
  191. }
  192. }