ProductBuying.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 $uid, string $lan, int $page)
  62. {
  63. return self::alias('a')->where('user_id', $uid)
  64. ->join('product_list p', 'a.product_id = p.id')
  65. ->field('a.*,p.' . $lan . '_name as name,p.thum')
  66. ->order('id desc')
  67. ->paginate($page);
  68. }
  69. public function getCreateTimeTextAttr($value, $data)
  70. {
  71. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  72. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  73. }
  74. public function getUpdateTimeTextAttr($value, $data)
  75. {
  76. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  77. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  78. }
  79. protected function setCreateTimeAttr($value)
  80. {
  81. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  82. }
  83. protected function setUpdateTimeAttr($value)
  84. {
  85. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  86. }
  87. }