ExchangeOrder.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class ExchangeOrder extends Model
  5. {
  6. // 表名
  7. protected $name = 'shop_exchange_order';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'type_text',
  17. 'status_text'
  18. ];
  19. public function getTypeList()
  20. {
  21. return ['virtual' => __('Type virtual'), 'reality' => __('Type reality')];
  22. }
  23. public function getStatusList()
  24. {
  25. return ['created' => __('待兑换'), 'inprogress' => __('处理中'), 'rejected' => __('已拒绝'), 'delivered' => __('已发货'), 'completed' => __('已完成')];
  26. }
  27. public function getTypeTextAttr($value, $data)
  28. {
  29. $value = $value ?: ($data['type'] ?? '');
  30. $list = $this->getTypeList();
  31. return $list[$value] ?? '';
  32. }
  33. public function getStatusTextAttr($value, $data)
  34. {
  35. $value = $value ?: ($data['status'] ?? '');
  36. $list = $this->getStatusList();
  37. return $list[$value] ?? '';
  38. }
  39. //常见兑换订单
  40. public static function createOrder($data)
  41. {
  42. $data['orderid'] = date("Ymdhis") . sprintf("%06d", $data['user_id']) . mt_rand(1000, 9999);
  43. $data['ip'] = request()->ip();
  44. $data['useragent'] = substr(request()->server('HTTP_USER_AGENT'), 0, 255);
  45. return (new self)->save($data);
  46. }
  47. //获取列表
  48. public static function tableList($param)
  49. {
  50. $pageNum = 10;
  51. if (!empty($param['num'])) {
  52. $pageNum = $param['num'];
  53. }
  54. return self::with(['Exchange' => function ($query) {
  55. $query->field('id,title,image');
  56. }])->field('id,status,nums,score,type,exchange_id,reason,expressname,expressno,createtime')->where(function ($query) use ($param) {
  57. if (!empty($param['type'])) {
  58. $query->where('type', $param['type']);
  59. }
  60. if (!empty($param['status'])) {
  61. $query->where('status', $param['status']);
  62. }
  63. if (!empty($param['user_id'])) {
  64. $query->where('user_id', $param['user_id']);
  65. }
  66. })->order('createtime desc')->paginate($pageNum);
  67. }
  68. public function Exchange()
  69. {
  70. return $this->hasOne('Exchange', 'id', 'exchange_id');
  71. }
  72. }