StoreProductAttr.php 8.1 KB

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