StoreProductAttr.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. /**
  68. * 商品条形码获取商品信息
  69. * @param $bar_code
  70. */
  71. public static function barCodeIndo($bar_code)
  72. {
  73. return self::storeProductAttrValueDb()->where('bar_code', $bar_code)->value('stock') ?: 0;
  74. }
  75. public static function uniqueByAttrInfo($unique, $field = '*')
  76. {
  77. return self::storeProductAttrValueDb()->field($field)->where('unique', $unique)->find();
  78. }
  79. public static function issetProductUnique($productId, $unique)
  80. {
  81. // $res = self::be(['product_id'=>$productId]);
  82. $res = self::where('product_id', $productId)->where('type', 0)->find();
  83. if ($unique) {
  84. return $res && self::storeProductAttrValueDb()->where('product_id', $productId)->where('unique', $unique)->where('type', 0)->count() > 0;
  85. } else {
  86. return !$res;
  87. }
  88. }
  89. }