StoreProductAttr.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\models\store;
  3. use crmeb\basic\BaseModel;
  4. use think\facade\Db;
  5. use crmeb\traits\ModelTrait;
  6. /**
  7. * TODO 产品属性Model
  8. * Class StoreProductAttr
  9. * @package app\models\store
  10. */
  11. class StoreProductAttr extends BaseModel
  12. {
  13. /**
  14. * 模型名称
  15. * @var string
  16. */
  17. protected $name = 'store_product_attr';
  18. use ModelTrait;
  19. protected function getAttrValuesAttr($value)
  20. {
  21. return explode(',', $value);
  22. }
  23. public static function storeProductAttrValueDb()
  24. {
  25. return Db::name('StoreProductAttrValue');
  26. }
  27. /**
  28. * 获取商品属性数据
  29. * @param $productId
  30. * @return array
  31. */
  32. public static function getProductAttrDetail($productId, $uid = 0, $type = 0, $type_id = 0)
  33. {
  34. $attrDetail = self::where('product_id', $productId)->where('type', $type_id)->order('attr_values asc')->select()->toArray() ?: [];
  35. $_values = self::storeProductAttrValueDb()->where('product_id', $productId)->where('type', $type_id)->select();
  36. $values = [];
  37. foreach ($_values as $value) {
  38. if ($type) {
  39. if ($uid)
  40. $value['cart_num'] = StoreCart::where('product_attr_unique', $value['unique'])->where('is_pay', 0)->where('is_del', 0)->where('is_new', 0)->where('type', 'product')->where('product_id', $productId)->where('uid', $uid)->value('cart_num');
  41. else
  42. $value['cart_num'] = 0;
  43. if (is_null($value['cart_num'])) $value['cart_num'] = 0;
  44. }
  45. unset($value['cost']);
  46. $values[$value['suk']] = $value;
  47. }
  48. foreach ($attrDetail as $k => $v) {
  49. $attr = $v['attr_values'];
  50. // unset($productAttr[$k]['attr_values']);
  51. foreach ($attr as $kk => $vv) {
  52. $attrDetail[$k]['attr_value'][$kk]['attr'] = $vv;
  53. $attrDetail[$k]['attr_value'][$kk]['check'] = false;
  54. }
  55. }
  56. return [$attrDetail, $values];
  57. }
  58. public static function uniqueByStock($unique)
  59. {
  60. return self::storeProductAttrValueDb()->where('unique', $unique)->value('stock') ?: 0;
  61. }
  62. public static function uniqueByAttrInfo($unique, $field = '*')
  63. {
  64. return self::storeProductAttrValueDb()->field($field)->where('unique', $unique)->find();
  65. }
  66. public static function issetProductUnique($productId, $unique)
  67. {
  68. // $res = self::be(['product_id'=>$productId]);
  69. $res = self::where('product_id', $productId)->where('type', 0)->find();
  70. if ($unique) {
  71. return $res && self::storeProductAttrValueDb()->where('product_id', $productId)->where('unique', $unique)->where('type', 0)->count() > 0;
  72. } else {
  73. return !$res;
  74. }
  75. }
  76. }