StoreProductAttr.php 6.5 KB

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