Product.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\common\model\plus\bargain;
  3. use app\common\model\BaseModel;
  4. /**
  5. * 砍价商品模型
  6. * @package app\common\model\plus\bargain
  7. */
  8. class Product extends BaseModel
  9. {
  10. protected $name = 'bargain_product';
  11. protected $pk = 'bargain_product_id';
  12. protected $append = ['product_sales', 'status_text'];
  13. /**
  14. * 计算显示销量 (初始销量 + 实际销量)
  15. */
  16. public function getProductSalesAttr($value, $data)
  17. {
  18. return $data['sales_initial'] + $data['total_sales'];
  19. }
  20. /**
  21. * 状态
  22. */
  23. public function getStatusTextAttr($value, $data)
  24. {
  25. if($data['status'] == 0){
  26. return '待审核';
  27. }
  28. if($data['status'] == 10){
  29. return '通过';
  30. }
  31. if($data['status'] == 20){
  32. return '未通过';
  33. }
  34. return '';
  35. }
  36. /**
  37. *关联商品主表
  38. */
  39. public function product()
  40. {
  41. return $this->hasOne('app\\common\\model\\product\\Product', 'product_id', 'product_id');
  42. }
  43. /**
  44. * 详情
  45. */
  46. public static function detail($bargain_product_id, $with = [])
  47. {
  48. return (new static())->with($with)->find($bargain_product_id);
  49. }
  50. /**
  51. *关联商品规格表
  52. */
  53. public function bargainSku()
  54. {
  55. return $this->hasMany('app\\common\\model\\plus\\bargain\\BargainSku', 'bargain_product_id', 'bargain_product_id');
  56. }
  57. /**
  58. *关联活动表
  59. */
  60. public function active()
  61. {
  62. return $this->belongsTo('app\\common\\model\\plus\\bargain\\Active', 'bargain_activity_id', 'bargain_activity_id');
  63. }
  64. /**
  65. * 关联供应商
  66. */
  67. public function supplier()
  68. {
  69. return $this->hasMany('app\\common\\model\\supplier\\Supplier', 'shop_supplier_id', 'shop_supplier_id');
  70. }
  71. /**
  72. * 商品ID是否存在
  73. */
  74. public static function isExistProductId($productId)
  75. {
  76. return (new static)->where('product_id', '=', $productId)
  77. ->where('is_delete', '=', 0)
  78. ->value('bargain_product_id');
  79. }
  80. }