StoreProductAttrValue.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\store;
  12. use basic\ModelBasic;
  13. use traits\ModelTrait;
  14. class StoreProductAttrValue extends ModelBasic
  15. {
  16. use ModelTrait;
  17. protected $insert = ['unique'];
  18. protected function setSukAttr($value)
  19. {
  20. return is_array($value) ? implode(',',$value) : $value;
  21. }
  22. protected function setUniqueAttr($value,$data)
  23. {
  24. if(is_array($data['suk'])) $data['suk'] = $this->setSukAttr($data['suk']);
  25. return self::uniqueId($data['product_id'].$data['suk'].uniqid(true));
  26. }
  27. public static function decProductAttrStock($productId,$unique,$num)
  28. {
  29. return false !== self::where('product_id',$productId)->where('unique',$unique)
  30. ->dec('stock',$num)->inc('sales',$num)->update();
  31. }
  32. public static function uniqueId($key)
  33. {
  34. return substr(md5($key),12,8);
  35. }
  36. public static function clearProductAttrValue($productId)
  37. {
  38. return self::where('product_id',$productId)->delete();
  39. }
  40. }