StoreProductAttr.php 8.5 KB

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