StoreProductAttrValue.php 4.6 KB

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