StoreProductAttr.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/12/08
  5. */
  6. namespace app\admin\model\store;
  7. use Config;
  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) {
  49. return self::setErrorInfo('请填写正确的商品信息');
  50. }
  51. if (!isset($value['price']) || !is_numeric($value['price']) || floatval($value['price']) != $value['price'])
  52. return self::setErrorInfo('请填写正确的商品价格');
  53. if (!isset($value['stock']) || !is_numeric($value['stock']) || intval($value['stock']) != $value['stock'])
  54. return self::setErrorInfo('请填写正确的商品库存');
  55. if (!isset($value['cost']) || !is_numeric($value['cost']) || floatval($value['cost']) != $value['cost'])
  56. return self::setErrorInfo('请填写正确的商品成本价格');
  57. if (!isset($value['pic']) || empty($value['pic']))
  58. return self::setErrorInfo('请上传商品图片');
  59. foreach ($value['detail'] as $attrName => $attrValue) {
  60. unset($value['detail'][$attrName]);
  61. $attrName = trim($attrName);
  62. $attrValue = trim($attrValue);
  63. if (!in_array($attrName, $attrNameList, true)) return self::setErrorInfo($attrName . '规则不存在');
  64. if (!in_array($attrValue, $attrValueList, true)) return self::setErrorInfo($attrName . '属性不存在');
  65. if (empty($attrName)) return self::setErrorInfo('请输入正确的属性');
  66. $value['detail'][$attrName] = $attrValue;
  67. }
  68. $valueList[$index] = $value;
  69. }
  70. $attrGroup = [];
  71. $valueGroup = [];
  72. foreach ($attrList as $k => $value) {
  73. $attrGroup[] = [
  74. 'product_id' => $productId,
  75. 'attr_name' => $value['value'],
  76. 'attr_values' => $value['detail'],
  77. 'type' => $type
  78. ];
  79. }
  80. foreach ($valueList as $k => $value) {
  81. sort($value['detail'], SORT_STRING);
  82. $suk = implode(',', $value['detail']);
  83. $valueGroup[$suk] = [
  84. 'product_id' => $productId,
  85. 'suk' => $suk,
  86. 'price' => $value['price'],
  87. 'cost' => $value['cost'],
  88. 'ot_price' => $value['ot_price'],
  89. 'stock' => $value['stock'],
  90. 'unique' => StoreProductAttrValue::where(['product_id'=>$productId,'suk'=>$suk,'type'=>$type])->value('unique') ? : '',
  91. 'image' => $value['pic'],
  92. 'bar_code' => $value['bar_code'] ?? '',
  93. 'weight' => $value['weight'] ?? 0,
  94. 'volume' => $value['volume'] ?? 0,
  95. 'brokerage' => $value['brokerage'] ?? 0,
  96. 'brokerage_two' => $value['brokerage_two'] ?? 0,
  97. 'type' => $type,
  98. 'quota' => $value['quota'] ?? 0,
  99. 'quota_show' => $value['quota'] ?? 0,
  100. ];
  101. }
  102. if (!count($attrGroup) || !count($valueGroup)) return self::setErrorInfo('请设置至少一个属性!');
  103. $attrModel = new self;
  104. $attrValueModel = new StoreProductAttrValue;
  105. if (!self::clearProductAttr($productId,$type)) return false;
  106. $res = false !== $attrModel->saveAll($attrGroup)
  107. && false !== $attrValueModel->saveAll($valueGroup)
  108. && false !== StoreProductAttrResult::setResult($result, $productId, $type);
  109. if ($res)
  110. return true;
  111. else
  112. return self::setErrorInfo('编辑商品属性失败!');
  113. }
  114. public static function clearProductAttr($productId,$type=0)
  115. {
  116. if (empty($productId) && $productId != 0) return self::setErrorInfo('商品不存在!');
  117. $res = false !== self::where('product_id', $productId)->where('type', $type)->delete()
  118. && false !== StoreProductAttrValue::clearProductAttrValue($productId,$type);
  119. if (!$res)
  120. return self::setErrorInfo('编辑属性失败,清除旧属性失败!');
  121. else
  122. return true;
  123. }
  124. /**
  125. * 获取产品属性
  126. * @param $productId
  127. * @return array|bool|null|\think\Model
  128. * @throws \think\db\exception\DataNotFoundException
  129. * @throws \think\db\exception\ModelNotFoundException
  130. * @throws \think\exception\DbException
  131. */
  132. public static function getProductAttr($productId)
  133. {
  134. if (empty($productId) && $productId != 0) return self::setErrorInfo('商品不存在!');
  135. $count = self::where('product_id', $productId)->count();
  136. if (!$count) return self::setErrorInfo('商品不存在!');
  137. return self::where('product_id', $productId)->select()->toArray();
  138. }
  139. /**
  140. *
  141. */
  142. public static function getLuckyAttrs(string $attr_name) : array
  143. {
  144. return self::where('attr_name', $attr_name)->select()->toArray();
  145. }
  146. }