StoreProductAttr.php 3.1 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\admin\model\system\SystemStoreProductStock;
  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, $store_id = 0)
  39. {
  40. $attrDetail = self::where('product_id', $productId)->where('type', $type_id)->order('id asc')->select()->toArray() ?: [];
  41. $_values = self::storeProductAttrValueDb()->where('product_id', $productId)->where('type', $type_id)->select();
  42. $values = [];
  43. foreach ($_values as $value) {
  44. if ($type) {
  45. if ($uid)
  46. $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');
  47. else
  48. $value['cart_num'] = 0;
  49. if (is_null($value['cart_num'])) $value['cart_num'] = 0;
  50. }
  51. if ($store_id && $type_id == 0) {
  52. $stockinfo = SystemStoreProductStock::where('unique', $value['unique'])->where('store_id', $store_id)->find();
  53. $value['price'] = ($stockinfo && $stockinfo['price'] > 0) ? $stockinfo['price'] : $value['price'];
  54. }
  55. $values[$value['suk']] = $value;
  56. }
  57. foreach ($attrDetail as $k => $v) {
  58. $attr = $v['attr_values'];
  59. // unset($productAttr[$k]['attr_values']);
  60. foreach ($attr as $kk => $vv) {
  61. $attrDetail[$k]['attr_value'][$kk]['attr'] = $vv;
  62. $attrDetail[$k]['attr_value'][$kk]['check'] = false;
  63. }
  64. }
  65. return [$attrDetail, $values];
  66. }
  67. public static function uniqueByStock($unique)
  68. {
  69. return self::storeProductAttrValueDb()->where('unique', $unique)->value('stock') ?: 0;
  70. }
  71. public static function uniqueByAttrInfo($unique, $field = '*')
  72. {
  73. return self::storeProductAttrValueDb()->field($field)->where('unique', $unique)->find();
  74. }
  75. public static function issetProductUnique($productId, $unique)
  76. {
  77. // $res = self::be(['product_id'=>$productId]);
  78. $res = self::where('product_id', $productId)->where('type', 0)->find();
  79. if ($unique) {
  80. return $res && self::storeProductAttrValueDb()->where('product_id', $productId)->where('unique', $unique)->where('type', 0)->count() > 0;
  81. } else {
  82. return !$res;
  83. }
  84. }
  85. }