StoreProductAttr.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. var_dump($store_id);
  52. if ($store_id && $type_id == 0) {
  53. $stockinfo = SystemStoreProductStock::where('unique', $value['unique'])->where('store_id', $store_id)->find();
  54. $value['price'] = $stockinfo ? $stockinfo['price'] : $value['price'];
  55. }
  56. $values[$value['suk']] = $value;
  57. var_dump($value);
  58. }
  59. foreach ($attrDetail as $k => $v) {
  60. $attr = $v['attr_values'];
  61. // unset($productAttr[$k]['attr_values']);
  62. foreach ($attr as $kk => $vv) {
  63. $attrDetail[$k]['attr_value'][$kk]['attr'] = $vv;
  64. $attrDetail[$k]['attr_value'][$kk]['check'] = false;
  65. }
  66. }
  67. return [$attrDetail, $values];
  68. }
  69. public static function uniqueByStock($unique)
  70. {
  71. return self::storeProductAttrValueDb()->where('unique', $unique)->value('stock') ?: 0;
  72. }
  73. public static function uniqueByAttrInfo($unique, $field = '*')
  74. {
  75. return self::storeProductAttrValueDb()->field($field)->where('unique', $unique)->find();
  76. }
  77. public static function issetProductUnique($productId, $unique)
  78. {
  79. // $res = self::be(['product_id'=>$productId]);
  80. $res = self::where('product_id', $productId)->where('type', 0)->find();
  81. if ($unique) {
  82. return $res && self::storeProductAttrValueDb()->where('product_id', $productId)->where('unique', $unique)->where('type', 0)->count() > 0;
  83. } else {
  84. return !$res;
  85. }
  86. }
  87. }