ProductBuying.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class ProductBuying extends Model
  5. {
  6. protected $name = "product_buying";
  7. // 自动写入时间戳字段
  8. protected $autoWriteTimestamp = 'int';
  9. // 定义时间戳字段名
  10. protected $createTime = 'create_time';
  11. protected $updateTime = 'update_time';
  12. protected $deleteTime = false;
  13. // 追加属性
  14. protected $append = [
  15. 'create_time_text',
  16. 'update_time_text'
  17. ];
  18. //1 正常 2 已出售 0 关闭
  19. const Normal = 1;
  20. const SaleOut = 2;
  21. const Close = 0;
  22. /*
  23. * 订单状态
  24. */
  25. public $aciton_name = [
  26. '-1' => '全部',
  27. self::Normal => '待成交',
  28. self::SaleOut => '已成交',
  29. self::Close => '已取消',
  30. ];
  31. public static function getProductBuyingMaxPrice(int $productId)
  32. {
  33. return self::where('product_id', $productId)->max('min_price');
  34. }
  35. //用户购买套数
  36. public static function getProductBuyingCount(int $userId, int $productId)
  37. {
  38. return self::where('user_id', $userId)->where('status', self::Normal)->count();
  39. }
  40. //创建订单
  41. public static function setCreateBuying(int $userId, int $productId, int $stock, float $minPrice, float $totalPrice)
  42. {
  43. return self::create([
  44. 'user_id' => $userId,
  45. 'product_id' => $productId,
  46. 'stock' => $stock,
  47. 'min_price' => $minPrice,
  48. 'total_price' => $totalPrice
  49. ]);
  50. }
  51. //求购详情
  52. public static function getProductBuyingDetail(int $buyingId, string $lan)
  53. {
  54. return self::alias('a')
  55. ->join('product_list p', 'a.product_id = p.id')
  56. ->field('a.*,p.' . $lan . '_name as name,p.thum')
  57. ->where('a.id', $buyingId)
  58. ->find();
  59. }
  60. //获取订单列表
  61. public static function getBuyingList(int $productId)
  62. {
  63. return self::where('product_id', $productId)->where('status', self::Normal)->field('id,min_price')->order('id desc')->select();
  64. }
  65. //获取订单列表
  66. public static function getUserBuyingList(int $uid, string $lan, int $page)
  67. {
  68. return self::alias('a')->where('user_id', $uid)
  69. ->join('product_list p', 'a.product_id = p.id')
  70. ->field('a.*,p.' . $lan . '_name as name,p.thum')
  71. ->order('id desc')
  72. ->paginate($page);
  73. }
  74. //用户
  75. public function users()
  76. {
  77. return $this->hasOne('UserModel', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  78. }
  79. //产品
  80. public function productlists()
  81. {
  82. return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0);
  83. }
  84. public function getCreateTimeTextAttr($value, $data)
  85. {
  86. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  87. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  88. }
  89. public function getUpdateTimeTextAttr($value, $data)
  90. {
  91. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  92. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  93. }
  94. protected function setCreateTimeAttr($value)
  95. {
  96. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  97. }
  98. protected function setUpdateTimeAttr($value)
  99. {
  100. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  101. }
  102. }