StoreProductAttrValue.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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)
  33. {
  34. $productAttr = self::where('unique', $unique)->where('product_id', $productId)->field('stock,sales')->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. 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. }