StoreProductAttrValue.php 5.1 KB

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