StoreProductAttr.php 3.0 KB

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