StoreProductAttr.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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['pic']) || empty($value['pic']))
  55. return self::setErrorInfo('请上传商品图片');
  56. foreach ($value['detail'] as $attrName => $attrValue) {
  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. 'integral' => $value['integral'] ?? 0,
  96. 'quota_show' => $value['quota'] ?? 0,
  97. 'point' => $value['point'] ?? 0,
  98. ];
  99. }
  100. if (!count($attrGroup) || !count($valueGroup)) return self::setErrorInfo('请设置至少一个属性!');
  101. $attrModel = new self;
  102. $attrValueModel = new StoreProductAttrValue;
  103. if (!self::clearProductAttr($productId, $type)) return false;
  104. $res = false !== $attrModel->saveAll($attrGroup)
  105. && false !== $attrValueModel->saveAll($valueGroup)
  106. && false !== StoreProductAttrResult::setResult($result, $productId, $type);
  107. if ($res)
  108. return true;
  109. else
  110. return self::setErrorInfo('编辑商品属性失败!');
  111. }
  112. public static function clearProductAttr($productId, $type = 0)
  113. {
  114. if (empty($productId) && $productId != 0) return self::setErrorInfo('商品不存在!');
  115. $res = false !== self::where('product_id', $productId)->where('type', $type)->delete()
  116. && false !== StoreProductAttrValue::clearProductAttrValue($productId, $type);
  117. if (!$res)
  118. return self::setErrorInfo('编辑属性失败,清除旧属性失败!');
  119. else
  120. return true;
  121. }
  122. /**
  123. * 获取产品属性
  124. * @param $productId
  125. * @return array|bool|null|\think\Model
  126. * @throws \think\db\exception\DataNotFoundException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. * @throws \think\exception\DbException
  129. */
  130. public static function getProductAttr($productId)
  131. {
  132. if (empty($productId) && $productId != 0) return self::setErrorInfo('商品不存在!');
  133. $count = self::where('product_id', $productId)->count();
  134. if (!$count) return self::setErrorInfo('商品不存在!');
  135. return self::where('product_id', $productId)->select()->toArray();
  136. }
  137. }