StoreProductAttr.php 7.1 KB

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