ProductAttrValue.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace app\model\system;
  3. use library\basic\BaseModel;
  4. use library\services\SystemConfigService;
  5. use library\services\workerman\ChannelService;
  6. use library\traits\ModelTrait;
  7. class ProductAttrValue extends BaseModel
  8. {
  9. /**
  10. * 模型名称
  11. * @var string
  12. */
  13. protected $name = 'product_attr_value';
  14. use ModelTrait;
  15. protected $insert = ['unique'];
  16. protected function setSukAttr($value)
  17. {
  18. return is_array($value) ? implode(',', $value) : $value;
  19. }
  20. protected function setUniqueAttr($value, $data)
  21. {
  22. if (is_array($data['suk'])) $data['suk'] = $this->setSukAttr($data['suk']);
  23. return self::uniqueId($data['product_id'] . $data['suk'] . uniqid(true));
  24. }
  25. /*
  26. * 减少销量增加库存
  27. * */
  28. public static function incProductAttrStock($productId, $unique, $num)
  29. {
  30. $productAttr = self::where('unique', $unique)->where('product_id', $productId)->field('stock,sales')->find();
  31. if (!$productAttr) return true;
  32. if ($productAttr->sales > 0) $productAttr->sales = bcsub($productAttr->sales, $num, 0);
  33. if ($productAttr->sales < 0) $productAttr->sales = 0;
  34. $productAttr->stock = bcadd($productAttr->stock, $num, 0);
  35. return $productAttr->save();
  36. }
  37. public static function decProductAttrStock($productId, $unique, $num, $type = 0)
  38. {
  39. if ($type == 0) {
  40. $res = self::where('product_id', $productId)->where('unique', $unique)->where('type', $type)
  41. ->dec('stock', $num)->inc('sales', $num)->update();
  42. } else {
  43. $res = self::where('product_id', $productId)->where('unique', $unique)->where('type', $type)
  44. ->dec('stock', $num)->dec('quota', $num)->inc('sales', $num)->update();
  45. }
  46. if ($res) {
  47. $stock = self::where('product_id', $productId)->where('unique', $unique)->where('type', $type)->value('stock');
  48. $replenishment_num = sys_config('store_stock') ?? 0;//库存预警界限
  49. if ($replenishment_num >= $stock) {
  50. try {
  51. ChannelService::instance()->send('STORE_STOCK', ['id' => $productId]);
  52. } catch (\Exception $e) {
  53. }
  54. }
  55. }
  56. return $res;
  57. }
  58. /**
  59. * 获取属性参数
  60. * @param $productId
  61. * @return array|string
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. * @throws \think\exception\DbException
  65. */
  66. public static function getStoreProductAttrResult($productId)
  67. {
  68. $productAttr = ProductAttr::getProductAttr($productId);
  69. if (!$productAttr) return [];
  70. $attr = [];
  71. foreach ($productAttr as $key => $value) {
  72. $attr[$key]['value'] = $value['attr_name'];
  73. $attr[$key]['detailValue'] = '';
  74. $attr[$key]['attrHidden'] = true;
  75. $attr[$key]['detail'] = $value['attr_values'];
  76. }
  77. $value = attr_format($attr)[1];
  78. $valueNew = [];
  79. $count = 0;
  80. foreach ($value as $key => $item) {
  81. $detail = $item['detail'];
  82. sort($item['detail'], SORT_STRING);
  83. $suk = implode(',', $item['detail']);
  84. $sukValue = self::where('product_id', $productId)->where('suk', $suk)->column('bar_code,cost,price,ot_price,stock,image as pic', 'suk');
  85. if (!count($sukValue)) {
  86. unset($value[$key]);
  87. } else {
  88. foreach (array_keys($detail) as $k => $title) {
  89. $header[$k]['title'] = $title;
  90. $header[$k]['align'] = 'center';
  91. $header[$k]['minWidth'] = 130;
  92. }
  93. foreach (array_values($detail) as $k => $v) {
  94. $valueNew[$count]['value' . ($k + 1)] = $v;
  95. $header[$k]['key'] = 'value' . ($k + 1);
  96. }
  97. $valueNew[$count]['detail'] = $detail;
  98. // $valueNew[$count]['unit_name'] = $product['unit_name'];
  99. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'];
  100. $valueNew[$count]['price'] = floatval($sukValue[$suk]['price']);
  101. $valueNew[$count]['cost'] = floatval($sukValue[$suk]['cost']);
  102. $valueNew[$count]['ot_price'] = floatval($sukValue[$suk]['ot_price']);
  103. $valueNew[$count]['stock'] = intval($sukValue[$suk]['stock']);
  104. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'];
  105. // $valueNew[$count]['check'] = false;
  106. $count++;
  107. }
  108. }
  109. // $header[] = ['title'=>'商品单位','key'=>'unit_name'];
  110. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 80];
  111. $header[] = ['title' => '售价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 120];
  112. $header[] = ['title' => '成本价', 'slot' => 'cost', 'align' => 'center', 'minWidth' => 140];
  113. $header[] = ['title' => '原价', 'slot' => 'ot_price', 'align' => 'center', 'minWidth' => 140];
  114. $header[] = ['title' => '库存', 'slot' => 'stock', 'align' => 'center', 'minWidth' => 140];
  115. $header[] = ['title' => '商品编号', 'slot' => 'bar_code', 'align' => 'center', 'minWidth' => 140];
  116. $header[] = ['title' => '操作', 'slot' => 'action', 'align' => 'center', 'minWidth' => 70];
  117. return ['attr' => $attr, 'value' => $valueNew, 'header' => $header];
  118. }
  119. public static function uniqueId($key)
  120. {
  121. return substr(md5($key), 12, 8);
  122. }
  123. public static function clearProductAttrValue($productId, $type = 0)
  124. {
  125. return self::where('product_id', $productId)->where('type', $type)->delete();
  126. }
  127. public static function activityRules($productId, $type = 0)
  128. {
  129. $productAttr = StoreProductAttr::getProductAttr($productId);
  130. if (!$productAttr) return [];
  131. $attr = [];
  132. foreach ($productAttr as $key => $value) {
  133. $attr[$key]['value'] = $value['attr_name'];
  134. $attr[$key]['detailValue'] = '';
  135. $attr[$key]['attrHidden'] = true;
  136. $attr[$key]['detail'] = $value['attr_values'];
  137. }
  138. $value = attr_format($attr)[1];
  139. $valueNew = [];
  140. $count = 0;
  141. foreach ($value as $key => $item) {
  142. $detail = $item['detail'];
  143. sort($item['detail'], SORT_STRING);
  144. $suk = implode(',', $item['detail']);
  145. $sukValue = self::where('product_id', $productId)->where('suk', $suk)->where('type', 0)->column('bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota', 'suk');
  146. if (!count($sukValue)) {
  147. unset($value[$key]);
  148. } else {
  149. foreach (array_keys($detail) as $k => $title) {
  150. $header[$k]['title'] = $title;
  151. $header[$k]['align'] = 'center';
  152. $header[$k]['minWidth'] = 80;
  153. }
  154. foreach (array_values($detail) as $k => $v) {
  155. $valueNew[$count]['value' . ($k + 1)] = $v;
  156. $header[$k]['key'] = 'value' . ($k + 1);
  157. }
  158. $valueNew[$count]['detail'] = $detail;
  159. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'];
  160. $valueNew[$count]['price'] = floatval($sukValue[$suk]['price']);
  161. if ($type == 2) $valueNew[$count]['min_price'] = 0;
  162. $valueNew[$count]['cost'] = floatval($sukValue[$suk]['cost']);
  163. $valueNew[$count]['ot_price'] = floatval($sukValue[$suk]['ot_price']);
  164. $valueNew[$count]['stock'] = intval($sukValue[$suk]['stock']);
  165. $valueNew[$count]['quota'] = intval($sukValue[$suk]['quota']);
  166. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'];
  167. if ($type == 5) {
  168. $valueNew[$count]['trade_price'] = 0;
  169. $valueNew[$count]['deposit'] = 0;
  170. }
  171. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  172. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  173. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  174. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  175. $count++;
  176. }
  177. }
  178. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
  179. if ($type == 1) {
  180. $header[] = ['title' => '秒杀价', 'key' => 'price', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  181. } elseif ($type == 2) {
  182. $header[] = ['title' => '砍价起始金额', 'slot' => 'price', 'align' => 'center', 'minWidth' => 80];
  183. $header[] = ['title' => '砍价最低价', 'slot' => 'min_price', 'align' => 'center', 'minWidth' => 80];
  184. } elseif ($type == 3) {
  185. $header[] = ['title' => '拼团价', 'key' => 'price', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  186. } elseif ($type == 4) {
  187. $header[] = ['title' => '助力价', 'key' => 'price', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  188. } else {
  189. $header[] = ['title' => '零售价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 80];
  190. }
  191. if ($type == 5) {
  192. $header[] = ['title' => '批发价', 'slot' => 'trade_price', 'align' => 'center', 'minWidth' => 80];
  193. $header[] = ['title' => '定金','slot' => 'deposit', 'align' => 'center', 'minWidth' => 80];
  194. } else {
  195. $header[] = ['title' => '成本价', 'key' => 'cost', 'align' => 'center', 'minWidth' => 80];
  196. $header[] = ['title' => '原价', 'key' => 'ot_price', 'align' => 'center', 'minWidth' => 80];
  197. }
  198. $header[] = ['title' => '库存', 'key' => 'stock', 'align' => 'center', 'minWidth' => 80];
  199. if ($type == 2 || $type == 5) {
  200. $header[] = ['title' => '限量', 'slot' => 'quota', 'align' => 'center', 'minWidth' => 80];
  201. } else {
  202. $header[] = ['title' => '限量', 'key' => 'quota', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  203. }
  204. $header[] = ['title' => '重量(KG)', 'key' => 'weight', 'align' => 'center', 'minWidth' => 80];
  205. $header[] = ['title' => '体积(m³)', 'key' => 'volume', 'align' => 'center', 'minWidth' => 80];
  206. $header[] = ['title' => '商品编号', 'key' => 'bar_code', 'align' => 'center', 'minWidth' => 80];
  207. return ['attr' => $attr, 'value' => $valueNew, 'header' => $header];
  208. }
  209. }