ProductAttr.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace app\model\system;
  3. use app\model\system\ProductAttrResult;
  4. use app\model\system\ProductAttrValue;
  5. use library\basic\BaseModel;
  6. use think\db\exception\DataNotFoundException;
  7. use think\db\exception\DbException;
  8. use think\db\exception\ModelNotFoundException;
  9. use think\facade\Db;
  10. use library\traits\ModelTrait;
  11. /**
  12. * TODO 商品属性Model
  13. * Class ProductAttr
  14. * @package app\models\system
  15. */
  16. class ProductAttr extends BaseModel
  17. {
  18. /**
  19. * 模型名称
  20. * @var string
  21. */
  22. protected $name = 'product_attr';
  23. use ModelTrait;
  24. protected function getAttrValuesAttr($value)
  25. {
  26. return explode(',', $value);
  27. }
  28. public static function storeProductAttrValueDb()
  29. {
  30. return Db::name('ProductAttrValue');
  31. }
  32. protected function setAttrValuesAttr($value)
  33. {
  34. return is_array($value) ? implode(',', $value) : $value;
  35. }
  36. /**
  37. * 获取商品属性数据
  38. * @param $productId
  39. * @param int $uid
  40. * @param int $type
  41. * @param int $type_id
  42. * @return array
  43. * @throws DataNotFoundException
  44. * @throws DbException
  45. * @throws ModelNotFoundException
  46. */
  47. public static function getProductAttrDetail($productId, $uid = 0, $type = 0, $type_id = 0)
  48. {
  49. $attrDetail = self::where('product_id', $productId)->where('type', $type_id)->order('attr_values asc')->select()->toArray() ?: [];
  50. $_values = self::storeProductAttrValueDb()->where('product_id', $productId)->where('type', $type_id)->select();
  51. $values = [];
  52. $sku = [];
  53. foreach ($_values as $value) {
  54. if ($type) {
  55. if ($uid)
  56. $value['cart_num'] = StoreCart::where('product_attr_unique', $value['unique'])->where('is_pay', 0)->where('is_del', 0)->where('is_new', 0)->where('type', 'product')->where('product_id', $productId)->where('uid', $uid)->value('cart_num');
  57. else
  58. $value['cart_num'] = 0;
  59. if (is_null($value['cart_num'])) $value['cart_num'] = 0;
  60. }
  61. $values[$value['suk']] = $value;
  62. $sku[] = $value;
  63. }
  64. foreach ($attrDetail as $k => $v) {
  65. $attr = $v['attr_values'];
  66. // unset($productAttr[$k]['attr_values']);
  67. foreach ($attr as $kk => $vv) {
  68. $attrDetail[$k]['attr_value'][$kk]['attr'] = $vv;
  69. $attrDetail[$k]['attr_value'][$kk]['check'] = false;
  70. }
  71. }
  72. return [$attrDetail, $values, $sku];
  73. }
  74. public static function uniqueByStock($unique)
  75. {
  76. return self::storeProductAttrValueDb()->where('unique', $unique)->value('stock') ?: 0;
  77. }
  78. public static function uniqueByAttrInfo($unique, $field = '*')
  79. {
  80. return self::storeProductAttrValueDb()->field($field)->where('unique', $unique)->find();
  81. }
  82. public static function uniqueByAttrInfoMore($unique, $field = '*')
  83. {
  84. return self::storeProductAttrValueDb()->field($field)->where('unique', 'in', $unique)->select()->toArray() ?: [];
  85. }
  86. public static function issetProductUnique($productId, $unique)
  87. {
  88. $res = self::where('product_id', $productId)->where('type', 0)->find();
  89. if ($unique) {
  90. return $res && self::storeProductAttrValueDb()->where('product_id', $productId)->where('unique', $unique)->where('type', 0)->count() > 0;
  91. } else {
  92. return !$res;
  93. }
  94. }
  95. public static function createProductAttr($attrList, $valueList, $productId, $type = 0)
  96. {
  97. $result = ['attr' => $attrList, 'value' => $valueList];
  98. $attrValueList = [];
  99. $attrNameList = [];
  100. foreach ($attrList as $index => $attr) {
  101. if (!isset($attr['value'])) return self::setErrorInfo('请输入规则名称!');
  102. $attr['value'] = trim($attr['value']);
  103. if (!isset($attr['value'])) return self::setErrorInfo('请输入规则名称!!');
  104. if (!isset($attr['detail']) || !count($attr['detail'])) return self::setErrorInfo('请输入属性名称!');
  105. foreach ($attr['detail'] as $k => $attrValue) {
  106. $attrValue = trim($attrValue);
  107. if (empty($attrValue)) return self::setErrorInfo('请输入正确的属性');
  108. $attr['detail'][$k] = $attrValue;
  109. $attrValueList[] = $attrValue;
  110. $attr['detail'][$k] = $attrValue;
  111. }
  112. $attrNameList[] = $attr['value'];
  113. $attrList[$index] = $attr;
  114. }
  115. $attrCount = count($attrList);
  116. foreach ($valueList as $index => $value) {
  117. if (!isset($value['detail']) || count($value['detail']) != $attrCount) return self::setErrorInfo('请填写正确的商品信息');
  118. if (!isset($value['price']) || !is_numeric($value['price']) || floatval($value['price']) != $value['price'])
  119. return self::setErrorInfo('请填写正确的商品价格');
  120. if (!isset($value['stock']) || !is_numeric($value['stock']) || intval($value['stock']) != $value['stock'])
  121. return self::setErrorInfo('请填写正确的现货库存');
  122. if (!isset($value['capacity']) || !is_numeric($value['capacity']) || intval($value['capacity']) != $value['capacity'])
  123. return self::setErrorInfo('请填写正确的单日产能');
  124. if (!isset($value['cost']) || !is_numeric($value['cost']) || floatval($value['cost']) != $value['cost'])
  125. return self::setErrorInfo('请填写正确的出厂价');
  126. if (!isset($value['pic']) || empty($value['pic']))
  127. return self::setErrorInfo('请上传商品图片');
  128. foreach ($value['detail'] as $attrName => $attrValue) {
  129. $attrName = trim($attrName);
  130. $attrValue = trim($attrValue);
  131. if (!in_array($attrName, $attrNameList, true)) return self::setErrorInfo($attrName . '规则不存在');
  132. if (!in_array($attrValue, $attrValueList, true)) return self::setErrorInfo($attrName . '属性不存在');
  133. if (empty($attrName)) return self::setErrorInfo('请输入正确的属性');
  134. $value['detail'][$attrName] = $attrValue;
  135. }
  136. $valueList[$index] = $value;
  137. }
  138. $attrGroup = [];
  139. $valueGroup = [];
  140. foreach ($attrList as $k => $value) {
  141. $attrGroup[] = [
  142. 'product_id' => $productId,
  143. 'attr_name' => $value['value'],
  144. 'attr_values' => $value['detail'],
  145. 'type' => $type
  146. ];
  147. }
  148. foreach ($valueList as $k => $value) {
  149. sort($value['detail'], SORT_STRING);
  150. $suk = implode(',', $value['detail']);
  151. $valueGroup[$suk] = [
  152. 'product_id' => $productId,
  153. 'suk' => $suk,
  154. 'price' => $value['price'],
  155. 'cost' => $value['cost'],
  156. 'ot_price' => $value['ot_price'],
  157. 'stock' => $value['stock'],
  158. 'capacity' => $value['capacity'],
  159. 'unique' => ProductAttrValue::where(['product_id' => $productId, 'suk' => $suk, 'type' => $type])->value('unique') ?: '',
  160. 'image' => $value['pic'],
  161. 'bar_code' => $value['bar_code'] ?? '',
  162. 'weight' => $value['weight'] ?? 0,
  163. 'volume' => $value['volume'] ?? 0,
  164. 'brokerage' => $value['brokerage'] ?? 0,
  165. 'brokerage_two' => $value['brokerage_two'] ?? 0,
  166. 'type' => $type,
  167. 'quota' => $value['quota'] ?? 0,
  168. 'quota_show' => $value['quota'] ?? 0,
  169. ];
  170. }
  171. if (!count($attrGroup) || !count($valueGroup)) return self::setErrorInfo('请设置至少一个属性!');
  172. $attrModel = new self;
  173. $attrValueModel = new ProductAttrValue;
  174. if (!self::clearProductAttr($productId, $type)) return false;
  175. $res = false !== $attrModel->saveAll($attrGroup)
  176. && false !== $attrValueModel->saveAll($valueGroup)
  177. && false !== ProductAttrResult::setResult($result, $productId, $type);
  178. if ($res)
  179. return true;
  180. else
  181. return self::setErrorInfo('编辑商品属性失败!');
  182. }
  183. /**
  184. * 获取商品属性
  185. * @param $productId
  186. * @return array|bool|null|\think\Model
  187. * @throws DataNotFoundException
  188. * @throws ModelNotFoundException
  189. * @throws \think\exception\DbException
  190. */
  191. public static function getProductAttr($productId, $type = 0)
  192. {
  193. if (empty($productId) && $productId != 0) return self::setErrorInfo('商品不存在!');
  194. $count = self::where('product_id', $productId)->where('type', $type)->count();
  195. if (!$count) return self::setErrorInfo('商品不存在!');
  196. return self::where('product_id', $productId)->where('type', $type)->select()->toArray();
  197. }
  198. public static function clearProductAttr($productId, $type = 0)
  199. {
  200. if (empty($productId) && $productId != 0) return self::setErrorInfo('商品不存在!');
  201. $res = false !== self::where('product_id', $productId)->where('type', $type)->delete()
  202. && false !== ProductAttrValue::clearProductAttrValue($productId, $type);
  203. if (!$res)
  204. return self::setErrorInfo('编辑属性失败,清除旧属性失败!');
  205. else
  206. return true;
  207. }
  208. }