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.

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