StoreProductAttr.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\traits\ModelTrait;
  9. class StoreProductAttr extends BaseModel
  10. {
  11. /**
  12. * 模型名称
  13. * @var string
  14. */
  15. protected $name = 'store_product_attr';
  16. use ModelTrait;
  17. protected function setAttrValuesAttr($value)
  18. {
  19. return is_array($value) ? implode(',', $value) : $value;
  20. }
  21. protected function getAttrValuesAttr($value)
  22. {
  23. return explode(',', $value);
  24. }
  25. public static function createProductAttr($attrList, $valueList, $productId, $type = 0)
  26. {
  27. $result = ['attr' => $attrList, 'value' => $valueList];
  28. $attrValueList = [];
  29. $attrNameList = [];
  30. foreach ($attrList as $index => $attr) {
  31. if (!isset($attr['value'])) return self::setErrorInfo('请输入规则名称!');
  32. $attr['value'] = trim($attr['value']);
  33. if (!isset($attr['value'])) return self::setErrorInfo('请输入规则名称!!');
  34. if (!isset($attr['detail']) || !count($attr['detail'])) return self::setErrorInfo('请输入属性名称!');
  35. foreach ($attr['detail'] as $k => $attrValue) {
  36. $attrValue = trim($attrValue);
  37. if (empty($attrValue)) return self::setErrorInfo('请输入正确的属性');
  38. $attr['detail'][$k] = $attrValue;
  39. $attrValueList[] = $attrValue;
  40. $attr['detail'][$k] = $attrValue;
  41. }
  42. $attrNameList[] = $attr['value'];
  43. $attrList[$index] = $attr;
  44. }
  45. $attrCount = count($attrList);
  46. foreach ($valueList as $index => $value) {
  47. if (!isset($value['detail']) || count($value['detail']) != $attrCount) return self::setErrorInfo('请填写正确的商品信息');
  48. if (!isset($value['price']) || !is_numeric($value['price']) || floatval($value['price']) != $value['price'])
  49. return self::setErrorInfo('请填写正确的商品价格');
  50. if (!isset($value['stock']) || !is_numeric($value['stock']) || intval($value['stock']) != $value['stock'])
  51. return self::setErrorInfo('请填写正确的商品库存');
  52. if (!isset($value['cost']) || !is_numeric($value['cost']) || floatval($value['cost']) != $value['cost'])
  53. return self::setErrorInfo('请填写正确的商品成本价格');
  54. if (!isset($value['deposit']) || !is_numeric($value['deposit']) || floatval($value['deposit']) != $value['deposit'])
  55. return self::setErrorInfo('请填写正确的商品押金');
  56. if (!isset($value['pic']) || empty($value['pic']))
  57. return self::setErrorInfo('请上传商品图片');
  58. foreach ($value['detail'] as $attrName => $attrValue) {
  59. $attrName = trim($attrName);
  60. $attrValue = trim($attrValue);
  61. if (!in_array($attrName, $attrNameList, true)) return self::setErrorInfo($attrName . '规则不存在');
  62. if (!in_array($attrValue, $attrValueList, true)) return self::setErrorInfo($attrName . '属性不存在');
  63. if (empty($attrName)) return self::setErrorInfo('请输入正确的属性');
  64. $value['detail'][$attrName] = $attrValue;
  65. }
  66. $valueList[$index] = $value;
  67. }
  68. $attrGroup = [];
  69. $valueGroup = [];
  70. foreach ($attrList as $k => $value) {
  71. $attrGroup[] = [
  72. 'product_id' => $productId,
  73. 'attr_name' => $value['value'],
  74. 'attr_values' => $value['detail'],
  75. 'type' => $type
  76. ];
  77. }
  78. foreach ($valueList as $k => $value) {
  79. // sort($value['detail'], SORT_STRING);
  80. $suk = implode(',', $value['detail']);
  81. $valueGroup[$suk] = [
  82. 'product_id' => $productId,
  83. 'suk' => $suk,
  84. 'price' => $value['price'],
  85. 'integral' => $value['integral'] ?? 0,
  86. 'deposit' => $value['deposit'] ?? 0,
  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. }