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.

255 lines
7.5 KiB

7 years ago
4 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
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 Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Log;
  5. class Order extends BaseModel
  6. {
  7. //订单
  8. protected $table = 'order';
  9. public $timestamps = false;
  10. protected $hidden = array();
  11. protected $guarded = array(); //$guarded包含你不想被赋值的字段数组。
  12. const ORDER_UN_COMMENT = 0;//未评价
  13. const ORDER_IS_COMMENT = 1;//是否评论,1已评价
  14. public function getDb()
  15. {
  16. return DB::table($this->table);
  17. }
  18. /**
  19. * 列表
  20. * @param array $where 查询条件
  21. * @param string $order 排序
  22. * @param string $field 字段
  23. * @param int $offset 偏移量
  24. * @param int $limit 取多少条
  25. * @return array
  26. */
  27. public function getList($where = array(), $order = '', $field = '*', $offset = 0, $limit = 15)
  28. {
  29. $model = $this->getDb();
  30. if($where){$model = $model->where($where);}
  31. $res['count'] = $model->count();
  32. $res['list'] = array();
  33. if($res['count'] > 0)
  34. {
  35. if($field){if(is_array($field)){$model = $model->select($field);}else{$model = $model->select(\DB::raw($field));}}
  36. if($order){$model = parent::getOrderByData($model, $order);}
  37. if($offset){}else{$offset = 0;}
  38. if($limit){}else{$limit = 15;}
  39. $res['list'] = $model->skip($offset)->take($limit)->get();
  40. }
  41. return $res;
  42. }
  43. /**
  44. * 分页,用于前端html输出
  45. * @param array $where 查询条件
  46. * @param string $order 排序
  47. * @param string $field 字段
  48. * @param int $limit 每页几条
  49. * @param int $page 当前第几页
  50. * @return array
  51. */
  52. public function getPaginate($where = array(), $order = '', $field = '*', $limit = 15)
  53. {
  54. $res = $this->getDb();
  55. if($where){$res = $res->where($where);}
  56. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  57. if($order){$res = parent::getOrderByData($res, $order);}
  58. if($limit){}else{$limit = 15;}
  59. return $res->paginate($limit);
  60. }
  61. /**
  62. * 查询全部
  63. * @param array $where 查询条件
  64. * @param string $order 排序
  65. * @param string $field 字段
  66. * @param int $limit 取多少条
  67. * @return array
  68. */
  69. public function getAll($where = array(), $order = '', $field = '*', $limit = '', $offset = '')
  70. {
  71. $res = $this->getDb();
  72. if($where){$res = $res->where($where);}
  73. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  74. if($order){$res = parent::getOrderByData($res, $order);}
  75. if($offset){$res = $res->skip($offset);}
  76. if($limit){$res = $res->take($limit);}
  77. $res = $res->get();
  78. return $res;
  79. }
  80. /**
  81. * 获取一条
  82. * @param array $where 条件
  83. * @param string $field 字段
  84. * @return array
  85. */
  86. public function getOne($where, $field = '*')
  87. {
  88. $res = $this->getDb();
  89. if($where){$res = $res->where($where);}
  90. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  91. $res = $res->first();
  92. return $res;
  93. }
  94. /**
  95. * 添加
  96. * @param array $data 数据
  97. * @return int
  98. */
  99. public function add(array $data,$type = 0)
  100. {
  101. if($type==0)
  102. {
  103. // 新增单条数据并返回主键值
  104. return self::insertGetId(parent::filterTableColumn($data,$this->table));
  105. }
  106. elseif($type==1)
  107. {
  108. /**
  109. * 添加单条数据
  110. * $data = ['foo' => 'bar', 'bar' => 'foo'];
  111. * 添加多条数据
  112. * $data = [
  113. * ['foo' => 'bar', 'bar' => 'foo'],
  114. * ['foo' => 'bar1', 'bar' => 'foo1'],
  115. * ['foo' => 'bar2', 'bar' => 'foo2']
  116. * ];
  117. */
  118. return self::insert($data);
  119. }
  120. }
  121. /**
  122. * 修改
  123. * @param array $data 数据
  124. * @param array $where 条件
  125. * @return int
  126. */
  127. public function edit($data, $where = array())
  128. {
  129. $res = $this->getDb();
  130. return $res->where($where)->update(parent::filterTableColumn($data, $this->table));
  131. }
  132. /**
  133. * 删除
  134. * @param array $where 条件
  135. * @return bool
  136. */
  137. public function del($where)
  138. {
  139. $res = $this->getDb();
  140. $res = $res->where($where)->delete();
  141. return $res;
  142. }
  143. //获取订单状态文字:1待付款,2待发货,3待收货,4待评价(确认收货,交易成功),5退款/售后,6已取消,7无效
  144. public function getOrderStatusAttr($data)
  145. {
  146. $res = '';
  147. if($data->order_status == 0 && $data->pay_status ==0)
  148. {
  149. $res = array('text'=>'待付款','num'=>1);
  150. }
  151. elseif($data->order_status == 0 && $data->shipping_status == 0 && $data->pay_status == 1)
  152. {
  153. $res = array('text'=>'待发货','num'=>2);
  154. }
  155. elseif($data->order_status == 0 && $data->refund_status == 0 && $data->shipping_status == 1 && $data->pay_status == 1)
  156. {
  157. $res = array('text'=>'待收货','num'=>3);
  158. }
  159. elseif($data->order_status == 3 && $data->refund_status == 0)
  160. {
  161. $res = array('text'=>'交易成功','num'=>4);
  162. }
  163. elseif($data->order_status == 3 && $data->refund_status == 1)
  164. {
  165. $res = array('text'=>'售后中','num'=>5);
  166. }
  167. elseif($data->order_status == 1)
  168. {
  169. $res = array('text'=>'已取消','num'=>6);
  170. }
  171. elseif($data->order_status == 2)
  172. {
  173. $res = array('text'=>'无效','num'=>7);
  174. }
  175. elseif($data->order_status == 3 && $data->refund_status == 2)
  176. {
  177. $res = array('text'=>'退款成功','num'=>8);
  178. }
  179. return $res;
  180. }
  181. //获取发票类型文字:0不索要发票,1个人,2企业
  182. public function getInvoiceAttr($data)
  183. {
  184. $arr = array(0 => '不索要发票', 1 => '个人', 2 => '企业');
  185. return $arr[$data->invoice];
  186. }
  187. //获取订单来源文字:1pc,2weixin,3app,4wap
  188. public function getPlaceTypeAttr($data)
  189. {
  190. $arr = array(0 => '未知', 1 => 'pc', 2 => 'weixin', 3 => 'app', 4 => 'wap');
  191. return $arr[$data->place_type];
  192. }
  193. //根据订单id返库存
  194. public function returnStock($order_id)
  195. {
  196. $order_goods = model('OrderGoods')->getDb()->where(array('order_id'=>$order_id))->get();
  197. if(!$order_goods){return false;}
  198. foreach($order_goods as $k=>$v)
  199. {
  200. //订单商品直接返库存
  201. model('Goods')->changeGoodsStock(array('goods_id'=>$v->goods_id,'goods_number'=>$v->goods_number,'type'=>1));
  202. }
  203. return true;
  204. }
  205. //订单超时,设为无效
  206. public function orderSetInvalid($order_id)
  207. {
  208. $order = self::where(array('id'=>$order_id,'order_status'=>0,'pay_status'=>0))->update(['order_status'=>2]);
  209. if(!$order){return false;}
  210. //返库存
  211. $this->returnStock($order_id);
  212. return true;
  213. }
  214. /**
  215. * 打印sql
  216. */
  217. public function toSql($where)
  218. {
  219. return $this->getDb()->where($where)->toSql();
  220. }
  221. }