ProductBuying.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 static function getBuyingList(int $productId)
  53. {
  54. return self::where('product_id', $productId)->where('status', self::Normal)->field('id,min_price')->order('id desc')->select();
  55. }
  56. public function getCreateTimeTextAttr($value, $data)
  57. {
  58. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  59. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  60. }
  61. public function getUpdateTimeTextAttr($value, $data)
  62. {
  63. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  64. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  65. }
  66. protected function setCreateTimeAttr($value)
  67. {
  68. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  69. }
  70. protected function setUpdateTimeAttr($value)
  71. {
  72. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  73. }
  74. }