StoreCombinationAttr.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\admin\model\ump;
  3. use crmeb\basic\BaseModel;
  4. use crmeb\traits\ModelTrait;
  5. class StoreCombinationAttr extends BaseModel
  6. {
  7. /**
  8. * 模型名称
  9. * @var string
  10. */
  11. protected $name = 'store_combination_attr';
  12. use ModelTrait;
  13. protected function setAttrValuesAttr($value)
  14. {
  15. return is_array($value) ? implode(',', $value) : $value;
  16. }
  17. protected function getAttrValuesAttr($value)
  18. {
  19. return explode(',', $value);
  20. }
  21. public static function createProductAttr($attrList, $valueList, $productId)
  22. {
  23. $result = ['attr' => $attrList, 'value' => $valueList];
  24. $attrValueList = [];
  25. $attrNameList = [];
  26. foreach ($attrList as $index => $attr) {
  27. if (!isset($attr['value'])) return self::setErrorInfo('请输入规则名称!');
  28. $attr['value'] = trim($attr['value']);
  29. if (!isset($attr['value'])) return self::setErrorInfo('请输入规则名称!!');
  30. if (!isset($attr['detail']) || !count($attr['detail'])) return self::setErrorInfo('请输入属性名称!');
  31. foreach ($attr['detail'] as $k => $attrValue) {
  32. $attrValue = trim($attrValue);
  33. if (empty($attrValue)) return self::setErrorInfo('请输入正确的属性');
  34. $attr['detail'][$k] = $attrValue;
  35. $attrValueList[] = $attrValue;
  36. $attr['detail'][$k] = $attrValue;
  37. }
  38. $attrNameList[] = $attr['value'];
  39. $attrList[$index] = $attr;
  40. }
  41. $attrCount = count($attrList);
  42. foreach ($valueList as $index => $value) {
  43. if (!isset($value['detail']) || count($value['detail']) != $attrCount) return self::setErrorInfo('请填写正确的商品信息');
  44. if (!isset($value['price']) || !is_numeric($value['price']) || floatval($value['price']) != $value['price'])
  45. return self::setErrorInfo('请填写正确的商品价格');
  46. if (!isset($value['sales']) || !is_numeric($value['sales']) || intval($value['sales']) != $value['sales'])
  47. return self::setErrorInfo('请填写正确的商品库存');
  48. if (!isset($value['pic']) || empty($value['pic']))
  49. return self::setErrorInfo('请上传商品图片');
  50. foreach ($value['detail'] as $attrName => $attrValue) {
  51. $attrName = trim($attrName);
  52. $attrValue = trim($attrValue);
  53. if (!in_array($attrName, $attrNameList, true)) return self::setErrorInfo($attrName . '规则不存在');
  54. if (!in_array($attrValue, $attrValueList, true)) return self::setErrorInfo($attrName . '属性不存在');
  55. if (empty($attrName)) return self::setErrorInfo('请输入正确的属性');
  56. $value['detail'][$attrName] = $attrValue;
  57. }
  58. $valueList[$index] = $value;
  59. }
  60. $attrGroup = [];
  61. $valueGroup = [];
  62. foreach ($attrList as $k => $value) {
  63. $attrGroup[] = [
  64. 'product_id' => $productId,
  65. 'attr_name' => $value['value'],
  66. 'attr_values' => $value['detail']
  67. ];
  68. }
  69. foreach ($valueList as $k => $value) {
  70. ksort($value['detail'], SORT_STRING);
  71. $suk = implode(',', $value['detail']);
  72. $valueGroup[$suk] = [
  73. 'product_id' => $productId,
  74. 'suk' => $suk,
  75. 'price' => $value['price'],
  76. 'stock' => $value['sales'],
  77. 'image' => $value['pic']
  78. ];
  79. }
  80. if (!count($attrGroup) || !count($valueGroup)) return self::setErrorInfo('请设置至少一个属性!');
  81. $attrModel = new self;
  82. $attrValueModel = new StoreCombinationAttrValue;
  83. self::beginTrans();
  84. if (!self::clearProductAttr($productId)) return false;
  85. $res = false !== $attrModel->saveAll($attrGroup)
  86. && false !== $attrValueModel->saveAll($valueGroup)
  87. && false !== StoreCombinationAttrResult::setResult($result, $productId);
  88. self::checkTrans($res);
  89. if ($res)
  90. return true;
  91. else
  92. return self::setErrorInfo('编辑商品属性失败!');
  93. }
  94. public static function clearProductAttr($productId)
  95. {
  96. if (empty($productId) && $productId != 0) return self::setErrorInfo('商品不存在!');
  97. $res = false !== self::where('product_id', $productId)->delete()
  98. && false !== StoreCombinationAttrValue::clearProductAttrValue($productId);
  99. if (!$res)
  100. return self::setErrorInfo('编辑属性失败,清除旧属性失败!');
  101. else
  102. return true;
  103. }
  104. }