ProductBuying.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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('product_id', $productId)->where('user_id', $userId)->where('status', self::Normal)->count();
  39. }
  40. //创建订单
  41. public static function setCreateBuying(int $userId, int $productId, int $num, float $minPrice, float $totalPrice)
  42. {
  43. return self::create([
  44. 'user_id' => $userId,
  45. 'product_id' => $productId,
  46. 'num' => $num,
  47. 'min_price' => $minPrice,
  48. 'total_price' => $totalPrice
  49. ]);
  50. }
  51. //获取订单列表
  52. public function getBuyingList(int $userId, int $page, int $size, int $status)
  53. {
  54. $where = [
  55. 'user_id' => $userId,
  56. 'status' => $status,
  57. ];
  58. $list = $this->where($where)->page($page, $size)->order('id desc')->select();
  59. $count = $this->where($where)->count();
  60. return compact('list', 'count');
  61. }
  62. public function getCreateTimeTextAttr($value, $data)
  63. {
  64. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  65. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  66. }
  67. public function getUpdateTimeTextAttr($value, $data)
  68. {
  69. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  70. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  71. }
  72. protected function setCreateTimeAttr($value)
  73. {
  74. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  75. }
  76. protected function setUpdateTimeAttr($value)
  77. {
  78. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  79. }
  80. }