UserBuying.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace app\common\model;
  3. use EasyWeChat\Kernel\Support\Str;
  4. use think\Model;
  5. //用户求购出售
  6. class UserBuying extends Model
  7. {
  8. protected $name = "user_buying";
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'create_time';
  13. protected $updateTime = 'update_time';
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'create_time_text',
  18. 'update_time_text'
  19. ];
  20. const DefaultAdders =0; //默认地址
  21. const TakeAdders =1; //收货地址
  22. const Waiting = 0;
  23. const Shipped = 1;
  24. const Finish = 2;
  25. /*
  26. * 订单状态 0代发货 1已发货 2完成
  27. */
  28. public $order_status = [
  29. '-1' => '全部',
  30. self::Waiting => '待发货',
  31. self::Shipped => '已发货',
  32. self::Finish => '完成'
  33. ];
  34. //出售记录
  35. public static function getCreateUserBuying(int $uid, int $buyingId, int $orderId, int $fromId, float $amount)
  36. {
  37. $fee = bcmul(config('market_transfer.serve_fee'), $amount, 2);
  38. $income = bcsub($amount, $fee, 2);
  39. self::create([
  40. 'user_id' => $uid,
  41. 'buying_id' => $buyingId,
  42. 'order_id' => $orderId,
  43. 'from_id' => $fromId,
  44. 'amount' => $amount,
  45. 'fee' => $fee,
  46. 'income' => $income,
  47. ]);
  48. return [$income,$fee];
  49. }
  50. //users
  51. //用户
  52. public function users()
  53. {
  54. return $this->hasOne('UserModel', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  55. }
  56. public function fromuser()
  57. {
  58. return $this->hasOne('UserModel', 'id', 'from_id', [], 'LEFT')->setEagerlyType(0);
  59. }
  60. //产品
  61. public function productlists()
  62. {
  63. return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0);
  64. }
  65. //订单
  66. public function orders()
  67. {
  68. return $this->hasOne('ProductOrder', 'id', 'order_id', [], 'LEFT')->setEagerlyType(0);
  69. }
  70. public function getCreateTimeTextAttr($value, $data)
  71. {
  72. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  73. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  74. }
  75. public function getUpdateTimeTextAttr($value, $data)
  76. {
  77. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  78. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  79. }
  80. protected function setCreateTimeAttr($value)
  81. {
  82. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  83. }
  84. protected function setUpdateTimeAttr($value)
  85. {
  86. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  87. }
  88. }