ProductAttr.php 9.4 KB

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