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.

445 lines
17 KiB

7 years ago
4 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
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
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Logic;
  3. use Illuminate\Support\Facades\Log;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Common\ReturnData;
  6. use App\Http\Model\Order;
  7. use App\Http\Requests\OrderRequest;
  8. use Validator;
  9. class OrderLogic extends BaseLogic
  10. {
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. }
  15. public function getModel()
  16. {
  17. return new Order();
  18. }
  19. public function getValidate($data, $scene_name)
  20. {
  21. //数据验证
  22. $validate = new OrderRequest();
  23. return Validator::make($data, $validate->getSceneRules($scene_name), $validate->getSceneRulesMessages());
  24. }
  25. //列表
  26. public function getList($where = array(), $order = '', $field = '*', $offset = '', $limit = '')
  27. {
  28. $res = $this->getModel()->getList($where, $order, $field, $offset, $limit);
  29. if($res['count'] > 0)
  30. {
  31. foreach($res['list'] as $k=>$v)
  32. {
  33. $res['list'][$k] = $this->getDataView($v);
  34. $order_status_arr = $this->getModel()->getOrderStatusAttr($v);
  35. $res['list'][$k]->order_status_text = $order_status_arr?$order_status_arr['text']:'';
  36. $res['list'][$k]->order_status_num = $order_status_arr?$order_status_arr['num']:'';
  37. $res['list'][$k]->province_name = model('Region')->getRegionName(['id'=>$v->province]);
  38. $res['list'][$k]->city_name = model('Region')->getRegionName(['id'=>$v->city]);
  39. $res['list'][$k]->district_name = model('Region')->getRegionName(['id'=>$v->district]);
  40. $order_goods = model('OrderGoods')->getDb()->where(array('order_id'=>$v->id))->get();
  41. $res['list'][$k]->goods_list = $order_goods;
  42. }
  43. }
  44. return $res;
  45. }
  46. //分页html
  47. public function getPaginate($where = array(), $order = '', $field = '*', $limit = '')
  48. {
  49. $res = $this->getModel()->getPaginate($where, $order, $field, $limit);
  50. if($res->count() > 0)
  51. {
  52. foreach($res as $k=>$v)
  53. {
  54. $res[$k] = $this->getDataView($v);
  55. }
  56. }
  57. return $res;
  58. }
  59. //全部列表
  60. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  61. {
  62. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  63. if($res)
  64. {
  65. foreach($res as $k=>$v)
  66. {
  67. $res[$k] = $this->getDataView($v);
  68. }
  69. }
  70. return $res;
  71. }
  72. //详情
  73. public function getOne($where = array(), $field = '*')
  74. {
  75. $res = $this->getModel()->getOne($where, $field);
  76. if(!$res){return false;}
  77. $res = $this->getDataView($res);
  78. $order_status_arr = $this->getModel()->getOrderStatusAttr($res);
  79. $res->order_status_text = $order_status_arr['text'];
  80. $res->order_status_num = $order_status_arr['num'];
  81. $res->province_name = model('Region')->getRegionName(['id'=>$res->province]);
  82. $res->city_name = model('Region')->getRegionName(['id'=>$res->city]);
  83. $res->district_name = model('Region')->getRegionName(['id'=>$res->district]);
  84. $order_goods = model('OrderGoods')->getDb()->where(array('order_id'=>$res->id))->get();
  85. $res->goods_list = $order_goods;
  86. return $res;
  87. }
  88. /**
  89. * 生成订单
  90. * @param string $data['cartids'] 购物车商品id,格式:1_2_3
  91. * @param int $data['user_id'] 用户id
  92. * @param int $data['user_bonus_id'] 用户优惠券id
  93. * @param string $data['default_address_id'] 用户默认收货地址
  94. * @param float $data['shipping_costs'] 运费
  95. * @param string $data['message'] 备注
  96. * @param int $data['place_type'] 订单来源,1pc,2微信,3app
  97. * @return array
  98. */
  99. public function add($data = array(), $type=0)
  100. {
  101. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  102. //总的验证规则
  103. $rules = [
  104. 'cartids' => 'required',
  105. 'user_id' => 'required|integer',
  106. 'user_bonus_id' => 'integer',
  107. 'default_address_id' => 'required|integer',
  108. 'shipping_costs' => ['regex:/^\d{0,10}(\.\d{0,2})?$/'],
  109. 'message' => 'max:150',
  110. 'place_type' => 'integer|between:0,5',
  111. ];
  112. //总的自定义错误信息
  113. $messages = [
  114. 'cartids.required' => '购物车商品id必填',
  115. 'user_id.required' => '用户ID必填',
  116. 'user_id.integer' => '用户ID必须为数字',
  117. 'user_bonus_id.integer' => '优惠券ID必须为数字',
  118. 'default_address_id.required' => '收货地址必填',
  119. 'default_address_id.integer' => '收货地址必须为数字',
  120. 'shipping_costs.regex' => '运费格式不正确,运费只能带2位小数的数字',
  121. 'message.max' => '备注不能超过150个字符',
  122. 'place_type.integer' => '订单来源必须是数字',
  123. 'place_type.between' => '订单来源,1pc,2微信,3app',
  124. ];
  125. $validator = Validator::make($data, $rules, $messages);
  126. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  127. //获取订单商品列表
  128. $cartCheckoutGoods = logic('Cart')->cartCheckoutGoodsList(array('ids'=>$data['cartids'],'user_id'=>$data['user_id']));
  129. $order_goods = $cartCheckoutGoods['data'];
  130. if(empty($order_goods['list'])){return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'订单商品不存在');}
  131. //获取收货地址
  132. $user_address = model('UserAddress')->getOne(['user_id'=>$data['user_id'],'id'=>$data['default_address_id']]);
  133. if(!$user_address){return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'收货地址不存在');}
  134. //获取优惠券信息
  135. $user_bonus = logic('UserBonus')->getUserBonusByid(array('user_bonus_id'=>$data['user_bonus_id'],'user_id'=>$data['user_id']));
  136. $discount = !empty($user_bonus)?$user_bonus['money']:0.00; //优惠金额=优惠券
  137. $order_amount = $order_goods['total_price'] - $discount;
  138. $pay_status = 0; //未付款
  139. //如果各种优惠金额大于订单实际金额跟运费之和,则默认订单状态为已付款
  140. if($order_amount < 0)
  141. {
  142. $order_amount = 0;
  143. $pay_status = 1; //已付款
  144. }
  145. $time = time();
  146. //构造订单字段
  147. $order_info = array(
  148. 'order_sn' => date('YmdHis').rand(1000,9999),
  149. 'add_time' => $time,
  150. 'updated_at' => $time,
  151. 'pay_status' => $pay_status,
  152. 'user_id' => $data['user_id'],
  153. 'goods_amount' => $order_goods['total_price'], //商品的总金额
  154. 'order_amount' => $order_amount, //应付金额=商品总价+运费-优惠(积分、红包)
  155. 'discount' => $discount, //优惠金额
  156. 'name' => $user_address->name,
  157. //'country' => $user_address->country,
  158. 'province' => $user_address->province,
  159. 'city' => $user_address->city,
  160. 'district' => $user_address->district,
  161. 'address' => $user_address->address,
  162. 'zipcode' => $user_address->zipcode,
  163. 'mobile' => $user_address->mobile,
  164. 'place_type' => $data['place_type'], //订单来源
  165. 'bonus_id' => $user_bonus?$user_bonus['id']:0,
  166. 'bonus_money' => $user_bonus?$user_bonus['money']:0.00,
  167. 'message' => $data['message'] ? $data['message'] : '',
  168. );
  169. //插入订单
  170. $order_id = $this->getModel()->add($order_info);
  171. if ($order_id)
  172. {
  173. //订单生成成功之后,扣除用户的积分和改变优惠券的使用状态
  174. //改变优惠券使用状态
  175. model('UserBonus')->getDb()->where(array('user_id'=>$data['user_id'],'id'=>$data['user_bonus_id']))->update(array('status'=>1,'used_time'=>time()));
  176. //扣除用户积分,预留
  177. //$updateMember['validscore'] = $addressInfo['validscore']-$PointPay;
  178. //M("Member")->where(array('id'=>$CustomerSysNo))->save($updateMember);
  179. //增加一条积分支出记录,一条购物获取积分记录
  180. //插入订单商品
  181. $order_goods_list = array();
  182. foreach($order_goods['list'] as $k=>$v)
  183. {
  184. $temp_order_goods = array(
  185. 'order_id' => $order_id,
  186. 'goods_id' => $v->goods_id,
  187. 'goods_name' => $v->title,
  188. 'goods_number' => $v->goods_number,
  189. 'market_price' => $v->market_price,
  190. 'goods_price' => $v->final_price,
  191. //'goods_attr' => '', //商品属性,预留
  192. 'goods_img' => $v->litpic
  193. );
  194. array_push($order_goods_list,$temp_order_goods);
  195. //订单商品直接减库存操作
  196. model('Goods')->changeGoodsStock(array('goods_id'=>$v->goods_id,'goods_number'=>$v->goods_number));
  197. }
  198. $result = model('OrderGoods')->add($order_goods_list,1);
  199. if($result)
  200. {
  201. //删除购物对应的记录
  202. model('Cart')->getDb()->where(array('user_id'=>$data['user_id']))->whereIn('id', explode("_",$data['cartids']))->delete();
  203. return ReturnData::create(ReturnData::SUCCESS, $order_id);
  204. }
  205. else
  206. {
  207. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'订单商品添加失败');
  208. }
  209. }
  210. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'生成订单失败');
  211. }
  212. //修改
  213. public function edit($data, $where = array())
  214. {
  215. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  216. $validator = $this->getValidate($data, 'edit');
  217. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  218. $res = $this->getModel()->edit($data,$where);
  219. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  220. return ReturnData::create(ReturnData::FAIL);
  221. }
  222. //删除
  223. public function del($where)
  224. {
  225. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  226. $validator = $this->getValidate($where,'del');
  227. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  228. $where2 = function ($query) use ($where) {
  229. $query->where($where)->where(function ($query2) {$query2->where(array('order_status'=>3,'refund_status'=>0))->orWhere(array('order_status'=>1))->orWhere(array('order_status'=>2));});
  230. };
  231. $data['is_delete'] = 1;
  232. $res = $this->getModel()->edit($data, $where2);
  233. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  234. return ReturnData::create(ReturnData::FAIL);
  235. }
  236. /**
  237. * 用户-取消订单
  238. * @param int $data['id'] 订单id
  239. * @param int $data['user_id'] 用户id
  240. * @return array
  241. */
  242. public function userCancelOrder($where = array())
  243. {
  244. if(empty($where)){return ReturnData::create(ReturnData::SUCCESS);}
  245. $where['order_status'] = 0;
  246. $where['pay_status'] = 0;
  247. $order = $this->getModel()->getOne($where);
  248. if(!$order){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'订单不存在');}
  249. $data['order_status'] = 1;
  250. $data['updated_at'] = time();
  251. $res = $this->getModel()->edit($data,$where);
  252. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  253. return ReturnData::create(ReturnData::FAIL);
  254. }
  255. /**
  256. * 订单-余额支付
  257. * @param int $where['id'] 订单id
  258. * @param int $where['user_id'] 用户id
  259. * @return array
  260. */
  261. public function orderYuepay($where = array())
  262. {
  263. if(empty($where)){return ReturnData::create(ReturnData::SUCCESS);}
  264. $where['order_status'] = 0;
  265. $where['pay_status'] = 0;
  266. $order = $this->getModel()->getOne($where);
  267. if(!$order){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'订单不存在');}
  268. //获取用户余额信息
  269. $user = model('User')->getOne(array('id'=>$where['user_id']));
  270. if(!$user){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'用户不存在');}
  271. if($user->money < $order->order_amount)
  272. {
  273. return ReturnData::create(ReturnData::PARAMS_ERROR, null, '余额不足');
  274. }
  275. DB::beginTransaction();
  276. $data['pay_status'] = 1;
  277. $data['pay_money'] = $order->order_amount; //支付金额
  278. $data['pay_id'] = 1;
  279. $data['pay_time'] = time();
  280. $data['updated_at'] = time();
  281. $res = $this->getModel()->edit($data,$where);
  282. if(!$res)
  283. {
  284. DB::rollBack();
  285. return ReturnData::create(ReturnData::FAIL);
  286. }
  287. $user_money_data['user_id'] = $where['user_id'];
  288. $user_money_data['type'] = 1;
  289. $user_money_data['money'] = $order->order_amount;
  290. $user_money_data['des'] = '订单余额支付';
  291. if(!logic('UserMoney')->add($user_money_data)){ DB::rollBack(); return ReturnData::create(ReturnData::FAIL); }
  292. DB::commit();
  293. return ReturnData::create(ReturnData::SUCCESS,$res,'支付成功');
  294. }
  295. /**
  296. * 订单-确认收货
  297. * @param int $data['id'] 订单id
  298. * @param int $data['user_id'] 用户id
  299. * @return array
  300. */
  301. public function orderReceiptConfirm($where = array())
  302. {
  303. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  304. //判断订单是否存在或本人
  305. $where['order_status'] = 0;
  306. $where['refund_status'] = 0;
  307. $where['pay_status'] = 1;
  308. $order = $this->getModel()->getOne($where);
  309. if(!$order){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'订单不存在');}
  310. $data['order_status'] = 3;
  311. $data['shipping_status'] = 2;
  312. $data['refund_status'] = 0;
  313. $data['is_comment'] = 0;
  314. $data['updated_at'] = time();
  315. $res = $this->getModel()->edit($data,$where);
  316. if($res)
  317. {
  318. return ReturnData::create(ReturnData::SUCCESS);
  319. }
  320. return ReturnData::create(ReturnData::FAIL);
  321. }
  322. /**
  323. * 订单-退款退货
  324. * @param int $data['id'] 订单id
  325. * @param int $data['user_id'] 用户id
  326. * @return array
  327. */
  328. public function orderRefund($where = array())
  329. {
  330. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  331. $where['order_status'] = 3;
  332. $where['refund_status'] = 0;
  333. $order = $this->getModel()->getOne($where);
  334. if(!$order){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'订单不存在');}
  335. $data['refund_status'] = 1;
  336. $data['updated_at'] = time();
  337. $res = $this->getModel()->edit($data,$where);
  338. if($res)
  339. {
  340. return ReturnData::create(ReturnData::SUCCESS);
  341. }
  342. return ReturnData::create(ReturnData::FAIL);
  343. }
  344. /**
  345. * 订单-设为评价
  346. * @param int $data['id'] 订单id
  347. * @param int $data['user_id'] 用户id
  348. * @return array
  349. */
  350. public function orderSetComment($where = array())
  351. {
  352. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  353. $where['order_status'] = 3;
  354. $where['refund_status'] = 0;
  355. $data['is_comment'] = Order::ORDER_UN_COMMENT;
  356. $order = $this->getModel()->getOne($where);
  357. if(!$order){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'订单不存在,或已评价');}
  358. $data['is_comment'] = Order::ORDER_IS_COMMENT;
  359. $data['updated_at'] = time();
  360. $res = $this->getModel()->edit($data,$where);
  361. if($res)
  362. {
  363. return ReturnData::create(ReturnData::SUCCESS);
  364. }
  365. return ReturnData::create(ReturnData::FAIL);
  366. }
  367. /**
  368. * 数据获取器
  369. * @param array $data 要转化的数据
  370. * @return array
  371. */
  372. private function getDataView($data = array())
  373. {
  374. return getDataAttr($this->getModel(),$data);
  375. }
  376. }