StoreProductAttr.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace app\admin\model\store;
  3. use Config;
  4. use crmeb\basic\BaseModel;
  5. use crmeb\traits\ModelTrait;
  6. class StoreProductAttr extends BaseModel
  7. {
  8. /**
  9. * 模型名称
  10. * @var string
  11. */
  12. protected $name = 'store_product_attr';
  13. use ModelTrait;
  14. protected function setAttrValuesAttr($value)
  15. {
  16. return is_array($value) ? implode(',', $value) : $value;
  17. }
  18. protected function getAttrValuesAttr($value)
  19. {
  20. return explode(',', $value);
  21. }
  22. public static function createProductAttr($attrList, $valueList, $productId, $type = 0)
  23. {
  24. $result = ['attr' => $attrList, 'value' => $valueList];
  25. $attrValueList = [];
  26. $attrNameList = [];
  27. foreach ($attrList as $index => $attr) {
  28. if (!isset($attr['value'])) return self::setErrorInfo('请输入规则名称!');
  29. $attr['value'] = trim($attr['value']);
  30. if (!isset($attr['value'])) return self::setErrorInfo('请输入规则名称!!');
  31. if (!isset($attr['detail']) || !count($attr['detail'])) return self::setErrorInfo('请输入属性名称!');
  32. foreach ($attr['detail'] as $k => $attrValue) {
  33. $attrValue = trim($attrValue);
  34. if (empty($attrValue)) return self::setErrorInfo('请输入正确的属性');
  35. $attr['detail'][$k] = $attrValue;
  36. $attrValueList[] = $attrValue;
  37. $attr['detail'][$k] = $attrValue;
  38. }
  39. $attrNameList[] = $attr['value'];
  40. $attrList[$index] = $attr;
  41. }
  42. $attrCount = count($attrList);
  43. foreach ($valueList as $index => $value) {
  44. if (!isset($value['detail']) || count($value['detail']) != $attrCount) {
  45. return self::setErrorInfo('请填写正确的商品信息');
  46. }
  47. if (!isset($value['price']) || !is_numeric($value['price']) || floatval($value['price']) != $value['price'])
  48. return self::setErrorInfo('请填写正确的商品价格');
  49. if (!isset($value['stock']) || !is_numeric($value['stock']) || intval($value['stock']) != $value['stock'])
  50. return self::setErrorInfo('请填写正确的商品库存');
  51. if (!isset($value['cost']) || !is_numeric($value['cost']) || floatval($value['cost']) != $value['cost'])
  52. return self::setErrorInfo('请填写正确的商品成本价格');
  53. if (!isset($value['pic']) || empty($value['pic']))
  54. return self::setErrorInfo('请上传商品图片');
  55. foreach ($value['detail'] as $attrName => $attrValue) {
  56. unset($value['detail'][$attrName]);
  57. $attrName = trim($attrName);
  58. $attrValue = trim($attrValue);
  59. if (!in_array($attrName, $attrNameList, true)) return self::setErrorInfo($attrName . '规则不存在');
  60. if (!in_array($attrValue, $attrValueList, true)) return self::setErrorInfo($attrName . '属性不存在');
  61. if (empty($attrName)) return self::setErrorInfo('请输入正确的属性');
  62. $value['detail'][$attrName] = $attrValue;
  63. }
  64. $valueList[$index] = $value;
  65. }
  66. $attrGroup = [];
  67. $valueGroup = [];
  68. foreach ($attrList as $k => $value) {
  69. $attrGroup[] = [
  70. 'product_id' => $productId,
  71. 'attr_name' => $value['value'],
  72. 'attr_values' => $value['detail'],
  73. 'type' => $type
  74. ];
  75. }
  76. foreach ($valueList as $k => $value) {
  77. sort($value['detail'], SORT_STRING);
  78. $suk = implode(',', $value['detail']);
  79. $valueGroup[$suk] = [
  80. 'product_id' => $productId,
  81. 'suk' => $suk,
  82. 'price' => $value['price'],
  83. 'cost' => $value['cost'],
  84. 'ot_price' => $value['ot_price'],
  85. 'stock' => $value['stock'],
  86. 'unique' => StoreProductAttrValue::where(['product_id' => $productId, 'suk' => $suk, 'type' => $type])->value('unique') ?: '',
  87. 'image' => $value['pic'],
  88. 'bar_code' => $value['bar_code'] ?? '',
  89. 'weight' => $value['weight'] ?? 0,
  90. 'volume' => $value['volume'] ?? 0,
  91. 'brokerage' => $value['brokerage'] ?? 0,
  92. 'brokerage_two' => $value['brokerage_two'] ?? 0,
  93. 'type' => $type,
  94. 'quota' => $value['quota'] ?? 0,
  95. 'quota_show' => $value['quota'] ?? 0,
  96. ];
  97. }
  98. if (!count($attrGroup) || !count($valueGroup)) return self::setErrorInfo('请设置至少一个属性!');
  99. $attrModel = new self;
  100. $attrValueModel = new StoreProductAttrValue;
  101. if (!self::clearProductAttr($productId, $type)) return false;
  102. $res = false !== $attrModel->saveAll($attrGroup)
  103. && false !== $attrValueModel->saveAll($valueGroup)
  104. && false !== StoreProductAttrResult::setResult($result, $productId, $type);
  105. if ($res)
  106. return true;
  107. else
  108. return self::setErrorInfo('编辑商品属性失败!');
  109. }
  110. public static function clearProductAttr($productId, $type = 0)
  111. {
  112. if (empty($productId) && $productId != 0) return self::setErrorInfo('商品不存在!');
  113. $res = false !== self::where('product_id', $productId)->where('type', $type)->delete()
  114. && false !== StoreProductAttrValue::clearProductAttrValue($productId, $type);
  115. if (!$res)
  116. return self::setErrorInfo('编辑属性失败,清除旧属性失败!');
  117. else
  118. return true;
  119. }
  120. /**
  121. * 获取产品属性
  122. * @param $productId
  123. * @return array|bool|null|\think\Model
  124. * @throws \think\db\exception\DataNotFoundException
  125. * @throws \think\db\exception\ModelNotFoundException
  126. * @throws \think\exception\DbException
  127. */
  128. public static function getProductAttr($productId)
  129. {
  130. if (empty($productId) && $productId != 0) return self::setErrorInfo('商品不存在!');
  131. $count = self::where('product_id', $productId)->count();
  132. if (!$count) return self::setErrorInfo('商品不存在!');
  133. return self::where('product_id', $productId)->select()->toArray();
  134. }
  135. /**
  136. *
  137. */
  138. public static function getLuckyAttrs(string $attr_name): array
  139. {
  140. return self::where('attr_name', $attr_name)->select()->toArray();
  141. }
  142. }