StoreProductAttr.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. $suk = implode(',', $value['detail']);
  50. if (!isset($value['detail']) || count($value['detail']) != $attrCount) return self::setErrorInfo('请填写正确的商品信息');
  51. if (!isset($value['price']) || !is_numeric($value['price']) || floatval($value['price']) != $value['price'])
  52. return self::setErrorInfo('请填写正确的商品价格');
  53. if (!isset($value['stock']) || !is_numeric($value['stock']))
  54. return self::setErrorInfo('请填写正确的商品库存');
  55. if (!isset($value['cost']) || !is_numeric($value['cost']) || floatval($value['cost']) != $value['cost'])
  56. return self::setErrorInfo('请填写正确的商品成本价格');
  57. if (isset($value['bar_code']) && $value['bar_code']) {
  58. if (StoreProductAttrValue::where('bar_code', $value['bar_code'])
  59. ->where(function ($query) use ($productId, $suk) {
  60. $query->where('product_id', '<>', $productId)
  61. ->whereOr('suk', '<>', $suk);
  62. })
  63. ->where('type', $type)
  64. ->find()
  65. ) {
  66. return self::setErrorInfo('编码已存在');
  67. }
  68. }
  69. if (!isset($value['pic']) || empty($value['pic']))
  70. return self::setErrorInfo('请上传商品图片');
  71. foreach ($value['detail'] as $attrName => $attrValue) {
  72. $attrName = trim($attrName);
  73. $attrValue = trim($attrValue);
  74. if (!in_array($attrName, $attrNameList, true)) return self::setErrorInfo($attrName . '规则不存在');
  75. if (!in_array($attrValue, $attrValueList, true)) return self::setErrorInfo($attrName . '属性不存在');
  76. if (empty($attrName)) return self::setErrorInfo('请输入正确的属性');
  77. $value['detail'][$attrName] = $attrValue;
  78. }
  79. $valueList[$index] = $value;
  80. }
  81. $attrGroup = [];
  82. $valueGroup = [];
  83. foreach ($attrList as $k => $value) {
  84. $attrGroup[] = [
  85. 'product_id' => $productId,
  86. 'attr_name' => $value['value'],
  87. 'attr_values' => $value['detail'],
  88. 'type' => $type
  89. ];
  90. }
  91. foreach ($valueList as $k => $value) {
  92. // sort($value['detail'], SORT_STRING);
  93. $suk = implode(',', $value['detail']);
  94. $valueGroup[$suk] = [
  95. 'product_id' => $productId,
  96. 'suk' => $suk,
  97. 'price' => $value['price'],
  98. 'cost' => $value['cost'],
  99. 'ot_price' => $value['ot_price'],
  100. 'stock' => $value['stock'],
  101. 'unique' => StoreProductAttrValue::where(['product_id' => $productId, 'suk' => $suk, 'type' => $type])->value('unique') ?: '',
  102. 'image' => $value['pic'],
  103. 'bar_code' => $value['bar_code'] ?? '',
  104. 'weight' => $value['weight'] ?? 0,
  105. 'volume' => $value['volume'] ?? 0,
  106. 'brokerage' => $value['brokerage'] ?? 0,
  107. 'brokerage_two' => $value['brokerage_two'] ?? 0,
  108. 'type' => $type,
  109. 'quota' => $value['quota'] ?? 0,
  110. 'quota_show' => $value['quota'] ?? 0,
  111. 'level_price' => isset($value['level_price']) ? json_encode($value['level_price']) : '',
  112. 'level_discount' => isset($value['level_discount']) ? json_encode($value['level_discount']) : '',
  113. 'stock_right' => $value['stock_right'] ?? 0,
  114. 'upgrade' => $value['upgrade'] ?? 0,
  115. ];
  116. }
  117. if (!count($attrGroup) || !count($valueGroup)) return self::setErrorInfo('请设置至少一个属性!');
  118. $attrModel = new self;
  119. $attrValueModel = new StoreProductAttrValue;
  120. if (!self::clearProductAttr($productId, $type)) return false;
  121. $res = false !== $attrModel->saveAll($attrGroup)
  122. && false !== $attrValueModel->saveAll($valueGroup)
  123. && false !== StoreProductAttrResult::setResult($result, $productId, $type);
  124. foreach ($valueList as $v) {
  125. if ($v['bar_code']) {
  126. SystemStoreStock::where('bar_code', $v['bar_code'])->update(['price' => $v['price']]);
  127. }
  128. }
  129. if ($res)
  130. return true;
  131. else
  132. return self::setErrorInfo('编辑商品属性失败!');
  133. }
  134. public static function clearProductAttr($productId, $type = 0)
  135. {
  136. if (empty($productId) && $productId != 0) return self::setErrorInfo('商品不存在!');
  137. $res = false !== self::where('product_id', $productId)->where('type', $type)->delete()
  138. && false !== StoreProductAttrValue::clearProductAttrValue($productId, $type);
  139. if (!$res)
  140. return self::setErrorInfo('编辑属性失败,清除旧属性失败!');
  141. else
  142. return true;
  143. }
  144. /**
  145. * 获取产品属性
  146. * @param $productId
  147. * @return array|bool|null|\think\Model
  148. * @throws \think\db\exception\DataNotFoundException
  149. * @throws \think\db\exception\ModelNotFoundException
  150. * @throws \think\exception\DbException
  151. */
  152. public static function getProductAttr($productId)
  153. {
  154. if (empty($productId) && $productId != 0) return self::setErrorInfo('商品不存在!');
  155. $count = self::where('product_id', $productId)->count();
  156. if (!$count) return self::setErrorInfo('商品不存在!');
  157. return self::where('product_id', $productId)->select()->toArray();
  158. }
  159. public static function reSaveFromOtherType($product_id_origin, $product_id_to, $type_origin, $type_to)
  160. {
  161. //修改规格
  162. $attrGroupOrigin = self::where('product_id', $product_id_origin)->where('type', $type_origin)->select()->toArray();
  163. $valueGroupOrigin = StoreProductAttrValue::where('product_id', $product_id_origin)->where('type', $type_origin)->select()->toArray();
  164. $result = StoreProductAttrResult::where('product_id', $product_id_origin)->where('type', $type_origin)->find()->toArray();
  165. $attrGroup = [];
  166. $valueGroup = [];
  167. foreach ($valueGroupOrigin as $v) {
  168. $info = $v;
  169. $info['type'] = $type_to;
  170. $info['product_id'] = $product_id_to;
  171. $info['unique'] = StoreProductAttrValue::where(['product_id' => $product_id_to, 'suk' => $v['suk'], 'type' => $type_to])->value('unique') ?: '';
  172. $valueGroup[$v['suk']] = $info;
  173. }
  174. foreach ($attrGroupOrigin as $v) {
  175. $attrGroup[] = [
  176. 'product_id' => $product_id_to,
  177. 'attr_name' => $v['attr_name'],
  178. 'attr_values' => $v['attr_values'],
  179. 'type' => $type_to
  180. ];
  181. }
  182. $res = true;
  183. $attrModel = new StoreProductAttr();
  184. $attrValueModel = new StoreProductAttrValue;
  185. if (!self::clearProductAttr($product_id_to, $type_to)) return false;
  186. $res = $res
  187. && false !== $attrModel->saveAll($attrGroup)
  188. && false !== $attrValueModel->saveAll($valueGroup)
  189. && false !== StoreProductAttrResult::setResult($result['result'], $product_id_to, $type_to);
  190. foreach ($valueGroup as $v) {
  191. if ($v['bar_code']) {
  192. SystemStoreStock::where('bar_code', $v['bar_code'])->update(['price' => $v['price']]);
  193. }
  194. }
  195. if ($res)
  196. return true;
  197. else
  198. return self::setErrorInfo('编辑商品属性失败!');
  199. }
  200. }