StoreProductAttr.php 2.7 KB

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