ProductAttr.php 9.0 KB

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