StoreProductAttrValue.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace app\admin\model\store;
  3. use crmeb\basic\BaseModel;
  4. use crmeb\services\SystemConfigService;
  5. use crmeb\services\workerman\ChannelService;
  6. use crmeb\traits\ModelTrait;
  7. class StoreProductAttrValue extends BaseModel
  8. {
  9. /**
  10. * 模型名称
  11. * @var string
  12. */
  13. protected $name = 'store_product_attr_value';
  14. use ModelTrait;
  15. protected $insert = ['unique'];
  16. protected function setSukAttr($value)
  17. {
  18. return is_array($value) ? implode(',', $value) : $value;
  19. }
  20. protected function setUniqueAttr($value, $data)
  21. {
  22. if (is_array($data['suk'])) $data['suk'] = $this->setSukAttr($data['suk']);
  23. return $data['unique'] ?: self::uniqueId($data['product_id'] . $data['suk'] . uniqid(true));
  24. }
  25. /*
  26. * 减少销量增加库存
  27. * */
  28. public static function incProductAttrStock($productId, $unique, $num, $type = 0)
  29. {
  30. $productAttr = self::where('unique', $unique)->where('product_id', $productId)->where('type', $type)->field('stock,sales,quota')->find();
  31. if (!$productAttr) return true;
  32. if ($productAttr->sales > 0) $productAttr->sales = bcsub($productAttr->sales, $num, 0);
  33. if ($productAttr->sales < 0) $productAttr->sales = 0;
  34. $productAttr->stock = bcadd($productAttr->stock, $num, 0);
  35. //活动商品有限量数
  36. if ($type > 0) {
  37. $productAttr->quota = bcadd($productAttr->quota, $num, 0);
  38. }
  39. return $productAttr->save();
  40. }
  41. public static function decProductAttrStock($productId, $unique, $num, $type = 0)
  42. {
  43. if ($type == 0) {
  44. $res = self::where('product_id', $productId)->where('unique', $unique)->where('type', $type)
  45. ->dec('stock', $num)->inc('sales', $num)->update();
  46. } else {
  47. $res = self::where('product_id', $productId)->where('unique', $unique)->where('type', $type)
  48. ->dec('stock', $num)->dec('quota', $num)->inc('sales', $num)->update();
  49. }
  50. if ($res) {
  51. $stock = self::where('product_id', $productId)->where('unique', $unique)->where('type', $type)->value('stock');
  52. $replenishment_num = sys_config('store_stock') ?? 0; //库存预警界限
  53. if ($replenishment_num >= $stock) {
  54. try {
  55. ChannelService::instance()->send('STORE_STOCK', ['id' => $productId]);
  56. } catch (\Exception $e) {
  57. }
  58. }
  59. }
  60. return $res;
  61. }
  62. /**
  63. * 获取属性参数
  64. * @param $productId
  65. * @return array|string
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. * @throws \think\exception\DbException
  69. */
  70. public static function getStoreProductAttrResult($productId)
  71. {
  72. $productAttr = StoreProductAttr::getProductAttr($productId);
  73. if (!$productAttr) return [];
  74. $attr = [];
  75. foreach ($productAttr as $key => $value) {
  76. $attr[$key]['value'] = $value['attr_name'];
  77. $attr[$key]['detailValue'] = '';
  78. $attr[$key]['attrHidden'] = true;
  79. $attr[$key]['detail'] = $value['attr_values'];
  80. }
  81. $value = attr_format($attr)[1];
  82. $valueNew = [];
  83. $count = 0;
  84. foreach ($value as $key => $item) {
  85. $detail = $item['detail'];
  86. sort($item['detail'], SORT_STRING);
  87. $suk = implode(',', $item['detail']);
  88. $sukValue = self::where('product_id', $productId)->where('type', 0)->where('suk', $suk)->column('bar_code,cost,price,stock as sales,image as pic', 'suk');
  89. if (!count($sukValue)) {
  90. unset($value[$key]);
  91. } else {
  92. $valueNew[$count]['detail'] = $detail;
  93. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'];
  94. $valueNew[$count]['price'] = $sukValue[$suk]['price'];
  95. $valueNew[$count]['sales'] = $sukValue[$suk]['sales'];
  96. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'];
  97. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  98. $valueNew[$count]['check'] = false;
  99. $count++;
  100. }
  101. }
  102. return ['attr' => $attr, 'value' => $valueNew];
  103. }
  104. public static function uniqueId($key)
  105. {
  106. return substr(md5($key), 12, 8);
  107. }
  108. public static function clearProductAttrValue($productId, $type = 0)
  109. {
  110. return self::where('product_id', $productId)->where('type', $type)->delete();
  111. }
  112. }