StoreProductAttr.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 app\models\system\SystemStoreStock;
  9. use crmeb\basic\BaseModel;
  10. use crmeb\traits\ModelTrait;
  11. class StoreProductAttr extends BaseModel
  12. {
  13. /**
  14. * 模型名称
  15. * @var string
  16. */
  17. protected $name = 'store_product_attr';
  18. use ModelTrait;
  19. protected function setAttrValuesAttr($value)
  20. {
  21. return is_array($value) ? implode(',', $value) : $value;
  22. }
  23. protected function getAttrValuesAttr($value)
  24. {
  25. return explode(',', $value);
  26. }
  27. public static function createProductAttr($attrList, $valueList, $productId, $type = 0)
  28. {
  29. $result = ['attr' => $attrList, 'value' => $valueList];
  30. $attrValueList = [];
  31. $attrNameList = [];
  32. foreach ($attrList as $index => $attr) {
  33. if (!isset($attr['value'])) return self::setErrorInfo('请输入规则名称!');
  34. $attr['value'] = trim($attr['value']);
  35. if (!isset($attr['value'])) return self::setErrorInfo('请输入规则名称!!');
  36. if (!isset($attr['detail']) || !count($attr['detail'])) return self::setErrorInfo('请输入属性名称!');
  37. foreach ($attr['detail'] as $k => $attrValue) {
  38. $attrValue = trim($attrValue);
  39. if (empty($attrValue)) return self::setErrorInfo('请输入正确的属性');
  40. $attr['detail'][$k] = $attrValue;
  41. $attrValueList[] = $attrValue;
  42. $attr['detail'][$k] = $attrValue;
  43. }
  44. $attrNameList[] = $attr['value'];
  45. $attrList[$index] = $attr;
  46. }
  47. $attrCount = count($attrList);
  48. foreach ($valueList as $index => $value) {
  49. if (!isset($value['detail']) || count($value['detail']) != $attrCount) return self::setErrorInfo('请填写正确的商品信息');
  50. if (!isset($value['price']) || !is_numeric($value['price']) || floatval($value['price']) != $value['price'])
  51. return self::setErrorInfo('请填写正确的商品价格');
  52. if (!isset($value['stock']) || !is_numeric($value['stock']))
  53. return self::setErrorInfo('请填写正确的商品库存');
  54. if (!isset($value['cost']) || !is_numeric($value['cost']) || floatval($value['cost']) != $value['cost'])
  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. 'cost' => $value['cost'],
  86. 'ot_price' => $value['ot_price'],
  87. 'stock' => $value['stock'],
  88. 'unique' => StoreProductAttrValue::where(['product_id' => $productId, 'suk' => $suk, 'type' => $type])->value('unique') ?: '',
  89. 'image' => $value['pic'],
  90. 'bar_code' => $value['bar_code'] ?? '',
  91. 'weight' => $value['weight'] ?? 0,
  92. 'volume' => $value['volume'] ?? 0,
  93. 'brokerage' => $value['brokerage'] ?? 0,
  94. 'brokerage_two' => $value['brokerage_two'] ?? 0,
  95. 'type' => $type,
  96. 'quota' => $value['quota'] ?? 0,
  97. 'quota_show' => $value['quota'] ?? 0,
  98. 'level_price' => isset($value['level_price']) ? json_encode($value['level_price']) : '',
  99. 'level_discount' => isset($value['level_discount']) ? json_encode($value['level_discount']) : '',
  100. 'stock_right' => $value['stock_right'] ?? 0,
  101. 'upgrade' => $value['upgrade'] ?? 0,
  102. ];
  103. }
  104. if (!count($attrGroup) || !count($valueGroup)) return self::setErrorInfo('请设置至少一个属性!');
  105. $attrModel = new self;
  106. $attrValueModel = new StoreProductAttrValue;
  107. if (!self::clearProductAttr($productId, $type)) return false;
  108. $res = false !== $attrModel->saveAll($attrGroup)
  109. && false !== $attrValueModel->saveAll($valueGroup)
  110. && false !== StoreProductAttrResult::setResult($result, $productId, $type);
  111. foreach ($valueList as $v) {
  112. if ($v['bar_code']) {
  113. SystemStoreStock::where('bar_code', $v['bar_code'])->update(['price' => $v['price']]);
  114. }
  115. }
  116. if ($res)
  117. return true;
  118. else
  119. return self::setErrorInfo('编辑商品属性失败!');
  120. }
  121. public static function clearProductAttr($productId, $type = 0)
  122. {
  123. if (empty($productId) && $productId != 0) return self::setErrorInfo('商品不存在!');
  124. $res = false !== self::where('product_id', $productId)->where('type', $type)->delete()
  125. && false !== StoreProductAttrValue::clearProductAttrValue($productId, $type);
  126. if (!$res)
  127. return self::setErrorInfo('编辑属性失败,清除旧属性失败!');
  128. else
  129. return true;
  130. }
  131. /**
  132. * 获取产品属性
  133. * @param $productId
  134. * @return array|bool|null|\think\Model
  135. * @throws \think\db\exception\DataNotFoundException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. * @throws \think\exception\DbException
  138. */
  139. public static function getProductAttr($productId)
  140. {
  141. if (empty($productId) && $productId != 0) return self::setErrorInfo('商品不存在!');
  142. $count = self::where('product_id', $productId)->count();
  143. if (!$count) return self::setErrorInfo('商品不存在!');
  144. return self::where('product_id', $productId)->select()->toArray();
  145. }
  146. public static function reSaveFromOtherType($product_id_origin, $product_id_to, $type_origin, $type_to)
  147. {
  148. //修改规格
  149. $attrGroupOrigin = self::where('product_id', $product_id_origin)->where('type', $type_origin)->select()->toArray();
  150. $valueGroupOrigin = StoreProductAttrValue::where('product_id', $product_id_origin)->where('type', $type_origin)->select()->toArray();
  151. $result = StoreProductAttrResult::where('product_id', $product_id_origin)->where('type', $type_origin)->find()->toArray();
  152. $attrGroup = [];
  153. $valueGroup = [];
  154. foreach ($valueGroupOrigin as $v) {
  155. $info = $v;
  156. $info['type'] = $type_to;
  157. $info['product_id'] = $product_id_to;
  158. $info['unique'] = StoreProductAttrValue::where(['product_id' => $product_id_to, 'suk' => $v['suk'], 'type' => $type_to])->value('unique') ?: '';
  159. $valueGroup[$v['suk']] = $info;
  160. }
  161. foreach ($attrGroupOrigin as $v) {
  162. $attrGroup[] = [
  163. 'product_id' => $product_id_to,
  164. 'attr_name' => $v['attr_name'],
  165. 'attr_values' => $v['attr_values'],
  166. 'type' => $type_to
  167. ];
  168. }
  169. $res = true;
  170. $attrModel = new StoreProductAttr();
  171. $attrValueModel = new StoreProductAttrValue;
  172. if (!self::clearProductAttr($product_id_to, $type_to)) return false;
  173. $res = $res
  174. && false !== $attrModel->saveAll($attrGroup)
  175. && false !== $attrValueModel->saveAll($valueGroup)
  176. && false !== StoreProductAttrResult::setResult($result['result'], $product_id_to, $type_to);
  177. foreach ($valueGroup as $v) {
  178. if ($v['bar_code']) {
  179. SystemStoreStock::where('bar_code', $v['bar_code'])->update(['price' => $v['price']]);
  180. }
  181. }
  182. if ($res)
  183. return true;
  184. else
  185. return self::setErrorInfo('编辑商品属性失败!');
  186. }
  187. }