StoreProductAttr.php 2.8 KB

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