StoreProductAttr.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\model\store;
  12. use basic\ModelBasic;
  13. use think\Db;
  14. use traits\ModelTrait;
  15. /**商品属性
  16. * Class StoreProductAttr
  17. * @package app\wap\model\store
  18. */
  19. class StoreProductAttr extends ModelBasic
  20. {
  21. use ModelTrait;
  22. protected function getAttrValuesAttr($value)
  23. {
  24. return explode(',', $value);
  25. }
  26. public static function storeProductAttrValueDb()
  27. {
  28. return Db::name('StoreProductAttrValue');
  29. }
  30. /**
  31. * 获取商品属性数据
  32. * @param $productId
  33. * @return array
  34. */
  35. public static function getProductAttrDetail($productId)
  36. {
  37. $attr = self::where('product_id', $productId)->select()->toArray() ?: [];
  38. $_values = self::storeProductAttrValueDb()->where('product_id', $productId)->select();
  39. $values = [];
  40. foreach ($_values as $value) {
  41. $values[$value['suk']] = $value;
  42. }
  43. return [$attr, $values];
  44. }
  45. public static function uniqueByStock($unique)
  46. {
  47. return self::storeProductAttrValueDb()->where('unique', $unique)->value('stock') ?: 0;
  48. }
  49. public static function uniqueByAttrInfo($unique, $field = '*')
  50. {
  51. return self::storeProductAttrValueDb()->field($field)->where('unique', $unique)->find();
  52. }
  53. public static function issetProductUnique($productId, $unique)
  54. {
  55. $res = self::be(['product_id' => $productId]);
  56. if ($unique) {
  57. return $res && self::storeProductAttrValueDb()->where('product_id', $productId)->where('unique', $unique)->count() > 0;
  58. } else {
  59. return !$res;
  60. }
  61. }
  62. }